31 lines
535 B
Swift
31 lines
535 B
Swift
|
|
import Foundation
|
|
|
|
@propertyWrapper
|
|
struct Atomic<Value> {
|
|
|
|
private var value: Value
|
|
private let lock = NSLock()
|
|
|
|
init(wrappedValue value: Value) {
|
|
self.value = value
|
|
}
|
|
|
|
var wrappedValue: Value {
|
|
get { return load() }
|
|
set { store(newValue: newValue) }
|
|
}
|
|
|
|
func load() -> Value {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return value
|
|
}
|
|
|
|
mutating func store(newValue: Value) {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
value = newValue
|
|
}
|
|
}
|