From e16343b8b9bcf69acddf038ba76e0320b70b66ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 7 Oct 2022 08:09:19 +0200 Subject: [PATCH] Rename Semaphore to AsyncSemaphore This avoids a conflict with Darwin.Semaphore, as well as a conflict with the module name. --- README.md | 8 ++--- .../{Semaphore.swift => AsyncSemaphore.swift} | 8 ++--- ...eTests.swift => AsyncSemaphoreTests.swift} | 30 +++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) rename Sources/Semaphore/{Semaphore.swift => AsyncSemaphore.swift} (96%) rename Tests/SemaphoreTests/{SemaphoreTests.swift => AsyncSemaphoreTests.swift} (92%) diff --git a/README.md b/README.md index 885b2c2..c5017da 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,15 @@ # Semaphore -`Semaphore` is an object that controls access to a resource across multiple execution contexts through use of a traditional counting semaphore. +`AsyncSemaphore` is an object that controls access to a resource across multiple execution contexts through use of a traditional counting semaphore. -Unlike [`DispatchSemaphore`], `Semaphore` does not block any thread. Instead, it suspends Swift concurrency tasks. +Unlike [`DispatchSemaphore`], `AsyncSemaphore` does not block any thread. Instead, it suspends Swift concurrency tasks. ### Usage You can use a semaphore to suspend a task and resume it later: ```swift -let semaphore = Semaphore(value: 0) +let semaphore = AsyncSemaphore(value: 0) Task { // Suspends the task until a signal occurs. @@ -25,7 +25,7 @@ You can use a semaphore in order to make sure an actor's methods can't run concu ```swift actor MyActor { - private let semaphore = Semaphore(value: 1) + private let semaphore = AsyncSemaphore(value: 1) func serializedMethod() async { // Makes sure no two tasks can execute self.serializedMethod() concurrently. diff --git a/Sources/Semaphore/Semaphore.swift b/Sources/Semaphore/AsyncSemaphore.swift similarity index 96% rename from Sources/Semaphore/Semaphore.swift rename to Sources/Semaphore/AsyncSemaphore.swift index 4f3987f..adf44e0 100644 --- a/Sources/Semaphore/Semaphore.swift +++ b/Sources/Semaphore/AsyncSemaphore.swift @@ -24,7 +24,7 @@ import Foundation /// An object that controls access to a resource across multiple execution /// contexts through use of a traditional counting semaphore. /// -/// Unlike `DispatchSemaphore`, ``Semaphore`` does not block any thread. +/// Unlike `DispatchSemaphore`, ``AsyncSemaphore`` does not block any thread. /// Instead, it suspends Swift concurrency tasks. /// /// ## Topics @@ -41,7 +41,7 @@ import Foundation /// /// - ``wait()`` /// - ``waitUnlessCancelled()`` -public final class Semaphore { +public final class AsyncSemaphore { /// "Waiting for a signal" is easily said, but several possible states exist. private class Suspension { enum State { @@ -93,12 +93,12 @@ public final class Semaphore { /// - parameter value: The starting value for the semaphore. Do not pass a /// value less than zero. public init(value: Int) { - precondition(value >= 0, "Semaphore requires a value equal or greater than zero") + precondition(value >= 0, "AsyncSemaphore requires a value equal or greater than zero") self.value = value } deinit { - precondition(suspensions.isEmpty, "Semaphore is deallocated while some task(s) are suspended waiting for a signal.") + precondition(suspensions.isEmpty, "AsyncSemaphore is deallocated while some task(s) are suspended waiting for a signal.") } // MARK: - Locking diff --git a/Tests/SemaphoreTests/SemaphoreTests.swift b/Tests/SemaphoreTests/AsyncSemaphoreTests.swift similarity index 92% rename from Tests/SemaphoreTests/SemaphoreTests.swift rename to Tests/SemaphoreTests/AsyncSemaphoreTests.swift index 8143e85..7ea06fe 100644 --- a/Tests/SemaphoreTests/SemaphoreTests.swift +++ b/Tests/SemaphoreTests/AsyncSemaphoreTests.swift @@ -2,7 +2,7 @@ import Dispatch import XCTest @testable import Semaphore -final class SemaphoreTests: XCTestCase { +final class AsyncSemaphoreTests: XCTestCase { func testSignalWithoutSuspendedTasks() async { // Check DispatchSemaphore behavior @@ -21,20 +21,20 @@ final class SemaphoreTests: XCTestCase { } } - // Test that Semaphore behaves identically + // Test that AsyncSemaphore behaves identically do { do { - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) let woken = sem.signal() XCTAssertFalse(woken) } do { - let sem = Semaphore(value: 1) + let sem = AsyncSemaphore(value: 1) let woken = sem.signal() XCTAssertFalse(woken) } do { - let sem = Semaphore(value: 2) + let sem = AsyncSemaphore(value: 2) let woken = sem.signal() XCTAssertFalse(woken) } @@ -57,10 +57,10 @@ final class SemaphoreTests: XCTestCase { XCTAssertFalse(sem.signal() != 0) } - // Test that Semaphore behaves identically + // Test that AsyncSemaphore behaves identically do { // Given a task suspended on the semaphore - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) Task { await sem.wait() } try await Task.sleep(nanoseconds: delay) @@ -95,10 +95,10 @@ final class SemaphoreTests: XCTestCase { wait(for: [ex2], timeout: 1) } - // Test that Semaphore behaves identically + // Test that AsyncSemaphore behaves identically do { // Given a zero semaphore - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) // When a task waits for this semaphore, let ex1 = expectation(description: "wait") @@ -120,7 +120,7 @@ final class SemaphoreTests: XCTestCase { } func test_cancellation_while_suspended_throws_CancellationError() async throws { - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) let ex = expectation(description: "cancellation") let task = Task { do { @@ -138,7 +138,7 @@ final class SemaphoreTests: XCTestCase { } func test_cancellation_before_suspension_throws_CancellationError() async throws { - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) let ex = expectation(description: "cancellation") let task = Task { // Uncancellable delay @@ -162,7 +162,7 @@ final class SemaphoreTests: XCTestCase { func test_that_cancellation_while_suspended_increments_the_semaphore() async throws { // Given a task cancelled while suspended on a semaphore, - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) let task = Task { try await sem.waitUnlessCancelled() } @@ -189,7 +189,7 @@ final class SemaphoreTests: XCTestCase { func test_that_cancellation_before_suspension_increments_the_semaphore() async throws { // Given a task cancelled before it waits on a semaphore, - let sem = Semaphore(value: 0) + let sem = AsyncSemaphore(value: 0) let task = Task { // Uncancellable delay await withUnsafeContinuation { continuation in @@ -236,7 +236,7 @@ final class SemaphoreTests: XCTestCase { let maxCount = 10 for count in 1...maxCount { let runner = Runner() - let sem = Semaphore(value: count) + let sem = AsyncSemaphore(value: count) // Spawn many concurrent tasks await withThrowingTaskGroup(of: Void.self) { group in @@ -273,7 +273,7 @@ final class SemaphoreTests: XCTestCase { let maxCount = 10 for count in 1...maxCount { let runner = Runner() - let sem = Semaphore(value: count) + let sem = AsyncSemaphore(value: count) // Spawn many concurrent tasks await withThrowingTaskGroup(of: Void.self) { group in