Skip to content

Commit

Permalink
Fix non-Darwin build issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Sep 16, 2024
1 parent a662bef commit 4a65e15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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 4a65e15

Please sign in to comment.