12
12
13
13
import Swift
14
14
15
- #if canImport(Glibc)
15
+ #if canImport(Darwin)
16
+ import Darwin
17
+ #elseif canImport(Glibc)
16
18
import Glibc
17
19
#elseif os(Windows)
18
20
import WinSDK
@@ -196,19 +198,23 @@ public struct LocalTestingDistributedActorSystemError: DistributedActorSystemErr
196
198
// === lock ----------------------------------------------------------------
197
199
198
200
fileprivate 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)
200
204
private let underlying : UnsafeMutablePointer < SRWLOCK >
201
- #elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
202
- private let underlying : UnsafeMutablePointer < pthread_mutex_t ? >
203
205
#elseif os(WASI)
204
206
// pthread is currently not available on WASI
207
+ #elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
208
+ private let underlying : UnsafeMutablePointer < pthread_mutex_t ? >
205
209
#else
206
210
private let underlying : UnsafeMutablePointer < pthread_mutex_t >
207
211
#endif
208
212
209
213
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
212
218
#elseif os(WASI)
213
219
// WASI environment has only a single thread
214
220
#else
@@ -224,7 +230,9 @@ fileprivate class _Lock {
224
230
}
225
231
226
232
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)
228
236
self . underlying = UnsafeMutablePointer . allocate ( capacity: 1 )
229
237
InitializeSRWLock ( self . underlying)
230
238
#elseif os(WASI)
@@ -239,7 +247,9 @@ fileprivate class _Lock {
239
247
240
248
@discardableResult
241
249
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)
243
253
AcquireSRWLockExclusive ( self . underlying)
244
254
#elseif os(WASI)
245
255
// WASI environment has only a single thread
@@ -250,7 +260,9 @@ fileprivate class _Lock {
250
260
#endif
251
261
252
262
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)
254
266
ReleaseSRWLockExclusive ( self . underlying)
255
267
#elseif os(WASI)
256
268
// WASI environment has only a single thread
0 commit comments