Skip to content

Commit cdc6dbe

Browse files
committed
[Concurrency] Deprecate Task.Priority.unspecified and use optionals instead
1 parent 92dd8ef commit cdc6dbe

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

stdlib/public/Concurrency/Task.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ extension Task {
104104
case `default` = 0x15
105105
case utility = 0x11
106106
case background = 0x09
107+
108+
@available(*, deprecated, message: "unspecified priority will be removed; use nil")
107109
case unspecified = 0x00
108110

109111
public static func < (lhs: Priority, rhs: Priority) -> Bool {
@@ -273,13 +275,13 @@ extension Task {
273275
var isAsyncTask: Bool { kind == .task }
274276

275277
/// The priority given to the job.
276-
var priority: Priority {
278+
var priority: Priority? {
277279
get {
278-
Priority(rawValue: (bits & 0xFF00) >> 8)!
280+
Priority(rawValue: (bits & 0xFF00) >> 8)
279281
}
280282

281283
set {
282-
bits = (bits & ~0xFF00) | (newValue.rawValue << 8)
284+
bits = (bits & ~0xFF00) | ((newValue?.rawValue ?? 0) << 8)
283285
}
284286
}
285287

@@ -470,23 +472,21 @@ public func detach<T>(
470472
}
471473

472474
@discardableResult
473-
@_alwaysEmitIntoClient
474475
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
475476
public func asyncDetached<T>(
476-
priority: Task.Priority = .unspecified,
477+
priority: Task.Priority? = nil,
477478
@_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
478479
) -> Task.Handle<T, Never> {
479-
return detach(priority: priority, operation: operation)
480+
return detach(priority: priority ?? .unspecified, operation: operation)
480481
}
481482

482483
@discardableResult
483-
@_alwaysEmitIntoClient
484484
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
485485
public func asyncDetached<T>(
486-
priority: Task.Priority = .unspecified,
486+
priority: Task.Priority? = nil,
487487
@_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
488488
) -> Task.Handle<T, Error> {
489-
return detach(priority: priority, operation: operation)
489+
return detach(priority: priority ?? .unspecified, operation: operation)
490490
}
491491

492492
/// ABI stub while we stage in the new signatures
@@ -520,6 +520,7 @@ func async(
520520
/// Task.currentPriority.
521521
/// - operation: the operation to execute
522522
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
523+
@discardableResult
523524
public func async<T>(
524525
priority: Task.Priority? = nil,
525526
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
@@ -555,6 +556,7 @@ public func async<T>(
555556
/// Task.currentPriority.
556557
/// - operation: the operation to execute
557558
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
559+
@discardableResult
558560
public func async<T>(
559561
priority: Task.Priority? = nil,
560562
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
@@ -721,12 +723,10 @@ public struct UnsafeCurrentTask {
721723

722724
/// Returns the `current` task's priority.
723725
///
724-
/// If no current `Task` is available, returns `Priority.default`.
725-
///
726726
/// - SeeAlso: `Task.Priority`
727727
/// - SeeAlso: `Task.currentPriority`
728728
public var priority: Task.Priority {
729-
getJobFlags(_task).priority
729+
getJobFlags(_task).priority ?? .default
730730
}
731731

732732
}

stdlib/public/Concurrency/TaskGroup.swift

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
220220
}
221221
}
222222

223+
// Historical entry point, maintained for ABI compatibility
224+
@usableFromInline
225+
mutating func spawn(
226+
priority: Task.Priority,
227+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
228+
) {
229+
let optPriority: Task.Priority? = priority
230+
spawn(priority: optPriority, operation: operation)
231+
}
232+
223233
/// Add a child task to the group.
224234
///
225235
/// ### Error handling
@@ -236,7 +246,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
236246
/// - `true` if the operation was added to the group successfully,
237247
/// `false` otherwise (e.g. because the group `isCancelled`)
238248
public mutating func spawn(
239-
priority: Task.Priority = .unspecified,
249+
priority: Task.Priority? = nil,
240250
operation: __owned @Sendable @escaping () async -> ChildTaskResult
241251
) {
242252
_ = _taskGroupAddPendingTask(group: _group, unconditionally: true)
@@ -260,6 +270,16 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
260270
_enqueueJobGlobal(Builtin.convertTaskToJob(childTask))
261271
}
262272

273+
// Historical entry point, maintained for ABI compatibility
274+
@usableFromInline
275+
mutating func spawnUnlessCancelled(
276+
priority: Task.Priority = .unspecified,
277+
operation: __owned @Sendable @escaping () async -> ChildTaskResult
278+
) -> Bool {
279+
let optPriority: Task.Priority? = priority
280+
return spawnUnlessCancelled(priority: optPriority, operation: operation)
281+
}
282+
263283
/// Add a child task to the group.
264284
///
265285
/// ### Error handling
@@ -276,7 +296,7 @@ public struct TaskGroup<ChildTaskResult: Sendable> {
276296
/// - `true` if the operation was added to the group successfully,
277297
/// `false` otherwise (e.g. because the group `isCancelled`)
278298
public mutating func spawnUnlessCancelled(
279-
priority: Task.Priority = .unspecified,
299+
priority: Task.Priority? = nil,
280300
operation: __owned @Sendable @escaping () async -> ChildTaskResult
281301
) -> Bool {
282302
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)
@@ -472,6 +492,16 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
472492
}
473493
}
474494

495+
// Historical entry point for ABI reasons
496+
@usableFromInline
497+
mutating func spawn(
498+
priority: Task.Priority = .unspecified,
499+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
500+
) {
501+
let optPriority: Task.Priority? = priority
502+
return spawn(priority: optPriority, operation: operation)
503+
}
504+
475505
/// Spawn, unconditionally, a child task in the group.
476506
///
477507
/// ### Error handling
@@ -488,7 +518,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
488518
/// - `true` if the operation was added to the group successfully,
489519
/// `false` otherwise (e.g. because the group `isCancelled`)
490520
public mutating func spawn(
491-
priority: Task.Priority = .unspecified,
521+
priority: Task.Priority? = nil,
492522
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
493523
) {
494524
// we always add, so no need to check if group was cancelled
@@ -513,6 +543,16 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
513543
_enqueueJobGlobal(Builtin.convertTaskToJob(childTask))
514544
}
515545

546+
// Historical entry point for ABI reasons
547+
@usableFromInline
548+
mutating func spawnUnlessCancelled(
549+
priority: Task.Priority,
550+
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
551+
) -> Bool {
552+
let optPriority: Task.Priority? = priority
553+
return spawnUnlessCancelled(priority: optPriority, operation: operation)
554+
}
555+
516556
/// Add a child task to the group.
517557
///
518558
/// ### Error handling
@@ -529,7 +569,7 @@ public struct ThrowingTaskGroup<ChildTaskResult: Sendable, Failure: Error> {
529569
/// - `true` if the operation was added to the group successfully,
530570
/// `false` otherwise (e.g. because the group `isCancelled`)
531571
public mutating func spawnUnlessCancelled(
532-
priority: Task.Priority = .unspecified,
572+
priority: Task.Priority? = nil,
533573
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
534574
) -> Bool {
535575
let canAdd = _taskGroupAddPendingTask(group: _group, unconditionally: false)

0 commit comments

Comments
 (0)