Schedule/Sources/Schedule/Atomic.swift

29 lines
479 B
Swift

//
// Atomic.swift
// Schedule
//
// Created by Quentin Jin on 2018/7/26.
//
import Foundation
final class Atomic<T> {
private var value: T
private let lock = Lock()
init(_ value: T) {
self.value = value
}
@inline(__always)
func read<U>(_ body: (T) -> U) -> U {
return lock.withLock { body(value) }
}
@inline(__always)
func write<U>(_ body: (inout T) -> U) -> U {
return lock.withLock { body(&value) }
}
}