@@ -4,46 +4,48 @@ import Foundation
44final class CurrentValueRelay < Output> : Publisher {
55 typealias Failure = Never
66
7- private var currentValue : Output
8- private let lock : NSLock
7+ private var _value : Output
8+ private let lock : os_unfair_lock_t
99 private var subscriptions = ContiguousArray < Subscription > ( )
1010
1111 var value : Output {
12- get { self . lock. withLock { self . currentValue } }
12+ get { self . lock. sync { self . _value } }
1313 set { self . send ( newValue) }
1414 }
1515
1616 init ( _ value: Output ) {
17- self . currentValue = value
18- self . lock = NSLock ( )
17+ self . _value = value
18+ self . lock = os_unfair_lock_t. allocate ( capacity: 1 )
19+ self . lock. initialize ( to: os_unfair_lock ( ) )
20+ }
21+
22+ deinit {
23+ self . lock. deinitialize ( count: 1 )
24+ self . lock. deallocate ( )
1925 }
2026
2127 func receive( subscriber: some Subscriber < Output , Never > ) {
2228 let subscription = Subscription ( upstream: self , downstream: subscriber)
23- self . lock. withLock {
29+ self . lock. sync {
2430 self . subscriptions. append ( subscription)
2531 }
2632 subscriber. receive ( subscription: subscription)
2733 }
2834
2935 func send( _ value: Output ) {
30- self . lock. withLock {
31- self . currentValue = value
36+ self . lock. sync {
37+ self . _value = value
3238 }
33- for subscription in self . lock. withLock ( { self . subscriptions } ) {
39+ for subscription in self . lock. sync ( { self . subscriptions } ) {
3440 subscription. receive ( value)
3541 }
3642 }
3743
38- private func _remove( _ subscription: Subscription ) {
39- guard let index = self . subscriptions. firstIndex ( of: subscription)
40- else { return }
41- self . subscriptions. remove ( at: index)
42- }
43-
4444 private func remove( _ subscription: Subscription ) {
45- self . lock. withLock {
46- self . _remove ( subscription)
45+ self . lock. sync {
46+ guard let index = self . subscriptions. firstIndex ( of: subscription)
47+ else { return }
48+ self . subscriptions. remove ( at: index)
4749 }
4850 }
4951}
@@ -55,24 +57,30 @@ extension CurrentValueRelay {
5557 private var _downstream : ( any Subscriber < Output , Never > ) ?
5658 var downstream : ( any Subscriber < Output , Never > ) ? {
5759 var downstream : ( any Subscriber < Output , Never > ) ?
58- self . lock. withLock { downstream = _downstream }
60+ self . lock. sync { downstream = _downstream }
5961 return downstream
6062 }
6163
62- private let lock : NSLock
64+ private let lock : os_unfair_lock_t
6365 private var receivedLastValue = false
6466 private var upstream : CurrentValueRelay ?
6567
6668 init ( upstream: CurrentValueRelay , downstream: any Subscriber < Output , Never > ) {
6769 self . upstream = upstream
6870 self . _downstream = downstream
69- self . lock = upstream. lock
71+ self . lock = os_unfair_lock_t. allocate ( capacity: 1 )
72+ self . lock. initialize ( to: os_unfair_lock ( ) )
73+ }
74+
75+ deinit {
76+ self . lock. deinitialize ( count: 1 )
77+ self . lock. deallocate ( )
7078 }
7179
7280 func cancel( ) {
73- self . lock. withLock {
81+ self . lock. sync {
7482 self . _downstream = nil
75- self . upstream? . _remove ( self )
83+ self . upstream? . remove ( self )
7684 self . upstream = nil
7785 }
7886 }
@@ -96,7 +104,7 @@ extension CurrentValueRelay {
96104 self . _demand -= 1
97105 self . lock. unlock ( )
98106 let moreDemand = downstream. receive ( value)
99- self . lock. withLock {
107+ self . lock. sync {
100108 self . _demand += moreDemand
101109 }
102110 }
@@ -112,7 +120,7 @@ extension CurrentValueRelay {
112120
113121 guard
114122 !self . receivedLastValue,
115- let value = self . upstream? . currentValue
123+ let value = self . upstream? . value
116124 else {
117125 self . lock. unlock ( )
118126 return
0 commit comments