Repository: apple/swift-nio
File: Sources/NIOConcurrencyHelpers/atomics.swift (Swift)

Code before:
    public static let atomic_sub = catmc_atomic_uintptr_t_sub
    public static let atomic_exchange = catmc_atomic_uintptr_t_exchange
    public static let atomic_load = catmc_atomic_uintptr_t_load
    public static let atomic_store = catmc_atomic_uintptr_t_store
}
#else
extension Int: AtomicPrimitive {
    public static let atomic_create = catmc_atomic_long_create
    public static let atomic_destroy = catmc_atomic_long_destroy
    public static let atomic_compare_and_exchange = catmc_atomic_long_compare_and_exchange
    public static let atomic_add = catmc_atomic_long_add
    public static let atomic_sub = catmc_atomic_long_sub
    public static let atomic_exchange = catmc_atomic_long_exchange
    public static let atomic_load = catmc_atomic_long_load
    public static let atomic_store = catmc_atomic_long_store
}

extension UInt: AtomicPrimitive {
    public static let atomic_create = catmc_atomic_unsigned_long_create
    public static let atomic_destroy = catmc_atomic_unsigned_long_destroy
    public static let atomic_compare_and_exchange = catmc_atomic_unsigned_long_compare_and_exchange
    public static let atomic_add = catmc_atomic_unsigned_long_add
    public static let atomic_sub = catmc_atomic_unsigned_long_sub
    public static let atomic_exchange = catmc_atomic_unsigned_long_exchange
    public static let atomic_load = catmc_atomic_unsigned_long_load
    public static let atomic_store = catmc_atomic_unsigned_long_store
}
#endif

/// `AtomicBox` is a heap-allocated box which allows lock-free access to an instance of a Swift class.
///
/// - warning: The use of `AtomicBox` should be avoided because it requires an implementation of a spin-lock
///            (more precisely a CAS loop) to operate correctly.
@available(
    *,
    deprecated,
    message: "AtomicBox is deprecated without replacement because the original implementation doesn't work."
)
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
public final class AtomicBox<T: AnyObject> {
    private let storage: NIOAtomic<UInt>

    public init(value: T) {
        let ptr = Unmanaged<T>.passRetained(value)
        self.storage = NIOAtomic.makeAtomic(value: UInt(bitPattern: ptr.toOpaque()))
    }

    deinit {
        let oldPtrBits = self.storage.exchange(with: 0xdeadbee)
        let oldPtr = Unmanaged<T>.fromOpaque(UnsafeRawPointer(bitPattern: oldPtrBits)!)
        oldPtr.release()

Review comment (suggestion):
This syntax is new to me, have you confirmed it does what you want?
```
@available(OpenBSD,
```

Code after:
    public static let atomic_load = catmc_atomic_long_load
    public static let atomic_store = catmc_atomic_long_store
}

extension UInt: AtomicPrimitive {
    public static let atomic_create = catmc_atomic_unsigned_long_create
    public static let atomic_destroy = catmc_atomic_unsigned_long_destroy
    public static let atomic_compare_and_exchange = catmc_atomic_unsigned_long_compare_and_exchange
    public static let atomic_add = catmc_atomic_unsigned_long_add
    public static let atomic_sub = catmc_atomic_unsigned_long_sub
    public static let atomic_exchange = catmc_atomic_unsigned_long_exchange
    public static let atomic_load = catmc_atomic_unsigned_long_load
    public static let atomic_store = catmc_atomic_unsigned_long_store
}
#endif

/// `AtomicBox` is a heap-allocated box which allows lock-free access to an instance of a Swift class.
///
/// - warning: The use of `AtomicBox` should be avoided because it requires an implementation of a spin-lock
///            (more precisely a CAS loop) to operate correctly.
@available(
    *,
    deprecated,
    message: "AtomicBox is deprecated without replacement because the original implementation doesn't work."
)
@available(OpenBSD, unavailable, message: "malloc_size is unavailable.")
public final class AtomicBox<T: AnyObject> {
    private let storage: NIOAtomic<UInt>

    public init(value: T) {
        let ptr = Unmanaged<T>.passRetained(value)
        self.storage = NIOAtomic.makeAtomic(value: UInt(bitPattern: ptr.toOpaque()))
    }

    deinit {
        let oldPtrBits = self.storage.exchange(with: 0xdeadbee)
        let oldPtr = Unmanaged<T>.fromOpaque(UnsafeRawPointer(bitPattern: oldPtrBits)!)
        oldPtr.release()
    }

    /// Atomically compares the value against `expected` and, if they are equal,
    /// replaces the value with `desired`.
    ///
    /// This implementation conforms to C11's `atomic_compare_exchange_strong`. This
    /// means that the compare-and-swap will always succeed if `expected` is equal to
    /// value. Additionally, it uses a *sequentially consistent ordering*. For more
    /// details on atomic memory models, check the documentation for C11's
    /// `stdatomic.h`.
    ///
    ///
    /// - warning: The implementation of `exchange` contains a _Compare and Exchange loop_, ie. it may busy wait with