Skip to content

Commit 70a2627

Browse files
authored
Merge pull request #3064 from ktoso/patch-16
Fixup: onPriorityEscalated handler includes old priority
2 parents ddb9a47 + e623f8f commit 70a2627

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

proposals/0462-task-priority-escalation-apis.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ await withTaskPriorityEscalationHandler {
9595
if let newPriority {
9696
Task.escalatePriority(of: task, to: newPriority)
9797
}
98-
} onPriorityEscalated: { newPriority in
98+
} onPriorityEscalated: { oldPriority, newPriority in
9999
state.withLock { state in
100100
switch state {
101101
case .initialized, .priority:
@@ -123,12 +123,12 @@ We propose the addition of a task priority escalation handler, similar to task c
123123
```swift
124124
public func withTaskPriorityEscalationHandler<T, E>(
125125
operation: () async throws(E) -> T,
126-
onPriorityEscalated handler: @Sendable (TaskPriority) -> Void,
126+
onPriorityEscalated handler: @Sendable (TaskPriority, TaskPriority) -> Void,
127127
isolation: isolated (any Actor)? = #isolation
128128
) async throws(E) -> T
129129
```
130130

131-
The shape of this API is similar to the `withTaskCancellationHandler` API present since initial Swift Concurrency release, however–unlike a cancellation handler–the `onPriorityEscalated` callback may be triggered multiple times. The `TaskPriority` passed to the handler is the "new priority" the surrounding task was escalated to.
131+
The shape of this API is similar to the `withTaskCancellationHandler` API present since initial Swift Concurrency release, however–unlike a cancellation handler–the `onPriorityEscalated` callback may be triggered multiple times. There are two `TaskPriority` arguments passed to the handler. The first argument is the "old" priority, from before the task priority was escalated, and the second argument is the new escalated-to task priority.
132132

133133
It is guaranteed that priority is ever only increasing, as Swift Concurrency does not allow for a task priority to ever be lowered after it has been escalated. If attempts are made to escalate the task priority from multiple other threads to the same priority, the handler will only trigger once. However if priority is escalated to a high and then even higher priority, the handler may be invoked twice.
134134

@@ -139,7 +139,7 @@ Task escalation handlers are inherently racy, and may sometimes miss an escalati
139139
// priority: high!
140140
await withTaskPriorityEscalationHandler {
141141
await work()
142-
} onPriorityEscalated: { newPriority in // may not be triggered if ->high escalation happened before handler was installed
142+
} onPriorityEscalated: { oldPriority, newPriority in // may not be triggered if ->high escalation happened before handler was installed
143143
// do something
144144
}
145145
```
@@ -155,10 +155,10 @@ let t = Task {
155155
group.addTask {
156156
await withTaskPriorityEscalationHandler {
157157
try? await Task.sleep(for: .seconds(1))
158-
} onPriorityEscalated: { newPriority in print("inner: \(newPriority)") }
158+
} onPriorityEscalated: { oldPriority, newPriority in print("inner: \(newPriority)") }
159159
}
160160
}
161-
} onPriorityEscalated: { newPriority in print("outer: \(newPriority)") }
161+
} onPriorityEscalated: { oldPriority, newPriority in print("outer: \(newPriority)") }
162162
}
163163

164164
// escalate t -> high

0 commit comments

Comments
 (0)