@@ -84,16 +84,16 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
84
84
internal let _paused : Bool
85
85
internal var _initClosure : _InitClosure ! // retained throughout task's lifetime
86
86
87
- public var state : TaskState { return self . _machine. state }
87
+ public var state : TaskState { return self . _machine. state. rawValue }
88
88
89
89
/// progress value (NOTE: always nil when `weakified = true`)
90
- public var progress : Progress ? { return self . _machine. progress }
90
+ public var progress : Progress ? { return self . _machine. progress. rawValue }
91
91
92
92
/// fulfilled value
93
- public var value : Value ? { return self . _machine. value }
93
+ public var value : Value ? { return self . _machine. value. rawValue }
94
94
95
95
/// rejected/cancelled tuple info
96
- public var errorInfo : ErrorInfo ? { return self . _machine. errorInfo }
96
+ public var errorInfo : ErrorInfo ? { return self . _machine. errorInfo. rawValue }
97
97
98
98
public var name : String = " DefaultTask "
99
99
@@ -424,7 +424,7 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
424
424
let selfMachine = self . _machine
425
425
426
426
self . _then ( & canceller) {
427
- let innerTask = thenClosure ( selfMachine. value, selfMachine. errorInfo)
427
+ let innerTask = thenClosure ( selfMachine. value. rawValue , selfMachine. errorInfo. rawValue )
428
428
_bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
429
429
}
430
430
@@ -484,11 +484,11 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
484
484
485
485
// NOTE: using `self._then()` + `selfMachine` instead of `self.then()` will reduce Task allocation
486
486
self . _then ( & canceller) {
487
- if let value = selfMachine. value {
487
+ if let value = selfMachine. value. rawValue {
488
488
let innerTask = successClosure ( value)
489
489
_bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
490
490
}
491
- else if let errorInfo = selfMachine. errorInfo {
491
+ else if let errorInfo = selfMachine. errorInfo. rawValue {
492
492
_reject ( errorInfo)
493
493
}
494
494
}
@@ -534,10 +534,10 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
534
534
let selfMachine = self . _machine
535
535
536
536
self . _then ( & canceller) {
537
- if let value = selfMachine. value {
537
+ if let value = selfMachine. value. rawValue {
538
538
fulfill ( value)
539
539
}
540
- else if let errorInfo = selfMachine. errorInfo {
540
+ else if let errorInfo = selfMachine. errorInfo. rawValue {
541
541
let innerTask = failureClosure ( errorInfo)
542
542
_bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
543
543
}
@@ -617,10 +617,10 @@ internal func _bindInnerTask<Progress2, Value2, Error>(
617
617
configure. cancel = { innerTask. cancel ( ) ; return }
618
618
619
619
// pause/cancel innerTask if descendant task is already paused/cancelled
620
- if newMachine. state == . Paused {
620
+ if newMachine. state. rawValue == . Paused {
621
621
innerTask. pause ( )
622
622
}
623
- else if newMachine. state == . Cancelled {
623
+ else if newMachine. state. rawValue == . Cancelled {
624
624
innerTask. cancel ( )
625
625
}
626
626
}
@@ -637,36 +637,37 @@ extension Task
637
637
638
638
var completedCount = 0
639
639
let totalCount = tasks. count
640
+ let lock = _RecursiveLock ( )
640
641
641
642
for task in tasks {
642
643
task. success { ( value: Value ) -> Void in
643
644
644
- synchronized ( self ) {
645
- completedCount++
646
-
647
- let progressTuple = BulkProgress ( completedCount: completedCount, totalCount: totalCount)
648
- progress ( progressTuple)
645
+ lock. lock ( )
646
+ completedCount++
647
+
648
+ let progressTuple = BulkProgress ( completedCount: completedCount, totalCount: totalCount)
649
+ progress ( progressTuple)
650
+
651
+ if completedCount == totalCount {
652
+ var values : [ Value ] = Array ( )
649
653
650
- if completedCount == totalCount {
651
- var values : [ Value ] = Array ( )
652
-
653
- for task in tasks {
654
- values. append ( task. value!)
655
- }
656
-
657
- fulfill ( values)
654
+ for task in tasks {
655
+ values. append ( task. value!)
658
656
}
657
+
658
+ fulfill ( values)
659
659
}
660
+ lock. unlock ( )
660
661
661
662
} . failure { ( errorInfo: ErrorInfo ) -> Void in
662
663
663
- synchronized ( self ) {
664
- _reject ( errorInfo)
665
-
666
- for task in tasks {
667
- task. cancel ( )
668
- }
664
+ lock. lock ( )
665
+ _reject ( errorInfo)
666
+
667
+ for task in tasks {
668
+ task. cancel ( )
669
669
}
670
+ lock. unlock ( )
670
671
}
671
672
}
672
673
@@ -684,32 +685,33 @@ extension Task
684
685
var completedCount = 0
685
686
var rejectedCount = 0
686
687
let totalCount = tasks. count
688
+ let lock = _RecursiveLock ( )
687
689
688
690
for task in tasks {
689
691
task. success { ( value: Value ) -> Void in
690
692
691
- synchronized ( self ) {
692
- completedCount++
693
+ lock. lock ( )
694
+ completedCount++
695
+
696
+ if completedCount == 1 {
697
+ fulfill ( value)
693
698
694
- if completedCount == 1 {
695
- fulfill ( value)
696
-
697
- self . cancelAll ( tasks)
698
- }
699
+ self . cancelAll ( tasks)
699
700
}
701
+ lock. unlock ( )
700
702
701
703
} . failure { ( errorInfo: ErrorInfo ) -> Void in
702
704
703
- synchronized ( self ) {
704
- rejectedCount++
705
+ lock. lock ( )
706
+ rejectedCount++
707
+
708
+ if rejectedCount == totalCount {
709
+ var isAnyCancelled = ( tasks. filter { task in task. state == . Cancelled } . count > 0 )
705
710
706
- if rejectedCount == totalCount {
707
- var isAnyCancelled = ( tasks. filter { task in task. state == . Cancelled } . count > 0 )
708
-
709
- let errorInfo = ErrorInfo ( error: nil , isCancelled: isAnyCancelled) // NOTE: Task.any error returns nil (spec)
710
- _reject ( errorInfo)
711
- }
711
+ let errorInfo = ErrorInfo ( error: nil , isCancelled: isAnyCancelled) // NOTE: Task.any error returns nil (spec)
712
+ _reject ( errorInfo)
712
713
}
714
+ lock. unlock ( )
713
715
}
714
716
}
715
717
@@ -728,28 +730,29 @@ extension Task
728
730
729
731
var completedCount = 0
730
732
let totalCount = tasks. count
733
+ let lock = _RecursiveLock ( )
731
734
732
735
for task in tasks {
733
736
task. then { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
734
737
735
- synchronized ( self ) {
736
- completedCount++
737
-
738
- let progressTuple = BulkProgress ( completedCount: completedCount, totalCount: totalCount)
739
- progress ( progressTuple)
738
+ lock. lock ( )
739
+ completedCount++
740
+
741
+ let progressTuple = BulkProgress ( completedCount: completedCount, totalCount: totalCount)
742
+ progress ( progressTuple)
743
+
744
+ if completedCount == totalCount {
745
+ var values : [ Value ] = Array ( )
740
746
741
- if completedCount == totalCount {
742
- var values : [ Value ] = Array ( )
743
-
744
- for task in tasks {
745
- if task. state == . Fulfilled {
746
- values. append ( task. value!)
747
- }
747
+ for task in tasks {
748
+ if task. state == . Fulfilled {
749
+ values. append ( task. value!)
748
750
}
749
-
750
- fulfill ( values)
751
751
}
752
+
753
+ fulfill ( values)
752
754
}
755
+ lock. unlock ( )
753
756
754
757
}
755
758
}
@@ -795,15 +798,4 @@ infix operator ~ { associativity left }
795
798
public func ~ < P, V, E> ( task: Task < P , V , E > , tryCount: Int ) -> Task < P , V , E >
796
799
{
797
800
return task. try ( tryCount)
798
- }
799
-
800
- //--------------------------------------------------
801
- // MARK: - Utility
802
- //--------------------------------------------------
803
-
804
- internal func synchronized( object: AnyObject , closure: Void -> Void )
805
- {
806
- objc_sync_enter ( object)
807
- closure ( )
808
- objc_sync_exit ( object)
809
801
}
0 commit comments