@@ -119,7 +119,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
119
119
}
120
120
121
121
func cancelNext( _ nextTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) {
122
- state. withCriticalRegion { state -> UnsafeContinuation < Element ? , Error > ? in
122
+ state. withCriticalRegion { state in
123
123
let continuation : UnsafeContinuation < Element ? , Error > ?
124
124
125
125
switch state. emission {
@@ -140,44 +140,41 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
140
140
}
141
141
}
142
142
143
- return continuation
144
- } ? . resume ( returning : nil )
143
+ continuation? . resume ( returning : nil )
144
+ }
145
145
}
146
146
147
147
func next( _ nextTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) async throws -> Element ? {
148
148
return try await withUnsafeThrowingContinuation { ( continuation: UnsafeContinuation < Element ? , Error > ) in
149
149
var cancelled = false
150
150
var potentialTermination : Termination ?
151
151
152
- state. withCriticalRegion { state -> UnsafeResumption < UnsafeContinuation < Element ? , Error > ? , Never > ? in
152
+ state. withCriticalRegion { state in
153
153
154
154
if nextTokenStatus. withCriticalRegion ( { $0 } ) == . cancelled {
155
155
cancelled = true
156
- return nil
156
+ return
157
157
}
158
158
159
159
switch state. emission {
160
160
case . idle:
161
161
state. emission = . awaiting( [ Awaiting ( generation: generation, continuation: continuation) ] )
162
- return nil
163
162
case . pending( var sends) :
164
163
let send = sends. removeFirst ( )
165
164
if sends. count == 0 {
166
165
state. emission = . idle
167
166
} else {
168
167
state. emission = . pending( sends)
169
168
}
170
- return UnsafeResumption ( continuation : send. continuation, success : continuation)
169
+ send. continuation? . resume ( returning : continuation)
171
170
case . awaiting( var nexts) :
172
171
nexts. updateOrAppend ( Awaiting ( generation: generation, continuation: continuation) )
173
172
state. emission = . awaiting( nexts)
174
- return nil
175
173
case . terminated( let termination) :
176
174
potentialTermination = termination
177
175
state. emission = . terminated( . finished)
178
- return nil
179
176
}
180
- } ? . resume ( )
177
+ }
181
178
182
179
if cancelled {
183
180
continuation. resume ( returning: nil )
@@ -198,7 +195,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
198
195
}
199
196
200
197
func cancelSend( _ sendTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int ) {
201
- state. withCriticalRegion { state -> UnsafeContinuation < UnsafeContinuation < Element ? , Error > ? , Never > ? in
198
+ state. withCriticalRegion { state in
202
199
let continuation : UnsafeContinuation < UnsafeContinuation < Element ? , Error > ? , Never > ?
203
200
204
201
switch state. emission {
@@ -220,44 +217,43 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
220
217
}
221
218
}
222
219
223
- return continuation
224
- } ? . resume ( returning : nil )
220
+ continuation? . resume ( returning : nil )
221
+ }
225
222
}
226
223
227
224
func send( _ sendTokenStatus: ManagedCriticalState < ChannelTokenStatus > , _ generation: Int , _ element: Element ) async {
228
225
let continuation : UnsafeContinuation < Element ? , Error > ? = await withUnsafeContinuation { continuation in
229
- state. withCriticalRegion { state -> UnsafeResumption < UnsafeContinuation < Element ? , Error > ? , Never > ? in
226
+ state. withCriticalRegion { state in
230
227
231
228
if sendTokenStatus. withCriticalRegion ( { $0 } ) == . cancelled {
232
- return UnsafeResumption ( continuation: continuation, success: nil )
229
+ continuation. resume ( returning: nil )
230
+ return
233
231
}
234
232
235
233
switch state. emission {
236
234
case . idle:
237
235
state. emission = . pending( [ Pending ( generation: generation, continuation: continuation) ] )
238
- return nil
239
236
case . pending( var sends) :
240
237
sends. updateOrAppend ( Pending ( generation: generation, continuation: continuation) )
241
238
state. emission = . pending( sends)
242
- return nil
243
239
case . awaiting( var nexts) :
244
240
let next = nexts. removeFirst ( ) . continuation
245
241
if nexts. count == 0 {
246
242
state. emission = . idle
247
243
} else {
248
244
state. emission = . awaiting( nexts)
249
245
}
250
- return UnsafeResumption ( continuation: continuation , success : next)
246
+ continuation. resume ( returning : next)
251
247
case . terminated:
252
- return UnsafeResumption ( continuation: continuation , success : nil )
248
+ continuation. resume ( returning : nil )
253
249
}
254
- } ? . resume ( )
250
+ }
255
251
}
256
252
continuation? . resume ( returning: element)
257
253
}
258
254
259
255
func terminateAll( error: Failure ? = nil ) {
260
- let ( sends , nexts ) = state. withCriticalRegion { state -> ( OrderedSet < Pending > , OrderedSet < Awaiting > ) in
256
+ state. withCriticalRegion { state in
261
257
262
258
let nextState : Emission
263
259
if let error = error {
@@ -269,29 +265,24 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
269
265
switch state. emission {
270
266
case . idle:
271
267
state. emission = nextState
272
- return ( [ ] , [ ] )
273
- case . pending( let nexts) :
268
+ case . pending( let sends) :
274
269
state. emission = nextState
275
- return ( nexts, [ ] )
270
+ for send in sends {
271
+ send. continuation? . resume ( returning: nil )
272
+ }
276
273
case . awaiting( let nexts) :
277
274
state. emission = nextState
278
- return ( [ ] , nexts)
275
+ if let error = error {
276
+ for next in nexts {
277
+ next. continuation? . resume ( throwing: error)
278
+ }
279
+ } else {
280
+ for next in nexts {
281
+ next. continuation? . resume ( returning: nil )
282
+ }
283
+ }
279
284
case . terminated:
280
- return ( [ ] , [ ] )
281
- }
282
- }
283
-
284
- for send in sends {
285
- send. continuation? . resume ( returning: nil )
286
- }
287
-
288
- if let error = error {
289
- for next in nexts {
290
- next. continuation? . resume ( throwing: error)
291
- }
292
- } else {
293
- for next in nexts {
294
- next. continuation? . resume ( returning: nil )
285
+ break
295
286
}
296
287
}
297
288
}
0 commit comments