Skip to content

Commit b32f878

Browse files
authored
Merge pull request #37182 from fredriss/stable-tsd-5.5
5.5 Cherry-pick: [Concurrency] Use a stable TSD on Darwin to store the current Task
2 parents 7b1e89e + cdfa45c commit b32f878

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
#include "swift/Runtime/Concurrency.h"
1919

20+
#ifdef _WIN32
21+
// On Windows, an include below triggers an indirect include of minwindef.h
22+
// which contains a definition of the `max` macro, generating an error in our
23+
// use of std::max in this file. This define prevents those macros from being
24+
// defined.
25+
#define NOMINMAX
26+
#endif
27+
2028
#include "../CompatibilityOverride/CompatibilityOverride.h"
29+
#include "../runtime/ThreadLocalStorage.h"
2130
#include "swift/Runtime/Atomic.h"
2231
#include "swift/Runtime/Casting.h"
2332
#include "swift/Runtime/Once.h"
@@ -177,6 +186,17 @@ class ExecutorTrackingInfo {
177186
}
178187
};
179188

189+
#ifdef SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
190+
class ActiveTask {
191+
public:
192+
static void set(AsyncTask *task) {
193+
SWIFT_THREAD_SETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY, task);
194+
}
195+
static AsyncTask *get() {
196+
return (AsyncTask *)SWIFT_THREAD_GETSPECIFIC(SWIFT_CONCURRENCY_TASK_KEY);
197+
}
198+
};
199+
#else
180200
class ActiveTask {
181201
/// A thread-local variable pointing to the active tracking
182202
/// information about the current thread, if any.
@@ -188,12 +208,13 @@ class ActiveTask {
188208
};
189209

190210
/// Define the thread-locals.
191-
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
192-
Pointer<ExecutorTrackingInfo>,
193-
ExecutorTrackingInfo::ActiveInfoInThread);
194211
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
195212
Pointer<AsyncTask>,
196213
ActiveTask::Value);
214+
#endif
215+
SWIFT_RUNTIME_DECLARE_THREAD_LOCAL(
216+
Pointer<ExecutorTrackingInfo>,
217+
ExecutorTrackingInfo::ActiveInfoInThread);
197218

198219
} // end anonymous namespace
199220

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)