Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ LANGUAGE_FEATURE(LifetimeDependenceMutableAccessors, 0, "Support mutable accesso
LANGUAGE_FEATURE(InoutLifetimeDependence, 0, "Support @_lifetime(&)")
SUPPRESSIBLE_LANGUAGE_FEATURE(NonexhaustiveAttribute, 487, "Nonexhaustive Enums")
LANGUAGE_FEATURE(ModuleSelector, 491, "Module selectors (`Module::name` syntax)")
LANGUAGE_FEATURE(BuiltinConcurrencyStackNesting, 0, "Concurrency Stack Nesting Builtins")

// Swift 6
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ static bool usesFeatureClosureBodyMacro(Decl *decl) {
return false;
}

static bool usesFeatureBuiltinConcurrencyStackNesting(Decl *decl) {
return false;
}

UNINTERESTING_FEATURE(StrictMemorySafety)
UNINTERESTING_FEATURE(LibraryEvolution)
UNINTERESTING_FEATURE(SafeInteropWrappers)
Expand Down
19 changes: 19 additions & 0 deletions stdlib/public/Concurrency/Task+PriorityEscalation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,30 @@ func __withTaskPriorityEscalationHandler0<T, E>(
onPriorityEscalated handler0: @Sendable (UInt8, UInt8) -> Void,
isolation: isolated (any Actor)? = #isolation
) async throws(E) -> T {
#if $BuiltinConcurrencyStackNesting
let record =
unsafe Builtin.taskAddPriorityEscalationHandler(handler: handler0)
defer {
unsafe Builtin.taskRemovePriorityEscalationHandler(record: record)
}
#else
let record = unsafe _taskAddPriorityEscalationHandler(handler: handler0)
defer { unsafe _taskRemovePriorityEscalationHandler(record: record) }
#endif

return try await operation()
}

@usableFromInline
@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_task_addPriorityEscalationHandler")
func _taskAddPriorityEscalationHandler(
handler: (UInt8, UInt8) -> Void
) -> UnsafeRawPointer /*EscalationNotificationStatusRecord*/

@usableFromInline
@available(SwiftStdlib 6.2, *)
@_silgen_name("swift_task_removePriorityEscalationHandler")
func _taskRemovePriorityEscalationHandler(
record: UnsafeRawPointer /*EscalationNotificationStatusRecord*/
)
25 changes: 23 additions & 2 deletions stdlib/public/Concurrency/TaskCancellation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ public func withTaskCancellationHandler<T>(
) async rethrows -> T {
// unconditionally add the cancellation record to the task.
// if the task was already cancelled, it will be executed right away.
#if $BuiltinConcurrencyStackNesting
let record = unsafe Builtin.taskAddCancellationHandler(handler: handler)
defer { unsafe Builtin.taskRemoveCancellationHandler(record: record) }

#else
let record = unsafe _taskAddCancellationHandler(handler: handler)
defer { unsafe _taskRemoveCancellationHandler(record: record) }
#endif
return try await operation()
}

Expand All @@ -105,9 +109,13 @@ public func _unsafeInheritExecutor_withTaskCancellationHandler<T>(
) async rethrows -> T {
// unconditionally add the cancellation record to the task.
// if the task was already cancelled, it will be executed right away.
#if $BuiltinConcurrencyStackNesting
let record = unsafe Builtin.taskAddCancellationHandler(handler: handler)
defer { unsafe Builtin.taskRemoveCancellationHandler(record: record) }

#else
let record = unsafe _taskAddCancellationHandler(handler: handler)
defer { unsafe _taskRemoveCancellationHandler(record: record) }
#endif
return try await operation()
}

Expand Down Expand Up @@ -163,3 +171,16 @@ public struct CancellationError: Error {
// no extra information, cancellation is intended to be light-weight
public init() {}
}

@usableFromInline
@available(SwiftStdlib 5.1, *)
@_silgen_name("swift_task_addCancellationHandler")
func _taskAddCancellationHandler(handler: () -> Void) -> UnsafeRawPointer /*CancellationNotificationStatusRecord*/

@usableFromInline
@available(SwiftStdlib 5.1, *)
@_silgen_name("swift_task_removeCancellationHandler")
func _taskRemoveCancellationHandler(
record: UnsafeRawPointer /*CancellationNotificationStatusRecord*/
)

28 changes: 28 additions & 0 deletions stdlib/public/Concurrency/TaskLocal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,13 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
operation: () async throws -> R,
isolation: isolated (any Actor)?,
file: String = #fileID, line: UInt = #line) async rethrows -> R {
#if $BuiltinConcurrencyStackNesting
Builtin.taskLocalValuePush(key, consume valueDuringOperation)
defer { Builtin.taskLocalValuePop() }
#else
_taskLocalValuePush(key: key, value: consume valueDuringOperation)
defer { _taskLocalValuePop() }
#endif

return try await operation()
}
Expand All @@ -275,8 +280,13 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
operation: () async throws -> R,
file: String = #fileID, line: UInt = #line
) async rethrows -> R {
#if $BuiltinConcurrencyStackNesting
Builtin.taskLocalValuePush(key, consume valueDuringOperation)
defer { Builtin.taskLocalValuePop() }
#else
_taskLocalValuePush(key: key, value: consume valueDuringOperation)
defer { _taskLocalValuePop() }
#endif

return try await operation()
}
Expand All @@ -299,8 +309,13 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
@discardableResult
public func withValue<R>(_ valueDuringOperation: Value, operation: () throws -> R,
file: String = #fileID, line: UInt = #line) rethrows -> R {
#if $BuiltinConcurrencyStackNesting
Builtin.taskLocalValuePush(key, valueDuringOperation)
defer { Builtin.taskLocalValuePop() }
#else
_taskLocalValuePush(key: key, value: valueDuringOperation)
defer { _taskLocalValuePop() }
#endif

return try operation()
}
Expand Down Expand Up @@ -344,6 +359,19 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible

// ==== ------------------------------------------------------------------------

@available(SwiftStdlib 5.1, *)
@usableFromInline
@_silgen_name("swift_task_localValuePush")
func _taskLocalValuePush<Value>(
key: Builtin.RawPointer/*: Key*/,
value: __owned Value
) // where Key: TaskLocal

@available(SwiftStdlib 5.1, *)
@usableFromInline
@_silgen_name("swift_task_localValuePop")
func _taskLocalValuePop()

@available(SwiftStdlib 5.1, *)
@_silgen_name("swift_task_localValueGet")
func _taskLocalValueGet(
Expand Down