diff --git a/Sources/Support/CrossPlatformLock.swift b/Sources/Support/CrossPlatformLock.swift index 8c890c1..abf2c02 100644 --- a/Sources/Support/CrossPlatformLock.swift +++ b/Sources/Support/CrossPlatformLock.swift @@ -5,34 +5,38 @@ // import Foundation -#if canImport(os) +#if canImport(os) && os(macOS) import os -#endif /// A cross-platform thread-safe lock wrapper public final class CrossPlatformLock: @unchecked Sendable { - #if canImport(os) && os(macOS) private let lock: OSAllocatedUnfairLock - #else + + public init(initialState: State) { + self.lock = OSAllocatedUnfairLock(initialState: initialState) + } + + public func withLock(_ operation: @Sendable (inout State) -> T) -> T { + lock.withLock(operation) + } +} + +#else + +/// A cross-platform thread-safe lock wrapper +public final class CrossPlatformLock: @unchecked Sendable { private let nsLock = NSLock() private var state: State - #endif public init(initialState: State) { - #if canImport(os) && os(macOS) - self.lock = OSAllocatedUnfairLock(initialState: initialState) - #else self.state = initialState - #endif } public func withLock(_ operation: @Sendable (inout State) -> T) -> T { - #if canImport(os) && os(macOS) - return lock.withLock(operation) - #else nsLock.lock() defer { nsLock.unlock() } return operation(&state) - #endif } } + +#endif