swiftmultithreadingsynchronizationstructured-concurrency

In Swift Structured Concurrency, how are Actors synchronized?


In Swift Structured Concurrency, when some Task tries to acquire an actor, it needs to check whether if the actor is already busy and work can be done synchronously, without suspension.

However, multiple tasks can easily need to check if actor is busy all at the same time, which means that some synchronization mechanism is in order.

So what is the synchronization mechanism that synchronizes the inner workings of the actor? Can you provide a link to the relevant parts of the Swift source code?


Solution

  • I believe it uses a dispatch_lock_t under the hood. Actors are implemented in the runtime in C++. It's open source, so you can inspect the code yourself:

    https://github.com/swiftlang/swift/blob/4510aee43d5e4b72957a9f5d37ab9b678aafae3e/stdlib/public/Concurrency/Actor.cpp#L687-L781