1717
1818#include " ConcurrencyRuntime.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-
2820#include " CompatibilityOverride.h"
21+ #include " swift/Basic/ListMerger.h"
2922#include " swift/Runtime/Atomic.h"
3023#include " swift/Runtime/Casting.h"
31- #include " swift/Runtime/Once.h"
32- #include " swift/Runtime/Mutex.h"
33- #include " swift/Runtime/ThreadLocal.h"
34- #include " swift/Runtime/ThreadLocalStorage.h"
35- #include " swift/Basic/ListMerger.h"
24+ #include " swift/Threading/Once.h"
25+ #include " swift/Threading/Mutex.h"
26+ #include " swift/Threading/Thread.h"
27+ #include " swift/Threading/ThreadLocalStorage.h"
3628#ifndef SWIFT_CONCURRENCY_BACK_DEPLOYMENT
3729#include " llvm/Config/config.h"
3830#else
7163#include < sys/syscall.h>
7264#endif
7365
74- #if HAVE_PTHREAD_H
75- #include < pthread.h>
76-
77- // Only use __has_include since HAVE_PTHREAD_NP_H is not provided.
78- #if __has_include(<pthread_np.h>)
79- #include < pthread_np.h>
80- #endif
81- #endif
82-
8366#if defined(_WIN32)
8467#include < io.h>
85- #include < handleapi.h>
86- #include < processthreadsapi.h>
8768#endif
8869
8970#if SWIFT_OBJC_INTEROP
@@ -129,9 +110,9 @@ class ExecutorTrackingInfo {
129110 // / the right executor. It would make sense for that to be a
130111 // / separate thread-local variable (or whatever is most efficient
131112 // / on the target platform).
132- static SWIFT_RUNTIME_DECLARE_THREAD_LOCAL (
133- Pointer<ExecutorTrackingInfo>, ActiveInfoInThread,
134- SWIFT_CONCURRENCY_EXECUTOR_TRACKING_INFO_KEY) ;
113+ static SWIFT_THREAD_LOCAL_TYPE (Pointer<ExecutorTrackingInfo>,
114+ tls_key::concurrency_executor_tracking_info)
115+ ActiveInfoInThread ;
135116
136117 // / The active executor.
137118 ExecutorRef ActiveExecutor = ExecutorRef::generic();
@@ -154,7 +135,7 @@ class ExecutorTrackingInfo {
154135
155136 // / Unconditionally initialize a fresh tracking state on the
156137 // / current state, shadowing any previous tracking state.
157- // / leave() must be called beforet the object goes out of scope.
138+ // / leave() must be called before the object goes out of scope.
158139 void enterAndShadow (ExecutorRef currentExecutor) {
159140 ActiveExecutor = currentExecutor;
160141 SavedInfo = ActiveInfoInThread.get ();
@@ -197,24 +178,21 @@ class ExecutorTrackingInfo {
197178class ActiveTask {
198179 // / A thread-local variable pointing to the active tracking
199180 // / information about the current thread, if any.
200- static SWIFT_RUNTIME_DECLARE_THREAD_LOCAL (Pointer<AsyncTask>, Value ,
201- SWIFT_CONCURRENCY_TASK_KEY) ;
181+ static SWIFT_THREAD_LOCAL_TYPE (Pointer<AsyncTask>,
182+ tls_key::concurrency_task) Value ;
202183
203184public:
204185 static void set (AsyncTask *task) { Value.set (task); }
205186 static AsyncTask *get () { return Value.get (); }
206187};
207188
208189// / Define the thread-locals.
209- SWIFT_RUNTIME_DECLARE_THREAD_LOCAL (
210- Pointer<AsyncTask>,
211- ActiveTask::Value,
212- SWIFT_CONCURRENCY_TASK_KEY);
190+ SWIFT_THREAD_LOCAL_TYPE (Pointer<AsyncTask>, tls_key::concurrency_task)
191+ ActiveTask::Value;
213192
214- SWIFT_RUNTIME_DECLARE_THREAD_LOCAL (
215- Pointer<ExecutorTrackingInfo>,
216- ExecutorTrackingInfo::ActiveInfoInThread,
217- SWIFT_CONCURRENCY_EXECUTOR_TRACKING_INFO_KEY);
193+ SWIFT_THREAD_LOCAL_TYPE (Pointer<ExecutorTrackingInfo>,
194+ tls_key::concurrency_executor_tracking_info)
195+ ExecutorTrackingInfo::ActiveInfoInThread;
218196
219197} // end anonymous namespace
220198
@@ -238,7 +216,7 @@ void swift::runJobInEstablishedExecutorContext(Job *job) {
238216 task->runInFullyEstablishedContext ();
239217
240218 assert (ActiveTask::get () == nullptr &&
241- " active task wasn't cleared before susspending ?" );
219+ " active task wasn't cleared before suspending ?" );
242220 } else {
243221 // There's no extra bookkeeping to do for simple jobs besides swapping in
244222 // the voucher.
@@ -281,30 +259,20 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
281259 return result;
282260}
283261
284- #if defined(_WIN32)
285- static HANDLE __initialPthread = INVALID_HANDLE_VALUE;
286- #endif
287-
288262// / Determine whether we are currently executing on the main thread
289263// / independently of whether we know that we are on the main actor.
290264static bool isExecutingOnMainThread () {
291- #if defined(__linux__)
292- return syscall (SYS_gettid) == getpid ();
293- #elif defined(_WIN32)
294- if (__initialPthread == INVALID_HANDLE_VALUE) {
295- DuplicateHandle (GetCurrentProcess (), GetCurrentThread (),
296- GetCurrentProcess (), &__initialPthread, 0 , FALSE ,
297- DUPLICATE_SAME_ACCESS);
298- }
299-
300- return __initialPthread == GetCurrentThread ();
265+ #if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
266+ return true ;
301267#else
302- return pthread_main_np () == 1 ;
268+ return Thread::onMainThread () ;
303269#endif
304270}
305271
306272JobPriority swift::swift_task_getCurrentThreadPriority () {
307- #if defined(__APPLE__)
273+ #if SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY
274+ return JobPriority::UserInitiated;
275+ #elif defined(__APPLE__)
308276 return static_cast <JobPriority>(qos_class_self ());
309277#else
310278 if (isExecutingOnMainThread ())
@@ -344,8 +312,8 @@ void swift::swift_task_reportUnexpectedExecutor(
344312 const unsigned char *file, uintptr_t fileLength, bool fileIsASCII,
345313 uintptr_t line, ExecutorRef executor) {
346314 // Make sure we have an appropriate log level.
347- static swift_once_t logLevelToken;
348- swift_once (& logLevelToken, checkUnexpectedExecutorLogLevel, nullptr );
315+ static swift:: once_t logLevelToken;
316+ swift::once ( logLevelToken, checkUnexpectedExecutorLogLevel, nullptr );
349317
350318 bool isFatalError = false ;
351319 switch (unexpectedExecutorLogLevel) {
0 commit comments