-
Thank you for open sourcing this approach. I don't have a real issue, but wanted to share some thoughts. There's no discussions tab hence the issue. I was curious to better understand how this works, because initially I assumed this might not be safe. I thought locking across awaits is not safe, because locks have to be unlocked on the same thread.
Source: https://forums.swift.org/t/incremental-migration-to-structured-concurrency/54939/21 public func wait() async {
lock()
// ...
await withUnsafeContinuation { continuation in
// ...
unlock()
}
} I wasn't sure if this could deadlock on small machine with a pool size of one, if at the suspension point marked by the await another async wait is run first and then trying to get the lock. Or if the closure would be executed on another thread as with Swift Concurrency there's not much guarantees about the thread on which an async function runs. I now think this works, because of special behavior of withUnsafeContinuation:
If withTaskCancellationHandler and withUnsafeThrowingContinuation also have behave like that it starts to make more sense for me. Or is there another reason why the locking code works in this case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi @KaiOelfke, I migrated the issue into a discussion :-) Thanks for opening this discussion. I am myself full of doubts, and can't pretend that the current implementation is the best possible one, or that it matches what was intended by the authors of Swift concurrency. Since the synchronization apis we need do not exist in the standard library, and that something has to be done, here we are.
We're not changing thread. The // On some thread
await withUnsafeContinuation { continuation in
// On the same thread, 100% guaranteed
} The lock is acquired before The semaphore value must be decremented before Now, since Is blocking a thread for the duration of
This one introduces the need for a recursive lock. I have tried to explain why in the code - I hope the explanation is not too muddy. |
Beta Was this translation helpful? Give feedback.
Hi @KaiOelfke,
I migrated the issue into a discussion :-)
Thanks for opening this discussion. I am myself full of doubts, and can't pretend that the current implementation is the best possible one, or that it matches what was intended by the authors of Swift concurrency. Since the synchronization apis we need do not exist in the standard library, and that something has to be done, here we are.
We're not changing thread.
The
withUnsafeContinuation
method accepts a closure that is documented to be immediately executed (as you have noticed). Also, it is not@escaping
. The closure MUST run on …