|
| 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 Nursery ----------------------------------------------------------- |
| 17 | + |
| 18 | +extension Task { |
| 19 | + |
| 20 | + /// Starts a new nursery which provides a scope in which a dynamic number of |
| 21 | + /// tasks may be spawned. |
| 22 | + /// |
| 23 | + /// Tasks added to the nursery by `nursery.add()` will automatically be |
| 24 | + /// awaited on when the scope exits. |
| 25 | + /// |
| 26 | + /// ### Implicit awaiting |
| 27 | + /// When results of tasks added to the nursery need to be collected, one will |
| 28 | + /// gather task's results using the `while let result = await nursery.next() { ... }` |
| 29 | + /// pattern. |
| 30 | + /// |
| 31 | + /// ### Cancellation |
| 32 | + /// If any of the tasks throws the nursery and all of its tasks will be cancelled, |
| 33 | + /// and the error will be re-thrown by `withNursery`. |
| 34 | + /// |
| 35 | + /// Postcondition: |
| 36 | + /// Once `withNursery` returns it is guaranteed that the *nursery* is *empty*. |
| 37 | + /// |
| 38 | + /// This is achieved in the following way: |
| 39 | + /// - if the body returns normally: |
| 40 | + /// - the nursery will await any not yet complete tasks, |
| 41 | + /// - if any of those tasks throws, the remaining tasks will be cancelled, |
| 42 | + /// - once the `withNursery` returns the nursery is guaranteed to be empty. |
| 43 | + /// - if the body throws: |
| 44 | + /// - all tasks remaining in the nursery will be automatically cancelled. |
| 45 | + /// |
| 46 | + // TODO: Do we have to add a different nursery type to accommodate throwing |
| 47 | + // tasks without forcing users to use Result? I can't think of how that |
| 48 | + // could be propagated out of the callback body reasonably, unless we |
| 49 | + // commit to doing multi-statement closure typechecking. |
| 50 | + public static func withNursery<TaskResult, BodyResult>( |
| 51 | + resultType: TaskResult.Type, |
| 52 | + returning returnType: BodyResult.Type = BodyResult.self, |
| 53 | + body: (inout Nursery<TaskResult>) async throws -> BodyResult |
| 54 | + ) async rethrows -> BodyResult { |
| 55 | + fatalError("\(#function) not implemented yet.") |
| 56 | + } |
| 57 | + |
| 58 | + /// A nursery provides a scope within which a dynamic number of tasks may be |
| 59 | + /// started and added to the nursery. |
| 60 | + /* @unmoveable */ |
| 61 | + public struct Nursery<TaskResult> { |
| 62 | + /// No public initializers |
| 63 | + private init() {} |
| 64 | + |
| 65 | + // Swift will statically prevent this type from being copied or moved. |
| 66 | + // For now, that implies that it cannot be used with generics. |
| 67 | + |
| 68 | + /// Add a child task to the nursery. |
| 69 | + /// |
| 70 | + /// ### Error handling |
| 71 | + /// Operations are allowed to throw. |
| 72 | + /// |
| 73 | + /// in which case the `await try next()` |
| 74 | + /// invocation corresponding to the failed task will re-throw the given task. |
| 75 | + /// |
| 76 | + /// - Parameters: |
| 77 | + /// - overridingPriority: override priority of the operation task |
| 78 | + /// - operation: operation to execute and add to the nursery |
| 79 | + public mutating func add( |
| 80 | + overridingPriority: Priority? = nil, |
| 81 | + operation: () async throws -> TaskResult |
| 82 | + ) async { |
| 83 | + fatalError("\(#function) not implemented yet.") |
| 84 | + } |
| 85 | + |
| 86 | + /// Add a child task and return a `Task.Handle` that can be used to manage it. |
| 87 | + /// |
| 88 | + /// The task's result is accessible either via the returned `handle` or the |
| 89 | + /// `nursery.next()` function (as any other `add`-ed task). |
| 90 | + /// |
| 91 | + /// - Parameters: |
| 92 | + /// - overridingPriority: override priority of the operation task |
| 93 | + /// - operation: operation to execute and add to the nursery |
| 94 | + public mutating func addWithHandle( |
| 95 | + overridingPriority: Priority? = nil, |
| 96 | + operation: () async throws -> TaskResult |
| 97 | + ) async -> Handle<TaskResult> { |
| 98 | + fatalError("\(#function) not implemented yet.") |
| 99 | + } |
| 100 | + |
| 101 | + /// Wait for a child task to complete and return the result it returned, |
| 102 | + /// or else return. |
| 103 | + /// |
| 104 | + /// |
| 105 | + public mutating func next() async throws -> TaskResult? { |
| 106 | + fatalError("\(#function) not implemented yet.") |
| 107 | + } |
| 108 | + |
| 109 | + /// Query whether the nursery has any remaining tasks. |
| 110 | + /// |
| 111 | + /// Nurseries are always empty upon entry to the `withNursery` body, and |
| 112 | + /// become empty again when `withNursery` returns (either by awaiting on all |
| 113 | + /// pending tasks or cancelling them). |
| 114 | + /// |
| 115 | + /// - Returns: `true` if the nursery has no pending tasks, `false` otherwise. |
| 116 | + public var isEmpty: Bool { |
| 117 | + fatalError("\(#function) not implemented yet.") |
| 118 | + } |
| 119 | + |
| 120 | + /// Cancel all the remaining tasks in the nursery. |
| 121 | + /// |
| 122 | + /// A cancelled nursery will not will NOT accept new tasks being added into it. |
| 123 | + /// |
| 124 | + /// Any results, including errors thrown by tasks affected by this |
| 125 | + /// cancellation, are silently discarded. |
| 126 | + /// |
| 127 | + /// - SeeAlso: `Task.addCancellationHandler` |
| 128 | + public mutating func cancelAll() { |
| 129 | + fatalError("\(#function) not implemented yet.") |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments