Skip to content

Commit 3744d4d

Browse files
[wasm] Port thread-related APIs for no thread platform
This change ports the thread-related APIs for the no thread platform by adding no-op implementations. Note that WASI is going to support threads in the near future, so this does not guard by `TARGET_OS_WASI` but by `!SWIFT_CORELIBS_FOUNDATION_HAS_THREADS` to make it easier for future WASI + threads support.
1 parent dbca8c7 commit 3744d4d

File tree

8 files changed

+136
-35
lines changed

8 files changed

+136
-35
lines changed

CoreFoundation/Base.subproj/CFInternal.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,37 @@ CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
654654
LeaveCriticalSection(mutex);
655655
return 0;
656656
}
657+
#elif TARGET_OS_WASI
658+
// For wasi-libc without pthread support (_POSIX_THREADS), just assume that it's single-threaded.
659+
// wasi-libc with pthread support is handled by the _POSIX_THREADS case above.
660+
typedef void *_CFMutex;
661+
#define _CF_MUTEX_STATIC_INITIALIZER {}
662+
CF_INLINE int _CFMutexCreate(_CFMutex *lock) {
663+
return 0;
664+
}
665+
CF_INLINE int _CFMutexDestroy(_CFMutex *lock) {
666+
return 0;
667+
}
668+
CF_INLINE int _CFMutexLock(_CFMutex *lock) {
669+
return 0;
670+
}
671+
CF_INLINE int _CFMutexUnlock(_CFMutex *lock) {
672+
return 0;
673+
}
674+
675+
typedef void *_CFRecursiveMutex;
676+
CF_INLINE int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
677+
return 0;
678+
}
679+
CF_INLINE int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
680+
return 0;
681+
}
682+
CF_INLINE int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
683+
return 0;
684+
}
685+
CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
686+
return 0;
687+
}
657688
#else
658689
#error "do not know how to define mutex and recursive mutex for this OS"
659690
#endif
@@ -677,7 +708,7 @@ typedef uint32_t os_unfair_lock_options_t;
677708
static void os_unfair_lock_lock(os_unfair_lock_t lock) { pthread_mutex_lock(lock); }
678709
static void os_unfair_lock_lock_with_options(os_unfair_lock_t lock, os_unfair_lock_options_t options) { pthread_mutex_lock(lock); }
679710
static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(lock); }
680-
#elif defined(_WIN32)
711+
#elif defined(_WIN32) || TARGET_OS_WASI
681712
#define OS_UNFAIR_LOCK_INIT CFLockInit
682713
#define os_unfair_lock CFLock_t
683714
#define os_unfair_lock_lock __CFLock

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,8 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) {
16281628
extern void swift_retain(void *);
16291629
extern void swift_release(void *);
16301630

1631+
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
1632+
16311633
#if TARGET_OS_WIN32
16321634
typedef struct _CFThreadSpecificData {
16331635
CFTypeRef value;
@@ -1806,6 +1808,7 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
18061808
#endif
18071809
return -1;
18081810
}
1811+
#endif // SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
18091812

18101813
CF_EXPORT char **_CFEnviron(void) {
18111814
#if TARGET_OS_MAC

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,13 @@ void __CFInitialize(void) {
11941194
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
11951195
GetCurrentProcess(), &_CFMainPThread, 0, FALSE,
11961196
DUPLICATE_SAME_ACCESS);
1197-
#else
1197+
#elif _POSIX_THREADS
11981198
// move this next line up into the #if above after Foundation gets off this symbol. Also: <rdar://problem/39622745> Stop using _CFMainPThread
11991199
_CFMainPThread = pthread_self();
1200+
#elif TARGET_OS_WASI
1201+
_CFMainPThread = kNilPthreadT;
1202+
#else
1203+
#error Dont know how to get the main thread on this platform
12001204
#endif
12011205

12021206
#if TARGET_OS_WIN32

CoreFoundation/Base.subproj/CFUtilities.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,13 @@ static void _populateBanner(char **banner, char **time, char **thread, int *bann
927927
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%lx] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId());
928928
asprintf(thread, "%lx", GetCurrentThreadId());
929929
#elif TARGET_OS_WASI
930-
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)pthread_self());
931-
asprintf(thread, "%lx", pthread_self());
930+
_CFThreadRef tid = 0;
931+
// When pthread API is available from wasi-libc, use it. Otherwise use the dummy value.
932+
# if _POSIX_THREADS
933+
tid = pthread_self();
934+
# endif
935+
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)tid);
936+
asprintf(thread, "%lx", tid);
932937
#else
933938
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%x] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), (unsigned int)pthread_self());
934939
asprintf(thread, "%lx", pthread_self());

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@ typedef unsigned long _CFThreadSpecificKey;
412412
typedef pthread_t _CFThreadRef;
413413
typedef pthread_attr_t _CFThreadAttributes;
414414
typedef pthread_key_t _CFThreadSpecificKey;
415+
#elif TARGET_OS_WASI // WASI without pthreads
416+
typedef void *_CFThreadRef;
417+
typedef void *_CFThreadAttributes;
418+
typedef void *_CFThreadSpecificKey;
415419
#endif
416420

417421
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void);
@@ -423,6 +427,7 @@ CF_EXPORT CFHashCode __CFHashDouble(double d);
423427
CF_CROSS_PLATFORM_EXPORT void CFSortIndexes(CFIndex *indexBuffer, CFIndex count, CFOptionFlags opts, CFComparisonResult (^cmp)(CFIndex, CFIndex));
424428
#endif
425429

430+
#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS
426431
CF_EXPORT CFTypeRef _Nullable _CFThreadSpecificGet(_CFThreadSpecificKey key);
427432
CF_EXPORT void _CFThreadSpecificSet(_CFThreadSpecificKey key, CFTypeRef _Nullable value);
428433
CF_EXPORT _CFThreadSpecificKey _CFThreadSpecificKeyCreate(void);
@@ -431,6 +436,7 @@ CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_N
431436

432437
CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name);
433438
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);
439+
#endif
434440

435441
CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
436442
CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);

CoreFoundation/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ else()
9999
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:DEPLOYMENT_RUNTIME_C>)
100100
endif()
101101

102+
if(Threads_FOUND)
103+
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:SWIFT_CORELIBS_FOUNDATION_HAS_THREADS>)
104+
endif()
105+
102106
# TODO(compnerd) ensure that the compiler supports the warning flag
103107
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wno-shorten-64-to-32>)
104108
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wno-deprecated-declarations>)

Sources/Foundation/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ add_library(Foundation
150150
WinSDK+Extensions.swift)
151151
target_compile_definitions(Foundation PRIVATE
152152
DEPLOYMENT_RUNTIME_SWIFT)
153+
if(Threads_FOUND)
154+
target_compile_definitions(Foundation PRIVATE
155+
SWIFT_CORELIBS_FOUNDATION_HAS_THREADS)
156+
target_compile_options(Foundation PRIVATE
157+
"SHELL:-Xcc -DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
158+
endif()
159+
153160
target_compile_options(Foundation PUBLIC
154161
$<$<BOOL:${ENABLE_TESTING}>:-enable-testing>
155162
"SHELL:-Xfrontend -disable-autolink-framework -Xfrontend CoreFoundation"

0 commit comments

Comments
 (0)