mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
628 B
Swift
28 lines
628 B
Swift
3 years ago
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
||
|
|
||
|
import Foundation
|
||
|
|
||
|
/// The `Atomic<T>` wrapper is a generic wrapper providing a thread-safe way to get and set a value
|
||
|
@propertyWrapper
|
||
|
struct Atomic<Value> {
|
||
|
private let lock = DispatchSemaphore(value: 1)
|
||
|
private var value: Value
|
||
|
|
||
|
init(_ initialValue: Value) {
|
||
|
self.value = initialValue
|
||
|
}
|
||
|
|
||
|
var wrappedValue: Value {
|
||
|
get {
|
||
|
lock.wait()
|
||
|
defer { lock.signal() }
|
||
|
return value
|
||
|
}
|
||
|
set {
|
||
|
lock.wait()
|
||
|
value = newValue
|
||
|
lock.signal()
|
||
|
}
|
||
|
}
|
||
|
}
|