Skip to content

Commit e3110da

Browse files
authored
[Infra] Rename 'FIRAllocatedUnfairLock' to 'UnfairLock' (#15090)
1 parent 2836788 commit e3110da

File tree

16 files changed

+63
-69
lines changed

16 files changed

+63
-69
lines changed

FirebaseAuth/Sources/Swift/ActionCode/ActionCodeSettings.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,22 +105,22 @@ import Foundation
105105
private extension ActionCodeSettings {
106106
/// Checked Sendable implementation of `ActionCodeSettings`.
107107
final class SendableActionCodeSettings: Sendable {
108-
let url = FIRAllocatedUnfairLock<URL?>(initialState: nil)
108+
let url = UnfairLock<URL?>(nil)
109109

110-
let handleCodeInApp = FIRAllocatedUnfairLock<Bool>(initialState: false)
110+
let handleCodeInApp = UnfairLock<Bool>(false)
111111

112-
let iOSBundleID: FIRAllocatedUnfairLock<String?>
112+
let iOSBundleID: UnfairLock<String?>
113113

114-
let androidPackageName = FIRAllocatedUnfairLock<String?>(initialState: nil)
114+
let androidPackageName = UnfairLock<String?>(nil)
115115

116-
let androidMinimumVersion = FIRAllocatedUnfairLock<String?>(initialState: nil)
116+
let androidMinimumVersion = UnfairLock<String?>(nil)
117117

118-
let androidInstallIfNotAvailable = FIRAllocatedUnfairLock<Bool>(initialState: false)
118+
let androidInstallIfNotAvailable = UnfairLock<Bool>(false)
119119

120-
let linkDomain = FIRAllocatedUnfairLock<String?>(initialState: nil)
120+
let linkDomain = UnfairLock<String?>(nil)
121121

122122
init() {
123-
iOSBundleID = FIRAllocatedUnfairLock<String?>(initialState: Bundle.main.bundleIdentifier)
123+
iOSBundleID = UnfairLock<String?>(Bundle.main.bundleIdentifier)
124124
}
125125

126126
func setAndroidPackageName(_ androidPackageName: String,

FirebaseAuth/Sources/Swift/Storage/AuthKeychainServices.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ final class AuthKeychainServices: Sendable {
102102
/// been deleted.
103103
///
104104
/// This dictionary is to avoid unnecessary keychain operations against legacy items.
105-
private let legacyEntryDeletedForKey = FIRAllocatedUnfairLock<Set<String>>(initialState: [])
105+
private let legacyEntryDeletedForKey = UnfairLock<Set<String>>([])
106106

107107
func data(forKey key: String) throws -> Data? {
108108
if let data = try getItemLegacy(query: genericPasswordQuery(key: key)) {

FirebaseAuth/Sources/Swift/SystemService/SecureTokenService.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ final class SecureTokenService: NSObject, NSSecureCoding, Sendable {
125125
set { _requestConfiguration.withLock { $0 = newValue } }
126126
}
127127

128-
let _requestConfiguration: FIRAllocatedUnfairLock<AuthRequestConfiguration?>
128+
let _requestConfiguration: UnfairLock<AuthRequestConfiguration?>
129129

130130
/// The cached access token.
131131
///
@@ -140,7 +140,7 @@ final class SecureTokenService: NSObject, NSSecureCoding, Sendable {
140140
set { _accessToken.withLock { $0 = newValue } }
141141
}
142142

143-
private let _accessToken: FIRAllocatedUnfairLock<String>
143+
private let _accessToken: UnfairLock<String>
144144

145145
/// The refresh token for the user, or `nil` if the user has yet completed sign-in flow.
146146
///
@@ -150,15 +150,15 @@ final class SecureTokenService: NSObject, NSSecureCoding, Sendable {
150150
set { _refreshToken.withLock { $0 = newValue } }
151151
}
152152

153-
private let _refreshToken: FIRAllocatedUnfairLock<String?>
153+
private let _refreshToken: UnfairLock<String?>
154154

155155
/// The expiration date of the cached access token.
156156
var accessTokenExpirationDate: Date? {
157157
get { _accessTokenExpirationDate.withLock { $0 } }
158158
set { _accessTokenExpirationDate.withLock { $0 = newValue } }
159159
}
160160

161-
private let _accessTokenExpirationDate: FIRAllocatedUnfairLock<Date?>
161+
private let _accessTokenExpirationDate: UnfairLock<Date?>
162162

163163
/// Creates a `SecureTokenService` with access and refresh tokens.
164164
/// - Parameter requestConfiguration: The configuration for making requests to server.
@@ -170,10 +170,10 @@ final class SecureTokenService: NSObject, NSSecureCoding, Sendable {
170170
accessTokenExpirationDate: Date?,
171171
refreshToken: String) {
172172
internalService = SecureTokenServiceInternal()
173-
_requestConfiguration = FIRAllocatedUnfairLock(initialState: requestConfiguration)
174-
_accessToken = FIRAllocatedUnfairLock(initialState: accessToken)
175-
_accessTokenExpirationDate = FIRAllocatedUnfairLock(initialState: accessTokenExpirationDate)
176-
_refreshToken = FIRAllocatedUnfairLock(initialState: refreshToken)
173+
_requestConfiguration = UnfairLock(requestConfiguration)
174+
_accessToken = UnfairLock(accessToken)
175+
_accessTokenExpirationDate = UnfairLock(accessTokenExpirationDate)
176+
_refreshToken = UnfairLock(refreshToken)
177177
}
178178

179179
/// Fetch a fresh ephemeral access token for the ID associated with this instance. The token

FirebaseAuth/Tests/Unit/AuthAPNSTokenManagerTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
func testCallback() throws {
6363
let expectation = self.expectation(description: #function)
6464
XCTAssertFalse(fakeApplication!.registerCalled)
65-
let firstCallbackCalled = FIRAllocatedUnfairLock(initialState: false)
65+
let firstCallbackCalled = UnfairLock(false)
6666
let manager = try XCTUnwrap(manager)
6767
manager.getTokenInternal { result in
6868
firstCallbackCalled.withLock { $0 = true }
@@ -77,7 +77,7 @@
7777
XCTAssertFalse(firstCallbackCalled.value())
7878

7979
// Add second callback, which is yet to be called either.
80-
let secondCallbackCalled = FIRAllocatedUnfairLock(initialState: false)
80+
let secondCallbackCalled = UnfairLock(false)
8181
manager.getTokenInternal { result in
8282
secondCallbackCalled.withLock { $0 = true }
8383
switch result {
@@ -104,7 +104,7 @@
104104
XCTAssertEqual(manager.token?.type, .sandbox)
105105

106106
// Add third callback, which should be called back immediately.
107-
let thirdCallbackCalled = FIRAllocatedUnfairLock(initialState: false)
107+
let thirdCallbackCalled = UnfairLock(false)
108108
manager.getTokenInternal { result in
109109
thirdCallbackCalled.withLock { $0 = true }
110110
switch result {
@@ -178,7 +178,7 @@
178178
XCTAssertGreaterThan(try XCTUnwrap(manager.timeout), 0)
179179

180180
// Add callback to cancel.
181-
let callbackCalled = FIRAllocatedUnfairLock(initialState: false)
181+
let callbackCalled = UnfairLock(false)
182182
manager.getTokenInternal { result in
183183
switch result {
184184
case let .success(token):

FirebaseAuth/Tests/Unit/Fakes/FakeAuthKeychainStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import XCTest
2121
@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
2222
final class FakeAuthKeychainStorage: AuthKeychainStorage {
2323
// Fake Keychain. It's a dictionary, keyed by service name, for each key-value store dictionary
24-
private let fakeKeychain = FIRAllocatedUnfairLock<[String: [String: Any]]>(initialState: [:])
24+
private let fakeKeychain = UnfairLock<[String: [String: Any]]>([:])
2525

26-
private let fakeLegacyKeychain = FIRAllocatedUnfairLock<[String: Any]>(initialState: [:])
26+
private let fakeLegacyKeychain = UnfairLock<[String: Any]>([:])
2727

2828
func get(query: [String: Any], result: inout AnyObject?) -> OSStatus {
2929
if let service = queryService(query) {

FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatStorage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ final class HeartbeatStorage: Sendable, HeartbeatStorageProtocol {
5252
// MARK: - Instance Management
5353

5454
/// Statically allocated cache of `HeartbeatStorage` instances keyed by string IDs.
55-
private static let cachedInstances: FIRAllocatedUnfairLock<
55+
private static let cachedInstances: UnfairLock<
5656
[String: WeakContainer<HeartbeatStorage>]
57-
> = FIRAllocatedUnfairLock(initialState: [:])
57+
> = UnfairLock([:])
5858

5959
/// Gets an existing `HeartbeatStorage` instance with the given `id` if one exists. Otherwise,
6060
/// makes a new instance with the given `id`.

FirebaseCore/Internal/Sources/HeartbeatLogging/WeakContainer.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ import Foundation
1818
struct WeakContainer<Object: AnyObject> {
1919
weak var object: Object?
2020
}
21+
22+
extension WeakContainer: Sendable where Object: Sendable {}

FirebaseCore/Internal/Sources/Utilities/FIRAllocatedUnfairLock.swift renamed to FirebaseCore/Internal/Sources/Utilities/UnfairLock.swift

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,54 @@
1313
// limitations under the License.
1414

1515
import Foundation
16-
import os.lock
16+
private import os.lock
1717

1818
/// A reference wrapper around `os_unfair_lock`. Replace this class with
1919
/// `OSAllocatedUnfairLock` once we support only iOS 16+. For an explanation
2020
/// on why this is necessary, see the docs:
2121
/// https://developer.apple.com/documentation/os/osallocatedunfairlock
22-
public final class FIRAllocatedUnfairLock<State>: @unchecked Sendable {
22+
public final class UnfairLock<Value>: @unchecked Sendable {
2323
private var lockPointer: UnsafeMutablePointer<os_unfair_lock>
24-
private var state: State
24+
private var _value: Value
2525

26-
public init(initialState: sending State) {
26+
public init(_ value: consuming sending Value) {
2727
lockPointer = UnsafeMutablePointer<os_unfair_lock>
2828
.allocate(capacity: 1)
2929
lockPointer.initialize(to: os_unfair_lock())
30-
state = initialState
30+
_value = value
3131
}
3232

33-
public convenience init() where State == Void {
34-
self.init(initialState: ())
35-
}
36-
37-
public func lock() {
38-
os_unfair_lock_lock(lockPointer)
39-
}
40-
41-
public func unlock() {
42-
os_unfair_lock_unlock(lockPointer)
33+
deinit {
34+
lockPointer.deallocate()
4335
}
4436

45-
public func value() -> State {
37+
public func value() -> Value {
4638
lock()
4739
defer { unlock() }
48-
return state
40+
return _value
4941
}
5042

5143
@discardableResult
52-
public func withLock<R>(_ body: (inout State) throws -> R) rethrows -> R {
53-
let value: R
44+
public borrowing func withLock<Result>(_ body: (inout sending Value) throws
45+
-> sending Result) rethrows -> sending Result {
5446
lock()
5547
defer { unlock() }
56-
value = try body(&state)
57-
return value
48+
return try body(&_value)
5849
}
5950

6051
@discardableResult
61-
public func withLock<R>(_ body: () throws -> R) rethrows -> R {
62-
let value: R
52+
public borrowing func withLock<Result>(_ body: (inout sending Value) -> sending Result)
53+
-> sending Result {
6354
lock()
6455
defer { unlock() }
65-
value = try body()
66-
return value
56+
return body(&_value)
6757
}
6858

69-
deinit {
70-
lockPointer.deallocate()
59+
private func lock() {
60+
os_unfair_lock_lock(lockPointer)
61+
}
62+
63+
private func unlock() {
64+
os_unfair_lock_unlock(lockPointer)
7165
}
7266
}

FirebaseCore/Internal/Tests/Unit/HeartbeatStorageTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ class HeartbeatStorageTests: XCTestCase {
407407
final class WeakRefs: @unchecked Sendable {
408408
// Lock is used to synchronize `weakRefs` during concurrent access.
409409
private(set) var weakRefs =
410-
FIRAllocatedUnfairLock<[WeakContainer<HeartbeatStorage>]>(initialState: [])
410+
UnfairLock<[WeakContainer<HeartbeatStorage>]>([])
411411

412412
func append(_ weakRef: WeakContainer<HeartbeatStorage>) {
413413
weakRefs.withLock {

FirebaseFunctions/Sources/Functions.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ enum FunctionsConstants {
5353

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

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

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

64-
private let _emulatorOrigin: FIRAllocatedUnfairLock<String?>
64+
private let _emulatorOrigin: UnfairLock<String?>
6565

6666
// MARK: - Public APIs
6767

@@ -341,7 +341,7 @@ enum FunctionsConstants {
341341
self.projectID = projectID
342342
self.region = region
343343
self.customDomain = customDomain
344-
_emulatorOrigin = FIRAllocatedUnfairLock(initialState: nil)
344+
_emulatorOrigin = UnfairLock(nil)
345345
contextProvider = FunctionsContextProvider(auth: auth,
346346
messaging: messaging,
347347
appCheck: appCheck)

0 commit comments

Comments
 (0)