Skip to content

Commit a722748

Browse files
committed
More Windows workarounds
1 parent ea666db commit a722748

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

stdlib/public/Concurrency/Actor.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454

5555
#if defined(_WIN32)
5656
#include <io.h>
57+
#include <handleapi.h>
58+
#include <processthreadsapi.h>
5759
#endif
5860

5961
using namespace swift;
@@ -235,11 +237,23 @@ static ExecutorRef swift_task_getCurrentExecutorImpl() {
235237
return result;
236238
}
237239

240+
#if defined(_WIN32)
241+
static _CFThreadRef __initialPthread = INVALID_HANDLE_VALUE;
242+
#endif
243+
238244
/// Determine whether we are currently executing on the main thread
239245
/// independently of whether we know that we are on the main actor.
240246
static bool isExecutingOnMainThread() {
241247
#if defined(__linux__)
242248
return syscall(SYS_gettid) == getpid();
249+
#elseif defined(_WIN32)
250+
if (__initialPthread == INVALID_HANDLE_VALUE) {
251+
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
252+
GetCurrentProcess(), &__initialPthread, 0, FALSE,
253+
DUPLICATE_SAME_ACCESS);
254+
}
255+
256+
return CompareObjectHandles(__initialPthread, GetCurrentThread());
243257
#else
244258
return pthread_main_np() == 1;
245259
#endif

0 commit comments

Comments
 (0)