|
| 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 ------------------------------------------------------------------- |
| 17 | + |
| 18 | +/// An asynchronous task (just "Task" hereafter) is the analogue of a thread for |
| 19 | +/// asynchronous functions. All asynchronous functions run as part of some task. |
| 20 | +/// |
| 21 | +/// A task's execution can be seen as a series of periods where the task was |
| 22 | +/// running. Each such period ends at a suspension point or -- finally -- the |
| 23 | +/// completion of the task. |
| 24 | +/// |
| 25 | +/// These partial periods towards the task's completion are `PartialAsyncTask`. |
| 26 | +/// Partial tasks are generally not interacted with by end-users directly, |
| 27 | +/// unless implementing a scheduler. |
| 28 | +public struct Task { |
| 29 | +} |
| 30 | + |
| 31 | +// ==== UnsafeContinuation ----------------------------------------------------- |
| 32 | + |
| 33 | +extension Task { |
| 34 | + public struct UnsafeContinuation<T> { |
| 35 | + /// Return a value into the continuation and make the task schedulable. |
| 36 | + /// |
| 37 | + /// The task will never run synchronously, even if the task does not |
| 38 | + /// need to be resumed on a specific executor. |
| 39 | + /// |
| 40 | + /// This is appropriate when the caller is something "busy", like an event |
| 41 | + /// loop, and doesn't want to be potentially delayed by arbitrary work. |
| 42 | + public func resume(returning: T) { |
| 43 | + fatalError("\(#function) not implemented yet.") |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public struct UnsafeThrowingContinuation<T, E: Error> { |
| 48 | + /// Return a value into the continuation and make the task schedulable. |
| 49 | + /// |
| 50 | + /// The task will never run synchronously, even if the task does not |
| 51 | + /// need to be resumed on a specific executor. |
| 52 | + /// |
| 53 | + /// This is appropriate when the caller is something "busy", like an event |
| 54 | + /// loop, and doesn't want to be potentially delayed by arbitrary work. |
| 55 | + public func resume(returning: T) { |
| 56 | + fatalError("\(#function) not implemented yet.") |
| 57 | + } |
| 58 | + |
| 59 | + /// Resume the continuation with an error and make the task schedulable. |
| 60 | + /// |
| 61 | + /// The task will never run synchronously, even if the task does not |
| 62 | + /// need to be resumed on a specific executor. |
| 63 | + /// |
| 64 | + /// This is appropriate when the caller is something "busy", like an event |
| 65 | + /// loop, and doesn't want to be potentially delayed by arbitrary work. |
| 66 | + public func resume(throwing: E) { |
| 67 | + fatalError("\(#function) not implemented yet.") |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// The operation functions must resume the continuation *exactly once*. |
| 72 | + /// |
| 73 | + /// The continuation will not begin executing until the operation function returns. |
| 74 | + public static func withUnsafeContinuation<T>( |
| 75 | + operation: (UnsafeContinuation<T>) -> Void |
| 76 | + ) async -> T { |
| 77 | + fatalError("\(#function) not implemented yet.") |
| 78 | + } |
| 79 | + |
| 80 | + /// The operation functions must resume the continuation *exactly once*. |
| 81 | + /// |
| 82 | + /// The continuation will not begin executing until the operation function returns. |
| 83 | + public static func withUnsafeThrowingContinuation<T>( |
| 84 | + operation: (UnsafeThrowingContinuation<T, Error>) -> Void |
| 85 | + ) async throws -> T { |
| 86 | + fatalError("\(#function) not implemented yet.") |
| 87 | + } |
| 88 | +} |
0 commit comments