1212
1313import Swift
1414
15- #if canImport(Glibc)
15+ #if canImport(Darwin)
16+ import Darwin
17+ #elseif canImport(Glibc)
1618import Glibc
1719#elseif os(Windows)
1820import WinSDK
@@ -196,19 +198,23 @@ public struct LocalTestingDistributedActorSystemError: DistributedActorSystemErr
196198// === lock ----------------------------------------------------------------
197199
198200fileprivate class _Lock {
199- #if os(Windows)
201+ #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
202+ private let underlying : UnsafeMutablePointer < os_unfair_lock >
203+ #elseif os(Windows)
200204 private let underlying : UnsafeMutablePointer < SRWLOCK >
201- #elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
202- private let underlying : UnsafeMutablePointer < pthread_mutex_t ? >
203205 #elseif os(WASI)
204206 // pthread is currently not available on WASI
207+ #elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
208+ private let underlying : UnsafeMutablePointer < pthread_mutex_t ? >
205209 #else
206210 private let underlying : UnsafeMutablePointer < pthread_mutex_t >
207211 #endif
208212
209213 deinit {
210- #if os(Windows)
211- // Mutexes do not need to be explicitly destroyed
214+ #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
215+ // `os_unfair_lock`s do not need to be explicitly destroyed
216+ #elseif os(Windows)
217+ // `SRWLOCK`s do not need to be explicitly destroyed
212218 #elseif os(WASI)
213219 // WASI environment has only a single thread
214220 #else
@@ -224,7 +230,9 @@ fileprivate class _Lock {
224230 }
225231
226232 init ( ) {
227- #if os(Windows)
233+ #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
234+ self . underlying = UnsafeMutablePointer . allocate ( capacity: 1 )
235+ #elseif os(Windows)
228236 self . underlying = UnsafeMutablePointer . allocate ( capacity: 1 )
229237 InitializeSRWLock ( self . underlying)
230238 #elseif os(WASI)
@@ -239,7 +247,9 @@ fileprivate class _Lock {
239247
240248 @discardableResult
241249 func withLock< T> ( _ body: ( ) -> T ) -> T {
242- #if os(Windows)
250+ #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
251+ os_unfair_lock_lock ( self . underlying)
252+ #elseif os(Windows)
243253 AcquireSRWLockExclusive ( self . underlying)
244254 #elseif os(WASI)
245255 // WASI environment has only a single thread
@@ -250,7 +260,9 @@ fileprivate class _Lock {
250260 #endif
251261
252262 defer {
253- #if os(Windows)
263+ #if os(iOS) || os(macOS) || os(tvOS) || os(watchOS)
264+ os_unfair_lock_unlock ( self . underlying)
265+ #elseif os(Windows)
254266 ReleaseSRWLockExclusive ( self . underlying)
255267 #elseif os(WASI)
256268 // WASI environment has only a single thread
0 commit comments