-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(POM-234): sdk configuration (#136)
* Makes `DefaultPhoneNumberMetadataProvider` prewarm operation asynchronous
- Loading branch information
1 parent
26ea718
commit 33aeedb
Showing
3 changed files
with
102 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
Sources/ProcessOut/Sources/Core/Utils/UnfairlyLocked.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// UnfairlyLocked.swift | ||
// ProcessOut | ||
// | ||
// Created by Andrii Vysotskyi on 14.07.2023. | ||
// | ||
|
||
@_implementationOnly import os | ||
|
||
/// A thread-safe wrapper around a value. | ||
@propertyWrapper | ||
final class UnfairlyLocked<Value> { | ||
|
||
init(wrappedValue: Value) { | ||
value = wrappedValue | ||
} | ||
|
||
/// The contained value. Unsafe for anything more than direct read or write. | ||
var wrappedValue: Value { | ||
lock.withLock { value } | ||
} | ||
|
||
var projectedValue: UnfairlyLocked<Value> { | ||
self | ||
} | ||
|
||
func withLock<R>(_ body: (inout Value) -> R) -> R { | ||
lock.withLock { | ||
body(&value) | ||
} | ||
} | ||
|
||
// MARK: - Private Properties | ||
|
||
private let lock = UnfairLock() | ||
private var value: Value | ||
} | ||
|
||
/// An `os_unfair_lock` wrapper. | ||
private final class UnfairLock { | ||
|
||
init() { | ||
unfairLock = .allocate(capacity: 1) | ||
unfairLock.initialize(to: os_unfair_lock()) | ||
} | ||
|
||
func withLock<R>(_ body: () -> R) -> R { | ||
defer { | ||
os_unfair_lock_unlock(unfairLock) | ||
} | ||
os_unfair_lock_lock(unfairLock) | ||
return body() | ||
} | ||
|
||
deinit { | ||
unfairLock.deinitialize(count: 1) | ||
unfairLock.deallocate() | ||
} | ||
|
||
// MARK: - Private Properties | ||
|
||
private let unfairLock: os_unfair_lock_t | ||
} |