Skip to content

Commit f396aeb

Browse files
authored
Adopt Mutex where possible (#2026)
Motivation: Swift 6 has a `Mutex` which we can use in place of `LockedValueBox` or `NIOLockedValueBox` in a bunch of places. Modifications: - Use `Mutex` where possible - This causes a number of other changes too as `Mutex` doesn't allocate: the allocations move to the type holding it instead. This makes sense as the objects which were previously structs didn't have value semantics. Result: Fewer uses of our own lock, no uses of NIOs lock
1 parent 95430b9 commit f396aeb

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Sources/Services/Health/HealthService.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
internal import GRPCCore
18+
private import Synchronization
1819

1920
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
2021
internal struct HealthService: Grpc_Health_V1_HealthServiceProtocol {
@@ -67,21 +68,21 @@ internal struct HealthService: Grpc_Health_V1_HealthServiceProtocol {
6768

6869
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
6970
extension HealthService {
70-
private struct State: Sendable {
71+
private final class State: Sendable {
7172
// The state of each service keyed by the fully qualified service name.
72-
private let lockedStorage = LockedValueBox([String: ServiceState]())
73+
private let lockedStorage = Mutex([String: ServiceState]())
7374

7475
fileprivate func currentStatus(
7576
ofService service: String
7677
) -> Grpc_Health_V1_HealthCheckResponse.ServingStatus? {
77-
return self.lockedStorage.withLockedValue { $0[service]?.currentStatus }
78+
return self.lockedStorage.withLock { $0[service]?.currentStatus }
7879
}
7980

8081
fileprivate func updateStatus(
8182
_ status: Grpc_Health_V1_HealthCheckResponse.ServingStatus,
8283
forService service: String
8384
) {
84-
self.lockedStorage.withLockedValue { storage in
85+
self.lockedStorage.withLock { storage in
8586
storage[service, default: ServiceState(status: status)].updateStatus(status)
8687
}
8788
}
@@ -90,7 +91,7 @@ extension HealthService {
9091
_ continuation: AsyncStream<Grpc_Health_V1_HealthCheckResponse.ServingStatus>.Continuation,
9192
forService service: String
9293
) {
93-
self.lockedStorage.withLockedValue { storage in
94+
self.lockedStorage.withLock { storage in
9495
storage[service, default: ServiceState(status: .serviceUnknown)]
9596
.addContinuation(continuation)
9697
}

0 commit comments

Comments
 (0)