swift-nio icon indicating copy to clipboard operation
swift-nio copied to clipboard

Update our locks to use Synchronization.Mutex or os_unfair_lock

Open asdf-bro opened this issue 2 months ago • 2 comments

Now that we require Swift 6.0, update LockStorage to use Synchronization.Mutex or os_unfair_lock

Motivation:

Simplify and optimize NIOLock and NIOLockedValueBox on every platform

Modifications:

  • Moved all pthread_mutex_t and SRWLOCK code into lock.swift, to support our condition variable type
  • Rebuild LockStorage around Synchronization.Mutex on most platforms
  • On Darwin, rebuild around os_unfair_lock instead to support back deployment
  • On Darwin, the pointer technique is copied from ManagedAtomic from swift-atomics

Result:

  • Much simpler code
  • Better or equal performance

asdf-bro avatar Oct 28 '25 21:10 asdf-bro

Mutex has platform availability guards:

@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
@frozen public struct Mutex<Value> : ~Copyable where Value : ~Copyable {

How do we want to adopt that without increasing our platform requirements?

fabianfett avatar Nov 10 '25 12:11 fabianfett

Only adopt it on non-Apple platforms.

Lukasa avatar Nov 10 '25 14:11 Lukasa