Skip to content

[Infra] Clean up 'AtomicBox' usage in favor of 'FIRAllocatedUnfairLock' #15082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions FirebaseCore/Internal/Sources/Utilities/AtomicBox.swift

This file was deleted.

38 changes: 4 additions & 34 deletions FirebaseFunctions/Sources/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,7 @@ import Foundation
#endif

internal import FirebaseCoreExtension

final class AtomicBox<T>: Sendable {
private nonisolated(unsafe) var _value: T
private let lock = NSLock()

public init(_ value: T) where T: Sendable {
_value = value
}

public func value() -> T {
lock.withLock {
_value
}
}

@discardableResult
public func withLock(_ mutatingBody: (_ value: inout T) -> Void) -> T {
lock.withLock {
mutatingBody(&_value)
return _value
}
}

@discardableResult
public func withLock<R>(_ mutatingBody: (_ value: inout T) throws -> R) rethrows -> R {
try lock.withLock {
try mutatingBody(&_value)
}
}
}
private import FirebaseCoreInternal

/// File specific constants.
private enum Constants {
Expand Down Expand Up @@ -82,16 +53,15 @@ enum FunctionsConstants {

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

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

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

private let _emulatorOrigin: AtomicBox<String?>
private let _emulatorOrigin: FIRAllocatedUnfairLock<String?>

// MARK: - Public APIs

Expand Down Expand Up @@ -371,7 +341,7 @@ enum FunctionsConstants {
self.projectID = projectID
self.region = region
self.customDomain = customDomain
_emulatorOrigin = AtomicBox(nil)
_emulatorOrigin = FIRAllocatedUnfairLock(initialState: nil)
contextProvider = FunctionsContextProvider(auth: auth,
messaging: messaging,
appCheck: appCheck)
Expand Down
4 changes: 3 additions & 1 deletion FirebaseFunctions/Sources/HTTPSCallable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import Foundation

private import FirebaseCoreInternal

/// A `HTTPSCallableResult` contains the result of calling a `HTTPSCallable`.
@objc(FIRHTTPSCallableResult)
open class HTTPSCallableResult: NSObject {
Expand Down Expand Up @@ -163,7 +165,7 @@ private extension HTTPSCallable {

// MARK: - Public Properties

let _timeoutInterval: AtomicBox<TimeInterval> = .init(70)
let _timeoutInterval = FIRAllocatedUnfairLock<TimeInterval>(initialState: 70)

/// The timeout to use when calling the function. Defaults to 70 seconds.
var timeoutInterval: TimeInterval {
Expand Down
28 changes: 3 additions & 25 deletions FirebaseSessions/Sources/Public/SessionsDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,7 @@

import Foundation

private final class AtomicBox<T> {
private var _value: T
private let lock = NSLock()

init(_ value: T) {
_value = value
}

func value() -> T {
lock.withLock {
_value
}
}

@discardableResult func withLock(_ body: (_ value: inout T) -> Void) -> T {
lock.withLock {
body(&_value)
return _value
}
}
}
private import FirebaseCoreInternal

/// Sessions Dependencies determines when a dependent SDK is
/// installed in the app. The Sessions SDK uses this to figure
Expand All @@ -46,10 +26,8 @@ private final class AtomicBox<T> {
/// dependent SDKs
@objc(FIRSessionsDependencies)
public class SessionsDependencies: NSObject {
private nonisolated(unsafe) static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> =
AtomicBox(
Set()
)
private static let _dependencies =
FIRAllocatedUnfairLock<Set<SessionsSubscriberName>>(initialState: Set())

static var dependencies: Set<SessionsSubscriberName> {
_dependencies.value()
Expand Down
Loading