mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-27 04:44:21 +00:00
Simplify resumptions to avoid needing a resumption type and mark continuations as resumed directly in critical regions (#203)
This commit is contained in:
1 parent
f05e450f0b
commit
af9ce91db4
3 files changed
+61
-117
No files matched your search
@@ -111,7 +111,7 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
}
|
||||
|
||||
func cancelNext(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Element?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
let continuation: UnsafeContinuation<Element?, Never>?
|
||||
|
||||
switch state.emission {
|
||||
@@ -132,25 +132,23 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
return continuation
|
||||
}?.resume(returning: nil)
|
||||
continuation?.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func next(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) async -> Element? {
|
||||
return await withUnsafeContinuation { (continuation: UnsafeContinuation<Element?, Never>) in
|
||||
var cancelled = false
|
||||
var terminal = false
|
||||
state.withCriticalRegion { state -> UnsafeResumption<UnsafeContinuation<Element?, Never>?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
if nextTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
||||
cancelled = true
|
||||
return nil
|
||||
}
|
||||
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
state.emission = .awaiting([Awaiting(generation: generation, continuation: continuation)])
|
||||
return nil
|
||||
case .pending(var sends):
|
||||
let send = sends.removeFirst()
|
||||
if sends.count == 0 {
|
||||
@@ -158,16 +156,14 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
} else {
|
||||
state.emission = .pending(sends)
|
||||
}
|
||||
return UnsafeResumption(continuation: send.continuation, success: continuation)
|
||||
send.continuation?.resume(returning: continuation)
|
||||
case .awaiting(var nexts):
|
||||
nexts.updateOrAppend(Awaiting(generation: generation, continuation: continuation))
|
||||
state.emission = .awaiting(nexts)
|
||||
return nil
|
||||
case .finished:
|
||||
terminal = true
|
||||
return nil
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
|
||||
if cancelled || terminal {
|
||||
continuation.resume(returning: nil)
|
||||
@@ -176,7 +172,7 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
}
|
||||
|
||||
func cancelSend(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<UnsafeContinuation<Element?, Never>?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
let continuation: UnsafeContinuation<UnsafeContinuation<Element?, Never>?, Never>?
|
||||
|
||||
switch state.emission {
|
||||
@@ -198,26 +194,25 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
return continuation
|
||||
}?.resume(returning: nil)
|
||||
continuation?.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func send(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int, _ element: Element) async {
|
||||
let continuation = await withUnsafeContinuation { continuation in
|
||||
state.withCriticalRegion { state -> UnsafeResumption<UnsafeContinuation<Element?, Never>?, Never>? in
|
||||
let continuation: UnsafeContinuation<Element?, Never>? = await withUnsafeContinuation { continuation in
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
if sendTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
||||
return UnsafeResumption(continuation: continuation, success: nil)
|
||||
continuation.resume(returning: nil)
|
||||
return
|
||||
}
|
||||
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
state.emission = .pending([Pending(generation: generation, continuation: continuation)])
|
||||
return nil
|
||||
case .pending(var sends):
|
||||
sends.updateOrAppend(Pending(generation: generation, continuation: continuation))
|
||||
state.emission = .pending(sends)
|
||||
return nil
|
||||
case .awaiting(var nexts):
|
||||
let next = nexts.removeFirst().continuation
|
||||
if nexts.count == 0 {
|
||||
@@ -225,11 +220,11 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
} else {
|
||||
state.emission = .awaiting(nexts)
|
||||
}
|
||||
return UnsafeResumption(continuation: continuation, success: next)
|
||||
continuation.resume(returning: next)
|
||||
case .finished:
|
||||
return UnsafeResumption(continuation: continuation, success: nil)
|
||||
continuation.resume(returning: nil)
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
}
|
||||
continuation?.resume(returning: element)
|
||||
}
|
||||
@@ -252,30 +247,25 @@ public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
|
||||
/// Send a finish to all awaiting iterations.
|
||||
/// All subsequent calls to `next(_:)` will resume immediately.
|
||||
public func finish() {
|
||||
let (sends, nexts) = state.withCriticalRegion { state -> (OrderedSet<Pending>, OrderedSet<Awaiting>) in
|
||||
let result: (OrderedSet<Pending>, OrderedSet<Awaiting>)
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
defer { state.emission = .finished }
|
||||
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
result = ([], [])
|
||||
case .pending(let nexts):
|
||||
result = (nexts, [])
|
||||
case .pending(let sends):
|
||||
for send in sends {
|
||||
send.continuation?.resume(returning: nil)
|
||||
}
|
||||
case .awaiting(let nexts):
|
||||
result = ([], nexts)
|
||||
case .finished:
|
||||
result = ([], [])
|
||||
for next in nexts {
|
||||
next.continuation?.resume(returning: nil)
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
state.emission = .finished
|
||||
|
||||
return result
|
||||
}
|
||||
for send in sends {
|
||||
send.continuation?.resume(returning: nil)
|
||||
}
|
||||
for next in nexts {
|
||||
next.continuation?.resume(returning: nil)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// Create an `Iterator` for iteration of an `AsyncChannel`
|
||||
|
||||
@@ -119,7 +119,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
|
||||
func cancelNext(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<Element?, Error>? in
|
||||
state.withCriticalRegion { state in
|
||||
let continuation: UnsafeContinuation<Element?, Error>?
|
||||
|
||||
switch state.emission {
|
||||
@@ -140,8 +140,8 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
}
|
||||
|
||||
return continuation
|
||||
}?.resume(returning: nil)
|
||||
continuation?.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func next(_ nextTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) async throws -> Element? {
|
||||
@@ -149,17 +149,16 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
var cancelled = false
|
||||
var potentialTermination: Termination?
|
||||
|
||||
state.withCriticalRegion { state -> UnsafeResumption<UnsafeContinuation<Element?, Error>?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
if nextTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
||||
cancelled = true
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
state.emission = .awaiting([Awaiting(generation: generation, continuation: continuation)])
|
||||
return nil
|
||||
case .pending(var sends):
|
||||
let send = sends.removeFirst()
|
||||
if sends.count == 0 {
|
||||
@@ -167,17 +166,15 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
} else {
|
||||
state.emission = .pending(sends)
|
||||
}
|
||||
return UnsafeResumption(continuation: send.continuation, success: continuation)
|
||||
send.continuation?.resume(returning: continuation)
|
||||
case .awaiting(var nexts):
|
||||
nexts.updateOrAppend(Awaiting(generation: generation, continuation: continuation))
|
||||
state.emission = .awaiting(nexts)
|
||||
return nil
|
||||
case .terminated(let termination):
|
||||
potentialTermination = termination
|
||||
state.emission = .terminated(.finished)
|
||||
return nil
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
|
||||
if cancelled {
|
||||
continuation.resume(returning: nil)
|
||||
@@ -198,7 +195,7 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
|
||||
func cancelSend(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int) {
|
||||
state.withCriticalRegion { state -> UnsafeContinuation<UnsafeContinuation<Element?, Error>?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
let continuation: UnsafeContinuation<UnsafeContinuation<Element?, Error>?, Never>?
|
||||
|
||||
switch state.emission {
|
||||
@@ -220,26 +217,25 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
}
|
||||
}
|
||||
|
||||
return continuation
|
||||
}?.resume(returning: nil)
|
||||
continuation?.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
|
||||
func send(_ sendTokenStatus: ManagedCriticalState<ChannelTokenStatus>, _ generation: Int, _ element: Element) async {
|
||||
let continuation: UnsafeContinuation<Element?, Error>? = await withUnsafeContinuation { continuation in
|
||||
state.withCriticalRegion { state -> UnsafeResumption<UnsafeContinuation<Element?, Error>?, Never>? in
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
if sendTokenStatus.withCriticalRegion({ $0 }) == .cancelled {
|
||||
return UnsafeResumption(continuation: continuation, success: nil)
|
||||
continuation.resume(returning: nil)
|
||||
return
|
||||
}
|
||||
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
state.emission = .pending([Pending(generation: generation, continuation: continuation)])
|
||||
return nil
|
||||
case .pending(var sends):
|
||||
sends.updateOrAppend(Pending(generation: generation, continuation: continuation))
|
||||
state.emission = .pending(sends)
|
||||
return nil
|
||||
case .awaiting(var nexts):
|
||||
let next = nexts.removeFirst().continuation
|
||||
if nexts.count == 0 {
|
||||
@@ -247,17 +243,17 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
} else {
|
||||
state.emission = .awaiting(nexts)
|
||||
}
|
||||
return UnsafeResumption(continuation: continuation, success: next)
|
||||
continuation.resume(returning: next)
|
||||
case .terminated:
|
||||
return UnsafeResumption(continuation: continuation, success: nil)
|
||||
continuation.resume(returning: nil)
|
||||
}
|
||||
}?.resume()
|
||||
}
|
||||
}
|
||||
continuation?.resume(returning: element)
|
||||
}
|
||||
|
||||
func terminateAll(error: Failure? = nil) {
|
||||
let (sends, nexts) = state.withCriticalRegion { state -> (OrderedSet<Pending>, OrderedSet<Awaiting>) in
|
||||
state.withCriticalRegion { state in
|
||||
|
||||
let nextState: Emission
|
||||
if let error = error {
|
||||
@@ -269,29 +265,24 @@ public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: Asyn
|
||||
switch state.emission {
|
||||
case .idle:
|
||||
state.emission = nextState
|
||||
return ([], [])
|
||||
case .pending(let nexts):
|
||||
case .pending(let sends):
|
||||
state.emission = nextState
|
||||
return (nexts, [])
|
||||
for send in sends {
|
||||
send.continuation?.resume(returning: nil)
|
||||
}
|
||||
case .awaiting(let nexts):
|
||||
state.emission = nextState
|
||||
return ([], nexts)
|
||||
if let error = error {
|
||||
for next in nexts {
|
||||
next.continuation?.resume(throwing: error)
|
||||
}
|
||||
} else {
|
||||
for next in nexts {
|
||||
next.continuation?.resume(returning: nil)
|
||||
}
|
||||
}
|
||||
case .terminated:
|
||||
return ([], [])
|
||||
}
|
||||
}
|
||||
|
||||
for send in sends {
|
||||
send.continuation?.resume(returning: nil)
|
||||
}
|
||||
|
||||
if let error = error {
|
||||
for next in nexts {
|
||||
next.continuation?.resume(throwing: error)
|
||||
}
|
||||
} else {
|
||||
for next in nexts {
|
||||
next.continuation?.resume(returning: nil)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
struct UnsafeResumption<Success, Failure: Error> {
|
||||
let continuation: UnsafeContinuation<Success, Failure>?
|
||||
let result: Result<Success, Failure>
|
||||
|
||||
init(continuation: UnsafeContinuation<Success, Failure>?, result: Result<Success, Failure>) {
|
||||
self.continuation = continuation
|
||||
self.result = result
|
||||
}
|
||||
|
||||
init(continuation: UnsafeContinuation<Success, Failure>?, success: Success) {
|
||||
self.init(continuation: continuation, result: .success(success))
|
||||
}
|
||||
|
||||
init(continuation: UnsafeContinuation<Success, Failure>?, failure: Failure) {
|
||||
self.init(continuation: continuation, result: .failure(failure))
|
||||
}
|
||||
|
||||
func resume() {
|
||||
continuation?.resume(with: result)
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeResumption where Failure == Error {
|
||||
init(continuation: UnsafeContinuation<Success, Failure>, catching body: () throws -> Success) {
|
||||
self.init(continuation: continuation, result: Result(catching: body))
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeResumption where Success == Void {
|
||||
init(continuation: UnsafeContinuation<Success, Failure>) {
|
||||
self.init(continuation: continuation, result: .success(()))
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeResumption: Sendable where Success: Sendable { }
|
||||
|
||||
|
||||
Reference in new issue
Block a user