-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# ``Semaphore`` | ||
|
||
A Synchronization Primitive for Swift Concurrency | ||
|
||
## Overview | ||
|
||
This package provides `AsyncSemaphore`, a [traditional counting semaphore](https://en.wikipedia.org/wiki/Semaphore_(programming)). | ||
|
||
Unlike [`DispatchSemaphore`], it does not block any thread. Instead, Swift concurrency tasks are suspended "awaiting" for the semaphore. | ||
|
||
## Usage | ||
|
||
You can use a semaphore to suspend a task and resume it later: | ||
|
||
```swift | ||
let semaphore = AsyncSemaphore(value: 0) | ||
|
||
Task { | ||
// Suspends the task until a signal occurs. | ||
await semaphore.wait() | ||
await doSomething() | ||
} | ||
|
||
// Resumes the suspended task. | ||
semaphore.signal() | ||
``` | ||
|
||
An actor can use a semaphore in order to make its methods can't run concurrently: | ||
|
||
```swift | ||
actor MyActor { | ||
private let semaphore = AsyncSemaphore(value: 1) | ||
|
||
func serializedMethod() async { | ||
// Makes sure no two tasks can execute | ||
// serializedMethod() concurrently. | ||
await semaphore.wait() | ||
defer { semaphore.signal() } | ||
|
||
await doSomething() | ||
await doSomethingElse() | ||
} | ||
} | ||
``` | ||
|
||
A semaphore can generally limit the number of concurrent accesses to a resource: | ||
|
||
```swift | ||
class Downloader { | ||
private let semaphore: AsyncSemaphore | ||
|
||
/// Creates a Downloader that can run at most | ||
/// `maxDownloadCount` concurrent downloads. | ||
init(maxDownloadCount: Int) { | ||
semaphore = AsyncSemaphore(value: maxDownloadCount) | ||
} | ||
|
||
func download(...) async throws -> Data { | ||
try await semaphore.waitUnlessCancelled() | ||
defer { semaphore.signal } | ||
return try await ... | ||
} | ||
} | ||
``` | ||
|
||
You can see in the latest example that the ``AsyncSemaphore/wait()`` method has a ``AsyncSemaphore/waitUnlessCancelled()`` variant that throws `CancellationError` if the task is cancelled before a signal occurs. | ||
|
||
For a nice introduction to semaphores, see [The Beauty of Semaphores in Swift 🚦](https://medium.com/@roykronenfeld/semaphores-in-swift-e296ea80f860). | ||
|
||
[`DispatchSemaphore`]: https://developer.apple.com/documentation/dispatch/dispatchsemaphore |