Skip to content

Commit 9c981af

Browse files
committed
more workarounds for embedded
1 parent 5ce6f68 commit 9c981af

File tree

2 files changed

+167
-41
lines changed

2 files changed

+167
-41
lines changed

stdlib/public/Concurrency/TaskGroup+Embedded.swift

Lines changed: 167 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,47 +38,46 @@ extension TaskGroup {
3838
let builtinSerialExecutor =
3939
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
4040

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!")
41+
42+
_ = Builtin.createTask(
43+
flags: flags,
44+
initialSerialExecutor: builtinSerialExecutor,
45+
taskGroup: _group,
46+
operation: operation).0
5147
}
5248

5349
@available(SwiftStdlib 5.1, *)
5450
@_alwaysEmitIntoClient
5551
public mutating func addTaskUnlessCancelled(
5652
priority: TaskPriority? = nil,
5753
operation: sending @escaping @isolated(any) () async -> ChildTaskResult
58-
) {
54+
) -> Bool {
55+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
56+
57+
guard canAdd else {
58+
return false
59+
}
60+
5961
let flags = taskCreateFlags(
6062
priority: priority,
6163
isChildTask: true,
6264
copyTaskLocals: false,
6365
inheritContext: false,
6466
enqueueJob: true,
6567
addPendingGroupTaskUnconditionally: false,
66-
isDiscardingTask: false
68+
isDiscardingTask: true
6769
)
6870

6971
let builtinSerialExecutor =
7072
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
7173

72-
var task: Builtin.NativeObject? =
73-
Builtin.createTask(
74-
flags: flags,
75-
initialSerialExecutor: builtinSerialExecutor,
76-
taskGroup: _group,
77-
operation: operation).0
74+
_ = Builtin.createTask(
75+
flags: flags,
76+
initialSerialExecutor: builtinSerialExecutor,
77+
taskGroup: _group,
78+
operation: operation).0
7879

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!")
80+
return true
8281
}
8382
}
8483

@@ -104,47 +103,174 @@ extension ThrowingTaskGroup {
104103
let builtinSerialExecutor =
105104
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
106105

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!")
106+
_ = Builtin.createTask(
107+
flags: flags,
108+
initialSerialExecutor: builtinSerialExecutor,
109+
taskGroup: _group,
110+
operation: operation).0
117111
}
118112

119113
@available(SwiftStdlib 5.1, *)
120114
@_alwaysEmitIntoClient
121115
public mutating func addTaskUnlessCancelled(
122116
priority: TaskPriority? = nil,
123117
operation: sending @escaping @isolated(any) () async throws -> ChildTaskResult
118+
) -> Bool {
119+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
120+
121+
guard canAdd else {
122+
return false
123+
}
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: true
133+
)
134+
135+
let builtinSerialExecutor =
136+
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
137+
138+
_ = Builtin.createTask(
139+
flags: flags,
140+
initialSerialExecutor: builtinSerialExecutor,
141+
taskGroup: _group,
142+
operation: operation).0
143+
144+
return true
145+
}
146+
}
147+
148+
@available(SwiftStdlib 5.9, *)
149+
extension DiscardingTaskGroup {
150+
151+
@available(SwiftStdlib 5.9, *)
152+
@_alwaysEmitIntoClient
153+
public mutating func addTask(
154+
priority: TaskPriority? = nil,
155+
operation: sending @escaping @isolated(any) () async -> Void
124156
) {
157+
let flags = taskCreateFlags(
158+
priority: priority,
159+
isChildTask: true,
160+
copyTaskLocals: false,
161+
inheritContext: false,
162+
enqueueJob: true,
163+
addPendingGroupTaskUnconditionally: true,
164+
isDiscardingTask: true
165+
)
166+
167+
let builtinSerialExecutor =
168+
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
169+
170+
_ = Builtin.createTask(
171+
flags: flags,
172+
initialSerialExecutor: builtinSerialExecutor,
173+
taskGroup: _group,
174+
operation: operation).0
175+
}
176+
177+
@available(SwiftStdlib 5.9, *)
178+
@_alwaysEmitIntoClient
179+
public mutating func addTaskUnlessCancelled(
180+
priority: TaskPriority? = nil,
181+
operation: sending @escaping @isolated(any) () async -> Void
182+
) -> Bool {
183+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
184+
185+
guard canAdd else {
186+
return false
187+
}
188+
125189
let flags = taskCreateFlags(
126190
priority: priority,
127191
isChildTask: true,
128192
copyTaskLocals: false,
129193
inheritContext: false,
130194
enqueueJob: true,
131195
addPendingGroupTaskUnconditionally: false,
132-
isDiscardingTask: false
196+
isDiscardingTask: true
197+
)
198+
199+
let builtinSerialExecutor =
200+
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
201+
202+
_ = Builtin.createTask(
203+
flags: flags,
204+
initialSerialExecutor: builtinSerialExecutor,
205+
taskGroup: _group,
206+
operation: operation).0
207+
208+
return true
209+
}
210+
}
211+
212+
@available(SwiftStdlib 5.9, *)
213+
extension ThrowingDiscardingTaskGroup {
214+
215+
@available(SwiftStdlib 5.9, *)
216+
@_alwaysEmitIntoClient
217+
public mutating func addTask(
218+
priority: TaskPriority? = nil,
219+
operation: sending @escaping @isolated(any) () async throws -> Void
220+
) {
221+
let flags = taskCreateFlags(
222+
priority: priority,
223+
isChildTask: true,
224+
copyTaskLocals: false,
225+
inheritContext: false,
226+
enqueueJob: true,
227+
addPendingGroupTaskUnconditionally: true,
228+
isDiscardingTask: true
229+
)
230+
231+
let builtinSerialExecutor =
232+
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
233+
234+
235+
_ = Builtin.createTask(
236+
flags: flags,
237+
initialSerialExecutor: builtinSerialExecutor,
238+
taskGroup: _group,
239+
operation: operation).0
240+
}
241+
242+
@available(SwiftStdlib 5.9, *)
243+
@_alwaysEmitIntoClient
244+
public mutating func addTaskUnlessCancelled(
245+
priority: TaskPriority? = nil,
246+
operation: sending @escaping @isolated(any) () async throws -> Void
247+
) -> Bool {
248+
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
249+
250+
guard canAdd else {
251+
return false
252+
}
253+
254+
let flags = taskCreateFlags(
255+
priority: priority,
256+
isChildTask: true,
257+
copyTaskLocals: false,
258+
inheritContext: false,
259+
enqueueJob: true,
260+
addPendingGroupTaskUnconditionally: false,
261+
isDiscardingTask: true
133262
)
134263

135264
let builtinSerialExecutor =
136265
Builtin.extractFunctionIsolation(operation)?.unownedExecutor.executor
137266

138-
var task: Builtin.NativeObject? =
139-
Builtin.createTask(
140-
flags: flags,
141-
initialSerialExecutor: builtinSerialExecutor,
142-
taskGroup: _group,
143-
operation: operation).0
267+
_ = Builtin.createTask(
268+
flags: flags,
269+
initialSerialExecutor: builtinSerialExecutor,
270+
taskGroup: _group,
271+
operation: operation).0
144272

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!")
273+
return true
148274
}
149275
}
150276

0 commit comments

Comments
 (0)