Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Update implementation #51

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]
}
}
166 changes: 166 additions & 0 deletions Sources/COpenSwiftUI/MovableLock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
//
// MovableLock.c
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

#include "MovableLock.h"
#include <stdlib.h>
#include <assert.h>

#if !OPENSWIFTUI_TARGET_OS_WASI
#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(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();
}
pthread_mutex_init(&lock->mutex, NULL);
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;
}

void _MovableLockDestory(MovableLock lock) {
pthread_cond_destroy(&lock->cond1);
pthread_cond_destroy(&lock->cond2);
pthread_cond_destroy(&lock->cond3);
pthread_mutex_destroy(&lock->mutex);
free(lock);
}

bool _MovableLockIsOwner(MovableLock lock) {
return pthread_self() == lock->owner;
}

bool _MovableLockIsOuterMostOwner(MovableLock lock) {
return pthread_self() == lock->owner && lock->level == 1;
}

void _MovableLockLock(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
pthread_t owner = pthread_self();
if (owner == lock->owner) {
lock->level += 1;
return;
}
pthread_mutex_lock(&lock->mutex);
while (lock->owner) {
wait_for_lock(lock, owner);
}
lock->owner = owner;
lock->level = 1;
#endif
}

void _MovableLockUnlock(MovableLock lock) {
#if OPENSWIFTUI_TARGET_OS_DARWIN
lock->level -= 1;
if (lock->level != 0) {
return;
}
if (lock->unknown != 0) {
pthread_cond_signal(&lock->cond1);
}
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 {
lock->function = function;
lock->context = context;
if (lock->unknown5) {
pthread_cond_signal_thread_np(&lock->cond1, lock->main);
} else if (!lock->unknown4) {
lock->unknown4 = true;
dispatch_async_f(dispatch_get_main_queue(), lock, &sync_main_callback);
if (lock->unknown5) {
pthread_cond_signal_thread_np(&lock->cond1, lock->main);
}
}
while (lock->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;
lock->owner = NULL;
if (lock->unknown != 0) {
pthread_cond_broadcast(&lock->cond1);
}
pthread_cond_wait(&lock->cond3, &lock->mutex);
while (lock->owner) {
wait_for_lock(lock, owner);
}
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;
if (lock->function) {
uint32_t original_level = lock->level;
pthread_t original_owner = lock->owner;
lock->owner = lock->main;
lock->level = original_level + 1;
lock->function(lock->context);
lock->level = original_level;
lock->owner = original_owner;
lock->function = NULL;
lock->context = NULL;
pthread_cond_signal(&lock->cond2);
}
}
pthread_cond_wait(&lock->cond1, &lock->mutex);
if (lock->main == 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
}
50 changes: 50 additions & 0 deletions Sources/COpenSwiftUI/include/MovableLock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// MovableLock.h
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: Complete

#ifndef MovableLock_h
#define MovableLock_h

#include "OpenSwiftUIBase.h"
#import <pthread.h>

OPENSWIFTUI_ASSUME_NONNULL_BEGIN

typedef struct MovableLock_s {
pthread_mutex_t mutex;
pthread_cond_t cond1;
pthread_cond_t cond2;
pthread_cond_t cond3;
pthread_t main;
pthread_t owner;
uint32_t level;
uint32_t unknown;
void (* _Nullable function)(const void *context);
const void * _Nullable context;
bool unknown4;
bool unknown5;
} MovableLock_t;

typedef MovableLock_t *MovableLock __attribute((swift_newtype(struct)));

OPENSWIFTUI_EXPORT
OPENSWIFTUI_REFINED_FOR_SWIFT
MovableLock _MovableLockCreate(void) OPENSWIFTUI_SWIFT_NAME(MovableLock.create());

OPENSWIFTUI_EXPORT
OPENSWIFTUI_REFINED_FOR_SWIFT
void _MovableLockDestory(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.destory(self:));
bool _MovableLockIsOwner(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.isOwner(self:));
bool _MovableLockIsOuterMostOwner(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.isOuterMostOwner(self:));
void _MovableLockLock(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.lock(self:));
void _MovableLockUnlock(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.unlock(self:));
void _MovableLockSyncMain(MovableLock lock, const void *context, void (*function)(const void *context)) OPENSWIFTUI_SWIFT_NAME(MovableLock.syncMain(self:_:function:));
void _MovableLockWait(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.wait(self:));
void _MovableLockBroadcast(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(MovableLock.broadcast(self:));

OPENSWIFTUI_ASSUME_NONNULL_END

#endif /* MovableLock_h */
44 changes: 44 additions & 0 deletions Sources/OpenSwiftUI/Core/Log/Signpost.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Signpost.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Status: WIP
// ID: 34756F646CF7AC3DBE2A8E0B344C962F

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

struct Signpost {
private let style: Style
private let stability: Stability

// TODO
var isEnabled: Bool {
switch stability {
case .disabled, .verbose, .debug:
return false
case .published:
return true
}
}

static let viewHost = Signpost(style: .kdebug(0), stability: .published)
}

extension Signpost {
private enum Style {
case kdebug(UInt8)
case os_log(StaticString)
}

private enum Stability: Hashable {
case disabled
case verbose
case debug
case published
}
}


83 changes: 83 additions & 0 deletions Sources/OpenSwiftUI/Core/Update/Update.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// Update.swift
// OpenSwiftUI
//
// Status: WIP
// ID: EA173074DA35FA471DC70643259B7E74

internal import COpenSwiftUI
internal import OpenGraphShims

extension MovableLock {
@inline(__always)
func withLock<R>(_ body: () -> R) -> R {
lock()
defer { unlock() }
return body()
}
}

enum Update {
static let trackHost: AnyObject = TraceHost()
private static let lock = MovableLock.create()
private static var depth = 0
private static var actions: [() -> Void] = []

static func begin() {
lock.lock()
depth += 1
if depth == 1 {
guard Signpost.viewHost.isEnabled else {
return
}
// TODO: Signpost
}
}

static func end() {
if depth == 1 {
dispatchActions()
// TODO: Signpost
}
depth -= 1
lock.unlock()
}

static func enqueueAction(_ action: @escaping () -> Void) {
begin()
actions.append(action)
end()
}

static func ensure<Value>(_ body: () -> Value) throws -> Value {
lock.withLock {
if depth == 0 {
begin()
}
defer {
if depth == 0 {
end()
}
}
return body()
}
}

@inlinable
static func dispatchActions() {
// FIXME
for action in actions {
action()
}
}

@inlinable
static func syncMain(_ body: () -> Void) {
// TODO
fatalError("TODO")
}
}

extension Update {
private class TraceHost {}
}
Loading
Loading