SHARE
TWEET

Untitled

a guest Oct 21st, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top