@@ -20,34 +20,34 @@ internal class _StateMachine<Progress, Value, Error>
20
20
internal typealias ProgressTupleHandler = Task < Progress , Value , Error > . _ProgressTupleHandler
21
21
22
22
internal let weakified : Bool
23
- internal private ( set ) var state : TaskState
23
+ internal let state : _Atomic < TaskState >
24
24
25
- internal private( set) var progress : Progress ? // NOTE: always nil if `weakified = true`
26
- internal private( set) var value : Value ?
27
- internal private( set) var errorInfo : ErrorInfo ?
25
+ internal let progress : _Atomic < Progress ? > = _Atomic ( nil ) // NOTE: always nil if `weakified = true`
26
+ internal let value : _Atomic < Value ? > = _Atomic ( nil )
27
+ internal let errorInfo : _Atomic < ErrorInfo ? > = _Atomic ( nil )
28
+
29
+ internal let configuration = TaskConfiguration ( )
28
30
29
31
/// wrapper closure for `_initClosure` to invoke only once when started `.Running`,
30
32
/// and will be set to `nil` afterward
31
33
internal var initResumeClosure : ( Void -> Void ) ?
32
34
33
- internal private( set) lazy var progressTupleHandlers = _Handlers < ProgressTupleHandler > ( )
34
- internal private( set) lazy var completionHandlers = _Handlers < Void -> Void > ( )
35
-
36
- internal let configuration = TaskConfiguration ( )
35
+ private lazy var _progressTupleHandlers = _Handlers < ProgressTupleHandler > ( )
36
+ private lazy var _completionHandlers = _Handlers < Void -> Void > ( )
37
37
38
38
private let _recursiveLock = _RecursiveLock ( )
39
39
40
40
internal init ( weakified: Bool , paused: Bool )
41
41
{
42
42
self . weakified = weakified
43
- self . state = paused ? . Paused : . Running
43
+ self . state = _Atomic ( paused ? . Paused : . Running)
44
44
}
45
45
46
46
internal func addProgressTupleHandler( inout token: _HandlerToken ? , _ progressTupleHandler: ProgressTupleHandler ) -> Bool
47
47
{
48
48
self . _recursiveLock. lock ( )
49
- if self . state == . Running || self . state == . Paused {
50
- token = self . progressTupleHandlers . append ( progressTupleHandler)
49
+ if self . state. rawValue == . Running || self . state. rawValue == . Paused {
50
+ token = self . _progressTupleHandlers . append ( progressTupleHandler)
51
51
self . _recursiveLock. unlock ( )
52
52
return token != nil
53
53
}
@@ -61,7 +61,7 @@ internal class _StateMachine<Progress, Value, Error>
61
61
{
62
62
self . _recursiveLock. lock ( )
63
63
if let handlerToken = handlerToken {
64
- let removedHandler = self . progressTupleHandlers . remove ( handlerToken)
64
+ let removedHandler = self . _progressTupleHandlers . remove ( handlerToken)
65
65
self . _recursiveLock. unlock ( )
66
66
return removedHandler != nil
67
67
}
@@ -74,8 +74,8 @@ internal class _StateMachine<Progress, Value, Error>
74
74
internal func addCompletionHandler( inout token: _HandlerToken ? , _ completionHandler: Void -> Void ) -> Bool
75
75
{
76
76
self . _recursiveLock. lock ( )
77
- if self . state == . Running || self . state == . Paused {
78
- token = self . completionHandlers . append ( completionHandler)
77
+ if self . state. rawValue == . Running || self . state. rawValue == . Paused {
78
+ token = self . _completionHandlers . append ( completionHandler)
79
79
self . _recursiveLock. unlock ( )
80
80
return token != nil
81
81
}
@@ -89,7 +89,7 @@ internal class _StateMachine<Progress, Value, Error>
89
89
{
90
90
self . _recursiveLock. lock ( )
91
91
if let handlerToken = handlerToken {
92
- let removedHandler = self . completionHandlers . remove ( handlerToken)
92
+ let removedHandler = self . _completionHandlers . remove ( handlerToken)
93
93
self . _recursiveLock. unlock ( )
94
94
return removedHandler != nil
95
95
}
@@ -102,16 +102,16 @@ internal class _StateMachine<Progress, Value, Error>
102
102
internal func handleProgress( progress: Progress )
103
103
{
104
104
self . _recursiveLock. lock ( )
105
- if self . state == . Running {
105
+ if self . state. rawValue == . Running {
106
106
107
- let oldProgress = self . progress
107
+ let oldProgress = self . progress. rawValue
108
108
109
109
// NOTE: if `weakified = false`, don't store progressValue for less memory footprint
110
110
if !self . weakified {
111
- self . progress = progress
111
+ self . progress. rawValue = progress
112
112
}
113
113
114
- for handler in self . progressTupleHandlers {
114
+ for handler in self . _progressTupleHandlers {
115
115
handler ( oldProgress: oldProgress, newProgress: progress)
116
116
}
117
117
self . _recursiveLock. unlock ( )
@@ -124,9 +124,9 @@ internal class _StateMachine<Progress, Value, Error>
124
124
internal func handleFulfill( value: Value )
125
125
{
126
126
self . _recursiveLock. lock ( )
127
- if self . state == . Running {
128
- self . state = . Fulfilled
129
- self . value = value
127
+ if self . state. rawValue == . Running {
128
+ self . state. rawValue = . Fulfilled
129
+ self . value. rawValue = value
130
130
self . _finish ( )
131
131
self . _recursiveLock. unlock ( )
132
132
}
@@ -138,9 +138,9 @@ internal class _StateMachine<Progress, Value, Error>
138
138
internal func handleRejectInfo( errorInfo: ErrorInfo )
139
139
{
140
140
self . _recursiveLock. lock ( )
141
- if self . state == . Running || self . state == . Paused {
142
- self . state = errorInfo. isCancelled ? . Cancelled : . Rejected
143
- self . errorInfo = errorInfo
141
+ if self . state. rawValue == . Running || self . state. rawValue == . Paused {
142
+ self . state. rawValue = errorInfo. isCancelled ? . Cancelled : . Rejected
143
+ self . errorInfo. rawValue = errorInfo
144
144
self . _finish ( )
145
145
self . _recursiveLock. unlock ( )
146
146
}
@@ -152,9 +152,9 @@ internal class _StateMachine<Progress, Value, Error>
152
152
internal func handlePause( ) -> Bool
153
153
{
154
154
self . _recursiveLock. lock ( )
155
- if self . state == . Running {
155
+ if self . state. rawValue == . Running {
156
156
self . configuration. pause ? ( )
157
- self . state = . Paused
157
+ self . state. rawValue = . Paused
158
158
self . _recursiveLock. unlock ( )
159
159
return true
160
160
}
@@ -196,9 +196,9 @@ internal class _StateMachine<Progress, Value, Error>
196
196
{
197
197
if ( self . initResumeClosure != nil ) {
198
198
199
- let isInitPaused = ( self . state == . Paused)
199
+ let isInitPaused = ( self . state. rawValue == . Paused)
200
200
if isInitPaused {
201
- self . state = . Running // switch `.Paused` => `.Resume` temporarily without invoking `configure.resume()`
201
+ self . state. rawValue = . Running // switch `.Paused` => `.Resume` temporarily without invoking `configure.resume()`
202
202
}
203
203
204
204
// NOTE: performing `initResumeClosure` might change `state` to `.Fulfilled` or `.Rejected` **immediately**
@@ -207,17 +207,17 @@ internal class _StateMachine<Progress, Value, Error>
207
207
208
208
// switch back to `.Paused` if temporary `.Running` has not changed
209
209
// so that consecutive `_handleResume()` can perform `configure.resume()`
210
- if isInitPaused && self . state == . Running {
211
- self . state = . Paused
210
+ if isInitPaused && self . state. rawValue == . Running {
211
+ self . state. rawValue = . Paused
212
212
}
213
213
}
214
214
}
215
215
216
216
private func _handleResume( ) -> Bool
217
217
{
218
- if self . state == . Paused {
218
+ if self . state. rawValue == . Paused {
219
219
self . configuration. resume ? ( )
220
- self . state = . Running
220
+ self . state. rawValue = . Running
221
221
return true
222
222
}
223
223
else {
@@ -228,9 +228,9 @@ internal class _StateMachine<Progress, Value, Error>
228
228
internal func handleCancel( error: Error ? = nil ) -> Bool
229
229
{
230
230
self . _recursiveLock. lock ( )
231
- if self . state == . Running || self . state == . Paused {
232
- self . state = . Cancelled
233
- self . errorInfo = ErrorInfo ( error: error, isCancelled: true )
231
+ if self . state. rawValue == . Running || self . state. rawValue == . Paused {
232
+ self . state. rawValue = . Cancelled
233
+ self . errorInfo. rawValue = ErrorInfo ( error: error, isCancelled: true )
234
234
self . _finish ( )
235
235
self . _recursiveLock. unlock ( )
236
236
return true
@@ -243,17 +243,17 @@ internal class _StateMachine<Progress, Value, Error>
243
243
244
244
private func _finish( )
245
245
{
246
- for handler in self . completionHandlers {
246
+ for handler in self . _completionHandlers {
247
247
handler ( )
248
248
}
249
249
250
- self . progressTupleHandlers . removeAll ( )
251
- self . completionHandlers . removeAll ( )
250
+ self . _progressTupleHandlers . removeAll ( )
251
+ self . _completionHandlers . removeAll ( )
252
252
253
253
self . configuration. finish ( )
254
254
255
255
self . initResumeClosure = nil
256
- self . progress = nil
256
+ self . progress. rawValue = nil
257
257
}
258
258
}
259
259
0 commit comments