@_implementationOnly import os // Backports the Swift interface around os_unfair_lock_t available in recent Darwin platforms // @available(iOS, deprecated: 16.0, message: "use OSAllocatedUnfairLock directly") @available(tvOS, deprecated: 16.0, message: "use OSAllocatedUnfairLock directly") @available(watchOS, deprecated: 9, message: "use OSAllocatedUnfairLock directly") @available(macOS, deprecated: 13.0, message: "use OSAllocatedUnfairLock directly") struct AllocatedUnfairLock { private let storage: Storage init(initialState: State) { self.storage = Storage(initialState: initialState) } func withLock(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable { os_unfair_lock_lock(storage.lock) defer { os_unfair_lock_unlock(storage.lock) } return try body(&storage.state) } private final class Storage { let lock: os_unfair_lock_t var state: State init(initialState: State) { self.lock = .allocate(capacity: 1) self.lock.initialize(to: os_unfair_lock()) self.state = initialState } deinit { self.lock.deinitialize(count: 1) self.lock.deallocate() } } } extension AllocatedUnfairLock where State == Void { init() { self.storage = .init(initialState: ()) } func lock() { os_unfair_lock_lock(storage.lock) } func unlock() { os_unfair_lock_unlock(storage.lock) } func withLock(_ body: @Sendable () throws -> R) rethrows -> R where R: Sendable { os_unfair_lock_lock(storage.lock) defer { os_unfair_lock_unlock(storage.lock) } return try body() } }