How do I atomically increment a variable in Swift?

后端 未结 7 782
别那么骄傲
别那么骄傲 2020-11-29 18:59

I want to be able to increment a counter atomically and I can\'t find any reference on how to do it.

Adding more information based on comments:

  • Are yo
7条回答
  •  星月不相逢
    2020-11-29 19:41

    Details

    • Xcode 10.1 (10B61)
    • Swift 4.2

    Solution

    import Foundation
    
    struct AtomicInteger: BinaryInteger where Type: BinaryInteger {
    
        typealias Magnitude = Type.Magnitude
        typealias IntegerLiteralType = Type.IntegerLiteralType
        typealias Words = Type.Words
        fileprivate var value: Type
    
        private var semaphore = DispatchSemaphore(value: 1)
        fileprivate func _wait() { semaphore.wait() }
        fileprivate func _signal() { semaphore.signal() }
    
        init() { value = Type() }
    
        init(integerLiteral value: AtomicInteger.IntegerLiteralType) {
            self.value = Type(integerLiteral: value)
        }
    
        init(_ source: T) where T : BinaryInteger {
            value = Type(source)
        }
    
        init(_ source: Int) {
            value = Type(source)
        }
    
        init(clamping source: T) where T : BinaryInteger {
            value = Type(clamping: source)
        }
    
        init?(exactly source: T) where T : BinaryInteger {
            guard let value = Type(exactly: source) else { return nil }
            self.value = value
        }
    
        init(truncatingIfNeeded source: T) where T : BinaryInteger {
            value = Type(truncatingIfNeeded: source)
        }
    
        init?(exactly source: T) where T : BinaryFloatingPoint {
            guard let value = Type(exactly: source) else { return nil }
            self.value = value
        }
    
        init(_ source: T) where T : BinaryFloatingPoint {
            value = Type(source)
        }
    }
    
    // Instance Properties
    
    extension AtomicInteger {
        var words: Type.Words {
            _wait(); defer { _signal() }
            return value.words
        }
        var bitWidth: Int {
            _wait(); defer { _signal() }
            return value.bitWidth
        }
        var trailingZeroBitCount: Int {
            _wait(); defer { _signal() }
            return value.trailingZeroBitCount
        }
        var magnitude: Type.Magnitude {
            _wait(); defer { _signal() }
            return value.magnitude
        }
    }
    
    // Type Properties
    
    extension AtomicInteger {
        static var isSigned: Bool { return Type.isSigned }
    }
    
    // Instance Methods
    
    extension AtomicInteger {
    
        func quotientAndRemainder(dividingBy rhs: AtomicInteger) -> (quotient: AtomicInteger, remainder: AtomicInteger) {
            _wait(); defer { _signal() }
            rhs._wait(); defer { rhs._signal() }
            let result = value.quotientAndRemainder(dividingBy: rhs.value)
            return (AtomicInteger(result.quotient), AtomicInteger(result.remainder))
        }
    
        func signum() -> AtomicInteger {
            _wait(); defer { _signal() }
            return AtomicInteger(value.signum())
        }
    }
    
    
    extension AtomicInteger {
    
        fileprivate static func atomicAction(lhs: AtomicInteger,
                                                            rhs: Other, closure: (Type, Type) -> (Result)) -> Result where Other : BinaryInteger {
            lhs._wait(); defer { lhs._signal() }
            var rhsValue = Type(rhs)
            if let rhs = rhs as? AtomicInteger {
                rhs._wait(); defer { rhs._signal() }
                rhsValue = rhs.value
            }
            let result = closure(lhs.value, rhsValue)
            return result
        }
    
        fileprivate static func atomicActionAndResultSaving(lhs: inout AtomicInteger,
                                                                   rhs: Other, closure: (Type, Type) -> (Type)) where Other : BinaryInteger {
            lhs._wait(); defer { lhs._signal() }
            var rhsValue = Type(rhs)
            if let rhs = rhs as? AtomicInteger {
                rhs._wait(); defer { rhs._signal() }
                rhsValue = rhs.value
            }
            let result = closure(lhs.value, rhsValue)
            lhs.value = result
        }
    }
    
    // Math Operator Functions
    
    extension AtomicInteger {
    
        static func != (lhs: AtomicInteger, rhs: Other) -> Bool where Other : BinaryInteger {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 != $1 }
        }
    
        static func != (lhs: AtomicInteger, rhs: AtomicInteger) -> Bool {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 != $1 }
        }
    
        static func % (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 % $1 }
            return self.init(value)
        }
    
        static func %= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 % $1 }
        }
    
        static func & (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 & $1 }
            return self.init(value)
        }
    
        static func &= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 & $1 }
        }
    
        static func * (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 * $1 }
            return self.init(value)
        }
    
        static func *= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 * $1 }
        }
    
        static func + (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 + $1 }
            return self.init(value)
        }
        static func += (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 + $1 }
        }
    
        static func - (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 - $1 }
            return self.init(value)
        }
    
        static func -= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 - $1 }
        }
    
        static func / (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 / $1 }
            return self.init(value)
        }
    
        static func /= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 / $1 }
        }
    }
    
    
    // Shifting Operator Functions
    
    extension AtomicInteger {
        static func << (lhs:  AtomicInteger, rhs: RHS) -> AtomicInteger where RHS : BinaryInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 << $1 }
            return self.init(value)
        }
    
        static func <<= (lhs: inout AtomicInteger, rhs: RHS) where RHS : BinaryInteger {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 << $1 }
        }
    
        static func >> (lhs: AtomicInteger, rhs: RHS) -> AtomicInteger where RHS : BinaryInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 >> $1 }
            return self.init(value)
        }
    
        static func >>= (lhs: inout AtomicInteger, rhs: RHS) where RHS : BinaryInteger {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 >> $1 }
        }
    }
    
    // Comparing Operator Functions
    
    extension AtomicInteger {
    
        static func < (lhs: AtomicInteger, rhs: Other) -> Bool where Other : BinaryInteger {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 < $1 }
        }
    
        static func <= (lhs: AtomicInteger, rhs: AtomicInteger) -> Bool {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 <= $1 }
        }
    
        static func == (lhs: AtomicInteger, rhs: Other) -> Bool where Other : BinaryInteger {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 == $1 }
        }
    
        static func > (lhs: AtomicInteger, rhs: Other) -> Bool where Other : BinaryInteger {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 > $1 }
        }
    
        static func > (lhs: AtomicInteger, rhs: AtomicInteger) -> Bool {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 > $1 }
        }
    
        static func >= (lhs: AtomicInteger, rhs: AtomicInteger) -> Bool {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 >= $1 }
        }
    
        static func >= (lhs: AtomicInteger, rhs: Other) -> Bool where Other : BinaryInteger {
            return atomicAction(lhs: lhs, rhs: rhs) { $0 >= $1 }
        }
    }
    
    // Binary Math Operator Functions
    
    extension AtomicInteger {
    
        static func ^ (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 ^ $1 }
            return self.init(value)
        }
    
        static func ^= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 ^ $1 }
        }
    
        static func | (lhs: AtomicInteger, rhs: AtomicInteger) -> AtomicInteger {
            let value = atomicAction(lhs: lhs, rhs: rhs) { $0 | $1 }
            return self.init(value)
        }
    
        static func |= (lhs: inout AtomicInteger, rhs: AtomicInteger) {
            atomicActionAndResultSaving(lhs: &lhs, rhs: rhs) { $0 | $1 }
        }
    
        static prefix func ~ (x: AtomicInteger) -> AtomicInteger {
            x._wait(); defer { x._signal() }
            return self.init(x.value)
        }
    }
    
    // Hashable
    
    extension AtomicInteger {
    
        var hashValue: Int {
            _wait(); defer { _signal() }
            return value.hashValue
        }
    
        func hash(into hasher: inout Hasher) {
            _wait(); defer { _signal() }
            value.hash(into: &hasher)
        }
    }
    
    // Get/Set
    
    extension AtomicInteger {
    
        // Single  actions
    
        func get() -> Type {
            _wait(); defer { _signal() }
            return value
        }
    
        mutating func set(value: Type) {
            _wait(); defer { _signal() }
            self.value = value
        }
    
        // Multi-actions
    
        func get(closure: (Type)->()) {
            _wait(); defer { _signal() }
            closure(value)
        }
    
        mutating func set(closure: (Type)->(Type)) {
            _wait(); defer { _signal() }
            self.value = closure(value)
        }
    }
    

    Usage

    // Usage Samples
    let numA = AtomicInteger(0)
    let numB = AtomicInteger(0)
    let numC = AtomicInteger(0)
    let numD = AtomicInteger(0)
    
    var num1 = AtomicInteger(0)
    num1 += 1
    num1 -= 1
    num1 = 10
    num1 = num1/2
    
    var num2 = 0
    num2 = num1.get()
    num1.set(value: num2*5)
    
    // lock num1 to do several actions
    num1.get { value in
        //...
    }
    
    num1.set { value in
        //...
        return value
    }
    

    Full Sample

    import Foundation
    
    var x = AtomicInteger(0)
    let dispatchGroup = DispatchGroup()
    private func async(dispatch: DispatchQueue, closure: @escaping (DispatchQueue)->()) {
        for _ in 0 ..< 100 {
            dispatchGroup.enter()
            dispatch.async {
                print("Queue: \(dispatch.qos.qosClass)")
                closure(dispatch)
                dispatchGroup.leave()
            }
        }
    }
    
    func sample() {
        let closure1: (DispatchQueue)->() = { _ in x += 1 }
        let closure2: (DispatchQueue)->() = { _ in x -= 1 }
        async(dispatch: .global(qos: .userInitiated), closure: closure1) // result: x += 100
        async(dispatch: .global(qos: .utility), closure: closure1) // result: x += 100
        async(dispatch: .global(qos: .background), closure: closure2) // result: x -= 100
        async(dispatch: .global(qos: .default), closure: closure2) // result: x -= 100
    }
    
    sample()
    dispatchGroup.wait()
    print(x) // expected result x = 0
    

提交回复
热议问题