@@ -59,27 +59,23 @@ final class HeartbeatStorage: Sendable, HeartbeatStorageProtocol {
5959 // `nonisolated(unsafe)` to disable concurrency-safety checks. The
6060 // property's access is protected by an external synchronization mechanism
6161 // (see `instancesLock` property).
62- private nonisolated ( unsafe) static var cachedInstances : [
63- String : WeakContainer < HeartbeatStorage >
64- ] = [ : ]
62+ private nonisolated ( unsafe) static var cachedInstances : AtomicBox <
63+ [ String : WeakContainer < HeartbeatStorage > ]
64+ > = AtomicBox ( [ : ] )
6565 #else
6666 // TODO(Xcode 16): Delete this block when minimum supported Xcode is
6767 // Xcode 16.
68- private static var cachedInstances : [
69- String : WeakContainer < HeartbeatStorage >
70- ] = [ : ]
68+ static var cachedInstances : AtomicBox < [ String : WeakContainer < HeartbeatStorage > ] > =
69+ AtomicBox ( [ : ] )
7170 #endif // compiler(>=6)
7271
73- /// Used to synchronize concurrent access to the `cachedInstances` property.
74- private static let instancesLock = NSLock ( )
75-
7672 /// Gets an existing `HeartbeatStorage` instance with the given `id` if one exists. Otherwise,
7773 /// makes a new instance with the given `id`.
7874 ///
7975 /// - Parameter id: A string identifier.
8076 /// - Returns: A `HeartbeatStorage` instance.
8177 static func getInstance( id: String ) -> HeartbeatStorage {
82- instancesLock . withLock {
78+ cachedInstances . withLock { cachedInstances in
8379 if let cachedInstance = cachedInstances [ id] ? . object {
8480 return cachedInstance
8581 } else {
@@ -110,8 +106,8 @@ final class HeartbeatStorage: Sendable, HeartbeatStorageProtocol {
110106
111107 deinit {
112108 // Removes the instance if it was cached.
113- _ = Self . instancesLock . withLock {
114- Self . cachedInstances . removeValue ( forKey: id)
109+ Self . cachedInstances . withLock { value in
110+ value . removeValue ( forKey: id)
115111 }
116112 }
117113
0 commit comments