Skip to content

Commit 3cc0488

Browse files
committed
nonrecursive lock
1 parent ebec38b commit 3cc0488

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

Sources/ComposableArchitecture/Internal/CurrentValueRelay.swift

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,45 @@ final class CurrentValueRelay<Output>: Publisher {
55
typealias Failure = Never
66

77
private var currentValue: Output
8-
private let lock: NSRecursiveLock
8+
private let lock: NSLock
99
private var subscriptions = ContiguousArray<Subscription>()
1010

1111
var value: Output {
12-
get { self.lock.sync { self.currentValue } }
12+
get { self.lock.withLock { self.currentValue } }
1313
set { self.send(newValue) }
1414
}
1515

1616
init(_ value: Output) {
1717
self.currentValue = value
18-
self.lock = NSRecursiveLock()
18+
self.lock = NSLock()
1919
}
2020

2121
func receive(subscriber: some Subscriber<Output, Never>) {
2222
let subscription = Subscription(upstream: self, downstream: subscriber)
23-
self.lock.sync {
23+
self.lock.withLock {
2424
self.subscriptions.append(subscription)
2525
}
2626
subscriber.receive(subscription: subscription)
2727
}
2828

2929
func send(_ value: Output) {
30-
self.lock.sync {
30+
self.lock.withLock {
3131
self.currentValue = value
3232
}
33-
for subscription in self.lock.sync(work: { self.subscriptions }) {
33+
for subscription in self.lock.withLock({ self.subscriptions }) {
3434
subscription.receive(value)
3535
}
3636
}
3737

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+
3844
private func remove(_ subscription: Subscription) {
39-
self.lock.sync {
40-
guard let index = self.subscriptions.firstIndex(of: subscription)
41-
else { return }
42-
self.subscriptions.remove(at: index)
45+
self.lock.withLock {
46+
self._remove(subscription)
4347
}
4448
}
4549
}
@@ -51,11 +55,11 @@ extension CurrentValueRelay {
5155
private var _downstream: (any Subscriber<Output, Never>)?
5256
var downstream: (any Subscriber<Output, Never>)? {
5357
var downstream: (any Subscriber<Output, Never>)?
54-
self.lock.sync { downstream = _downstream }
58+
self.lock.withLock { downstream = _downstream }
5559
return downstream
5660
}
5761

58-
private let lock: NSRecursiveLock
62+
private let lock: NSLock
5963
private var receivedLastValue = false
6064
private var upstream: CurrentValueRelay?
6165

@@ -66,9 +70,9 @@ extension CurrentValueRelay {
6670
}
6771

6872
func cancel() {
69-
self.lock.sync {
73+
self.lock.withLock {
7074
self._downstream = nil
71-
self.upstream?.remove(self)
75+
self.upstream?._remove(self)
7276
self.upstream = nil
7377
}
7478
}
@@ -92,7 +96,7 @@ extension CurrentValueRelay {
9296
self._demand -= 1
9397
self.lock.unlock()
9498
let moreDemand = downstream.receive(value)
95-
self.lock.sync {
99+
self.lock.withLock {
96100
self._demand += moreDemand
97101
}
98102
}

0 commit comments

Comments
 (0)