Skip to content

Commit

Permalink
Use _ManagedCriticalState (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrandonw authored Apr 30, 2024
1 parent 7ae342f commit 8e8ca36
Showing 1 changed file with 2 additions and 19 deletions.
21 changes: 2 additions & 19 deletions Sources/Perception/PerceptionRegistrar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct PerceptionRegistrar: Sendable {
private let _rawValue: AnySendable
#if DEBUG
private let isPerceptionCheckingEnabled: Bool
fileprivate let perceptionChecks = LockIsolated<[Location: Bool]>([:])
fileprivate let perceptionChecks = _ManagedCriticalState<[Location: Bool]>([:])
#endif

/// Creates an instance of the observation registrar.
Expand Down Expand Up @@ -223,7 +223,7 @@ extension PerceptionRegistrar: Hashable {
}

fileprivate func isInSwiftUIBody(file: StaticString, line: UInt) -> Bool {
self.perceptionChecks.withValue { perceptionChecks in
self.perceptionChecks.withCriticalRegion { perceptionChecks in
if let result = perceptionChecks[Location(file: file, line: line)] {
return result
}
Expand Down Expand Up @@ -363,23 +363,6 @@ extension PerceptionRegistrar: Hashable {
}
}

private final class LockIsolated<Value>: @unchecked Sendable {
private var _value: Value
private let lock = NSRecursiveLock()
init(_ value: @autoclosure @Sendable () throws -> Value) rethrows {
self._value = try value()
}
func withValue<T: Sendable>(
_ operation: @Sendable (inout Value) throws -> T
) rethrows -> T {
try self.lock.withLock {
var value = self._value
defer { self._value = value }
return try operation(&value)
}
}
}

private struct Location: Hashable {
let file: String
let line: UInt
Expand Down

0 comments on commit 8e8ca36

Please sign in to comment.