Skip to content

Commit 9b3cd28

Browse files
committed
Concurrency: Implement high-level withUnsafe[Throwing]Continuation entry points
These functions call the corresponding built-ins, wrapping the Builtin.RawUnsafeContinuation value in an Unsafe[Throwing]Continuation<T>.
1 parent 5c5f012 commit 9b3cd28

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

stdlib/public/Concurrency/PartialAsyncTask.swift

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,34 @@ public struct PartialAsyncTask {
2222

2323
@frozen
2424
public struct UnsafeContinuation<T> {
25-
private var context: Builtin.RawUnsafeContinuation
25+
@usableFromInline internal var context: Builtin.RawUnsafeContinuation
2626

2727
public func resume(returning: __owned T) { }
28+
29+
@_alwaysEmitIntoClient
30+
internal init(_ context: Builtin.RawUnsafeContinuation) {
31+
self.context = context
32+
}
2833
}
2934

3035
@frozen
3136
public struct UnsafeThrowingContinuation<T> {
32-
private var context: Builtin.RawUnsafeContinuation
37+
@usableFromInline internal var context: Builtin.RawUnsafeContinuation
3338

3439
public func resume(returning: __owned T) { }
3540
public func resume(throwing: __owned Error) { }
41+
42+
@_alwaysEmitIntoClient
43+
internal init(_ context: Builtin.RawUnsafeContinuation) {
44+
self.context = context
45+
}
3646
}
3747

3848
#if _runtime(_ObjC)
3949

4050
// Intrinsics used by SILGen to resume or fail continuations
4151
// for
4252
@_alwaysEmitIntoClient
43-
@usableFromInline
4453
internal func _resumeUnsafeContinuation<T>(
4554
_ continuation: UnsafeContinuation<T>,
4655
_ value: __owned T
@@ -49,7 +58,6 @@ internal func _resumeUnsafeContinuation<T>(
4958
}
5059

5160
@_alwaysEmitIntoClient
52-
@usableFromInline
5361
internal func _resumeUnsafeThrowingContinuation<T>(
5462
_ continuation: UnsafeThrowingContinuation<T>,
5563
_ value: __owned T
@@ -58,7 +66,6 @@ internal func _resumeUnsafeThrowingContinuation<T>(
5866
}
5967

6068
@_alwaysEmitIntoClient
61-
@usableFromInline
6269
internal func _resumeUnsafeThrowingContinuationWithError<T>(
6370
_ continuation: UnsafeThrowingContinuation<T>,
6471
_ error: __owned Error
@@ -67,3 +74,22 @@ internal func _resumeUnsafeThrowingContinuationWithError<T>(
6774
}
6875

6976
#endif
77+
78+
// Wrappers around unsafe continuation builtins
79+
@_alwaysEmitIntoClient
80+
public func withUnsafeContinuation<T>(
81+
_ fn: (UnsafeContinuation<T>) -> Void
82+
) async -> T {
83+
return await Builtin.withUnsafeContinuation {
84+
fn(UnsafeContinuation<T>($0))
85+
}
86+
}
87+
88+
@_alwaysEmitIntoClient
89+
public func withUnsafeThrowingContinuation<T>(
90+
_ fn: (UnsafeThrowingContinuation<T>) -> Void
91+
) async throws -> T {
92+
return try await Builtin.withUnsafeThrowingContinuation {
93+
fn(UnsafeThrowingContinuation<T>($0))
94+
}
95+
}

0 commit comments

Comments
 (0)