Skip to content

Commit 48b253d

Browse files
committed
[concurrency] Use the new builtins.
1 parent 97b0e2e commit 48b253d

File tree

5 files changed

+23
-84
lines changed

5 files changed

+23
-84
lines changed

stdlib/public/Concurrency/Task+PriorityEscalation.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ func __withTaskPriorityEscalationHandler0<T, E>(
125125
onPriorityEscalated handler0: @Sendable (UInt8, UInt8) -> Void,
126126
isolation: isolated (any Actor)? = #isolation
127127
) async throws(E) -> T {
128-
let record = unsafe _taskAddPriorityEscalationHandler(handler: handler0)
129-
defer { unsafe _taskRemovePriorityEscalationHandler(record: record) }
128+
let record =
129+
unsafe Builtin.taskAddPriorityEscalationHandler(handler: handler0)
130+
defer {
131+
unsafe Builtin.taskRemovePriorityEscalationHandler(record: record)
132+
}
130133

131134
return try await operation()
132135
}

stdlib/public/Concurrency/Task.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -829,20 +829,6 @@ func _enqueueJobGlobalWithDeadline(_ seconds: Int64, _ nanoseconds: Int64,
829829
_ toleranceSec: Int64, _ toleranceNSec: Int64,
830830
_ clock: Int32, _ task: UnownedJob)
831831

832-
@usableFromInline
833-
@available(SwiftStdlib 6.2, *)
834-
@_silgen_name("swift_task_addPriorityEscalationHandler")
835-
func _taskAddPriorityEscalationHandler(
836-
handler: (UInt8, UInt8) -> Void
837-
) -> UnsafeRawPointer /*EscalationNotificationStatusRecord*/
838-
839-
@usableFromInline
840-
@available(SwiftStdlib 6.2, *)
841-
@_silgen_name("swift_task_removePriorityEscalationHandler")
842-
func _taskRemovePriorityEscalationHandler(
843-
record: UnsafeRawPointer /*EscalationNotificationStatusRecord*/
844-
)
845-
846832
@available(SwiftStdlib 5.1, *)
847833
@usableFromInline
848834
@_silgen_name("swift_task_asyncMainDrainQueue")

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public func withTaskCancellationHandler<T>(
8484
) async rethrows -> T {
8585
// unconditionally add the cancellation record to the task.
8686
// if the task was already cancelled, it will be executed right away.
87-
let record = unsafe _taskAddCancellationHandler(handler: handler)
88-
defer { unsafe _taskRemoveCancellationHandler(record: record) }
87+
let record = unsafe Builtin.taskAddCancellationHandler(handler: handler)
88+
defer { unsafe Builtin.taskRemoveCancellationHandler(record: record) }
8989

9090
return try await operation()
9191
}
@@ -105,8 +105,8 @@ public func _unsafeInheritExecutor_withTaskCancellationHandler<T>(
105105
) async rethrows -> T {
106106
// unconditionally add the cancellation record to the task.
107107
// if the task was already cancelled, it will be executed right away.
108-
let record = unsafe _taskAddCancellationHandler(handler: handler)
109-
defer { unsafe _taskRemoveCancellationHandler(record: record) }
108+
let record = unsafe Builtin.taskAddCancellationHandler(handler: handler)
109+
defer { unsafe Builtin.taskRemoveCancellationHandler(record: record) }
110110

111111
return try await operation()
112112
}
@@ -163,15 +163,3 @@ public struct CancellationError: Error {
163163
// no extra information, cancellation is intended to be light-weight
164164
public init() {}
165165
}
166-
167-
@usableFromInline
168-
@available(SwiftStdlib 5.1, *)
169-
@_silgen_name("swift_task_addCancellationHandler")
170-
func _taskAddCancellationHandler(handler: () -> Void) -> UnsafeRawPointer /*CancellationNotificationStatusRecord*/
171-
172-
@usableFromInline
173-
@available(SwiftStdlib 5.1, *)
174-
@_silgen_name("swift_task_removeCancellationHandler")
175-
func _taskRemoveCancellationHandler(
176-
record: UnsafeRawPointer /*CancellationNotificationStatusRecord*/
177-
)

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -235,22 +235,22 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
235235

236236
/// Implementation for withValue that consumes valueDuringOperation.
237237
///
238-
/// Because _taskLocalValuePush and _taskLocalValuePop involve calls to
239-
/// swift_task_alloc/swift_task_dealloc respectively unbeknownst to the
240-
/// compiler, compiler-emitted calls to swift_task_de/alloc must be avoided
241-
/// in a function that calls them.
238+
/// Because Builtin.taskLocalValuePush and Builtin.taskLocalValuePop involve
239+
/// calls to swift_task_alloc/swift_task_dealloc respectively unbeknownst to
240+
/// the compiler, compiler-emitted calls to swift_task_de/alloc must be
241+
/// avoided in a function that calls them.
242242
///
243243
/// A copy of valueDuringOperation is required because withValue borrows its
244-
/// argument but _taskLocalValuePush consumes its. Because
244+
/// argument but Builtin.taskLocalValuePush consumes its. Because
245245
/// valueDuringOperation is of generic type, its size is not generally known,
246246
/// so such a copy entails a stack allocation and a copy to that allocation.
247247
/// That stack traffic gets lowered to calls to
248248
/// swift_task_alloc/swift_task_deallloc.
249249
///
250-
/// Split the calls _taskLocalValuePush/Pop from the compiler-emitted calls
250+
/// Split the calls Builtin.taskLocalValuePush/Pop from the compiler-emitted calls
251251
/// to swift_task_de/alloc for the copy as follows:
252252
/// - withValue contains the compiler-emitted calls swift_task_de/alloc.
253-
/// - withValueImpl contains the calls to _taskLocalValuePush/Pop
253+
/// - withValueImpl contains the calls to Builtin.taskLocalValuePush/Pop
254254
@inlinable
255255
@discardableResult
256256
@available(SwiftStdlib 5.1, *)
@@ -259,8 +259,8 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
259259
operation: () async throws -> R,
260260
isolation: isolated (any Actor)?,
261261
file: String = #fileID, line: UInt = #line) async rethrows -> R {
262-
_taskLocalValuePush(key: key, value: consume valueDuringOperation)
263-
defer { _taskLocalValuePop() }
262+
Builtin.taskLocalValuePush(key, consume valueDuringOperation)
263+
defer { Builtin.taskLocalValuePop() }
264264

265265
return try await operation()
266266
}
@@ -275,8 +275,8 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
275275
operation: () async throws -> R,
276276
file: String = #fileID, line: UInt = #line
277277
) async rethrows -> R {
278-
_taskLocalValuePush(key: key, value: consume valueDuringOperation)
279-
defer { _taskLocalValuePop() }
278+
Builtin.taskLocalValuePush(key, consume valueDuringOperation)
279+
defer { Builtin.taskLocalValuePop() }
280280

281281
return try await operation()
282282
}
@@ -299,8 +299,8 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
299299
@discardableResult
300300
public func withValue<R>(_ valueDuringOperation: Value, operation: () throws -> R,
301301
file: String = #fileID, line: UInt = #line) rethrows -> R {
302-
_taskLocalValuePush(key: key, value: valueDuringOperation)
303-
defer { _taskLocalValuePop() }
302+
Builtin.taskLocalValuePush(key, valueDuringOperation)
303+
defer { Builtin.taskLocalValuePop() }
304304

305305
return try operation()
306306
}
@@ -344,19 +344,6 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
344344

345345
// ==== ------------------------------------------------------------------------
346346

347-
@available(SwiftStdlib 5.1, *)
348-
@usableFromInline
349-
@_silgen_name("swift_task_localValuePush")
350-
func _taskLocalValuePush<Value>(
351-
key: Builtin.RawPointer/*: Key*/,
352-
value: __owned Value
353-
) // where Key: TaskLocal
354-
355-
@available(SwiftStdlib 5.1, *)
356-
@usableFromInline
357-
@_silgen_name("swift_task_localValuePop")
358-
func _taskLocalValuePop()
359-
360347
@available(SwiftStdlib 5.1, *)
361348
@_silgen_name("swift_task_localValueGet")
362349
func _taskLocalValueGet(

stdlib/public/Observation/Sources/Observation/Observations.swift

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,6 @@
1111

1212
import _Concurrency
1313

14-
@usableFromInline
15-
@available(SwiftStdlib 5.1, *)
16-
@_silgen_name("swift_task_addCancellationHandler")
17-
func _taskAddCancellationHandler(handler: () -> Void) -> UnsafeRawPointer /*CancellationNotificationStatusRecord*/
18-
19-
@usableFromInline
20-
@available(SwiftStdlib 5.1, *)
21-
@_silgen_name("swift_task_removeCancellationHandler")
22-
func _taskRemoveCancellationHandler(
23-
record: UnsafeRawPointer /*CancellationNotificationStatusRecord*/
24-
)
25-
26-
func withIsolatedTaskCancellationHandler<T: Sendable>(
27-
operation: @isolated(any) () async throws -> T,
28-
onCancel handler: @Sendable () -> Void,
29-
isolation: isolated (any Actor)? = #isolation
30-
) async rethrows -> T {
31-
// unconditionally add the cancellation record to the task.
32-
// if the task was already cancelled, it will be executed right away.
33-
let record = _taskAddCancellationHandler(handler: handler)
34-
defer { _taskRemoveCancellationHandler(record: record) }
35-
36-
return try await operation()
37-
}
38-
3914
/// An asychronous sequence generated from a closure that tracks the transactional changes of `@Observable` types.
4015
///
4116
/// `Observations` conforms to `AsyncSequence`, providing a intutive and safe mechanism to track changes to
@@ -260,7 +235,7 @@ public struct Observations<Element: Sendable, Failure: Error>: AsyncSequence, Se
260235
// this will mean our next await for the emission will ensure the suspension return of the willChange context
261236
// back to the trailing edges of the mutations. In short, this enables the transactionality bounded by the
262237
// isolation of the mutation.
263-
await withIsolatedTaskCancellationHandler(operation: {
238+
await withTaskCancellationHandler(operation: {
264239
await State.willChange(isolation: iterationIsolation, state: state, id: id)
265240
}, onCancel: {
266241
// ensure to clean out our continuation uon cancellation

0 commit comments

Comments
 (0)