Skip to content

Commit 8e9823c

Browse files
committed
Store the current task and executor in task-local storage.
1 parent 81c0d09 commit 8e9823c

File tree

8 files changed

+213
-123
lines changed

8 files changed

+213
-123
lines changed

include/swift/ABI/Task.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,17 @@ class alignas(2 * alignof(void*)) Job {
8080
return Flags.getPriority();
8181
}
8282

83-
/// Run this job.
84-
void run(ExecutorRef currentExecutor);
83+
/// Given that we've fully established the job context in the current
84+
/// thread, actually start running this job. To establish the context
85+
/// correctly, call swift_job_run or runJobInExecutorContext.
86+
void runInFullyEstablishedContext(ExecutorRef currentExecutor);
87+
88+
/// Given that we've fully established the job context in the
89+
/// current thread, and that the job is a simple (non-task) job,
90+
/// actually start running this job.
91+
void runSimpleInFullyEstablishedContext(ExecutorRef currentExecutor) {
92+
RunJob(this, currentExecutor);
93+
}
8594
};
8695

8796
// The compiler will eventually assume these.
@@ -173,7 +182,11 @@ class AsyncTask : public HeapObject, public Job {
173182
assert(flags.isAsyncTask());
174183
}
175184

176-
void run(ExecutorRef currentExecutor) {
185+
/// Given that we've already fully established the job context
186+
/// in the current thread, start running this task. To establish
187+
/// the job context correctly, call swift_job_run or
188+
/// runInExecutorContext.
189+
void runInFullyEstablishedContext(ExecutorRef currentExecutor) {
177190
ResumeTask(this, currentExecutor, ResumeContext);
178191
}
179192

@@ -941,11 +954,11 @@ static_assert(sizeof(AsyncTask) == 12 * sizeof(void*),
941954
static_assert(alignof(AsyncTask) == 2 * alignof(void*),
942955
"AsyncTask alignment is wrong");
943956

944-
inline void Job::run(ExecutorRef currentExecutor) {
957+
inline void Job::runInFullyEstablishedContext(ExecutorRef currentExecutor) {
945958
if (auto task = dyn_cast<AsyncTask>(this))
946-
task->run(currentExecutor);
959+
task->runInFullyEstablishedContext(currentExecutor);
947960
else
948-
RunJob(this, currentExecutor);
961+
runSimpleInFullyEstablishedContext(currentExecutor);
949962
}
950963

951964
/// An asynchronous context within a task. Generally contexts are

include/swift/Runtime/Concurrency.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,16 @@ void swift_continuation_logFailedCheck(const char *message);
422422
/// Otherwise it uses dispatchMain.
423423
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
424424
void swift_task_asyncMainDrainQueue();
425+
426+
/// Establish that the current thread is running as the given
427+
/// executor, then run a job.
428+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
429+
void swift_job_run(Job *job, ExecutorRef executor);
430+
431+
/// Return the current thread's active task reference.
432+
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
433+
AsyncTask *swift_task_getCurrent(void);
434+
425435
}
426436

427437
#endif

0 commit comments

Comments
 (0)