99//
1010
1111internal import _TestingInternals
12+ #if canImport(Synchronization)
1213private import Synchronization
14+ #endif
1315
1416/// A type that wraps a value requiring access from a synchronous caller during
1517/// concurrent execution.
@@ -24,7 +26,7 @@ private import Synchronization
2426/// This type is not part of the public interface of the testing library.
2527struct Locked < T> {
2628 /// A type providing storage for the underlying lock and wrapped value.
27- #if SWT_TARGET_OS_APPLE && canImport(os)
29+ #if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK
2830 private typealias _Storage = ManagedBuffer < T , os_unfair_lock_s >
2931#elseif !SWT_FIXED_85448 && (os(Linux) || os(Android))
3032 private final class _Storage : ManagedBuffer < T , pthread_mutex_t > {
@@ -34,14 +36,16 @@ struct Locked<T> {
3436 }
3537 }
3638 }
37- #else
39+ #elseif canImport(Synchronization)
3840 private final class _Storage {
3941 let mutex : Mutex < T >
4042
4143 init ( _ rawValue: consuming sending T) {
4244 mutex = Mutex ( rawValue)
4345 }
4446 }
47+ #else
48+ #error("Platform-specific misconfiguration: no mutex or lock type available")
4549#endif
4650
4751 /// Storage for the underlying lock and wrapped value.
@@ -52,7 +56,7 @@ extension Locked: Sendable where T: Sendable {}
5256
5357extension Locked : RawRepresentable {
5458 init ( rawValue: T ) {
55- #if SWT_TARGET_OS_APPLE && canImport(os)
59+ #if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK
5660 _storage = . create( minimumCapacity: 1 , makingHeaderWith: { _ in rawValue } )
5761 _storage. withUnsafeMutablePointerToElements { lock in
5862 lock. initialize ( to: . init( ) )
@@ -62,9 +66,11 @@ extension Locked: RawRepresentable {
6266 _storage. withUnsafeMutablePointerToElements { lock in
6367 _ = pthread_mutex_init ( lock, nil )
6468 }
65- #else
69+ #elseif canImport(Synchronization)
6670 nonisolated ( unsafe) let rawValue = rawValue
6771 _storage = _Storage ( rawValue)
72+ #else
73+ #error("Platform-specific misconfiguration: no mutex or lock type available")
6874#endif
6975 }
7076
@@ -91,7 +97,7 @@ extension Locked {
9197 /// concurrency tools.
9298 func withLock< R> ( _ body: ( inout T ) throws -> sending R) rethrows -> sending R where R: ~ Copyable {
9399 nonisolated ( unsafe) let result : R
94- #if SWT_TARGET_OS_APPLE && canImport(os)
100+ #if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK
95101 result = try _storage. withUnsafeMutablePointers { rawValue, lock in
96102 os_unfair_lock_lock ( lock)
97103 defer {
@@ -107,10 +113,12 @@ extension Locked {
107113 }
108114 return try body ( & rawValue. pointee)
109115 }
110- #else
116+ #elseif canImport(Synchronization)
111117 result = try _storage. mutex. withLock { rawValue in
112118 try body ( & rawValue)
113119 }
120+ #else
121+ #error("Platform-specific misconfiguration: no mutex or lock type available")
114122#endif
115123 return result
116124 }
@@ -130,7 +138,7 @@ extension Locked {
130138 /// concurrency tools.
131139 func withLockIfAvailable< R> ( _ body: ( inout T ) throws -> sending R) rethrows -> sending R? where R: ~ Copyable {
132140 nonisolated ( unsafe ) let result: R?
133- #if SWT_TARGET_OS_APPLE && canImport(os)
141+ #if SWT_TARGET_OS_APPLE && !SWT_NO_OS_UNFAIR_LOCK
134142 result = try _storage. withUnsafeMutablePointers { rawValue, lock in
135143 guard os_unfair_lock_trylock ( lock) else {
136144 return nil
@@ -150,10 +158,12 @@ extension Locked {
150158 }
151159 return try body ( & rawValue. pointee)
152160 }
153- #else
161+ #elseif canImport(Synchronization)
154162 result = try _storage. mutex. withLockIfAvailable { rawValue in
155163 return try body ( & rawValue)
156164 }
165+ #else
166+ #error("Platform-specific misconfiguration: no mutex or lock type available")
157167#endif
158168 return result
159169 }
0 commit comments