|
| 1 | +////===----------------------------------------------------------------------===// |
| 2 | +//// |
| 3 | +//// This source file is part of the Swift.org open source project |
| 4 | +//// |
| 5 | +//// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +//// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +//// |
| 8 | +//// See https://swift.org/LICENSE.txt for license information |
| 9 | +//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +//// |
| 11 | +////===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Swift |
| 14 | +@_implementationOnly import _SwiftConcurrencyShims |
| 15 | + |
| 16 | +// ==== Task Cancellation ------------------------------------------------------ |
| 17 | + |
| 18 | +extension Task { |
| 19 | + |
| 20 | + /// Returns `true` if the task is cancelled, and should stop executing. |
| 21 | + /// |
| 22 | + /// ### Suspension |
| 23 | + /// This function returns instantly and will never suspend. |
| 24 | + /// |
| 25 | + /// - SeeAlso: `checkCancellation()` |
| 26 | + /* @instantaneous */ |
| 27 | + public static func isCancelled() async -> Bool { |
| 28 | + // let task = __getTask() // TODO: pending internal API to get tasks |
| 29 | + // task.isCancelled || task.deadline.isOverdue |
| 30 | + fatalError("\(#function) not implemented yet.") |
| 31 | + } |
| 32 | + |
| 33 | + /// Check if the task is cancelled and throw an `CancellationError` if it was. |
| 34 | + /// |
| 35 | + /// It is intentional that no information is passed to the task about why it |
| 36 | + /// was cancelled. A task may be cancelled for many reasons, and additional |
| 37 | + /// reasons may accrue / after the initial cancellation (for example, if the |
| 38 | + /// task fails to immediately exit, it may pass a deadline). |
| 39 | + /// |
| 40 | + /// The goal of cancellation is to allow tasks to be cancelled in a |
| 41 | + /// lightweight way, not to be a secondary method of inter-task communication. |
| 42 | + /// |
| 43 | + /// ### Suspension |
| 44 | + /// This function returns instantly and will never suspend. |
| 45 | + /// |
| 46 | + /// - SeeAlso: `isCancelled()` |
| 47 | + /* @instantaneous */ |
| 48 | + public static func checkCancellation() async throws { |
| 49 | + if await Task.isCancelled() { |
| 50 | + throw CancellationError() |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /// Execute an operation with cancellation handler which will immediately be |
| 55 | + /// invoked if the current task is cancelled. |
| 56 | + /// |
| 57 | + /// This differs from the operation cooperatively checking for cancellation |
| 58 | + /// and reacting to it in that the cancellation handler is _always_ and |
| 59 | + /// _immediately_ invoked when the task is cancelled. For example, even if the |
| 60 | + /// operation is running code which never checks for cancellation, a cancellation |
| 61 | + /// handler still would run and give us a chance to run some cleanup code. |
| 62 | + /// |
| 63 | + /// Does not check for cancellation, and always executes the passed `operation`. |
| 64 | + /// |
| 65 | + /// ### Suspension |
| 66 | + /// This function returns instantly and will never suspend. |
| 67 | + /* @instantaneous */ |
| 68 | + public static func withCancellationHandler<T>( |
| 69 | + handler: /* @concurrent */ () -> (), |
| 70 | + operation: () async throws -> T |
| 71 | + ) async throws -> T { |
| 72 | + fatalError("\(#function) not implemented yet.") |
| 73 | + } |
| 74 | + |
| 75 | + /// The default cancellation thrown when a task is cancelled. |
| 76 | + /// |
| 77 | + /// This error is also thrown automatically by `Task.checkCancellation()`, |
| 78 | + /// if the current task has been cancelled. |
| 79 | + public struct CancellationError: Error { |
| 80 | + // no extra information, cancellation is intended to be light-weight |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +// ==== Task Deadlines --------------------------------------------------------- |
| 86 | + |
| 87 | +extension Task { |
| 88 | + |
| 89 | + /// Returns the earliest deadline set on the current task. |
| 90 | + /// |
| 91 | + /// If no deadline was set for the task the `Deadline.distantFuture` is returned, |
| 92 | + /// as it is effective in conveying that there still is time remaining and the |
| 93 | + /// deadline is not overdue yet. |
| 94 | + /// |
| 95 | + /// ### Suspension |
| 96 | + /// This function returns instantly and will never suspend. |
| 97 | + /* @instantaneous */ |
| 98 | + public static func currentDeadline() async -> Deadline { |
| 99 | + fatalError("\(#function) not implemented yet.") |
| 100 | + } |
| 101 | + |
| 102 | + /// Execute a code block with a deadline in `interval`. |
| 103 | + /// |
| 104 | + /// If the current task already has a deadline set that is _prior_ |
| 105 | + /// to the newly set deadline with this API, that deadline will remain in effect. |
| 106 | + /// |
| 107 | + /// This allows higher level tasks to set an upper bound on the deadline they |
| 108 | + /// are willing cumulatively willing to wait for the entire task to execute, |
| 109 | + /// regardless of the inner deadlines of the specific child tasks. |
| 110 | + /// |
| 111 | + /// Cancellation is co-operative and must be checked for by the operation, e.g. |
| 112 | + /// by invoking `Task.checkCancellation`, or `Task.isCancelled`. |
| 113 | + /// |
| 114 | + /// ### Suspension |
| 115 | + /// This function returns instantly and will never suspend. |
| 116 | + /// |
| 117 | + /// - Parameters: |
| 118 | + /// - interval: interval after which (from `now()`) the operation task should |
| 119 | + /// be considered cancelled. |
| 120 | + /// - operation: the operation to execute |
| 121 | + /* @instantaneous */ |
| 122 | + public static func withDeadline<T>( |
| 123 | + in interval: _TimeInterval, |
| 124 | + operation: () async throws -> T |
| 125 | + ) async rethrows -> T { |
| 126 | + fatalError("\(#function) not implemented yet.") |
| 127 | + } |
| 128 | + |
| 129 | + /// Execute a code block with the passed in deadline (unless a shorter deadline is already set). |
| 130 | + /// |
| 131 | + /// If the current task already has a deadline set that is _prior_ |
| 132 | + /// to the newly set deadline with this API, that deadline will remain in effect. |
| 133 | + /// |
| 134 | + /// This allows higher level tasks to set an upper bound on the deadline they |
| 135 | + /// are willing cumulatively willing to wait for the entire task to execute, |
| 136 | + /// regardless of the inner deadlines of the specific child tasks. |
| 137 | + /// |
| 138 | + /// Cancellation is co-operative and must be checked for by the operation, e.g. |
| 139 | + /// by invoking `Task.checkCancellation` or `Task.isCancelled`. |
| 140 | + /// |
| 141 | + /// ### Suspension |
| 142 | + /// This function returns instantly and will never suspend. |
| 143 | + /// |
| 144 | + /// - Parameters: |
| 145 | + /// - deadline: the point in time after which the operation task should be |
| 146 | + /// considered cancelled. |
| 147 | + /// - operation: the operation to execute |
| 148 | + /* @instantaneous */ |
| 149 | + public static func withDeadline<T>( |
| 150 | + _ deadline: Deadline, |
| 151 | + operation: () async throws -> T |
| 152 | + ) async rethrows -> T { |
| 153 | + fatalError("\(#function) not implemented yet.") |
| 154 | + } |
| 155 | + |
| 156 | + /// A deadline is a point in time past-which a task should be considered cancelled. |
| 157 | + /// |
| 158 | + /// Deadlines function the same was as pure cancellation, in the sense that they |
| 159 | + /// are cooperative and require the cancelled (deadline exceeding) task to check |
| 160 | + /// for this as it is performing its execution. |
| 161 | + /// |
| 162 | + /// Generally tasks (or partial tasks) should perform such check before they |
| 163 | + /// start executing, however this is not a strict rule, and some tasks may |
| 164 | + /// choose to be un-cancellable. |
| 165 | + public struct Deadline { |
| 166 | + public typealias WallTime = UInt64 // equivalent to DispatchWallTime |
| 167 | + internal let time: WallTime |
| 168 | + |
| 169 | + public init(at time: WallTime) { |
| 170 | + self.time = time |
| 171 | + } |
| 172 | + |
| 173 | + public static var distantFuture: Self { |
| 174 | + .init(at: .max) |
| 175 | + } |
| 176 | + |
| 177 | + public static func `in`(_ interval: _TimeInterval) -> Self { |
| 178 | + // now() + interval |
| 179 | + fatalError("#\(#function) not implemented yet.") |
| 180 | + } |
| 181 | + |
| 182 | + /// Returns `true` if the deadline is overdue and deadline should be |
| 183 | + /// considered overdue (or "exceeded"). |
| 184 | + /// |
| 185 | + /// If this deadline was related to a `Task`, that task should be considered |
| 186 | + /// cancelled if the deadline is overdue. |
| 187 | + public var isOverdue: Bool { |
| 188 | + !self.hasTimeLeft |
| 189 | + } |
| 190 | + |
| 191 | + /// Returns `true` if the deadline is still pending with respect to "now". |
| 192 | + public var hasTimeLeft: Bool { |
| 193 | + fatalError("\(#function) not implemented yet.")// self.hasTimeLeft(until: now()) |
| 194 | + } |
| 195 | + |
| 196 | + // TODO: public func hasTimeLeft(until: DispatchWallTime or whichever time type we'll use) -> Bool |
| 197 | + |
| 198 | + } |
| 199 | +} |
0 commit comments