|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift Distributed Actors open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift Distributed Actors project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift Distributed Actors project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import DistributedActorsConcurrencyHelpers |
| 16 | +import NIOConcurrencyHelpers |
| 17 | + |
| 18 | +/// A checked continuation that offers easier APIs for working with cancellation, |
| 19 | +/// as well as has its unique identity. |
| 20 | +internal final class ClusterCancellableCheckedContinuation<Success>: Hashable, @unchecked Sendable where Success: Sendable { |
| 21 | + private struct _State: Sendable { |
| 22 | + var cancelled: Bool = false |
| 23 | + var onCancel: (@Sendable (ClusterCancellableCheckedContinuation<Success>) -> Void)? |
| 24 | + var continuation: CheckedContinuation<Success, any Error>? |
| 25 | + } |
| 26 | + |
| 27 | + private let state: NIOLockedValueBox<_State> = .init(_State()) |
| 28 | + |
| 29 | + fileprivate init() {} |
| 30 | + |
| 31 | + func setContinuation(_ continuation: CheckedContinuation<Success, any Error>) -> Bool { |
| 32 | + var alreadyCancelled = false |
| 33 | + self.state.withLockedValue { state in |
| 34 | + if state.cancelled { |
| 35 | + alreadyCancelled = true |
| 36 | + } else { |
| 37 | + state.continuation = continuation |
| 38 | + } |
| 39 | + } |
| 40 | + if alreadyCancelled { |
| 41 | + continuation.resume(throwing: CancellationError()) |
| 42 | + } |
| 43 | + return !alreadyCancelled |
| 44 | + } |
| 45 | + |
| 46 | + /// Register a cancellation handler, or call it immediately if the continuation was already cancelled. |
| 47 | + @Sendable |
| 48 | + func onCancel(handler: @Sendable @escaping (ClusterCancellableCheckedContinuation<Success>) -> Void) { |
| 49 | + var alreadyCancelled: Bool = self.state.withLockedValue { state in |
| 50 | + if state.cancelled { |
| 51 | + return true |
| 52 | + } |
| 53 | + |
| 54 | + state.onCancel = handler |
| 55 | + return false |
| 56 | + } |
| 57 | + if alreadyCancelled { |
| 58 | + handler(self) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private func withContinuation(cancelled: Bool = false, _ operation: (CheckedContinuation<Success, any Error>) -> Void) { |
| 63 | + var safeContinuation: CheckedContinuation<Success, any Error>? |
| 64 | + var safeOnCancel: (@Sendable (ClusterCancellableCheckedContinuation<Success>) -> Void)? |
| 65 | + self.state.withLockedValue { (state: inout _State) -> Void in |
| 66 | + state.cancelled = state.cancelled || cancelled |
| 67 | + safeContinuation = state.continuation |
| 68 | + safeOnCancel = state.onCancel |
| 69 | + state.continuation = nil |
| 70 | + state.onCancel = nil |
| 71 | + } |
| 72 | + if let safeContinuation { |
| 73 | + operation(safeContinuation) |
| 74 | + } |
| 75 | + if cancelled { |
| 76 | + safeOnCancel?(self) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + func resume(returning value: Success) { |
| 81 | + self.withContinuation { |
| 82 | + $0.resume(returning: value) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func resume(throwing error: any Error) { |
| 87 | + self.withContinuation { |
| 88 | + $0.resume(throwing: error) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + var isCancelled: Bool { |
| 93 | + self.state.withLockedValue { $0.cancelled } |
| 94 | + } |
| 95 | + |
| 96 | + func cancel() { |
| 97 | + self.withContinuation(cancelled: true) { |
| 98 | + $0.resume(throwing: CancellationError()) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +extension ClusterCancellableCheckedContinuation where Success == Void { |
| 104 | + func resume() { |
| 105 | + self.resume(returning: ()) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +extension ClusterCancellableCheckedContinuation { |
| 110 | + static func == (lhs: ClusterCancellableCheckedContinuation, rhs: ClusterCancellableCheckedContinuation) -> Bool { |
| 111 | + ObjectIdentifier(lhs) == ObjectIdentifier(rhs) |
| 112 | + } |
| 113 | + |
| 114 | + func hash(into hasher: inout Hasher) { |
| 115 | + hasher.combine(ObjectIdentifier(self)) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func _withClusterCancellableCheckedContinuation<Success>( |
| 120 | + of successType: Success.Type = Success.self, |
| 121 | + _ body: @escaping (ClusterCancellableCheckedContinuation<Success>) -> Void, |
| 122 | + function: String = #function |
| 123 | +) async throws -> Success where Success: Sendable { |
| 124 | + let cccc = ClusterCancellableCheckedContinuation<Success>() |
| 125 | + return try await withTaskCancellationHandler { |
| 126 | + try await withCheckedThrowingContinuation(function: function) { continuation in |
| 127 | + if cccc.setContinuation(continuation) { |
| 128 | + body(cccc) |
| 129 | + } |
| 130 | + } |
| 131 | + } onCancel: { |
| 132 | + cccc.cancel() |
| 133 | + } |
| 134 | +} |
0 commit comments