Skip to content

Commit

Permalink
Update ConcurrentSequenceExecutor to use AutoReleasingSemaphore (#33)
Browse files Browse the repository at this point in the history
* Update ConcurrentSequenceExecutor to use AutoReleasingSemaphore

Fixes #29

* Remove debugging code
  • Loading branch information
neakor authored Feb 20, 2019
1 parent 60edf0a commit e29e42c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Sources/Concurrency/AutoReleasingSemaphore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Concurrency/Executor/ConcurrentSequenceExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -66,7 +66,7 @@ public class ConcurrentSequenceExecutor: SequenceExecutor {
// MARK: - Private

private let taskQueue: DispatchQueue
private let taskSemaphore: DispatchSemaphore?
private let taskSemaphore: AutoReleasingSemaphore?
private let shouldTrackTaskId: Bool

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

0 comments on commit e29e42c

Please sign in to comment.