Skip to content

Conversation

@asdf-bro
Copy link
Contributor

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

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>
Copy link
Contributor

Choose a reason for hiding this comment

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

For the Mutex case, let's put the storage into the Mutex as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's avoid _fixLifetime. We can use withExtendedLifetime(self) { } which has an awkward spelling but avoids the underscored field.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, I changed it.

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.

2 participants