Skip to content

Instantly share code, notes, and snippets.

@comfly
Last active April 14, 2016 15:09
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 comfly/2e153c9c273d904ef13bfb1c941f340b to your computer and use it in GitHub Desktop.
Save comfly/2e153c9c273d904ef13bfb1c941f340b to your computer and use it in GitHub Desktop.
protocol Lock {
func lock() throws
func unlock()
}
protocol Atomic {
associatedtype Protected
associatedtype Primitive: Lock
var lock: Primitive { get }
var value: Protected { get set }
}
extension Atomic {
var value: Protected {
get {
try! lock.lock()
defer { lock.unlock() }
return value
}
set {
try! lock.lock()
defer { lock.unlock() }
value = newValue
}
}
}
class SpinLock: Lock {
func lock() throws { }
func unlock() { }
}
class AtomicSpinLock<P>: Atomic {
typealias Protected = P
var lock: SpinLock
}
var i: AtomicSpinLock<Int>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment