     }
 
     private func _finish() {
-        // Make sure that we ignore if `finish` is called more than once.
-        if OSAtomicCompareAndSwap32Barrier(0, 1, isFinishCalled) {
-            isExecuting = false
-            isFinished = true
+        os_unfair_lock_lock(lock)
+        if isFinishCalled {
+            os_unfair_lock_unlock(lock)
+            return
         }
+        isFinishCalled = true
+        os_unfair_lock_unlock(lock)
+
+        isExecuting = false
+        isFinished = true
     }
 }
 

import Foundation

final class Operation: Foundation.Operation {
    private let _isExecuting: UnsafeMutablePointer<Int32>
    private let _isFinished: UnsafeMutablePointer<Int32>
    private let isFinishCalled: UnsafeMutablePointer<Int32>

    override var isExecuting: Bool {
        get { _isExecuting.pointee == 1 }
        set {
            guard OSAtomicCompareAndSwap32Barrier(newValue ? 0 : 1, newValue ? 1 : 0, _isExecuting) else {
                return assertionFailure("Invalid state, operation is already (not) executing")
            }
            willChangeValue(forKey: "isExecuting")
            didChangeValue(forKey: "isExecuting")
        }
    }
    override var isFinished: Bool {
        get { _isFinished.pointee == 1 }
        set {
            guard OSAtomicCompareAndSwap32Barrier(newValue ? 0 : 1, newValue ? 1 : 0, _isFinished) else {
                return assertionFailure("Invalid state, operation is already finished")
            }
            willChangeValue(forKey: "isFinished")
            didChangeValue(forKey: "isFinished")
        }

    init(starter: @escaping Starter) {
        self.starter = starter

        self.lock = .allocate(capacity: 1)
        self.lock.initialize(to: os_unfair_lock())

        #if TRACK_ALLOCATIONS
        Allocations.increment("Operation")
