@@ -900,6 +900,70 @@ extension Task where Success == Never, Failure == Never {
900
900
}
901
901
}
902
902
903
+ // ==== Manual Priority Escalation ---------------------------------------------
904
+
905
+ extension Task {
906
+ /// Escalate the task `priority` of the passed in task to the `newPriority`.
907
+ ///
908
+ /// - Warning: This API should rarely be used, and instead you can rely on
909
+ /// structured concurrency and implicit priority escalation which happens
910
+ /// when a higher priority task awaits on a result of a lower priority task.
911
+ ///
912
+ /// I.e. using `await` on the target task usually is the correct way to
913
+ /// escalate the target task to the current priority of the calling task,
914
+ /// especially because in such setup if the waiting task gets escalated,
915
+ /// the waited on task would be escalated automatically as well.
916
+ ///
917
+ /// The concurrency runtime is free to interpret and handle escalation
918
+ /// depending on platform characteristics.
919
+ ///
920
+ /// Priority escalation is propagated to child tasks of the waited-on task,
921
+ /// and will trigger any priority escalation handlers, if any were registered.
922
+ ///
923
+ /// Escalation can only *increase* the priority of a task, and
924
+ /// de-escalating priority is not supported.
925
+ ///
926
+ /// This method can be called from any task or thread.
927
+ ///
928
+ /// - Parameters:
929
+ /// - task: the task which to escalate the priority of
930
+ /// - newPriority: the new priority the task should continue executing on
931
+ @available ( SwiftStdlib 9999 , * )
932
+ public static func escalatePriority( _ task: UnsafeCurrentTask , to newPriority: TaskPriority ) {
933
+ _taskEscalate ( task. _task, newPriority: newPriority. rawValue)
934
+ }
935
+
936
+ /// Escalate the task `priority` of the passed in task to the `newPriority`.
937
+ ///
938
+ /// - Warning: This API should rarely be used, and instead you can rely on
939
+ /// structured concurrency and implicit priority escalation which happens
940
+ /// when a higher priority task awaits on a result of a lower priority task.
941
+ ///
942
+ /// I.e. using `await` on the target task usually is the correct way to
943
+ /// escalate the target task to the current priority of the calling task,
944
+ /// especially because in such setup if the waiting task gets escalated,
945
+ /// the waited on task would be escalated automatically as well.
946
+ ///
947
+ /// The concurrency runtime is free to interpret and handle escalation
948
+ /// depending on platform characteristics.
949
+ ///
950
+ /// Priority escalation is propagated to child tasks of the waited-on task,
951
+ /// and will trigger any priority escalation handlers, if any were registered.
952
+ ///
953
+ /// Escalation can only *increase* the priority of a task, and
954
+ /// de-escalating priority is not supported.
955
+ ///
956
+ /// This method can be called from any task or thread.
957
+ ///
958
+ /// - Parameters:
959
+ /// - task: the task which to escalate the priority of
960
+ /// - newPriority: the new priority the task should continue executing on
961
+ @available ( SwiftStdlib 9999 , * )
962
+ public static func escalatePriority( _ task: UnsafeCurrentTask , to newPriority: TaskPriority ) {
963
+ _taskEscalate ( task. _task, newPriority: newPriority. rawValue)
964
+ }
965
+ }
966
+
903
967
// ==== UnsafeCurrentTask ------------------------------------------------------
904
968
905
969
/// Calls a closure with an unsafe reference to the current task.
@@ -1152,6 +1216,11 @@ func _taskIsCancelled(_ task: Builtin.NativeObject) -> Bool
1152
1216
@_silgen_name ( " swift_task_currentPriority " )
1153
1217
internal func _taskCurrentPriority( _ task: Builtin . NativeObject ) -> UInt8
1154
1218
1219
+ @available ( SwiftStdlib 9999 , * ) // TODO: determine how far back this can back-deploy because it already was in runtime
1220
+ @_silgen_name ( " swift_task_escalate " )
1221
+ internal func _taskEscalate( _ task: Builtin . NativeObject , newPriority: UInt8 )
1222
+
1223
+ @available( SwiftStdlib 5.1 , * )
1155
1224
@_silgen_name( " swift_task_basePriority" )
1156
1225
internal func _taskBasePriority( _ task: Builtin . NativeObject ) -> UInt8
1157
1226
0 commit comments