Last active
April 14, 2016 15:09
-
-
Save comfly/2e153c9c273d904ef13bfb1c941f340b to your computer and use it in GitHub Desktop.
This file contains 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
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