groue / semaphore Goto Github PK
View Code? Open in Web Editor NEWA Synchronization Primitive for Swift Concurrency
License: MIT License
A Synchronization Primitive for Swift Concurrency
License: MIT License
I stumbled across https://github.com/adam-fowler/async-collections/blob/main/Sources/AsyncCollections/AsyncSemaphore.swift and I should ask questions to the author.
Could you add this function, thanks
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.
"NSLock can be used safely but require caution to only use in synchronous code and not across an await."
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:
"The closure is then immediately executed in the same execution context (in other words, the current thread) [...]"
Source: https://forums.swift.org/t/clarification-needed-on-unsafecontinuation-documentation/57803/2
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.