Skip to content

Update locks to use Mutex#27

Closed
0xLeif wants to merge 6 commits intomainfrom
leif/mutex
Closed

Update locks to use Mutex#27
0xLeif wants to merge 6 commits intomainfrom
leif/mutex

Conversation

@0xLeif
Copy link
Owner

@0xLeif 0xLeif commented Dec 12, 2025

This pull request modernizes the Cache Swift library by updating platform requirements and refactoring its concurrency mechanisms for improved thread safety and future compatibility. The main change is replacing usage of NSLock/NSRecursiveLock with a new CacheLock abstraction that uses Swift's Mutex on supported platforms (macOS 15+, iOS 18+, etc.), and updating all cache operations to use a safer, lock-scoped API. Documentation and platform requirements are also updated for clarity.

Platform and Documentation Updates

  • Updated the minimum Swift version to 6.2 and raised minimum platform requirements to macOS 15, iOS 18, watchOS 11, tvOS 18, and visionOS 2 in both Package.swift and README.md. Added a table of supported platforms and guidance for users needing older platform support. [1] [2]
  • Clarified that thread safety is now provided by Swift's Mutex (via CacheLock) in the documentation.
  • Improved installation instructions in the README.md to distinguish between new and legacy platform usage.

Thread Safety and Locking Refactor

  • Replaced all direct usage of NSLock/NSRecursiveLock with a new CacheLock abstraction throughout the codebase (Cache, AnyCacheable, ComposableCache, PersistableCache, RequiredKeysCache, and LRUCache). All cache operations are now wrapped in withLock/try withLock for safer, more idiomatic concurrency. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19]

Cache API and Internal Improvements

  • Added internal, unlocked cache methods (_get, _set, _remove, _contains) to Cache for use by subclasses like LRUCache, enabling more efficient lock management and customization.
  • Refactored LRUCache to use the new locking API and internal methods, ensuring thread safety and correctness with the new concurrency model. [1] [2]

These changes collectively improve the safety, maintainability, and platform support of the library, preparing it for modern Swift concurrency and future platform releases.

@0xLeif 0xLeif requested a review from Copilot December 12, 2025 02:25
@gemini-code-assist
Copy link

Summary of Changes

Hello @0xLeif, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly modernizes the Cache Swift library by upgrading its core concurrency mechanisms and platform compatibility. The primary goal is to enhance thread safety and align with contemporary Swift development standards by replacing older locking primitives with Swift's Mutex through a new CacheLock abstraction. This change also involves updating minimum platform requirements and refining the internal API for more efficient and safer cache operations, ensuring the library remains robust and future-proof.

Highlights

  • Concurrency Refactor: Replaced all direct usages of NSLock and NSRecursiveLock with a new CacheLock abstraction, which leverages Swift's Mutex for improved thread safety and modern concurrency practices. All cache operations now utilize a safer, lock-scoped withLock API.
  • Platform Requirements Update: The minimum Swift version has been updated to 6.2, and platform requirements have been raised to macOS 15, iOS 18, watchOS 11, tvOS 18, and visionOS 2. The README.md and Package.swift files reflect these changes, with guidance for users needing older platform support.
  • Internal Cache API Improvements: Introduced internal, unlocked cache methods (_get, _set, _remove, _contains) within the Cache class. This allows subclasses like LRUCache to manage locking more efficiently and customize behavior while maintaining thread safety under the new concurrency model.
  • Documentation Enhancements: The README.md has been updated to clarify the new thread safety mechanism (Swift's Mutex via CacheLock) and to provide distinct installation instructions for both the new and legacy platform versions of the library.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a great step forward in modernizing the library's concurrency model. Replacing the various NSLocks with a unified CacheLock abstraction based on Swift's Mutex is a solid improvement for thread safety and performance. The consistent application of the withLock pattern makes the code much cleaner and safer. The introduction of internal, non-locking methods for subclasses like LRUCache is an excellent design choice that correctly handles the complexities of locking in an inheritance hierarchy.

I have one high-severity suggestion for RequiredKeysCache to further improve the locking strategy by consolidating to a single lock, which would enhance robustness and prevent potential deadlocks. Other than that, the changes are well-implemented and significantly improve the quality of the codebase.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request modernizes the Cache library by replacing NSLock/NSRecursiveLock with a new CacheLock abstraction that wraps Swift's Mutex from the Synchronization framework. The changes include updating platform requirements to support the new concurrency primitives, refactoring all cache operations to use the safer withLock API pattern, and introducing internal unlocked methods for efficient subclass implementations.

  • Updated minimum platform requirements to macOS 15+, iOS 18+, watchOS 11+, tvOS 18+, and visionOS 2+ to support the Synchronization framework
  • Replaced all NSLock/NSRecursiveLock usage with a new CacheLock abstraction that uses Swift's Mutex
  • Refactored all cache operations across the codebase to use withLock/try withLock for improved thread safety and idiomatic concurrency

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
Package.swift Updated Swift tools version to 6.2 and raised minimum platform versions to support Synchronization framework
README.md Updated Swift version requirement to 6.2, added platform requirements table, and updated thread-safety documentation to reference Mutex
Sources/Cache/CacheLock.swift New abstraction wrapping Swift's Mutex to provide thread-safe locking with withLock API
Sources/Cache/Cache/Cache.swift Replaced NSRecursiveLock with CacheLock, refactored all methods to use withLock, and added internal unlocked methods for subclass use
Sources/Cache/Cache/LRUCache.swift Removed NSRecursiveLock, updated to use parent's CacheLock and internal methods to avoid recursive locking
Sources/Cache/Cache/AnyCacheable.swift Replaced NSRecursiveLock with CacheLock and refactored all methods to use withLock pattern
Sources/Cache/Cache/ComposableCache.swift Replaced NSRecursiveLock with CacheLock and refactored all cache composition methods to use withLock
Sources/Cache/Cache/RequiredKeysCache.swift Replaced NSLock with CacheLock and refactored required keys validation with withLock pattern
Sources/Cache/Cache/PersistableCache.swift Replaced NSLock with CacheLock and updated save/delete operations to use withLock
Sources/Cache/JSON/JSON.swift Replaced NSLock with CacheLock and refactored all JSON operations to use withLock pattern

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@0xLeif 0xLeif closed this Dec 13, 2025
@0xLeif 0xLeif deleted the leif/mutex branch December 13, 2025 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments