Created
July 22, 2021 09:19
-
-
Save geor-kasapidi/a0b2488be75296e1511af61a9406adfe to your computer and use it in GitHub Desktop.
_modfiy lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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