Skip to content

Commit fd32673

Browse files
committed
[Concurrency] Deprecate Task.Priority.unspecified and use optionals instead
(cherry picked from commit cdc6dbe)
1 parent 73748ab commit fd32673

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
@@ -141,6 +141,8 @@ extension Task {
141141
case `default` = 0x15
142142
case utility = 0x11
143143
case background = 0x09
144+
145+
@available(*, deprecated, message: "unspecified priority will be removed; use nil")
144146
case unspecified = 0x00
145147

146148
public static func < (lhs: Priority, rhs: Priority) -> Bool {
@@ -324,13 +326,13 @@ extension Task {
324326
var isAsyncTask: Bool { kind == .task }
325327

326328
/// The priority given to the job.
327-
var priority: Priority {
329+
var priority: Priority? {
328330
get {
329-
Priority(rawValue: (bits & 0xFF00) >> 8)!
331+
Priority(rawValue: (bits & 0xFF00) >> 8)
330332
}
331333

332334
set {
333-
bits = (bits & ~0xFF00) | (newValue.rawValue << 8)
335+
bits = (bits & ~0xFF00) | ((newValue?.rawValue ?? 0) << 8)
334336
}
335337
}
336338

@@ -521,23 +523,21 @@ public func detach<T>(
521523
}
522524

523525
@discardableResult
524-
@_alwaysEmitIntoClient
525526
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
526527
public func asyncDetached<T>(
527-
priority: Task.Priority = .unspecified,
528+
priority: Task.Priority? = nil,
528529
@_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
529530
) -> Task.Handle<T, Never> {
530-
return detach(priority: priority, operation: operation)
531+
return detach(priority: priority ?? .unspecified, operation: operation)
531532
}
532533

533534
@discardableResult
534-
@_alwaysEmitIntoClient
535535
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
536536
public func asyncDetached<T>(
537-
priority: Task.Priority = .unspecified,
537+
priority: Task.Priority? = nil,
538538
@_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
539539
) -> Task.Handle<T, Error> {
540-
return detach(priority: priority, operation: operation)
540+
return detach(priority: priority ?? .unspecified, operation: operation)
541541
}
542542

543543
/// ABI stub while we stage in the new signatures
@@ -571,6 +571,7 @@ func async(
571571
/// Task.currentPriority.
572572
/// - operation: the operation to execute
573573
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
574+
@discardableResult
574575
public func async<T>(
575576
priority: Task.Priority? = nil,
576577
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
@@ -606,6 +607,7 @@ public func async<T>(
606607
/// Task.currentPriority.
607608
/// - operation: the operation to execute
608609
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
610+
@discardableResult
609611
public func async<T>(
610612
priority: Task.Priority? = nil,
611613
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
@@ -781,12 +783,10 @@ public struct UnsafeCurrentTask {
781783

782784
/// Returns the `current` task's priority.
783785
///
784-
/// If no current `Task` is available, returns `Priority.default`.
785-
///
786786
/// - SeeAlso: `Task.Priority`
787787
/// - SeeAlso: `Task.currentPriority`
788788
public var priority: Task.Priority {
789-
getJobFlags(_task).priority
789+
getJobFlags(_task).priority ?? .default
790790
}
791791

792792
}

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)