You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Atomic.swift 535B

123456789101112131415161718192021222324252627282930
  1. import Foundation
  2. @propertyWrapper
  3. struct Atomic<Value> {
  4. private var value: Value
  5. private let lock = NSLock()
  6. init(wrappedValue value: Value) {
  7. self.value = value
  8. }
  9. var wrappedValue: Value {
  10. get { return load() }
  11. set { store(newValue: newValue) }
  12. }
  13. func load() -> Value {
  14. lock.lock()
  15. defer { lock.unlock() }
  16. return value
  17. }
  18. mutating func store(newValue: Value) {
  19. lock.lock()
  20. defer { lock.unlock() }
  21. value = newValue
  22. }
  23. }