You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
72 lines
1.4 KiB
![]()
2 years ago
|
//
|
||
|
// AtomicInt.swift
|
||
|
// Platform
|
||
|
//
|
||
|
// Created by Krunoslav Zaher on 10/28/18.
|
||
|
// Copyright © 2018 Krunoslav Zaher. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import class Foundation.NSLock
|
||
|
|
||
|
final class AtomicInt: NSLock {
|
||
|
fileprivate var value: Int32
|
||
|
public init(_ value: Int32 = 0) {
|
||
|
self.value = value
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@discardableResult
|
||
|
@inline(__always)
|
||
|
func add(_ this: AtomicInt, _ value: Int32) -> Int32 {
|
||
|
this.lock()
|
||
|
let oldValue = this.value
|
||
|
this.value += value
|
||
|
this.unlock()
|
||
|
return oldValue
|
||
|
}
|
||
|
|
||
|
@discardableResult
|
||
|
@inline(__always)
|
||
|
func sub(_ this: AtomicInt, _ value: Int32) -> Int32 {
|
||
|
this.lock()
|
||
|
let oldValue = this.value
|
||
|
this.value -= value
|
||
|
this.unlock()
|
||
|
return oldValue
|
||
|
}
|
||
|
|
||
|
@discardableResult
|
||
|
@inline(__always)
|
||
|
func fetchOr(_ this: AtomicInt, _ mask: Int32) -> Int32 {
|
||
|
this.lock()
|
||
|
let oldValue = this.value
|
||
|
this.value |= mask
|
||
|
this.unlock()
|
||
|
return oldValue
|
||
|
}
|
||
|
|
||
|
@inline(__always)
|
||
|
func load(_ this: AtomicInt) -> Int32 {
|
||
|
this.lock()
|
||
|
let oldValue = this.value
|
||
|
this.unlock()
|
||
|
return oldValue
|
||
|
}
|
||
|
|
||
|
@discardableResult
|
||
|
@inline(__always)
|
||
|
func increment(_ this: AtomicInt) -> Int32 {
|
||
|
return add(this, 1)
|
||
|
}
|
||
|
|
||
|
@discardableResult
|
||
|
@inline(__always)
|
||
|
func decrement(_ this: AtomicInt) -> Int32 {
|
||
|
return sub(this, 1)
|
||
|
}
|
||
|
|
||
|
@inline(__always)
|
||
|
func isFlagSet(_ this: AtomicInt, _ mask: Int32) -> Bool {
|
||
|
return (load(this) & mask) != 0
|
||
|
}
|