Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. /// Wraps a value with a DispatchSemaphore(value: 1) to prevent overlapping access.
  2. @propertyWrapper public struct Semaphored<T> {
  3.  
  4. private var value: T
  5. private var semaphore = DispatchSemaphore(value: 1)
  6.  
  7. public var wrappedValue: T {
  8. get {
  9. defer { semaphore.signal() }
  10. semaphore.wait()
  11. return value
  12. }
  13. set {
  14. semaphore.wait()
  15. value = newValue
  16. semaphore.signal()
  17. }
  18. }
  19.  
  20. public init(wrappedValue: T) {
  21. self.value = wrappedValue
  22. }
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement