Skip to content

Commit 32c3c73

Browse files
authored
[Infra] Clean up 'AtomicBox' usage in favor of 'FIRAllocatedUnfairLock' (#15082)
1 parent d7894f6 commit 32c3c73

File tree

4 files changed

+10
-105
lines changed

4 files changed

+10
-105
lines changed

FirebaseCore/Internal/Sources/Utilities/AtomicBox.swift

Lines changed: 0 additions & 45 deletions
This file was deleted.

FirebaseFunctions/Sources/Functions.swift

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,7 @@ import Foundation
2525
#endif
2626

2727
internal import FirebaseCoreExtension
28-
29-
final class AtomicBox<T>: Sendable {
30-
private nonisolated(unsafe) var _value: T
31-
private let lock = NSLock()
32-
33-
public init(_ value: T) where T: Sendable {
34-
_value = value
35-
}
36-
37-
public func value() -> T {
38-
lock.withLock {
39-
_value
40-
}
41-
}
42-
43-
@discardableResult
44-
public func withLock(_ mutatingBody: (_ value: inout T) -> Void) -> T {
45-
lock.withLock {
46-
mutatingBody(&_value)
47-
return _value
48-
}
49-
}
50-
51-
@discardableResult
52-
public func withLock<R>(_ mutatingBody: (_ value: inout T) throws -> R) rethrows -> R {
53-
try lock.withLock {
54-
try mutatingBody(&_value)
55-
}
56-
}
57-
}
28+
private import FirebaseCoreInternal
5829

5930
/// File specific constants.
6031
private enum Constants {
@@ -82,16 +53,15 @@ enum FunctionsConstants {
8253

8354
/// A map of active instances, grouped by app. Keys are FirebaseApp names and values are arrays
8455
/// containing all instances of Functions associated with the given app.
85-
private static let instances: AtomicBox<[String: [Functions]]> =
86-
AtomicBox([:])
56+
private static let instances = FIRAllocatedUnfairLock<[String: [Functions]]>(initialState: [:])
8757

8858
/// The custom domain to use for all functions references (optional).
8959
let customDomain: String?
9060

9161
/// The region to use for all function references.
9262
let region: String
9363

94-
private let _emulatorOrigin: AtomicBox<String?>
64+
private let _emulatorOrigin: FIRAllocatedUnfairLock<String?>
9565

9666
// MARK: - Public APIs
9767

@@ -371,7 +341,7 @@ enum FunctionsConstants {
371341
self.projectID = projectID
372342
self.region = region
373343
self.customDomain = customDomain
374-
_emulatorOrigin = AtomicBox(nil)
344+
_emulatorOrigin = FIRAllocatedUnfairLock(initialState: nil)
375345
contextProvider = FunctionsContextProvider(auth: auth,
376346
messaging: messaging,
377347
appCheck: appCheck)

FirebaseFunctions/Sources/HTTPSCallable.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import Foundation
1616

17+
private import FirebaseCoreInternal
18+
1719
/// A `HTTPSCallableResult` contains the result of calling a `HTTPSCallable`.
1820
@objc(FIRHTTPSCallableResult)
1921
open class HTTPSCallableResult: NSObject {
@@ -163,7 +165,7 @@ private extension HTTPSCallable {
163165

164166
// MARK: - Public Properties
165167

166-
let _timeoutInterval: AtomicBox<TimeInterval> = .init(70)
168+
let _timeoutInterval = FIRAllocatedUnfairLock<TimeInterval>(initialState: 70)
167169

168170
/// The timeout to use when calling the function. Defaults to 70 seconds.
169171
var timeoutInterval: TimeInterval {

FirebaseSessions/Sources/Public/SessionsDependencies.swift

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,7 @@
1515

1616
import Foundation
1717

18-
private final class AtomicBox<T> {
19-
private var _value: T
20-
private let lock = NSLock()
21-
22-
init(_ value: T) {
23-
_value = value
24-
}
25-
26-
func value() -> T {
27-
lock.withLock {
28-
_value
29-
}
30-
}
31-
32-
@discardableResult func withLock(_ body: (_ value: inout T) -> Void) -> T {
33-
lock.withLock {
34-
body(&_value)
35-
return _value
36-
}
37-
}
38-
}
18+
private import FirebaseCoreInternal
3919

4020
/// Sessions Dependencies determines when a dependent SDK is
4121
/// installed in the app. The Sessions SDK uses this to figure
@@ -46,10 +26,8 @@ private final class AtomicBox<T> {
4626
/// dependent SDKs
4727
@objc(FIRSessionsDependencies)
4828
public class SessionsDependencies: NSObject {
49-
private nonisolated(unsafe) static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> =
50-
AtomicBox(
51-
Set()
52-
)
29+
private static let _dependencies =
30+
FIRAllocatedUnfairLock<Set<SessionsSubscriberName>>(initialState: Set())
5331

5432
static var dependencies: Set<SessionsSubscriberName> {
5533
_dependencies.value()

0 commit comments

Comments
 (0)