From 4a65e15fbcd6bd95df1f1c598eee2c0043c859fc Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 04:22:56 +0800 Subject: [PATCH 1/3] 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 68d9b63..92f79d1 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 859f7e9..911c009 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 f29ad47..67410bd 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 From 339f4d61edeb2aba7dd6fddb618ba75f71ee693b Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 13:15:34 +0800 Subject: [PATCH 2/3] Fix multiple definition of '__OPENSWIFTUI_Lock' issue --- .../COpenSwiftUICore/Other/LockedPointer.c | 13 +++++++++ .../COpenSwiftUICore/include/LockedPointer.h | 27 +++++++++++-------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Sources/COpenSwiftUICore/Other/LockedPointer.c b/Sources/COpenSwiftUICore/Other/LockedPointer.c index 728e246..ee117c4 100644 --- a/Sources/COpenSwiftUICore/Other/LockedPointer.c +++ b/Sources/COpenSwiftUICore/Other/LockedPointer.c @@ -7,6 +7,19 @@ #include "LockedPointer.h" #include +#if !__has_include() +void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { + while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) { + sleep(0); + } +} + +void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { + __sync_synchronize(); + *lock = 0; +} +#endif + LockedPointer _LockedPointerCreate(size_t size, size_t alignment) { // alignment is expected to be a power of 2. // If alignment > 8, diff --git a/Sources/COpenSwiftUICore/include/LockedPointer.h b/Sources/COpenSwiftUICore/include/LockedPointer.h index caf4fef..a100cbd 100644 --- a/Sources/COpenSwiftUICore/include/LockedPointer.h +++ b/Sources/COpenSwiftUICore/include/LockedPointer.h @@ -20,18 +20,23 @@ #define OPENSWIFTUI_LOCK_T int32_t #include #include -OPENSWIFTUI_INLINE -void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { - while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) { - sleep(0); - } -} -OPENSWIFTUI_INLINE -void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { - __sync_synchronize(); - *lock = 0; -} +// FIXME: Revert me after we fix ProtocolDescriptor.m issue / multiple definition of '__OPENSWIFTUI_Lock' +//OPENSWIFTUI_INLINE +//void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { +// while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) { +// sleep(0); +// } +//} +// +//OPENSWIFTUI_INLINE +//void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) { +// __sync_synchronize(); +// *lock = 0; +//} +void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock); +void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock); + #define OPENSWIFTUI_LOCK_INIT 0 #define OPENSWIFTUI_LOCK_LOCK(lock) __OPENSWIFTUI_Lock(lock) #define OPENSWIFTUI_LOCK_UNLOCK(lock) __OPENSWIFTUI_Unlock(lock) From afad32f62c9dd4f483dc26406c0e4ffd9801c131 Mon Sep 17 00:00:00 2001 From: Kyle Date: Tue, 17 Sep 2024 13:21:27 +0800 Subject: [PATCH 3/3] Fix wasm CI invalid format issue --- .github/workflows/wasm.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/wasm.yml b/.github/workflows/wasm.yml index adaebdc..a2afad6 100644 --- a/.github/workflows/wasm.yml +++ b/.github/workflows/wasm.yml @@ -12,9 +12,9 @@ jobs: strategy: fail-fast: false matrix: - swift_version: ["5.10-RELEASE"] + swift_version: ["5.10.0-RELEASE"] os: [ubuntu-22.04] - extra_params: [] + extra_params: [""] runs-on: ${{ matrix.os }} env: OPENSWIFTUI_WERROR: 1