Skip to content

Commit f2099c5

Browse files
committed
Make sure that current thread's priority propagates to a runInline task
1 parent 481a561 commit f2099c5

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,9 @@ class TaskCreateFlags : public FlagSet<size_t> {
22472247
RequestedPriority_width = 8,
22482248

22492249
Task_IsChildTask = 8,
2250-
// bit 9 is unused
2250+
// Should only be set in task-to-thread model where Task.runInline is
2251+
// available
2252+
Task_IsInlineTask = 9,
22512253
Task_CopyTaskLocals = 10,
22522254
Task_InheritContext = 11,
22532255
Task_EnqueueJob = 12,
@@ -2263,6 +2265,9 @@ class TaskCreateFlags : public FlagSet<size_t> {
22632265
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsChildTask,
22642266
isChildTask,
22652267
setIsChildTask)
2268+
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_IsInlineTask,
2269+
isInlineTask,
2270+
setIsInlineTask)
22662271
FLAGSET_DEFINE_FLAG_ACCESSORS(Task_CopyTaskLocals,
22672272
copyTaskLocals,
22682273
setCopyTaskLocals)

stdlib/public/Concurrency/Task.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,16 @@ static inline bool isUnspecified(JobPriority priority) {
574574
return priority == JobPriority::Unspecified;
575575
}
576576

577-
static inline bool taskIsUnstructured(JobFlags jobFlags) {
578-
return !jobFlags.task_isAsyncLetTask() && !jobFlags.task_isGroupChildTask();
577+
static inline bool taskIsStructured(JobFlags jobFlags) {
578+
return jobFlags.task_isAsyncLetTask() || jobFlags.task_isGroupChildTask();
579+
}
580+
581+
static inline bool taskIsUnstructured(TaskCreateFlags createFlags, JobFlags jobFlags) {
582+
return !taskIsStructured(jobFlags) && !createFlags.isInlineTask();
579583
}
580584

581585
static inline bool taskIsDetached(TaskCreateFlags createFlags, JobFlags jobFlags) {
582-
return taskIsUnstructured(jobFlags) && !createFlags.copyTaskLocals();
586+
return taskIsUnstructured(createFlags, jobFlags) && !createFlags.copyTaskLocals();
583587
}
584588

585589
static std::pair<size_t, size_t> amountToAllocateForHeaderAndTask(
@@ -692,20 +696,27 @@ static AsyncTaskAndContext swift_task_create_commonImpl(
692696
// Start with user specified priority at creation time (if any)
693697
JobPriority basePriority = (taskCreateFlags.getRequestedPriority());
694698

695-
if (taskIsDetached(taskCreateFlags, jobFlags)) {
696-
SWIFT_TASK_DEBUG_LOG("Creating a detached task from %p", currentTask);
697-
// Case 1: No priority specified
698-
// Base priority = UN
699-
// Escalated priority = UN
700-
// Case 2: Priority specified
701-
// Base priority = user specified priority
702-
// Escalated priority = UN
703-
//
704-
// Task will be created with max priority = max(base priority, UN) = base
705-
// priority. We shouldn't need to do any additional manipulations here since
706-
// basePriority should already be the right value
699+
if (taskCreateFlags.isInlineTask()) {
700+
SWIFT_TASK_DEBUG_LOG("Creating an inline task from %p", currentTask);
701+
702+
// We'll take the current priority and set it as base and escalated
703+
// priority of the task. No UI->IN downgrade needed.
704+
basePriority = swift_task_getCurrentThreadPriority();
707705

708-
} else if (taskIsUnstructured(jobFlags)) {
706+
} else if (taskIsDetached(taskCreateFlags, jobFlags)) {
707+
SWIFT_TASK_DEBUG_LOG("Creating a detached task from %p", currentTask);
708+
// Case 1: No priority specified
709+
// Base priority = UN
710+
// Escalated priority = UN
711+
// Case 2: Priority specified
712+
// Base priority = user specified priority
713+
// Escalated priority = UN
714+
//
715+
// Task will be created with max priority = max(base priority, UN) = base
716+
// priority. We shouldn't need to do any additional manipulations here since
717+
// basePriority should already be the right value
718+
719+
} else if (taskIsUnstructured(taskCreateFlags, jobFlags)) {
709720
SWIFT_TASK_DEBUG_LOG("Creating an unstructured task from %p", currentTask);
710721

711722
if (isUnspecified(basePriority)) {
@@ -1009,8 +1020,10 @@ void swift::swift_task_run_inline(OpaqueValue *result, void *closureAFP,
10091020
// containing a pointer to the allocation enabling us to provide our stack
10101021
// allocation rather than swift_task_create_common having to malloc it.
10111022
RunInlineTaskOptionRecord option(allocation, allocationBytes);
1023+
size_t taskCreateFlags = 1 << TaskCreateFlags::Task_IsInlineTask;
1024+
10121025
auto taskAndContext = swift_task_create_common(
1013-
/*rawTaskCreateFlags=*/0, &option, futureResultType,
1026+
taskCreateFlags, &option, futureResultType,
10141027
reinterpret_cast<TaskContinuationFunction *>(closure), closureContext,
10151028
/*initialContextSize=*/closureContextSize);
10161029

0 commit comments

Comments
 (0)