Skip to content

Commit 03c0884

Browse files
kateinoigakukunMaxDesiatov
authored andcommitted
Port LockedState and _ThreadLocal to WASI without any locking (#780)
(cherry picked from commit aa68eeb)
1 parent 779e11a commit 03c0884

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

Sources/FoundationEssentials/LockedState.swift

+9
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ package struct LockedState<State> {
3333
typealias Primitive = pthread_mutex_t
3434
#elseif canImport(WinSDK)
3535
typealias Primitive = SRWLOCK
36+
#elseif os(WASI)
37+
// WASI is single-threaded, so we don't need a lock.
38+
typealias Primitive = Void
3639
#endif
3740

3841
typealias PlatformLock = UnsafeMutablePointer<Primitive>
@@ -45,6 +48,8 @@ package struct LockedState<State> {
4548
pthread_mutex_init(platformLock, nil)
4649
#elseif canImport(WinSDK)
4750
InitializeSRWLock(platformLock)
51+
#elseif os(WASI)
52+
// no-op
4853
#endif
4954
}
5055

@@ -62,6 +67,8 @@ package struct LockedState<State> {
6267
pthread_mutex_lock(platformLock)
6368
#elseif canImport(WinSDK)
6469
AcquireSRWLockExclusive(platformLock)
70+
#elseif os(WASI)
71+
// no-op
6572
#endif
6673
}
6774

@@ -72,6 +79,8 @@ package struct LockedState<State> {
7279
pthread_mutex_unlock(platformLock)
7380
#elseif canImport(WinSDK)
7481
ReleaseSRWLockExclusive(platformLock)
82+
#elseif os(WASI)
83+
// no-op
7584
#endif
7685
}
7786
}

Sources/FoundationEssentials/_ThreadLocal.swift

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct _ThreadLocal {
3030
fileprivate typealias PlatformKey = tss_t
3131
#elseif canImport(WinSDK)
3232
fileprivate typealias PlatformKey = DWORD
33+
#elseif os(WASI)
34+
fileprivate typealias PlatformKey = UnsafeMutablePointer<UnsafeMutableRawPointer?>
3335
#endif
3436

3537
struct Key<Value> {
@@ -46,6 +48,8 @@ struct _ThreadLocal {
4648
self.key = key
4749
#elseif canImport(WinSDK)
4850
key = FlsAlloc(nil)
51+
#elseif os(WASI)
52+
key = UnsafeMutablePointer<UnsafeMutableRawPointer?>.allocate(capacity: 1)
4953
#endif
5054
}
5155
}
@@ -58,6 +62,8 @@ struct _ThreadLocal {
5862
tss_get(key)
5963
#elseif canImport(WinSDK)
6064
FlsGetValue(key)
65+
#elseif os(WASI)
66+
key.pointee
6167
#endif
6268
}
6369

@@ -68,6 +74,8 @@ struct _ThreadLocal {
6874
tss_set(key, newValue)
6975
#elseif canImport(WinSDK)
7076
FlsSetValue(key, newValue)
77+
#elseif os(WASI)
78+
key.pointee = newValue
7179
#endif
7280
}
7381
}

0 commit comments

Comments
 (0)