Skip to content

Instantly share code, notes, and snippets.

@comfly comfly/Atomic.swift
Last active Apr 14, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.