Skip to content

Commit d80c8e6

Browse files
authored
Fix non-Darwin build issue (#116)
* Fix non-Darwin build issue * Fix multiple definition of '__OPENSWIFTUI_Lock' issue * Fix wasm CI invalid format issue
1 parent a662bef commit d80c8e6

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

.github/workflows/wasm.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ jobs:
1212
strategy:
1313
fail-fast: false
1414
matrix:
15-
swift_version: ["5.10-RELEASE"]
15+
swift_version: ["5.10.0-RELEASE"]
1616
os: [ubuntu-22.04]
17-
extra_params: []
17+
extra_params: [""]
1818
runs-on: ${{ matrix.os }}
1919
env:
2020
OPENSWIFTUI_WERROR: 1

Sources/COpenSwiftUICore/Other/LockedPointer.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
#include "LockedPointer.h"
88
#include <stdlib.h>
99

10+
#if !__has_include(<os/lock.h>)
11+
void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
12+
while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
13+
sleep(0);
14+
}
15+
}
16+
17+
void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
18+
__sync_synchronize();
19+
*lock = 0;
20+
}
21+
#endif
22+
1023
LockedPointer _LockedPointerCreate(size_t size, size_t alignment) {
1124
// alignment is expected to be a power of 2.
1225
// If alignment > 8,

Sources/COpenSwiftUICore/include/LockedPointer.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,23 @@
2020
#define OPENSWIFTUI_LOCK_T int32_t
2121
#include <stdint.h>
2222
#include <unistd.h>
23-
OPENSWIFTUI_INLINE
24-
void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
25-
while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
26-
sleep(0);
27-
}
28-
}
2923

30-
OPENSWIFTUI_INLINE
31-
void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
32-
__sync_synchronize();
33-
*lock = 0;
34-
}
24+
// FIXME: Revert me after we fix ProtocolDescriptor.m issue / multiple definition of '__OPENSWIFTUI_Lock'
25+
//OPENSWIFTUI_INLINE
26+
//void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
27+
// while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
28+
// sleep(0);
29+
// }
30+
//}
31+
//
32+
//OPENSWIFTUI_INLINE
33+
//void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock) {
34+
// __sync_synchronize();
35+
// *lock = 0;
36+
//}
37+
void __OPENSWIFTUI_Lock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock);
38+
void __OPENSWIFTUI_Unlock(volatile OPENSWIFTUI_LOCK_T * _Nonnull lock);
39+
3540
#define OPENSWIFTUI_LOCK_INIT 0
3641
#define OPENSWIFTUI_LOCK_LOCK(lock) __OPENSWIFTUI_Lock(lock)
3742
#define OPENSWIFTUI_LOCK_UNLOCK(lock) __OPENSWIFTUI_Unlock(lock)

Sources/OpenSwiftUI/View/Graphic/Color/CoreColor+Extension.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
// OpenSwiftUI
44
//
55
// Audited for RELEASE_2024
6-
// Status: WIP
6+
// Status: Complete
77

8+
#if canImport(Darwin)
89
import COpenSwiftUICore
910

1011
extension CoreColor {
@@ -71,3 +72,4 @@ extension CoreColor {
7172
#endif
7273
}
7374

75+
#endif

Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
import Foundation
1010

11+
// FIXME:
12+
// Replace with Swift's Mutex and Atomic to simplify cross-platform maintain cost
13+
// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0433-mutex.md
14+
// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0410-atomics.md
15+
#if canImport(Darwin)
1116
private final class AtomicBuffer<Value>: ManagedBuffer<os_unfair_lock_s, Value> {
1217
static func allocate(value: Value) -> AtomicBuffer<Value> {
1318
let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in
@@ -19,6 +24,19 @@ private final class AtomicBuffer<Value>: ManagedBuffer<os_unfair_lock_s, Value>
1924
return unsafeDowncast(buffer, to: AtomicBuffer<Value>.self)
2025
}
2126
}
27+
#else
28+
private final class AtomicBuffer<Value>: ManagedBuffer<NSLock, Value> {
29+
static func allocate(value: Value) -> AtomicBuffer<Value> {
30+
let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in
31+
NSLock()
32+
}
33+
buffer.withUnsafeMutablePointerToElements { pointer in
34+
pointer.initialize(to: value)
35+
}
36+
return unsafeDowncast(buffer, to: AtomicBuffer<Value>.self)
37+
}
38+
}
39+
#endif
2240

2341
@propertyWrapper
2442
package struct AtomicBox<Value> {
@@ -31,13 +49,23 @@ package struct AtomicBox<Value> {
3149
@inline(__always)
3250
package var wrappedValue: Value {
3351
get {
52+
#if canImport(Darwin)
3453
os_unfair_lock_lock(&buffer.header)
3554
defer { os_unfair_lock_unlock(&buffer.header) }
55+
#else
56+
buffer.header.lock()
57+
defer { buffer.header.unlock() }
58+
#endif
3659
return buffer.withUnsafeMutablePointerToElements { $0.pointee }
3760
}
3861
nonmutating _modify {
62+
#if canImport(Darwin)
3963
os_unfair_lock_lock(&buffer.header)
4064
defer { os_unfair_lock_unlock(&buffer.header) }
65+
#else
66+
buffer.header.lock()
67+
defer { buffer.header.unlock() }
68+
#endif
4169
yield &buffer.withUnsafeMutablePointerToElements { $0 }.pointee
4270
}
4371
}

Tests/OpenSwiftUITests/View/Graphics/Color/ColorResolvedTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// ColorResolvedTests.swift
33
// OpenSwiftUITests
44

5+
#if canImport(Darwin)
6+
57
@testable import OpenSwiftUI
68
import Testing
79

@@ -17,3 +19,4 @@ struct ColorResolvedTests {
1719
#expect(r3.kitColor !== r1.kitColor)
1820
}
1921
}
22+
#endif

0 commit comments

Comments
 (0)