Skip to content

[wasm] Port thread-related APIs for no thread platform #4870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion CoreFoundation/Base.subproj/CFInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,37 @@ CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
LeaveCriticalSection(mutex);
return 0;
}
#elif TARGET_OS_WASI
// For wasi-libc without pthread support (_POSIX_THREADS), just assume that it's single-threaded.
// wasi-libc with pthread support is handled by the _POSIX_THREADS case above.
typedef void *_CFMutex;
#define _CF_MUTEX_STATIC_INITIALIZER {}
CF_INLINE int _CFMutexCreate(_CFMutex *lock) {
return 0;
}
CF_INLINE int _CFMutexDestroy(_CFMutex *lock) {
return 0;
}
CF_INLINE int _CFMutexLock(_CFMutex *lock) {
return 0;
}
CF_INLINE int _CFMutexUnlock(_CFMutex *lock) {
return 0;
}

typedef void *_CFRecursiveMutex;
CF_INLINE int _CFRecursiveMutexCreate(_CFRecursiveMutex *mutex) {
return 0;
}
CF_INLINE int _CFRecursiveMutexDestroy(_CFRecursiveMutex *mutex) {
return 0;
}
CF_INLINE int _CFRecursiveMutexLock(_CFRecursiveMutex *mutex) {
return 0;
}
CF_INLINE int _CFRecursiveMutexUnlock(_CFRecursiveMutex *mutex) {
return 0;
}
#else
#error "do not know how to define mutex and recursive mutex for this OS"
#endif
Expand All @@ -677,7 +708,7 @@ typedef uint32_t os_unfair_lock_options_t;
static void os_unfair_lock_lock(os_unfair_lock_t lock) { pthread_mutex_lock(lock); }
static void os_unfair_lock_lock_with_options(os_unfair_lock_t lock, os_unfair_lock_options_t options) { pthread_mutex_lock(lock); }
static void os_unfair_lock_unlock(os_unfair_lock_t lock) { pthread_mutex_unlock(lock); }
#elif defined(_WIN32)
#elif defined(_WIN32) || TARGET_OS_WASI
#define OS_UNFAIR_LOCK_INIT CFLockInit
#define os_unfair_lock CFLock_t
#define os_unfair_lock_lock __CFLock
Expand Down
3 changes: 3 additions & 0 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,8 @@ CF_PRIVATE int asprintf(char **ret, const char *format, ...) {
extern void swift_retain(void *);
extern void swift_release(void *);

#if SWIFT_CORELIBS_FOUNDATION_HAS_THREADS

#if TARGET_OS_WIN32
typedef struct _CFThreadSpecificData {
CFTypeRef value;
Expand Down Expand Up @@ -1806,6 +1808,7 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
#endif
return -1;
}
#endif // SWIFT_CORELIBS_FOUNDATION_HAS_THREADS

CF_EXPORT char **_CFEnviron(void) {
#if TARGET_OS_MAC
Expand Down
6 changes: 5 additions & 1 deletion CoreFoundation/Base.subproj/CFRuntime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1194,9 +1194,13 @@ void __CFInitialize(void) {
DuplicateHandle(GetCurrentProcess(), GetCurrentThread(),
GetCurrentProcess(), &_CFMainPThread, 0, FALSE,
DUPLICATE_SAME_ACCESS);
#else
#elif _POSIX_THREADS
// move this next line up into the #if above after Foundation gets off this symbol. Also: <rdar://problem/39622745> Stop using _CFMainPThread
_CFMainPThread = pthread_self();
#elif TARGET_OS_WASI
_CFMainPThread = kNilPthreadT;
#else
#error Dont know how to get the main thread on this platform
#endif

#if TARGET_OS_WIN32
Expand Down
9 changes: 7 additions & 2 deletions CoreFoundation/Base.subproj/CFUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -927,8 +927,13 @@ static void _populateBanner(char **banner, char **time, char **thread, int *bann
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d %s[%d:%lx] ", year, month, day, hour, minute, second, ms, *_CFGetProgname(), getpid(), GetCurrentThreadId());
asprintf(thread, "%lx", GetCurrentThreadId());
#elif TARGET_OS_WASI
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)pthread_self());
asprintf(thread, "%lx", pthread_self());
_CFThreadRef tid = 0;
// When pthread API is available from wasi-libc, use it. Otherwise use the dummy value.
# if _POSIX_THREADS
tid = pthread_self();
# endif
bannerLen = asprintf(banner, "%04d-%02d-%02d %02d:%02d:%02d.%03d [%x] ", year, month, day, hour, minute, second, ms, (unsigned int)tid);
asprintf(thread, "%lx", tid);
#else
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());
asprintf(thread, "%lx", pthread_self());
Expand Down
6 changes: 6 additions & 0 deletions CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ typedef unsigned long _CFThreadSpecificKey;
typedef pthread_t _CFThreadRef;
typedef pthread_attr_t _CFThreadAttributes;
typedef pthread_key_t _CFThreadSpecificKey;
#elif TARGET_OS_WASI // WASI without pthreads
typedef void *_CFThreadRef;
typedef void *_CFThreadAttributes;
typedef void *_CFThreadSpecificKey;
#endif

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

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

CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_Nonnull name);
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);
#endif

CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);
Expand Down
4 changes: 4 additions & 0 deletions CoreFoundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ else()
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:DEPLOYMENT_RUNTIME_C>)
endif()

if(Threads_FOUND)
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:SWIFT_CORELIBS_FOUNDATION_HAS_THREADS>)
endif()

# TODO(compnerd) ensure that the compiler supports the warning flag
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wno-shorten-64-to-32>)
add_compile_options($<$<COMPILE_LANGUAGE:C>:-Wno-deprecated-declarations>)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Foundation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ add_library(Foundation
WinSDK+Extensions.swift)
target_compile_definitions(Foundation PRIVATE
DEPLOYMENT_RUNTIME_SWIFT)
if(Threads_FOUND)
target_compile_definitions(Foundation PRIVATE
SWIFT_CORELIBS_FOUNDATION_HAS_THREADS)
target_compile_options(Foundation PRIVATE
"SHELL:-Xcc -DSWIFT_CORELIBS_FOUNDATION_HAS_THREADS")
endif()

target_compile_options(Foundation PUBLIC
$<$<BOOL:${ENABLE_TESTING}>:-enable-testing>
"SHELL:-Xfrontend -disable-autolink-framework -Xfrontend CoreFoundation"
Expand Down
Loading