Skip to content

Commit 0b4b4e1

Browse files
committed
[Concurrency] Use a stable TSD on Darwin to store the current Task
This gives low-level tools (e.g. backtracers) an ABI way to access the current task. (cherry picked from commit 8f1a73c)
1 parent cce9259 commit 0b4b4e1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Runtime/Concurrency.h"
1919

2020
#include "../CompatibilityOverride/CompatibilityOverride.h"
21+
#include "../runtime/ThreadLocalStorage.h"
2122
#include "swift/Runtime/Atomic.h"
2223
#include "swift/Runtime/Casting.h"
2324
#include "swift/Runtime/Once.h"
@@ -177,6 +178,17 @@ class ExecutorTrackingInfo {
177178
}
178179
};
179180

181+
#ifdef SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
182+
class ActiveTask {
183+
public:
184+
static void set(AsyncTask *task) {
185+
SWIFT_THREAD_SETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY, task);
186+
}
187+
static AsyncTask *get() {
188+
return (AsyncTask *)SWIFT_THREAD_GETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY);
189+
}
190+
};
191+
#else
180192
class ActiveTask {
181193
/// A thread-local variable pointing to the active tracking
182194
/// information about the current thread, if any.
@@ -188,12 +200,13 @@ class ActiveTask {
188200
};
189201

190202
/// Define the thread-locals.
191-
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
192-
Pointer<ExecutorTrackingInfo>,
193-
ExecutorTrackingInfo::ActiveInfoInThread);
194203
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
195204
Pointer<AsyncTask>,
196205
ActiveTask::Value);
206+
#endif
207+
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
208+
Pointer<ExecutorTrackingInfo>,
209+
ExecutorTrackingInfo::ActiveInfoInThread);
197210

198211
} // end anonymous namespace
199212

stdlib/public/runtime/ThreadLocalStorage.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ extern "C" int pthread_key_init_np(int key, void (*destructor)(void *));
4545
# ifndef __PTK_FRAMEWORK_SWIFT_KEY2
4646
# define __PTK_FRAMEWORK_SWIFT_KEY2 102
4747
# endif
48+
# ifndef __PTK_FRAMEWORK_SWIFT_KEY3
49+
# define __PTK_FRAMEWORK_SWIFT_KEY3 103
50+
# endif
4851

4952

5053
# define SWIFT_RUNTIME_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY0
5154
# define SWIFT_STDLIB_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY1
5255
# define SWIFT_COMPATIBILITY_50_TLS_KEY __PTK_FRAMEWORK_SWIFT_KEY2
56+
# define SWIFT_CONCURRENCY_TASK_KEY __PTK_FRAMEWORK_SWIFT_KEY3
5357

5458
#endif
5559

0 commit comments

Comments
 (0)