Skip to content

Commit 143914a

Browse files
committed
Use os_unfair_lock on Apple platforms
1 parent 2adcd72 commit 143914a

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

stdlib/public/Distributed/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ add_swift_target_library(swift_Distributed ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} I
2121
DistributedMetadata.swift
2222
LocalTestingDistributedActorSystem.swift
2323

24+
SWIFT_MODULE_DEPENDS_IOS Darwin
25+
SWIFT_MODULE_DEPENDS_OSX Darwin
26+
SWIFT_MODULE_DEPENDS_TVOS Darwin
27+
SWIFT_MODULE_DEPENDS_WATCHOS Darwin
2428
SWIFT_MODULE_DEPENDS_LINUX Glibc
2529
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
2630
SWIFT_MODULE_DEPENDS_OPENBSD Glibc

stdlib/public/Distributed/LocalTestingDistributedActorSystem.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
import Swift
1414

15-
#if canImport(Glibc)
15+
#if canImport(Darwin)
16+
import Darwin
17+
#elseif canImport(Glibc)
1618
import Glibc
1719
#elseif os(Windows)
1820
import WinSDK
@@ -196,19 +198,23 @@ public struct LocalTestingDistributedActorSystemError: DistributedActorSystemErr
196198
// === lock ----------------------------------------------------------------
197199

198200
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)
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

Comments
 (0)