@@ -84,16 +84,16 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
8484 internal let _paused : Bool
8585 internal var _initClosure : _InitClosure ! // retained throughout task's lifetime
8686
87- public var state : TaskState { return self . _machine. state }
87+ public var state : TaskState { return self . _machine. state. rawValue }
8888
8989 /// 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 }
9191
9292 /// fulfilled value
93- public var value : Value ? { return self . _machine. value }
93+ public var value : Value ? { return self . _machine. value. rawValue }
9494
9595 /// rejected/cancelled tuple info
96- public var errorInfo : ErrorInfo ? { return self . _machine. errorInfo }
96+ public var errorInfo : ErrorInfo ? { return self . _machine. errorInfo. rawValue }
9797
9898 public var name : String = " DefaultTask "
9999
@@ -424,7 +424,7 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
424424 let selfMachine = self . _machine
425425
426426 self . _then ( & canceller) {
427- let innerTask = thenClosure ( selfMachine. value, selfMachine. errorInfo)
427+ let innerTask = thenClosure ( selfMachine. value. rawValue , selfMachine. errorInfo. rawValue )
428428 _bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
429429 }
430430
@@ -484,11 +484,11 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
484484
485485 // NOTE: using `self._then()` + `selfMachine` instead of `self.then()` will reduce Task allocation
486486 self . _then ( & canceller) {
487- if let value = selfMachine. value {
487+ if let value = selfMachine. value. rawValue {
488488 let innerTask = successClosure ( value)
489489 _bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
490490 }
491- else if let errorInfo = selfMachine. errorInfo {
491+ else if let errorInfo = selfMachine. errorInfo. rawValue {
492492 _reject ( errorInfo)
493493 }
494494 }
@@ -534,10 +534,10 @@ public class Task<Progress, Value, Error>: Cancellable, Printable
534534 let selfMachine = self . _machine
535535
536536 self . _then ( & canceller) {
537- if let value = selfMachine. value {
537+ if let value = selfMachine. value. rawValue {
538538 fulfill ( value)
539539 }
540- else if let errorInfo = selfMachine. errorInfo {
540+ else if let errorInfo = selfMachine. errorInfo. rawValue {
541541 let innerTask = failureClosure ( errorInfo)
542542 _bindInnerTask ( innerTask, newMachine, progress, fulfill, _reject, configure)
543543 }
@@ -617,10 +617,10 @@ internal func _bindInnerTask<Progress2, Value2, Error>(
617617 configure. cancel = { innerTask. cancel ( ) ; return }
618618
619619 // pause/cancel innerTask if descendant task is already paused/cancelled
620- if newMachine. state == . Paused {
620+ if newMachine. state. rawValue == . Paused {
621621 innerTask. pause ( )
622622 }
623- else if newMachine. state == . Cancelled {
623+ else if newMachine. state. rawValue == . Cancelled {
624624 innerTask. cancel ( )
625625 }
626626}
@@ -637,36 +637,37 @@ extension Task
637637
638638 var completedCount = 0
639639 let totalCount = tasks. count
640+ let lock = _RecursiveLock ( )
640641
641642 for task in tasks {
642643 task. success { ( value: Value ) -> Void in
643644
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 ( )
649653
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!)
658656 }
657+
658+ fulfill ( values)
659659 }
660+ lock. unlock ( )
660661
661662 } . failure { ( errorInfo: ErrorInfo ) -> Void in
662663
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 ( )
669669 }
670+ lock. unlock ( )
670671 }
671672 }
672673
@@ -684,32 +685,33 @@ extension Task
684685 var completedCount = 0
685686 var rejectedCount = 0
686687 let totalCount = tasks. count
688+ let lock = _RecursiveLock ( )
687689
688690 for task in tasks {
689691 task. success { ( value: Value ) -> Void in
690692
691- synchronized ( self ) {
692- completedCount++
693+ lock. lock ( )
694+ completedCount++
695+
696+ if completedCount == 1 {
697+ fulfill ( value)
693698
694- if completedCount == 1 {
695- fulfill ( value)
696-
697- self . cancelAll ( tasks)
698- }
699+ self . cancelAll ( tasks)
699700 }
701+ lock. unlock ( )
700702
701703 } . failure { ( errorInfo: ErrorInfo ) -> Void in
702704
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 )
705710
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)
712713 }
714+ lock. unlock ( )
713715 }
714716 }
715717
@@ -728,28 +730,29 @@ extension Task
728730
729731 var completedCount = 0
730732 let totalCount = tasks. count
733+ let lock = _RecursiveLock ( )
731734
732735 for task in tasks {
733736 task. then { ( value: Value ? , errorInfo: ErrorInfo ? ) -> Void in
734737
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 ( )
740746
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!)
748750 }
749-
750- fulfill ( values)
751751 }
752+
753+ fulfill ( values)
752754 }
755+ lock. unlock ( )
753756
754757 }
755758 }
@@ -795,15 +798,4 @@ infix operator ~ { associativity left }
795798public func ~ < P, V, E> ( task: Task < P , V , E > , tryCount: Int ) -> Task < P , V , E >
796799{
797800 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)
809801}
0 commit comments