From 17eb8178a644102a95aafd89a51e6171f2e5b769 Mon Sep 17 00:00:00 2001 From: aelam Date: Wed, 17 Sep 2025 20:28:53 +0900 Subject: [PATCH] clean up CrossPlatformLock --- Sources/Support/CrossPlatformLock.swift | 30 ++++++++++++++----------- 1 file changed, 17 insertions(+), 13 deletions(-) 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