[Pitch] Synchronous Mutual Exclusion Lock

I can provide an example where lock() and unlock() can not be replaced with withLock { ... }.

In two (1, 2) distinct implementations of an async semaphore, we can see that both call unlock() from the withUnsafeContinuation closure argument, in order to only release the lock after the continuation was acquired:

mutex.lock()
...
await withUnsafeContinuation { continuation in
  ... // use continuation
  mutex.unlock()
}

Such code can not be written with withLock only:

mutex.withLock {
  // Can't call `await withUnsafeContinuation` here
}

@Alejandro (and other forum readers), what would be the suggested way to implement a semaphore, if the Mutex of the standard library had no lock and unlock method?

7 Likes