1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
struct ChannelStorage<Element: Sendable, Failure: Error>: Sendable {
private let stateMachine: ManagedCriticalState<ChannelStateMachine<Element, Failure>>
private let ids = ManagedCriticalState<UInt64>(0)
init() {
self.stateMachine = ManagedCriticalState(ChannelStateMachine())
}
func generateId() -> UInt64 {
self.ids.withCriticalRegion { ids in
defer { ids &+= 1 }
return ids
}
}
func send(element: Element) async {
// check if a suspension is needed
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.send()
}
switch action {
case .suspend:
break
case .resumeConsumer(let continuation):
continuation?.resume(returning: element)
return
}
let producerID = self.generateId()
await withTaskCancellationHandler {
// a suspension is needed
await withUnsafeContinuation { (continuation: UnsafeContinuation<Void, Never>) in
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.sendSuspended(continuation: continuation, element: element, producerID: producerID)
}
switch action {
case .none:
break
case .resumeProducer:
continuation.resume()
case .resumeProducerAndConsumer(let consumerContinuation):
continuation.resume()
consumerContinuation?.resume(returning: element)
}
}
} onCancel: {
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.sendCancelled(producerID: producerID)
}
switch action {
case .none:
break
case .resumeProducer(let continuation):
continuation?.resume()
}
}
}
func finish(error: Failure? = nil) {
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.finish(error: error)
}
switch action {
case .none:
break
case .resumeProducersAndConsumers(let producerContinuations, let consumerContinuations):
producerContinuations.forEach { $0?.resume() }
if let error {
consumerContinuations.forEach { $0?.resume(throwing: error) }
} else {
consumerContinuations.forEach { $0?.resume(returning: nil) }
}
}
}
func next() async throws -> Element? {
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.next()
}
switch action {
case .suspend:
break
case .resumeProducer(let producerContinuation, let result):
producerContinuation?.resume()
return try result._rethrowGet()
}
let consumerID = self.generateId()
return try await withTaskCancellationHandler {
try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Element?, any Error>) in
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.nextSuspended(
continuation: continuation,
consumerID: consumerID
)
}
switch action {
case .none:
break
case .resumeConsumer(let element):
continuation.resume(returning: element)
case .resumeConsumerWithError(let error):
continuation.resume(throwing: error)
case .resumeProducerAndConsumer(let producerContinuation, let element):
producerContinuation?.resume()
continuation.resume(returning: element)
}
}
} onCancel: {
let action = self.stateMachine.withCriticalRegion { stateMachine in
stateMachine.nextCancelled(consumerID: consumerID)
}
switch action {
case .none:
break
case .resumeConsumer(let continuation):
continuation?.resume(returning: nil)
}
}
}
}
|