Skip to content

Commit 6ac957c

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

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import WinSDK
2121
import Bionic
2222
#elseif canImport(wasi_pthread)
2323
import wasi_pthread
24+
#elseif canImport(WASILibc)
25+
// No locking on WASILibc provided by the current Swift for WebAssembly SDK as of Dec 2025.
26+
// That SDK is also single-threaded, so we can safely avoid locking altogether.
2427
#else
2528
#error("Unsupported platform")
2629
#endif
@@ -30,6 +33,8 @@ internal struct Lock {
3033
typealias Primitive = os_unfair_lock
3134
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
3235
typealias Primitive = pthread_mutex_t
36+
#elseif canImport(WASILibc)
37+
// This WASILibc variation is single threaded, provides no locks
3338
#elseif canImport(WinSDK)
3439
typealias Primitive = SRWLOCK
3540
#else
@@ -49,6 +54,8 @@ internal struct Lock {
4954
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
5055
let result = pthread_mutex_init(platformLock, nil)
5156
precondition(result == 0, "pthread_mutex_init failed")
57+
#elseif canImport(WASILibc)
58+
// This WASILibc variation is single threaded, provides no locks
5259
#elseif canImport(WinSDK)
5360
InitializeSRWLock(platformLock)
5461
#else
@@ -69,6 +76,9 @@ internal struct Lock {
6976
os_unfair_lock_lock(platformLock)
7077
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
7178
pthread_mutex_lock(platformLock)
79+
#elseif canImport(WASILibc)
80+
// This WASILibc variation is single threaded, provides no locks
81+
return
7282
#elseif canImport(WinSDK)
7383
AcquireSRWLockExclusive(platformLock)
7484
#else
@@ -82,6 +92,9 @@ internal struct Lock {
8292
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic) || canImport(wasi_pthread)
8393
let result = pthread_mutex_unlock(platformLock)
8494
precondition(result == 0, "pthread_mutex_unlock failed")
95+
#elseif canImport(WASILibc)
96+
// This WASILibc variation is single threaded, provides no locks
97+
return
8598
#elseif canImport(WinSDK)
8699
ReleaseSRWLockExclusive(platformLock)
87100
#else

0 commit comments

Comments
 (0)