|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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 | + |
| 15 | +// FIXME: This is a workaround for trouble including gyb-generated sources in the embedded build. |
| 16 | + |
| 17 | +#if SWIFT_CONCURRENCY_EMBEDDED |
| 18 | + |
| 19 | +@available(SwiftStdlib 5.1, *) |
| 20 | +extension TaskGroup { |
| 21 | + |
| 22 | + @available(SwiftStdlib 5.1, *) |
| 23 | + @_alwaysEmitIntoClient |
| 24 | + public mutating func addTask( |
| 25 | + priority: TaskPriority? = nil, |
| 26 | + operation: sending @escaping @isolated(any) () async -> ChildTaskResult |
| 27 | + ) { |
| 28 | + let flags = taskCreateFlags( |
| 29 | + priority: priority, |
| 30 | + isChildTask: true, |
| 31 | + copyTaskLocals: false, |
| 32 | + inheritContext: false, |
| 33 | + enqueueJob: true, |
| 34 | + addPendingGroupTaskUnconditionally: true, |
| 35 | + isDiscardingTask: false |
| 36 | + ) |
| 37 | + |
| 38 | + let builtinSerialExecutor = |
| 39 | + Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor |
| 40 | + |
| 41 | + var task: Builtin.NativeObject? = |
| 42 | + Builtin.createTask( |
| 43 | + flags: flags, |
| 44 | + initialSerialExecutor: builtinSerialExecutor, |
| 45 | + taskGroup: _group, |
| 46 | + operation: operation).0 |
| 47 | + |
| 48 | + // Assert that we did create the task, but there's no need to store it, |
| 49 | + // as it was added to the group itself. |
| 50 | + assert(task != nil, "Expected task to be created!") |
| 51 | + } |
| 52 | + |
| 53 | + @available(SwiftStdlib 5.1, *) |
| 54 | + @_alwaysEmitIntoClient |
| 55 | + public mutating func addTaskUnlessCancelled( |
| 56 | + priority: TaskPriority? = nil, |
| 57 | + operation: sending @escaping @isolated(any) () async -> ChildTaskResult |
| 58 | + ) { |
| 59 | + let flags = taskCreateFlags( |
| 60 | + priority: priority, |
| 61 | + isChildTask: true, |
| 62 | + copyTaskLocals: false, |
| 63 | + inheritContext: false, |
| 64 | + enqueueJob: true, |
| 65 | + addPendingGroupTaskUnconditionally: false, |
| 66 | + isDiscardingTask: false |
| 67 | + ) |
| 68 | + |
| 69 | + let builtinSerialExecutor = |
| 70 | + Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor |
| 71 | + |
| 72 | + var task: Builtin.NativeObject? = |
| 73 | + Builtin.createTask( |
| 74 | + flags: flags, |
| 75 | + initialSerialExecutor: builtinSerialExecutor, |
| 76 | + taskGroup: _group, |
| 77 | + operation: operation).0 |
| 78 | + |
| 79 | + // Assert that we did create the task, but there's no need to store it, |
| 80 | + // as it was added to the group itself. |
| 81 | + assert(task != nil, "Expected task to be created!") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +@available(SwiftStdlib 5.1, *) |
| 86 | +extension ThrowingTaskGroup { |
| 87 | + |
| 88 | + @available(SwiftStdlib 5.1, *) |
| 89 | + @_alwaysEmitIntoClient |
| 90 | + public mutating func addTask( |
| 91 | + priority: TaskPriority? = nil, |
| 92 | + operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult |
| 93 | + ) { |
| 94 | + let flags = taskCreateFlags( |
| 95 | + priority: priority, |
| 96 | + isChildTask: true, |
| 97 | + copyTaskLocals: false, |
| 98 | + inheritContext: false, |
| 99 | + enqueueJob: true, |
| 100 | + addPendingGroupTaskUnconditionally: true, |
| 101 | + isDiscardingTask: false |
| 102 | + ) |
| 103 | + |
| 104 | + let builtinSerialExecutor = |
| 105 | + Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor |
| 106 | + |
| 107 | + var task: Builtin.NativeObject? = |
| 108 | + Builtin.createTask( |
| 109 | + flags: flags, |
| 110 | + initialSerialExecutor: builtinSerialExecutor, |
| 111 | + taskGroup: _group, |
| 112 | + operation: operation).0 |
| 113 | + |
| 114 | + // Assert that we did create the task, but there's no need to store it, |
| 115 | + // as it was added to the group itself. |
| 116 | + assert(task != nil, "Expected task to be created!") |
| 117 | + } |
| 118 | + |
| 119 | + @available(SwiftStdlib 5.1, *) |
| 120 | + @_alwaysEmitIntoClient |
| 121 | + public mutating func addTaskUnlessCancelled( |
| 122 | + priority: TaskPriority? = nil, |
| 123 | + operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult |
| 124 | + ) { |
| 125 | + let flags = taskCreateFlags( |
| 126 | + priority: priority, |
| 127 | + isChildTask: true, |
| 128 | + copyTaskLocals: false, |
| 129 | + inheritContext: false, |
| 130 | + enqueueJob: true, |
| 131 | + addPendingGroupTaskUnconditionally: false, |
| 132 | + isDiscardingTask: false |
| 133 | + ) |
| 134 | + |
| 135 | + let builtinSerialExecutor = |
| 136 | + Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor |
| 137 | + |
| 138 | + var task: Builtin.NativeObject? = |
| 139 | + Builtin.createTask( |
| 140 | + flags: flags, |
| 141 | + initialSerialExecutor: builtinSerialExecutor, |
| 142 | + taskGroup: _group, |
| 143 | + operation: operation).0 |
| 144 | + |
| 145 | + // Assert that we did create the task, but there's no need to store it, |
| 146 | + // as it was added to the group itself. |
| 147 | + assert(task != nil, "Expected task to be created!") |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#endif |
0 commit comments