Skip to content

Commit 6936781

Browse files
committed
Enable pthread_mutex_t use on WASI with a multithreaded runtime.
This PR opts WASI builds into using `pthread_mutex_t` in `Locked` when the WASI environment supports threading. `_runtime(_multithreaded)` was added very recently with swiftlang/swift#72649, so we need an additional compiler version check before testing the runtime flag. WASI with Swift 5.10, as well as WASI without threading, will continue to stub out `Locked`.
1 parent b4bdb91 commit 6936781

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

Sources/Testing/Support/Locked.swift

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
3636
/// To keep the implementation of this type as simple as possible,
3737
/// `pthread_mutex_t` is used on Apple platforms instead of `os_unfair_lock`
3838
/// or `OSAllocatedUnfairLock`.
39-
#if SWT_TARGET_OS_APPLE || os(Linux)
39+
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.0) && _runtime(_multithreaded))
4040
private typealias _Lock = pthread_mutex_t
4141
#elseif os(Windows)
4242
private typealias _Lock = SRWLOCK
4343
#elseif os(WASI)
44-
// No locks on WASI.
44+
// No locks on WASI without multithreaded runtime.
4545
#else
4646
#warning("Platform-specific implementation missing: locking unavailable")
4747
private typealias _Lock = Void
@@ -51,12 +51,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
5151
private final class _Storage: ManagedBuffer<T, _Lock> {
5252
deinit {
5353
withUnsafeMutablePointerToElements { lock in
54-
#if SWT_TARGET_OS_APPLE || os(Linux)
54+
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.0) && _runtime(_multithreaded))
5555
_ = pthread_mutex_destroy(lock)
5656
#elseif os(Windows)
5757
// No deinitialization needed.
5858
#elseif os(WASI)
59-
// No locks on WASI.
59+
// No locks on WASI without multithreaded runtime.
6060
#else
6161
#warning("Platform-specific implementation missing: locking unavailable")
6262
#endif
@@ -70,12 +70,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
7070
init(rawValue: T) {
7171
let storage = _Storage.create(minimumCapacity: 1, makingHeaderWith: { _ in rawValue })
7272
storage.withUnsafeMutablePointerToElements { lock in
73-
#if SWT_TARGET_OS_APPLE || os(Linux)
73+
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.0) && _runtime(_multithreaded))
7474
_ = pthread_mutex_init(lock, nil)
7575
#elseif os(Windows)
7676
InitializeSRWLock(lock)
7777
#elseif os(WASI)
78-
// No locks on WASI.
78+
// No locks on WASI without multithreaded runtime.
7979
#else
8080
#warning("Platform-specific implementation missing: locking unavailable")
8181
#endif
@@ -101,7 +101,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
101101
/// concurrency tools.
102102
nonmutating func withLock<R>(_ body: (inout T) throws -> R) rethrows -> R {
103103
try _storage.rawValue.withUnsafeMutablePointers { rawValue, lock in
104-
#if SWT_TARGET_OS_APPLE || os(Linux)
104+
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.0) && _runtime(_multithreaded))
105105
_ = pthread_mutex_lock(lock)
106106
defer {
107107
_ = pthread_mutex_unlock(lock)
@@ -112,7 +112,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
112112
ReleaseSRWLockExclusive(lock)
113113
}
114114
#elseif os(WASI)
115-
// No locks on WASI.
115+
// No locks on WASI without multithreaded runtime.
116116
#else
117117
#warning("Platform-specific implementation missing: locking unavailable")
118118
#endif
@@ -150,11 +150,6 @@ extension Locked where T: Numeric {
150150
}
151151

152152
extension Locked {
153-
/// Initialize an instance of this type with a raw value of `0`.
154-
init() where T: AdditiveArithmetic {
155-
self.init(rawValue: .zero)
156-
}
157-
158153
/// Initialize an instance of this type with a raw value of `nil`.
159154
init<V>() where T == V? {
160155
self.init(rawValue: nil)

0 commit comments

Comments
 (0)