Skip to content

Commit

Permalink
Make CompletedCondtion optional, should improve memory usage slightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardjrossiii committed Jun 1, 2016
1 parent a8d4526 commit 9e1bb31
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Sources/BoltsSwift/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final class Task<TResult> {
public typealias Continuation = () -> Void

private let synchronizationQueue = dispatch_queue_create("com.bolts.task", DISPATCH_QUEUE_CONCURRENT)
private let completedCondition = NSCondition()
private var _completedCondition: NSCondition?

private var _state: TaskState<TResult> = .Pending()
private var _continuations: [Continuation] = Array()
Expand Down Expand Up @@ -206,14 +206,30 @@ public final class Task<TResult> {
if NSThread.isMainThread() {
debugPrint("Warning: A long-running operation is being executed on the main thread waiting on \(self).")
}
if completed {

var conditon: NSCondition?
dispatch_barrier_sync(synchronizationQueue) {
if case .Pending = self._state {
conditon = self._completedCondition ?? NSCondition()
self._completedCondition = conditon
}
}

guard let condition = conditon else {
// Task should have been completed
precondition(completed)
return
}
completedCondition.lock()

condition.lock()
while !completed {
completedCondition.wait()
condition.wait()
}
condition.unlock()

dispatch_barrier_sync(synchronizationQueue) {
self._completedCondition = nil
}
completedCondition.unlock()
}

// MARK: State Change
Expand All @@ -222,21 +238,23 @@ public final class Task<TResult> {
var stateChanged = false

var continuations: [Continuation]?
var completedCondition: NSCondition?
dispatch_barrier_sync(synchronizationQueue) {
switch self._state {
case .Pending():
stateChanged = true
self._state = state
continuations = self._continuations
completedCondition = self._completedCondition
self._continuations.removeAll()
default:
break
}
}
if stateChanged {
completedCondition.lock()
completedCondition.broadcast()
completedCondition.unlock()
completedCondition?.lock()
completedCondition?.broadcast()
completedCondition?.unlock()

for continuation in continuations! {
continuation()
Expand Down

0 comments on commit 9e1bb31

Please sign in to comment.