Skip to content

Commit 1b47910

Browse files
committed
stdlib: repair the Windows runtime build
Fix syntactic issues in the swift runtime build from the recent TLS changes. Move the helper definitions into the TLS stubs rather than emitting them everywhere. This allows the runtime to build again on Windows.
1 parent 8ce03fa commit 1b47910

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

stdlib/public/runtime/ThreadLocalStorage.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,6 @@ typedef unsigned long __swift_thread_key_t;
8888
static_assert(std::is_same<__swift_thread_key_t, DWORD>::value,
8989
"__swift_thread_key_t is not a DWORD");
9090

91-
# if defined(_M_IX86)
92-
typedef stdcall void (*__swift_thread_key_destructor)(void *)
93-
# else
94-
typedef void (*__swift_thread_key_destructor)(void *)
95-
# endif
96-
97-
static inline
98-
_stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,
99-
__swift_thread_key_destructor _Nullable destructor) {
100-
*key = FlsAlloc(destroyTLS_CCAdjustmentThunk);
101-
return *key != FLS_OUT_OF_INDEXES;
102-
}
103-
10491
# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create
10592
# define SWIFT_THREAD_GETSPECIFIC FlsGetValue
10693
# define SWIFT_THREAD_SETSPECIFIC(key, value) (FlsSetValue(key, value) == TRUE)

stdlib/public/stubs/ThreadLocalStorage.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,39 @@
1919

2020
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
2121
void _stdlib_destroyTLS(void *);
22+
2223
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
2324
void *_stdlib_createTLS(void);
2425

26+
#ifndef SWIFT_THREAD_GETSPECIFIC
27+
28+
# if defined(_WIN32) && !defined(__CYGWIN__)
29+
30+
# if defined(_M_IX86)
31+
typedef __stdcall void (*__swift_thread_key_destructor)(void *);
32+
# else
33+
typedef void (*__swift_thread_key_destructor)(void *);
34+
# endif
35+
36+
static void
37+
#if defined(_M_IX86)
38+
__stdcall
39+
#endif
40+
destroyTLS_CCAdjustmentThunk(void *ptr) {
41+
_stdlib_destroyTLS(ptr);
42+
}
43+
44+
static inline int
45+
_stdlib_thread_key_create(__swift_thread_key_t * _Nonnull key,
46+
__swift_thread_key_destructor _Nullable destructor) {
47+
*key = FlsAlloc(destroyTLS_CCAdjustmentThunk);
48+
return *key != FLS_OUT_OF_INDEXES;
49+
}
50+
51+
# endif
52+
53+
#endif
54+
2555
#if SWIFT_TLS_HAS_RESERVED_PTHREAD_SPECIFIC
2656

2757
SWIFT_RUNTIME_STDLIB_INTERNAL
@@ -52,7 +82,8 @@ SWIFT_RUNTIME_STDLIB_INTERNAL
5282
void *
5383
_swift_stdlib_threadLocalStorageGet(void) {
5484
static swift::OnceToken_t token;
55-
static pthread_key_t key;
85+
static __swift_thread_key_t key;
86+
5687
SWIFT_ONCE_F(token, [](void *) {
5788
int result = SWIFT_THREAD_KEY_CREATE(&key, [](void *pointer) {
5889
_stdlib_destroyTLS(pointer);
@@ -61,7 +92,7 @@ _swift_stdlib_threadLocalStorageGet(void) {
6192
swift::fatalError(0, "couldn't create pthread key for stdlib TLS: %s\n",
6293
std::strerror(result));
6394
}, nullptr);
64-
95+
6596
void *value = SWIFT_THREAD_GETSPECIFIC(key);
6697
if (!value) {
6798
value = _stdlib_createTLS();

0 commit comments

Comments
 (0)