Skip to content

Commit

Permalink
Fix non-Darwin build issue (#116)
Browse files Browse the repository at this point in the history
* Fix non-Darwin build issue

* Fix multiple definition of '__OPENSWIFTUI_Lock' issue

* Fix wasm CI invalid format issue
  • Loading branch information
Kyle-Ye authored Sep 17, 2024
1 parent a662bef commit d80c8e6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions Sources/COpenSwiftUICore/Other/LockedPointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
#include "LockedPointer.h"
#include <stdlib.h>

#if !__has_include(<os/lock.h>)
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,
Expand Down
27 changes: 16 additions & 11 deletions Sources/COpenSwiftUICore/include/LockedPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
#define OPENSWIFTUI_LOCK_T int32_t
#include <stdint.h>
#include <unistd.h>
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// OpenSwiftUI
//
// Audited for RELEASE_2024
// Status: WIP
// Status: Complete

#if canImport(Darwin)
import COpenSwiftUICore

extension CoreColor {
Expand Down Expand Up @@ -71,3 +72,4 @@ extension CoreColor {
#endif
}

#endif
28 changes: 28 additions & 0 deletions Sources/OpenSwiftUICore/Data/Other/AtomicBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>: ManagedBuffer<os_unfair_lock_s, Value> {
static func allocate(value: Value) -> AtomicBuffer<Value> {
let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in
Expand All @@ -19,6 +24,19 @@ private final class AtomicBuffer<Value>: ManagedBuffer<os_unfair_lock_s, Value>
return unsafeDowncast(buffer, to: AtomicBuffer<Value>.self)
}
}
#else
private final class AtomicBuffer<Value>: ManagedBuffer<NSLock, Value> {
static func allocate(value: Value) -> AtomicBuffer<Value> {
let buffer = AtomicBuffer.create(minimumCapacity: 1) { buffer in
NSLock()
}
buffer.withUnsafeMutablePointerToElements { pointer in
pointer.initialize(to: value)
}
return unsafeDowncast(buffer, to: AtomicBuffer<Value>.self)
}
}
#endif

@propertyWrapper
package struct AtomicBox<Value> {
Expand All @@ -31,13 +49,23 @@ package struct AtomicBox<Value> {
@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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// ColorResolvedTests.swift
// OpenSwiftUITests

#if canImport(Darwin)

@testable import OpenSwiftUI
import Testing

Expand All @@ -17,3 +19,4 @@ struct ColorResolvedTests {
#expect(r3.kitColor !== r1.kitColor)
}
}
#endif

0 comments on commit d80c8e6

Please sign in to comment.