Skip to content

Commit 762e789

Browse files
authored
Merge pull request swiftlang#76760 from kubamracek/embedded-asyncstream
[Concurrency] Include more pieces of Concurrency in Embedded (AsyncStream, continuations)
2 parents 963e0d9 + 246477a commit 762e789

18 files changed

+223
-44
lines changed

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -263,39 +263,13 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
263263
IS_STDLIB IS_FRAGILE
264264

265265
${SWIFT_RUNTIME_CONCURRENCY_C_SOURCES}
266+
${SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES}
266267
CooperativeGlobalExecutor.cpp
267268

268-
# TODO: Only a subset of Swift Concurrency .swift sources, for now.
269-
Actor.swift
270-
AsyncLet.swift
271-
CheckedContinuation.swift
272-
Errors.swift
273-
Executor.swift
274-
ExecutorAssertions.swift
275-
AsyncCompactMapSequence.swift
276-
AsyncDropFirstSequence.swift
277-
AsyncDropWhileSequence.swift
278-
AsyncFilterSequence.swift
279-
AsyncFlatMapSequence.swift
280-
AsyncIteratorProtocol.swift
281-
AsyncMapSequence.swift
282-
AsyncPrefixSequence.swift
283-
AsyncPrefixWhileSequence.swift
284-
AsyncSequence.swift
285-
AsyncThrowingCompactMapSequence.swift
286-
AsyncThrowingDropWhileSequence.swift
287-
AsyncThrowingFilterSequence.swift
288-
AsyncThrowingFlatMapSequence.swift
289-
AsyncThrowingMapSequence.swift
290-
AsyncThrowingPrefixWhileSequence.swift
291-
GlobalActor.swift
292-
PartialAsyncTask.swift
293-
Task.swift
294-
TaskCancellation.swift
295-
296269
SWIFT_COMPILE_FLAGS
297270
${extra_swift_compile_flags} -enable-experimental-feature Embedded
298271
-parse-stdlib -DSWIFT_CONCURRENCY_EMBEDDED
272+
-Xfrontend -emit-empty-object-file
299273
${SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS}
300274
C_COMPILE_FLAGS
301275
${extra_c_compile_flags} ${SWIFT_RUNTIME_CONCURRENCY_C_FLAGS} -DSWIFT_CONCURRENCY_EMBEDDED=1

stdlib/public/Concurrency/CheckedContinuation.swift

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ internal func logFailedCheck(_ message: UnsafeRawPointer)
1919
/// Implementation class that holds the `UnsafeContinuation` instance for
2020
/// a `CheckedContinuation`.
2121
@available(SwiftStdlib 5.1, *)
22-
@_unavailableInEmbedded
2322
internal final class CheckedContinuationCanary: @unchecked Sendable {
2423
// The instance state is stored in tail-allocated raw memory, so that
2524
// we can atomically check the continuation state.
2625

2726
private init() { fatalError("must use create") }
2827

2928
private static func _create(continuation: UnsafeRawPointer, function: String)
30-
-> Self {
31-
let instance = Builtin.allocWithTailElems_1(self,
29+
-> CheckedContinuationCanary {
30+
let instance = Builtin.allocWithTailElems_1(CheckedContinuationCanary.self,
3231
1._builtinWordValue,
3332
(UnsafeRawPointer?, String).self)
3433

@@ -52,7 +51,7 @@ internal final class CheckedContinuationCanary: @unchecked Sendable {
5251
}
5352

5453
internal static func create<T, E>(continuation: UnsafeContinuation<T, E>,
55-
function: String) -> Self {
54+
function: String) -> CheckedContinuationCanary {
5655
return _create(
5756
continuation: unsafeBitCast(continuation, to: UnsafeRawPointer.self),
5857
function: function)
@@ -79,7 +78,11 @@ internal final class CheckedContinuationCanary: @unchecked Sendable {
7978
// Log if the continuation was never consumed before the instance was
8079
// destructed.
8180
if _continuationPtr.pointee != nil {
81+
#if !$Embedded
8282
logFailedCheck("SWIFT TASK CONTINUATION MISUSE: \(function) leaked its continuation without resuming it. This may cause tasks waiting on it to remain suspended forever.\n")
83+
#else
84+
fatalError("SWIFT TASK CONTINUATION MISUSE")
85+
#endif
8386
}
8487
}
8588
}
@@ -120,7 +123,6 @@ internal final class CheckedContinuationCanary: @unchecked Sendable {
120123
/// you can replace one with the other in most circumstances,
121124
/// without making other changes.
122125
@available(SwiftStdlib 5.1, *)
123-
@_unavailableInEmbedded
124126
public struct CheckedContinuation<T, E: Error>: Sendable {
125127
private let canary: CheckedContinuationCanary
126128

@@ -163,7 +165,11 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
163165
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
164166
c.resume(returning: value)
165167
} else {
168+
#if !$Embedded
166169
fatalError("SWIFT TASK CONTINUATION MISUSE: \(canary.function) tried to resume its continuation more than once, returning \(value)!\n")
170+
#else
171+
fatalError("SWIFT TASK CONTINUATION MISUSE")
172+
#endif
167173
}
168174
}
169175

@@ -183,13 +189,16 @@ public struct CheckedContinuation<T, E: Error>: Sendable {
183189
if let c: UnsafeContinuation<T, E> = canary.takeContinuation() {
184190
c.resume(throwing: error)
185191
} else {
192+
#if !$Embedded
186193
fatalError("SWIFT TASK CONTINUATION MISUSE: \(canary.function) tried to resume its continuation more than once, throwing \(error)!\n")
194+
#else
195+
fatalError("SWIFT TASK CONTINUATION MISUSE")
196+
#endif
187197
}
188198
}
189199
}
190200

191201
@available(SwiftStdlib 5.1, *)
192-
@_unavailableInEmbedded
193202
extension CheckedContinuation {
194203
/// Resume the task awaiting the continuation by having it either
195204
/// return normally or throw an error based on the state of the given
@@ -282,7 +291,6 @@ extension CheckedContinuation {
282291
/// - SeeAlso: `withUnsafeContinuation(function:_:)`
283292
/// - SeeAlso: `withUnsafeThrowingContinuation(function:_:)`
284293
@inlinable
285-
@_unavailableInEmbedded
286294
@available(SwiftStdlib 5.1, *)
287295
#if !$Embedded
288296
@backDeployed(before: SwiftStdlib 6.0)
@@ -307,7 +315,6 @@ public func withCheckedContinuation<T>(
307315
// introduction of #isolation.
308316
@available(SwiftStdlib 5.1, *)
309317
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
310-
@_unavailableInEmbedded
311318
@_silgen_name("$ss23withCheckedContinuation8function_xSS_yScCyxs5NeverOGXEtYalF")
312319
public func _unsafeInheritExecutor_withCheckedContinuation<T>(
313320
function: String = #function,
@@ -348,7 +355,6 @@ public func _unsafeInheritExecutor_withCheckedContinuation<T>(
348355
/// - SeeAlso: `withUnsafeContinuation(function:_:)`
349356
/// - SeeAlso: `withUnsafeThrowingContinuation(function:_:)`
350357
@inlinable
351-
@_unavailableInEmbedded
352358
@available(SwiftStdlib 5.1, *)
353359
#if !$Embedded
354360
@backDeployed(before: SwiftStdlib 6.0)
@@ -373,7 +379,6 @@ public func withCheckedThrowingContinuation<T>(
373379
// introduction of #isolation.
374380
@available(SwiftStdlib 5.1, *)
375381
@_unsafeInheritExecutor // ABI compatibility with Swift 5.1
376-
@_unavailableInEmbedded
377382
@_silgen_name("$ss31withCheckedThrowingContinuation8function_xSS_yScCyxs5Error_pGXEtYaKlF")
378383
public func _unsafeInheritExecutor_withCheckedThrowingContinuation<T>(
379384
function: String = #function,

stdlib/public/Concurrency/ContinuousClock.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212
import Swift
1313

14+
#if !$Embedded
1415
/// A clock that measures time that always increments and does not stop
1516
/// incrementing while the system is asleep.
1617
///
@@ -33,6 +34,7 @@ public struct ContinuousClock: Sendable {
3334

3435
public init() { }
3536
}
37+
#endif
3638

3739
@available(SwiftStdlib 5.7, *)
3840
extension Duration {
@@ -49,6 +51,8 @@ extension Duration {
4951
}
5052
}
5153

54+
#if !$Embedded
55+
5256
@available(SwiftStdlib 5.7, *)
5357
extension Clock where Self == ContinuousClock {
5458
/// A clock that measures time that always increments but does not stop
@@ -189,3 +193,5 @@ extension ContinuousClock.Instant: InstantProtocol {
189193
rhs.duration(to: lhs)
190194
}
191195
}
196+
197+
#endif

stdlib/public/Concurrency/Deque/Deque+Codable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import Swift
1616

17+
#if !$Embedded
18+
1719
extension _Deque: Encodable where Element: Encodable {
1820
/// Encodes the elements of this deque into the given encoder in an unkeyed
1921
/// container.
@@ -50,3 +52,5 @@ extension _Deque: Decodable where Element: Decodable {
5052
}
5153
}
5254
}
55+
56+
#endif

stdlib/public/Concurrency/Deque/Deque+CustomDebugStringConvertible.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// This file is copied from swift-collections and should not be modified here.
1313
/// Rather all changes should be made to swift-collections and copied back.
1414

15-
#if !SWIFT_STDLIB_STATIC_PRINT
15+
#if !SWIFT_STDLIB_STATIC_PRINT && !$Embedded
16+
1617
import Swift
1718

1819
extension _Deque: CustomDebugStringConvertible {
@@ -32,4 +33,5 @@ extension _Deque: CustomDebugStringConvertible {
3233
return result
3334
}
3435
}
35-
#endif
36+
37+
#endif

stdlib/public/Concurrency/Deque/Deque+CustomStringConvertible.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
/// This file is copied from swift-collections and should not be modified here.
1313
/// Rather all changes should be made to swift-collections and copied back.
1414

15-
#if !SWIFT_STDLIB_STATIC_PRINT
15+
#if !SWIFT_STDLIB_STATIC_PRINT && !$Embedded
16+
1617
import Swift
1718

1819
extension _Deque: CustomStringConvertible {
@@ -32,4 +33,5 @@ extension _Deque: CustomStringConvertible {
3233
return result
3334
}
3435
}
35-
#endif
36+
37+
#endif

stdlib/public/Concurrency/GlobalConcurrentExecutor.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Swift
3131
///
3232
/// Customizing the global concurrent executor is currently not supported.
3333
@available(SwiftStdlib 6.0, *)
34+
@_unavailableInEmbedded
3435
public var globalConcurrentExecutor: any TaskExecutor {
3536
get {
3637
_DefaultGlobalConcurrentExecutor.shared
@@ -43,6 +44,7 @@ public var globalConcurrentExecutor: any TaskExecutor {
4344
/// thread pool that is used as the default executor for Swift concurrency
4445
/// tasks.
4546
@available(SwiftStdlib 6.0, *)
47+
@_unavailableInEmbedded
4648
internal final class _DefaultGlobalConcurrentExecutor: TaskExecutor {
4749
public static let shared: _DefaultGlobalConcurrentExecutor = .init()
4850

stdlib/public/Concurrency/MainActor.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import Swift
1414

15+
#if !$Embedded
16+
1517
#if SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
1618
@available(SwiftStdlib 5.1, *)
1719
@available(*, unavailable, message: "Unavailable in task-to-thread concurrency model")
@@ -157,3 +159,5 @@ extension MainActor {
157159
}
158160
}
159161
#endif
162+
163+
#endif

stdlib/public/Concurrency/SuspendingClock.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212
import Swift
1313

14+
#if !$Embedded
15+
1416
/// A clock that measures time that always increments but stops incrementing
1517
/// while the system is asleep.
1618
///
@@ -179,3 +181,4 @@ extension SuspendingClock.Instant: InstantProtocol {
179181
}
180182
}
181183

184+
#endif

stdlib/public/Concurrency/Task+TaskExecutor.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public func _unsafeInheritExecutor_withTaskExecutorPreference<T: Sendable>(
190190
/// Task with specified executor -----------------------------------------------
191191

192192
@available(SwiftStdlib 6.0, *)
193+
@_unavailableInEmbedded
193194
extension Task where Failure == Never {
194195
/// Runs the given nonthrowing operation asynchronously
195196
/// as part of a new top-level task on behalf of the current actor.
@@ -257,6 +258,7 @@ extension Task where Failure == Never {
257258
}
258259

259260
@available(SwiftStdlib 6.0, *)
261+
@_unavailableInEmbedded
260262
extension Task where Failure == Error {
261263
/// Runs the given throwing operation asynchronously
262264
/// as part of a new top-level task on behalf of the current actor.
@@ -321,6 +323,7 @@ extension Task where Failure == Error {
321323
// ==== Detached tasks ---------------------------------------------------------
322324

323325
@available(SwiftStdlib 6.0, *)
326+
@_unavailableInEmbedded
324327
extension Task where Failure == Never {
325328
/// Runs the given nonthrowing operation asynchronously
326329
/// as part of a new top-level task.
@@ -379,6 +382,7 @@ extension Task where Failure == Never {
379382
}
380383

381384
@available(SwiftStdlib 6.0, *)
385+
@_unavailableInEmbedded
382386
extension Task where Failure == Error {
383387
/// Runs the given throwing operation asynchronously
384388
/// as part of a new top-level task.
@@ -441,6 +445,7 @@ extension Task where Failure == Error {
441445
// ==== Unsafe Current Task ----------------------------------------------------
442446

443447
@available(SwiftStdlib 6.0, *)
448+
@_unavailableInEmbedded
444449
extension UnsafeCurrentTask {
445450

446451
/// The current ``TaskExecutor`` preference, if this task has one configured.

stdlib/public/Concurrency/Task.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ static LazyMutex ActiveContinuationsLock;
14021402
static Lazy<std::unordered_set<AsyncTask *>> ActiveContinuations;
14031403

14041404
static bool isEnabled() {
1405+
#if !SWIFT_CONCURRENCY_EMBEDDED
14051406
auto state = CurrentState.load(std::memory_order_relaxed);
14061407
if (state == State::Uninitialized) {
14071408
bool enabled =
@@ -1410,6 +1411,9 @@ static bool isEnabled() {
14101411
CurrentState.store(state, std::memory_order_relaxed);
14111412
}
14121413
return state == State::On;
1414+
#else
1415+
return false;
1416+
#endif
14131417
}
14141418

14151419
static void init(AsyncTask *task) {

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ import Swift
6767
/// Therefore, if a cancellation handler must acquire a lock, other code should
6868
/// not cancel tasks or resume continuations while holding that lock.
6969
@available(SwiftStdlib 5.1, *)
70+
#if !$Embedded
7071
@backDeployed(before: SwiftStdlib 6.0)
72+
#endif
7173
public func withTaskCancellationHandler<T>(
7274
operation: () async throws -> T,
7375
onCancel handler: @Sendable () -> Void,

stdlib/public/Concurrency/TaskSleep.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Swift
1414

1515
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
1616
@available(SwiftStdlib 5.1, *)
17+
@_unavailableInEmbedded
1718
extension Task where Success == Never, Failure == Never {
1819
@available(*, deprecated, renamed: "Task.sleep(nanoseconds:)")
1920
/// Suspends the current task for at least the given duration

stdlib/public/Concurrency/TaskSleepDuration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Swift
1414

1515
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
1616
@available(SwiftStdlib 5.7, *)
17+
@_unavailableInEmbedded
1718
extension Task where Success == Never, Failure == Never {
1819
@available(SwiftStdlib 5.7, *)
1920
internal static func _sleep(

0 commit comments

Comments
 (0)