Skip to content

Commit 0365398

Browse files
committed
feat: Add support for compiling to wasm when pthreads aren't available
1 parent 6c050d5 commit 0365398

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

.github/workflows/pull_request.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ jobs:
1515
linux_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}, {\"swift_version\": \"5.10\"}]]"
1616
windows_exclude_swift_versions: "[{\"swift_version\": \"5.9\"}]"
1717
enable_wasm_sdk_build: true
18-
wasm_sdk_build_command: swift build -Xcc -D_WASI_EMULATED_PTHREAD
1918

2019
soundness:
2120
name: Soundness

Sources/AsyncAlgorithms/Locking.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ import Musl
1919
import WinSDK
2020
#elseif canImport(Bionic)
2121
import Bionic
22-
#elseif canImport(wasi_pthread)
22+
#elseif canImport(WASILibc)
23+
#if canImport(wasi_pthread)
24+
import WASILibc
2325
import wasi_pthread
2426
#else
27+
// No locking on WASILibc provided by the current Swift for WebAssembly SDK as of Dec 2025.
28+
// That SDK is also single-threaded, so we can safely avoid locking altogether.
29+
#endif // canImport(wasi_pthread)
30+
#else
2531
#error("Unsupported platform")
2632
#endif
2733

@@ -30,6 +36,8 @@ internal struct Lock {
3036
typealias Primitive = os_unfair_lock
3137
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
3238
typealias Primitive = pthread_mutex_t
39+
#elseif canImport(WASILibc)
40+
// This WASILibc variation is single threaded, provides no locks
3341
#elseif canImport(WinSDK)
3442
typealias Primitive = SRWLOCK
3543
#else
@@ -49,6 +57,8 @@ internal struct Lock {
4957
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
5058
let result = pthread_mutex_init(platformLock, nil)
5159
precondition(result == 0, "pthread_mutex_init failed")
60+
#elseif canImport(WASILibc)
61+
// This WASILibc variation is single threaded, provides no locks
5262
#elseif canImport(WinSDK)
5363
InitializeSRWLock(platformLock)
5464
#else
@@ -69,6 +79,9 @@ internal struct Lock {
6979
os_unfair_lock_lock(platformLock)
7080
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
7181
pthread_mutex_lock(platformLock)
82+
#elseif canImport(WASILibc)
83+
// This WASILibc variation is single threaded, provides no locks
84+
return
7285
#elseif canImport(WinSDK)
7386
AcquireSRWLockExclusive(platformLock)
7487
#else
@@ -82,6 +95,9 @@ internal struct Lock {
8295
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
8396
let result = pthread_mutex_unlock(platformLock)
8497
precondition(result == 0, "pthread_mutex_unlock failed")
98+
#elseif canImport(WASILibc)
99+
// This WASILibc variation is single threaded, provides no locks
100+
return
85101
#elseif canImport(WinSDK)
86102
ReleaseSRWLockExclusive(platformLock)
87103
#else

0 commit comments

Comments
 (0)