Skip to content

Enable pthread_mutex_t use on WASI with a multithreaded runtime. #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions Sources/Testing/Support/Locked.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
/// To keep the implementation of this type as simple as possible,
/// `pthread_mutex_t` is used on Apple platforms instead of `os_unfair_lock`
/// or `OSAllocatedUnfairLock`.
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
private typealias _Lock = pthread_mutex_t
#elseif os(Windows)
private typealias _Lock = SRWLOCK
#elseif os(WASI)
// No locks on WASI.
// No locks on WASI without multithreaded runtime.
#else
#warning("Platform-specific implementation missing: locking unavailable")
private typealias _Lock = Void
Expand All @@ -51,12 +51,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
private final class _Storage: ManagedBuffer<T, _Lock> {
deinit {
withUnsafeMutablePointerToElements { lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
_ = pthread_mutex_destroy(lock)
#elseif os(Windows)
// No deinitialization needed.
#elseif os(WASI)
// No locks on WASI.
// No locks on WASI without multithreaded runtime.
#else
#warning("Platform-specific implementation missing: locking unavailable")
#endif
Expand All @@ -70,12 +70,12 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
init(rawValue: T) {
let storage = _Storage.create(minimumCapacity: 1, makingHeaderWith: { _ in rawValue })
storage.withUnsafeMutablePointerToElements { lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
_ = pthread_mutex_init(lock, nil)
#elseif os(Windows)
InitializeSRWLock(lock)
#elseif os(WASI)
// No locks on WASI.
// No locks on WASI without multithreaded runtime.
#else
#warning("Platform-specific implementation missing: locking unavailable")
#endif
Expand All @@ -101,7 +101,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
/// concurrency tools.
nonmutating func withLock<R>(_ body: (inout T) throws -> R) rethrows -> R {
try _storage.rawValue.withUnsafeMutablePointers { rawValue, lock in
#if SWT_TARGET_OS_APPLE || os(Linux)
#if SWT_TARGET_OS_APPLE || os(Linux) || (os(WASI) && compiler(>=6.1) && _runtime(_multithreaded))
_ = pthread_mutex_lock(lock)
defer {
_ = pthread_mutex_unlock(lock)
Expand All @@ -112,7 +112,7 @@ struct Locked<T>: RawRepresentable, Sendable where T: Sendable {
ReleaseSRWLockExclusive(lock)
}
#elseif os(WASI)
// No locks on WASI.
// No locks on WASI without multithreaded runtime.
#else
#warning("Platform-specific implementation missing: locking unavailable")
#endif
Expand Down Expand Up @@ -150,11 +150,6 @@ extension Locked where T: Numeric {
}

extension Locked {
/// Initialize an instance of this type with a raw value of `0`.
init() where T: AdditiveArithmetic {
self.init(rawValue: .zero)
}

/// Initialize an instance of this type with a raw value of `nil`.
init<V>() where T == V? {
self.init(rawValue: nil)
Expand Down