Skip to content

Commit e29e42c

Browse files
authored
Update ConcurrentSequenceExecutor to use AutoReleasingSemaphore (#33)
* Update ConcurrentSequenceExecutor to use AutoReleasingSemaphore Fixes #29 * Remove debugging code
1 parent 60edf0a commit e29e42c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Sources/Concurrency/AutoReleasingSemaphore.swift

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class AutoReleasingSemaphore {
3838
/// in dispatch_semaphore_wait(_:_:).
3939
/// - returns: This function returns non-zero if a thread is woken.
4040
/// Otherwise, zero is returned.
41+
@discardableResult
4142
public func signal() -> Int {
4243
let newValue = waitingCount.decrementAndGet()
4344
if newValue < 0 {
@@ -65,6 +66,7 @@ public class AutoReleasingSemaphore {
6566
/// - parameter timeout: The amount of time in seconds to wait
6667
/// before returning with failure.
6768
/// - returns: The waiting result.
69+
@discardableResult
6870
public func wait(timeout: TimeInterval) -> DispatchTimeoutResult {
6971
waitingCount.incrementAndGet()
7072
return semaphore.wait(timeout: DispatchTime.now() + timeout)

Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ConcurrentSequenceExecutor: SequenceExecutor {
3939
public init(name: String, qos: DispatchQoS = .userInitiated, shouldTrackTaskId: Bool = false, maxConcurrentTasks: Int? = nil) {
4040
taskQueue = DispatchQueue(label: "Executor.taskQueue-\(name)", qos: qos, attributes: .concurrent)
4141
if let maxConcurrentTasks = maxConcurrentTasks {
42-
taskSemaphore = DispatchSemaphore(value: maxConcurrentTasks)
42+
taskSemaphore = AutoReleasingSemaphore(value: maxConcurrentTasks)
4343
} else {
4444
taskSemaphore = nil
4545
}
@@ -66,7 +66,7 @@ public class ConcurrentSequenceExecutor: SequenceExecutor {
6666
// MARK: - Private
6767

6868
private let taskQueue: DispatchQueue
69-
private let taskSemaphore: DispatchSemaphore?
69+
private let taskSemaphore: AutoReleasingSemaphore?
7070
private let shouldTrackTaskId: Bool
7171

7272
private func execute<SequenceResultType>(_ task: Task, with sequenceHandle: SynchronizedSequenceExecutionHandle<SequenceResultType>, _ execution: @escaping (Task, Any) -> SequenceExecution<SequenceResultType>) {

0 commit comments

Comments
 (0)