@@ -59,27 +59,23 @@ final class HeartbeatStorage: Sendable, HeartbeatStorageProtocol {
59
59
// `nonisolated(unsafe)` to disable concurrency-safety checks. The
60
60
// property's access is protected by an external synchronization mechanism
61
61
// (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 ( [ : ] )
65
65
#else
66
66
// TODO(Xcode 16): Delete this block when minimum supported Xcode is
67
67
// Xcode 16.
68
- private static var cachedInstances : [
69
- String : WeakContainer < HeartbeatStorage >
70
- ] = [ : ]
68
+ static var cachedInstances : AtomicBox < [ String : WeakContainer < HeartbeatStorage > ] > =
69
+ AtomicBox ( [ : ] )
71
70
#endif // compiler(>=6)
72
71
73
- /// Used to synchronize concurrent access to the `cachedInstances` property.
74
- private static let instancesLock = NSLock ( )
75
-
76
72
/// Gets an existing `HeartbeatStorage` instance with the given `id` if one exists. Otherwise,
77
73
/// makes a new instance with the given `id`.
78
74
///
79
75
/// - Parameter id: A string identifier.
80
76
/// - Returns: A `HeartbeatStorage` instance.
81
77
static func getInstance( id: String ) -> HeartbeatStorage {
82
- instancesLock . withLock {
78
+ cachedInstances . withLock { cachedInstances in
83
79
if let cachedInstance = cachedInstances [ id] ? . object {
84
80
return cachedInstance
85
81
} else {
@@ -110,8 +106,8 @@ final class HeartbeatStorage: Sendable, HeartbeatStorageProtocol {
110
106
111
107
deinit {
112
108
// 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)
115
111
}
116
112
}
117
113
0 commit comments