Update our locks to use Synchronization.Mutex or os_unfair_lock#3428
Update our locks to use Synchronization.Mutex or os_unfair_lock#3428asdf-bro wants to merge 4 commits intoapple:mainfrom
Conversation
Now that we require Swift 6.0, update `NIOLock` and `NIOLockedValueBox` to use `Synchronization.Mutex`. Additionally, move all `pthread_mutex_t` and `SRWLOCK` code into `lock.swift` to support our condition variable type. Results: - Much simpler code - Better or equal performance, depending on the platform
Update NIOLock to use Synchronization.Mutex
| var mutex: os_unfair_lock_s | ||
| #else | ||
| @usableFromInline | ||
| let mutex: Mutex<Void> |
There was a problem hiding this comment.
For the Mutex case, let's put the storage into the Mutex as well.
There was a problem hiding this comment.
If we do that, we can't implement NIOLockedValueBox.Unsafe; the only public APIs on Mutex<non Void> are the two withLock methods.
We could use Synchronization._MutexHandle instead of Mutex<Void>, if you prefer. _MutexHandle is public and has public locking methods.
There was a problem hiding this comment.
Oh that's unfortunate. I'd like us to avoid using underscored parts of Swift to the greatest extent possible.
| #if canImport(Darwin) | ||
| let mutex_ptr = _getUnsafePointerToStoredProperties(self).assumingMemoryBound(to: os_unfair_lock_s.self) | ||
| os_unfair_lock_unlock(mutex_ptr) | ||
| _fixLifetime(self) |
There was a problem hiding this comment.
Let's avoid _fixLifetime. We can use withExtendedLifetime(self) { } which has an awkward spelling but avoids the underscored field.
There was a problem hiding this comment.
Okay, I changed it.
|
Mutex has platform availability guards: How do we want to adopt that without increasing our platform requirements? |
|
Only adopt it on non-Apple platforms. |
weissi
left a comment
There was a problem hiding this comment.
I'd be curious about the performance delta. There are (IIRC) 4 lock benchmarks: no contention, 2, 4 and ~8 parallel attempts to access. Ideally on Linux & macOS
Now that we require Swift 6.0, update
LockStorageto useSynchronization.Mutexoros_unfair_lockMotivation:
Simplify and optimize
NIOLockandNIOLockedValueBoxon every platformModifications:
pthread_mutex_tandSRWLOCKcode intolock.swift, to support our condition variable typeLockStoragearoundSynchronization.Mutexon most platformsos_unfair_lockinstead to support back deploymentManagedAtomicfromswift-atomicsResult: