Skip to content

Commit

Permalink
Fix build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Mar 16, 2024
1 parent 263a51b commit 0b5881f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ let openSwiftUICompatibilityTestTarget = Target.testTarget(
swiftSettings: sharedSwiftSettings
)

let swiftBinPath = Context.environment["_"] ?? ""
let swiftBinURL = URL(fileURLWithPath: swiftBinPath)
let SDKPath = swiftBinURL.deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().path
let includePath = SDKPath.appending("/usr/lib/swift_static")

let package = Package(
name: "OpenSwiftUI",
platforms: [
Expand All @@ -87,7 +92,11 @@ let package = Package(
]
),
.target(
name: "COpenSwiftUI"
name: "COpenSwiftUI",
cSettings: [
.unsafeFlags(["-I", includePath], .when(platforms: .nonDarwinPlatforms)),
.define("__COREFOUNDATION_FORSWIFTFOUNDATIONONLY__", to: "1", .when(platforms: .nonDarwinPlatforms)),
]
),
.binaryTarget(name: "CoreServices", path: "PrivateFrameworks/CoreServices.xcframework"),
openSwiftUITarget,
Expand Down Expand Up @@ -196,3 +205,9 @@ if useLocalDeps {
.package(url: "https://github.com/OpenSwiftUIProject/OpenGraph", branch: "main"),
]
}

extension [Platform] {
static var nonDarwinPlatforms: [Platform] {
[.linux, .android, .wasi, .openbsd, .windows]
}
}
22 changes: 21 additions & 1 deletion Sources/COpenSwiftUI/MovableLock.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
#include <dispatch/dispatch.h>
#endif

#if OPENSWIFTUI_TARGET_OS_DARWIN
extern pthread_t pthread_main_thread_np(void);
#endif

void wait_for_lock(MovableLock lock, pthread_t thread);
void sync_main_callback();
void sync_main_callback(MovableLock lock);

MovableLock _MovableLockCreate() {
#if OPENSWIFTUI_TARGET_OS_DARWIN
static_assert(sizeof(MovableLock_t) == 0x100, "MovebleLock size is not 0x100 bytes.");
#endif
MovableLock lock = calloc(1, sizeof(MovableLock_t));
if (!lock) {
abort();
Expand All @@ -28,7 +32,9 @@ MovableLock _MovableLockCreate() {
pthread_cond_init(&lock->cond1, NULL);
pthread_cond_init(&lock->cond2, NULL);
pthread_cond_init(&lock->cond3, NULL);
#if OPENSWIFTUI_TARGET_OS_DARWIN
lock->main = pthread_main_thread_np();
#endif
return lock;
}

Expand All @@ -49,6 +55,7 @@ bool _MovableLockIsOuterMostOwner(MovableLock lock) {
}

void _MovableLockLock(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
pthread_t owner = pthread_self();
if (owner == lock->owner) {
lock->level += 1;
Expand All @@ -60,9 +67,11 @@ void _MovableLockLock(MovableLock lock) {
}
lock->owner = owner;
lock->level = 1;
#endif
}

void _MovableLockUnlock(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
lock->level -= 1;
if (lock->level != 0) {
return;
Expand All @@ -72,9 +81,11 @@ void _MovableLockUnlock(MovableLock lock) {
}
lock->owner = NULL;
pthread_mutex_unlock(&lock->mutex);
#endif
}

void _MovableLockSyncMain(MovableLock lock, const void *context, void (*function)(const void *context)) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
if (pthread_self() == lock->main) {
function(context);
} else {
Expand All @@ -93,9 +104,11 @@ void _MovableLockSyncMain(MovableLock lock, const void *context, void (*function
pthread_cond_wait(&lock->cond2, &lock->mutex);
}
}
#endif
}

void _MovableLockWait(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
pthread_t owner = pthread_self();
uint32_t level = lock->level;
lock->level = 0;
Expand All @@ -109,13 +122,17 @@ void _MovableLockWait(MovableLock lock) {
}
lock->owner = owner;
lock->level = level;
#endif
}

void _MovableLockBroadcast(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
pthread_cond_broadcast(&lock->cond3);
#endif
}

void wait_for_lock(MovableLock lock, pthread_t owner) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
lock->unknown += 1;
if (lock->main == owner) {
lock->unknown5 = 1;
Expand All @@ -137,10 +154,13 @@ void wait_for_lock(MovableLock lock, pthread_t owner) {
lock->unknown5 = 0;
}
lock->unknown -= 1;
#endif
}

void sync_main_callback(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
_MovableLockLock(lock);
lock->unknown4 = 0;
_MovableLockUnlock(lock);
#endif
}
2 changes: 2 additions & 0 deletions Sources/OpenSwiftUI/Core/Log/Signpost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// Status: WIP
// ID: 34756F646CF7AC3DBE2A8E0B344C962F

#if canImport(os)
import os.signpost
#endif

struct Signpost {
private let style: Style
Expand Down
2 changes: 2 additions & 0 deletions Tests/OpenSwiftUITests/COpenSwiftUI/MovableLockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import COpenSwiftUI
import Testing

#if canImport(Darwin)
final class MovableLockTests {
let lock: MovableLock

Expand All @@ -31,3 +32,4 @@ final class MovableLockTests {
#expect(lock.isOuterMostOwner() == false)
}
}
#endif

0 comments on commit 0b5881f

Please sign in to comment.