From 4a65e15fbcd6bd95df1f1c598eee2c0043c859fc Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 04:22:56 +0800 Subject: [PATCH] Fix non-Darwin build issue --- .../Graphic/Color/CoreColor+Extension.swift | 4 ++- .../Data/Other/AtomicBox.swift | 28 +++++++++++++++++++ .../Graphics/Color/ColorResolvedTests.swift | 3 ++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift b/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift index 68d9b63e..92f79d19 100644 --- a/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift +++ b/Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift @@ -3,8 +3,9 @@ // OpenSwiftUI // // Audited for RELEASE_2024 -// Status: WIP +// Status: Complete +#if canImport(Darwin) import COpenSwiftUICore extension CoreColor { @@ -71,3 +72,4 @@ extension CoreColor { #endif } +#endif diff --git a/Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift b/Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift index 859f7e98..911c009c 100644 --- a/Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift +++ b/Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift @@ -8,6 +8,11 @@ import Foundation +// FIXME: +// Replace with Swift's Mutex and Atomic to simplify cross-platform maintain cost +// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0433-mutex.md +// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0410-atomics.md +#if canImport(Darwin) private final class AtomicBuffer: ManagedBuffer { static func allocate(value: Value) -> AtomicBuffer { let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in @@ -19,6 +24,19 @@ private final class AtomicBuffer: ManagedBuffer return unsafeDowncast(buffer, to: AtomicBuffer.self) } } +#else +private final class AtomicBuffer: ManagedBuffer { + static func allocate(value: Value) -> AtomicBuffer { + let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in + NSLock() + } + buffer.withUnsafeMutablePointerToElements { pointer in + pointer.initialize(to: value) + } + return unsafeDowncast(buffer, to: AtomicBuffer.self) + } +} +#endif @propertyWrapper package struct AtomicBox { @@ -31,13 +49,23 @@ package struct AtomicBox { @inline(__always) package var wrappedValue: Value { get { + #if canImport(Darwin) os_unfair_lock_lock(&buffer.header) defer { os_unfair_lock_unlock(&buffer.header) } + #else + buffer.header.lock() + defer { buffer.header.unlock() } + #endif return buffer.withUnsafeMutablePointerToElements { $0.pointee } } nonmutating _modify { + #if canImport(Darwin) os_unfair_lock_lock(&buffer.header) defer { os_unfair_lock_unlock(&buffer.header) } + #else + buffer.header.lock() + defer { buffer.header.unlock() } + #endif yield &buffer.withUnsafeMutablePointerToElements { $0 }.pointee } } diff --git a/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift b/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift index f29ad47e..67410bd8 100644 --- a/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift +++ b/Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift @@ -2,6 +2,8 @@ // ColorResolvedTests.swift // OpenSwiftUITests +#if canImport(Darwin) + @testable import OpenSwiftUI import Testing @@ -17,3 +19,4 @@ struct ColorResolvedTests { #expect(r3.kitColor !== r1.kitColor) } } +#endif