Skip to content

Instantly share code, notes, and snippets.

@serhiybutz
Created September 24, 2021 12:31
Show Gist options
  • Save serhiybutz/2fd29ac4319c5f02e70af390b6a73fd6 to your computer and use it in GitHub Desktop.
Save serhiybutz/2fd29ac4319c5f02e70af390b6a73fd6 to your computer and use it in GitHub Desktop.
final class Borrowing {
private let props: [AnyObject]
private let revokeCond = NSCondition()
private var isRevoked = false
init(_ props: [AnyObject]) {
self.props = props
}
func wait() {
revokeCond.lock()
defer { revokeCond.unlock() }
while !isRevoked {
revokeCond.wait()
}
}
func revoke() {
revokeCond.lock()
defer { revokeCond.unlock() }
if !isRevoked {
isRevoked = true
revokeCond.broadcast()
}
}
func hasConfictWith(_ another: Borrowing) -> Bool {
return props.contains { a in another.props.contains { b in a === b } }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment