@@ -196,6 +196,9 @@ class ActiveTask {
196196public:
197197 static void set (AsyncTask *task) { Value.set (task); }
198198 static AsyncTask *get () { return Value.get (); }
199+ static AsyncTask *swap (AsyncTask *newTask) {
200+ return Value.swap (newTask);
201+ }
199202};
200203
201204// / Define the thread-locals.
@@ -217,7 +220,7 @@ void swift::runJobInEstablishedExecutorContext(Job *job) {
217220
218221 if (auto task = dyn_cast<AsyncTask>(job)) {
219222 // Update the active task in the current thread.
220- ActiveTask::set (task);
223+ auto oldTask = ActiveTask::swap (task);
221224
222225 // Update the task status to say that it's running on the
223226 // current thread. If the task suspends somewhere, it should
@@ -231,6 +234,7 @@ void swift::runJobInEstablishedExecutorContext(Job *job) {
231234
232235 assert (ActiveTask::get () == nullptr &&
233236 " active task wasn't cleared before suspending?" );
237+ if (oldTask) ActiveTask::set (oldTask);
234238 } else {
235239 // There's no extra bookkeeping to do for simple jobs besides swapping in
236240 // the voucher.
@@ -259,15 +263,11 @@ AsyncTask *swift::swift_task_getCurrent() {
259263}
260264
261265AsyncTask *swift::_swift_task_clearCurrent () {
262- auto task = ActiveTask::get ();
263- ActiveTask::set (nullptr );
264- return task;
266+ return ActiveTask::swap (nullptr );
265267}
266268
267269AsyncTask *swift::_swift_task_setCurrent (AsyncTask *new_task) {
268- auto task = ActiveTask::get ();
269- ActiveTask::set (new_task);
270- return task;
270+ return ActiveTask::swap (new_task);
271271}
272272
273273SWIFT_CC (swift)
0 commit comments