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

[Feature] Update Update API #124

Merged
merged 3 commits into from
Sep 24, 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
4 changes: 2 additions & 2 deletions Sources/COpenSwiftUICore/include/MovableLock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ 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:));
bool _MovableLockIsOwner(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(getter:MovableLock.isOwner(self:));
bool _MovableLockIsOuterMostOwner(MovableLock lock) OPENSWIFTUI_SWIFT_NAME(getter: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:));
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Core/Render/DisplayLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class DisplayLink: NSObject {
}

func invalidate() {
Update.lock.withLock {
Update.locked {
// TODO
}
}
Expand Down
126 changes: 0 additions & 126 deletions Sources/OpenSwiftUI/Core/Update/Update.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

extension ViewRendererHost {
func updateViewGraph<Value>(body: (ViewGraph) -> Value) -> Value {
Update.perform {
Update.dispatchImmediately {

Check warning on line 24 in Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift#L24

Added line #L24 was not covered by tests
OGGraph.withoutUpdate {
updateGraph()
return body(viewGraph)
Expand All @@ -37,7 +37,7 @@
}

func invalidateProperties(_ properties: ViewRendererHostProperties, mayDeferUpdate: Bool) {
Update.lock.withLock {
Update.locked {

Check warning on line 40 in Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift#L40

Added line #L40 was not covered by tests
guard !propertiesNeedingUpdate.contains(properties) else {
return
}
Expand All @@ -56,7 +56,7 @@
}

func render(interval: Double, updateDisplayList: Bool = true) {
Update.perform {
Update.dispatchImmediately {

Check warning on line 59 in Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUI/Core/View/ViewRendererHost.swift#L59

Added line #L59 was not covered by tests
guard !isRendering else {
return
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUI/Integration/UIKit/UIHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ open class _UIHostingView<Content>: UIView where Content: View {
guard canAdvanceTimeAutomatically else {
return
}
Update.lock.withLock {
Update.locked {
cancelAsyncRendering()
let interval: Double
if let displayLink, displayLink.willRender {
Expand Down Expand Up @@ -129,7 +129,7 @@ open class _UIHostingView<Content>: UIView where Content: View {
}

func cancelAsyncRendering() {
Update.lock.withLock {
Update.locked {
displayLink?.cancelAsyncRendering()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/View/Toggle/Switch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private class PlatformSwitchCoordinator: PlatformViewCoordinator {

@objc
func isOnChanged(_ sender: UISwitch) {
Update.perform {
Update.dispatchImmediately {
_isOn.wrappedValue = sender.isOn
}
sender.setOn(_isOn.wrappedValue, animated: true)
Expand Down
179 changes: 179 additions & 0 deletions Sources/OpenSwiftUICore/Data/Update.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
//
// Update.swift
// OpenSwiftUI
//
// Audited for RELEASE_2024
// Status: Blocked by Signpost
// ID: EA173074DA35FA471DC70643259B7E74 (RELEASE_2021)
// ID: 61534957AEEC2EDC447ABDC13B4D426F (RELEASE_2024)

internal import COpenSwiftUICore
internal import OpenGraphShims
import Foundation

package enum Update {
private final class TraceHost {}
static let trackHost: AnyObject = TraceHost()

private static var depth = 0
private static var dispatchDepth = 0
private static let _lock = MovableLock.create()
private static var actions: [() -> Void] = []
private static let lockAssertionsAreEnabled = EnvironmentHelper.bool(for: "OPENSWIFTUI_ASSERT_LOCKS")

@inlinable
package static var isActive: Bool {
depth != 0

Check warning on line 26 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L25-L26

Added lines #L25 - L26 were not covered by tests
}

@inlinable
package static var threadIsUpdating: Bool {
depth < dispatchDepth ? isOwner : false

Check warning on line 31 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L30-L31

Added lines #L30 - L31 were not covered by tests
}

@inlinable
package static func assertIsActive() {
assert(isActive)

Check warning on line 36 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L35-L36

Added lines #L35 - L36 were not covered by tests
}

package static func lock() {
_lock.lock()

Check warning on line 40 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L39-L40

Added lines #L39 - L40 were not covered by tests
}
package static func unlock() {
_lock.unlock()

Check warning on line 43 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L42-L43

Added lines #L42 - L43 were not covered by tests
}

package static var isOwner: Bool {
_lock.isOwner

Check warning on line 47 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L46-L47

Added lines #L46 - L47 were not covered by tests
}

package static func wait() {
_lock.wait()

Check warning on line 51 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L50-L51

Added lines #L50 - L51 were not covered by tests
}

package static func broadcast() {
_lock.broadcast()

Check warning on line 55 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L54-L55

Added lines #L54 - L55 were not covered by tests
}

package static func assertIsLocked() {
guard lockAssertionsAreEnabled else {
return

Check warning on line 60 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L58-L60

Added lines #L58 - L60 were not covered by tests
}
guard isOwner else {
fatalError("OpenSwiftUI is active without having taken its own lock - missing Update.ensure()?")

Check warning on line 63 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}

package static func begin() {
lock()
depth += 1
if depth == 1 {
// TODO: Signpost.renderUpdate.trace + trackHost

Check warning on line 71 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L67-L71

Added lines #L67 - L71 were not covered by tests
}
}

package static func end() {
if depth == 1 {
dispatchActions()
// TODO: Signpost.renderUpdate.trace + trackHost

Check warning on line 78 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L75-L78

Added lines #L75 - L78 were not covered by tests
}
depth -= 1
unlock()

Check warning on line 81 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L80-L81

Added lines #L80 - L81 were not covered by tests
}

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

Check warning on line 87 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L84-L87

Added lines #L84 - L87 were not covered by tests
}

@inlinable
@inline(__always)
package static func locked<T>(_ body: () throws -> T) rethrows -> T {
lock()
defer { unlock() }
return try body()

Check warning on line 95 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L92-L95

Added lines #L92 - L95 were not covered by tests
}

package static func syncMain(_ body: () -> Void) {

Check warning on line 98 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L98

Added line #L98 was not covered by tests
#if os(WASI)
// FIXME: See #76
body()
#else
if Thread.isMainThread {
body()
} else {
withoutActuallyEscaping(body) { escapableBody in
let context = AnyRuleContext(attribute: AnyOptionalAttribute.current.identifier)
MovableLock.syncMain(lock: _lock) {

Check warning on line 108 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L103-L108

Added lines #L103 - L108 were not covered by tests
#if canImport(Darwin)
context.update(body: escapableBody)

Check warning on line 110 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L110

Added line #L110 was not covered by tests
#else
fatalError("See #39")
#endif
}
}
}
#endif
}

package static func ensure<T>(_ callback: () throws -> T) rethrows -> T {
try locked {
begin()
defer { end() }
return try callback()

Check warning on line 124 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L120-L124

Added lines #L120 - L124 were not covered by tests
}
}

package static var canDispatch: Bool {
assertIsLocked()
guard depth == 1 else {
return false

Check warning on line 131 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L128-L131

Added lines #L128 - L131 were not covered by tests
}
return !actions.isEmpty

Check warning on line 133 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L133

Added line #L133 was not covered by tests
}

package static func dispatchActions() {
guard canDispatch else { return }
repeat {
let actions = Update.actions
Update.actions = []
onMainThread {
Signpost.postUpdateActions.traceInterval(object: trackHost, nil) {
begin()
let oldDispatchDepth = dispatchDepth
let oldDepth = depth
dispatchDepth = oldDepth
defer {
dispatchDepth = oldDispatchDepth
end()

Check warning on line 149 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L136-L149

Added lines #L136 - L149 were not covered by tests
}
for action in actions {
action()
let newDepth = depth
guard newDepth == oldDepth else {
fatalError("Action caused unbalanced updates.")

Check warning on line 155 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L151-L155

Added lines #L151 - L155 were not covered by tests
}
}
}
}
} while(!Update.actions.isEmpty)

Check warning on line 160 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L160

Added line #L160 was not covered by tests
}

package static func dispatchImmediately<T>(_ body: () -> T) -> T {
begin()
let oldDispatchDepth = dispatchDepth
dispatchDepth = depth
defer {
dispatchDepth = oldDispatchDepth
end()

Check warning on line 169 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L163-L169

Added lines #L163 - L169 were not covered by tests
}
return body()

Check warning on line 171 in Sources/OpenSwiftUICore/Data/Update.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Data/Update.swift#L171

Added line #L171 was not covered by tests
}
}

// FIXME: migrate to use @_extern(c, "xx") in Swift 6
extension MovableLock {
@_silgen_name("_MovableLockSyncMain")
static func syncMain(lock: MovableLock, body: @escaping () -> Void)
}
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUICore/Log/Signpost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Signpost.swift
// OpenSwiftUI
//
// Audited for RELEASE_2021
// Audited for RELEASE_2024
// Status: WIP
// ID: 34756F646CF7AC3DBE2A8E0B344C962F (RELEASE_2021)
// ID: 59349949219F590F26B6A55CEC9D59A2 (RELEASE_2024)
Expand Down
Loading
Loading