feat: Add support for compiling to wasm when pthreads aren't available (#389)

This commit is contained in:
scottmarchant authored and GitHub committed 2026-01-12 10:37:45 -08:00
1 parent 6c050d5ef8
commit d98a48b8c2
2 files changed
+13 -1

No files matched your search

-1
View File
@@ -15,7 +15,6 @@ jobs:
linux_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}, {\"swift_version\": \"5.10\"}]]"
windows_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}]"
enable_wasm_sdk_build: true
wasm_sdk_build_command: swift build -Xcc -D_WASI_EMULATED_PTHREAD
soundness:
name: Soundness
+13
View File
@@ -21,6 +21,9 @@ import WinSDK
import Bionic
#elseif canImport(wasi_pthread)
import wasi_pthread
#elseif canImport(WASILibc)
// No locking on WASILibc provided by the current Swift for WebAssembly SDK as of Dec 2025.
// That SDK is also single-threaded, so we can safely avoid locking altogether.
#else
#error("Unsupported platform")
#endif
@@ -30,6 +33,8 @@ internal struct Lock {
typealias Primitive = os_unfair_lock
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
typealias Primitive = pthread_mutex_t
#elseif canImport(WASILibc)
// This WASILibc variation is single threaded, provides no locks
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
#else
@@ -49,6 +54,8 @@ internal struct Lock {
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
let result = pthread_mutex_init(platformLock, nil)
precondition(result == 0, "pthread_mutex_init failed")
#elseif canImport(WASILibc)
// This WASILibc variation is single threaded, provides no locks
#elseif canImport(WinSDK)
InitializeSRWLock(platformLock)
#else
@@ -69,6 +76,9 @@ internal struct Lock {
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
pthread_mutex_lock(platformLock)
#elseif canImport(WASILibc)
// This WASILibc variation is single threaded, provides no locks
return
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
#else
@@ -82,6 +92,9 @@ internal struct Lock {
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
let result = pthread_mutex_unlock(platformLock)
precondition(result == 0, "pthread_mutex_unlock failed")
#elseif canImport(WASILibc)
// This WASILibc variation is single threaded, provides no locks
return
#elseif canImport(WinSDK)
ReleaseSRWLockExclusive(platformLock)
#else