From b4c894e3e0b8726177a99b14b263ef09f9c19ff6 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Wed, 20 Feb 2019 11:35:42 -0800 Subject: [PATCH 1/2] Update ConcurrentSequenceExecutor to use AutoReleasingSemaphore Fixes #29 --- Sources/Concurrency/AutoReleasingSemaphore.swift | 2 ++ .../Concurrency/Executor/ConcurrentSequenceExecutor.swift | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/Concurrency/AutoReleasingSemaphore.swift b/Sources/Concurrency/AutoReleasingSemaphore.swift index 67b7db6..34a1c20 100644 --- a/Sources/Concurrency/AutoReleasingSemaphore.swift +++ b/Sources/Concurrency/AutoReleasingSemaphore.swift @@ -38,6 +38,7 @@ public class AutoReleasingSemaphore { /// in dispatch_semaphore_wait(_:_:). /// - returns: This function returns non-zero if a thread is woken. /// Otherwise, zero is returned. + @discardableResult public func signal() -> Int { let newValue = waitingCount.decrementAndGet() if newValue < 0 { @@ -65,6 +66,7 @@ public class AutoReleasingSemaphore { /// - parameter timeout: The amount of time in seconds to wait /// before returning with failure. /// - returns: The waiting result. + @discardableResult public func wait(timeout: TimeInterval) -> DispatchTimeoutResult { waitingCount.incrementAndGet() return semaphore.wait(timeout: DispatchTime.now() + timeout) diff --git a/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift b/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift index ef27fa1..5a8cb12 100644 --- a/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift +++ b/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift @@ -39,7 +39,7 @@ public class ConcurrentSequenceExecutor: SequenceExecutor { public init(name: String, qos: DispatchQoS = .userInitiated, shouldTrackTaskId: Bool = false, maxConcurrentTasks: Int? = nil) { taskQueue = DispatchQueue(label: "Executor.taskQueue-\(name)", qos: qos, attributes: .concurrent) if let maxConcurrentTasks = maxConcurrentTasks { - taskSemaphore = DispatchSemaphore(value: maxConcurrentTasks) + taskSemaphore = AutoReleasingSemaphore(value: maxConcurrentTasks) } else { taskSemaphore = nil } @@ -63,10 +63,14 @@ public class ConcurrentSequenceExecutor: SequenceExecutor { return handle } + deinit { + print("deinit") + } + // MARK: - Private private let taskQueue: DispatchQueue - private let taskSemaphore: DispatchSemaphore? + private let taskSemaphore: AutoReleasingSemaphore? private let shouldTrackTaskId: Bool private func execute(_ task: Task, with sequenceHandle: SynchronizedSequenceExecutionHandle, _ execution: @escaping (Task, Any) -> SequenceExecution) { From 90fb4f4ff0e0afa3ad693a006906fd8a1e749bd1 Mon Sep 17 00:00:00 2001 From: Yi Wang Date: Wed, 20 Feb 2019 12:47:55 -0800 Subject: [PATCH 2/2] Remove debugging code --- Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift b/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift index 5a8cb12..35f6570 100644 --- a/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift +++ b/Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift @@ -63,10 +63,6 @@ public class ConcurrentSequenceExecutor: SequenceExecutor { return handle } - deinit { - print("deinit") - } - // MARK: - Private private let taskQueue: DispatchQueue