Skip to content

Instantly share code, notes, and snippets.

@geor-kasapidi
Created July 22, 2021 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geor-kasapidi/a0b2488be75296e1511af61a9406adfe to your computer and use it in GitHub Desktop.
Save geor-kasapidi/a0b2488be75296e1511af61a9406adfe to your computer and use it in GitHub Desktop.
_modfiy lock
@propertyWrapper
public final class ThreadSafe<Root> {
private let lock = OSUnfairLock()
private var value: Root
public init(wrappedValue: Root) {
self.value = wrappedValue
}
public var projectedValue: ThreadSafe<Root> { self }
public var wrappedValue: Root {
get {
self.lock.lock()
defer {
self.lock.unlock()
}
return self.value
}
_modify {
self.lock.lock()
yield &self.value
self.lock.unlock()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment