{"id":17242454,"url":"https://github.com/groue/semaphore","last_synced_at":"2025-04-13T07:51:36.117Z","repository":{"id":61065913,"uuid":"543977789","full_name":"groue/Semaphore","owner":"groue","description":"A Synchronization Primitive for Swift Concurrency","archived":false,"fork":false,"pushed_at":"2024-08-02T15:54:50.000Z","size":43,"stargazers_count":565,"open_issues_count":1,"forks_count":22,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-04-04T05:08:54.528Z","etag":null,"topics":["async","await","concurrency","counting-semaphore","semaphore"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/groue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["groue"]}},"created_at":"2022-10-01T10:04:09.000Z","updated_at":"2025-04-02T09:32:53.000Z","dependencies_parsed_at":"2024-01-16T05:11:33.569Z","dependency_job_id":"6e58df43-3e66-46bb-bd17-4860fa37c63c","html_url":"https://github.com/groue/Semaphore","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.038461538461538436","last_synced_commit":"f5d49bf4fe685fc709f49cc524d97eceecf7bff8"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groue%2FSemaphore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groue%2FSemaphore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groue%2FSemaphore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/groue%2FSemaphore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/groue","download_url":"https://codeload.github.com/groue/Semaphore/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681494,"owners_count":21144700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","await","concurrency","counting-semaphore","semaphore"],"created_at":"2024-10-15T06:13:18.569Z","updated_at":"2025-04-13T07:51:36.071Z","avatar_url":"https://github.com/groue.png","language":"Swift","funding_links":["https://github.com/sponsors/groue"],"categories":[],"sub_categories":[],"readme":"# Semaphore\n\n**A Synchronization Primitive for Swift Concurrency**\n\n**Requirements**: iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+ • Swift 5.10+ / Xcode 15.3+\n\n📖 **[Documentation](https://swiftpackageindex.com/groue/Semaphore/documentation)**\n\n---\n\nThis package provides `AsyncSemaphore`, a [traditional counting semaphore](https://en.wikipedia.org/wiki/Semaphore_(programming)).\n\nUnlike [`DispatchSemaphore`], it does not block any thread. Instead, Swift concurrency tasks are suspended \"awaiting\" for the semaphore.\n\n### Usage\n\nYou can use a semaphore to suspend a task and resume it later:\n\n```swift\nlet semaphore = AsyncSemaphore(value: 0)\n\nTask {\n  // Suspends the task until a signal occurs.\n  await semaphore.wait()\n  await doSomething()\n}\n\n// Resumes the suspended task.\nsemaphore.signal()\n```\n\nAn actor can use a semaphore so that its methods can't run concurrently, avoiding the \"actor reentrancy problem\":\n\n```swift\nactor MyActor {\n  private let semaphore = AsyncSemaphore(value: 1)\n  \n  func serializedMethod() async {\n    // Makes sure no two tasks can execute\n    // serializedMethod() concurrently. \n    await semaphore.wait()\n    defer { semaphore.signal() }\n    \n    await doSomething()\n    await doSomethingElse()\n  }\n}\n```\n\nA semaphore can generally limit the number of concurrent accesses to a resource:\n\n```swift\nclass Downloader {\n  private let semaphore: AsyncSemaphore\n\n  /// Creates a Downloader that can run at most\n  /// `maxDownloadCount` concurrent downloads. \n  init(maxDownloadCount: Int) {\n    semaphore = AsyncSemaphore(value: maxDownloadCount) \n  }\n\n  func download(...) async throws -\u003e Data {\n    try await semaphore.waitUnlessCancelled()\n    defer { semaphore.signal() }\n    return try await ...\n  }\n}\n```\n\nYou can see in the latest example that the `wait()` method has a `waitUnlessCancelled` variant that throws `CancellationError` if the task is cancelled before a signal occurs.\n\nFor a nice introduction to semaphores, see [The Beauty of Semaphores in Swift 🚦](https://medium.com/@roykronenfeld/semaphores-in-swift-e296ea80f860). The article discusses [`DispatchSemaphore`], but it can easily be ported to Swift concurrency: get inspiration from the above examples. \n\n[`DispatchSemaphore`]: https://developer.apple.com/documentation/dispatch/dispatchsemaphore\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgroue%2Fsemaphore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgroue%2Fsemaphore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgroue%2Fsemaphore/lists"}