diff --git a/Sources/CAtomics/include/CAtomics.h b/Sources/CAtomics/include/CAtomics.h index c202bbc..26a906e 100644 --- a/Sources/CAtomics/include/CAtomics.h +++ b/Sources/CAtomics/include/CAtomics.h @@ -134,18 +134,38 @@ SWIFT_ENUM(CASType, closed) parameterType CAtomics##opName(swiftType *_Nonnull atomic, parameterType pName, enum MemoryOrder order) \ { return atomic_##op##_explicit(&(atomic->a), pName, order); } +#define CLANG_ATOMICS_WEAK_CAS(swiftType, parameterType) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_weak_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \ + } + +#define CLANG_ATOMICS_STRONG_CAS(swiftType, parameterType) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_strong_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \ + } + #define CLANG_ATOMICS_CAS(swiftType, parameterType) \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ _Bool CAtomicsCompareAndExchange(swiftType *_Nonnull atomic, parameterType *_Nonnull current, parameterType future, \ enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ { \ - assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ - assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ if(type == __ATOMIC_CAS_TYPE_STRONG) \ - return atomic_compare_exchange_strong_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \ else \ - return atomic_compare_exchange_weak_explicit(&(atomic->a), current, future, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \ } \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ @@ -166,6 +186,8 @@ SWIFT_ENUM(CASType, closed) CLANG_ATOMICS_LOAD(swiftType, parameterType) \ CLANG_ATOMICS_STORE(swiftType, parameterType) \ CLANG_ATOMICS_SWAP(swiftType, parameterType) \ + CLANG_ATOMICS_STRONG_CAS(swiftType, parameterType) \ + CLANG_ATOMICS_WEAK_CAS(swiftType, parameterType) \ CLANG_ATOMICS_CAS(swiftType, parameterType) // macro to generate atomic struct + functions for integer types @@ -237,6 +259,30 @@ CLANG_ATOMICS_BOOL_GENERATE(AtomicBool, atomic_bool, _Bool, _Alignof(atomic_bool parameterType nullability CAtomicsExchange(swiftType *_Nonnull atomic, parameterType nullability value, enum MemoryOrder order) \ { return (parameterType) atomic_exchange_explicit(&(atomic->a), (uintptr_t)value, order); } +#define CLANG_ATOMICS_POINTER_WEAK_CAS(swiftType, parameterType, nullability) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, \ + parameterType nullability* _Nonnull current, parameterType nullability future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_weak_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \ + } + +#define CLANG_ATOMICS_POINTER_STRONG_CAS(swiftType, parameterType, nullability) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, \ + parameterType nullability* _Nonnull current, parameterType nullability future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_strong_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \ + } + #define CLANG_ATOMICS_POINTER_CAS(swiftType, parameterType, nullability) \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ @@ -244,12 +290,10 @@ CLANG_ATOMICS_BOOL_GENERATE(AtomicBool, atomic_bool, _Bool, _Alignof(atomic_bool parameterType nullability* _Nonnull current, parameterType nullability future, \ enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ { \ - assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ - assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ if(type == __ATOMIC_CAS_TYPE_STRONG) \ - return atomic_compare_exchange_strong_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \ else \ - return atomic_compare_exchange_weak_explicit(&(atomic->a), (uintptr_t*)current, (uintptr_t)future, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \ } \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ @@ -271,6 +315,8 @@ CLANG_ATOMICS_BOOL_GENERATE(AtomicBool, atomic_bool, _Bool, _Alignof(atomic_bool CLANG_ATOMICS_POINTER_LOAD(swiftType, parameterType, nullability) \ CLANG_ATOMICS_POINTER_STORE(swiftType, parameterType, nullability) \ CLANG_ATOMICS_POINTER_SWAP(swiftType, parameterType, nullability) \ + CLANG_ATOMICS_POINTER_STRONG_CAS(swiftType, parameterType, nullability) \ + CLANG_ATOMICS_POINTER_WEAK_CAS(swiftType, parameterType, nullability) \ CLANG_ATOMICS_POINTER_CAS(swiftType, parameterType, nullability) // generate atomic pointer types + functions @@ -345,18 +391,38 @@ CLANG_ATOMICS_POINTER_GENERATE(AtomicOptionalOpaquePointer, atomic_uintptr_t, st structType CAtomicsExchange(atomicType *_Nonnull atomic, structType value, enum MemoryOrder order) \ { structType rp; rp.tag_ptr = atomic_exchange_explicit(&(atomic->a), value.tag_ptr, order); return rp; } +#define CLANG_ATOMICS_TAGGED_POINTER_WEAK_CAS(swiftType, structType) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeWeak(swiftType *_Nonnull atomic, structType *_Nonnull current, structType future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_weak_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \ + } + +#define CLANG_ATOMICS_TAGGED_POINTER_STRONG_CAS(swiftType, structType) \ + static __inline__ __attribute__((__always_inline__)) \ + __attribute__((overloadable)) \ + _Bool CAtomicsCompareAndExchangeStrong(swiftType *_Nonnull atomic, structType *_Nonnull current, structType future, \ + enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ + { \ + assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ + assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ + return atomic_compare_exchange_strong_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \ + } + #define CLANG_ATOMICS_TAGGED_POINTER_CAS(atomicType, structType) \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ _Bool CAtomicsCompareAndExchange(atomicType *_Nonnull atomic, structType *_Nonnull current, structType future, \ enum CASType type, enum MemoryOrder orderSwap, enum LoadMemoryOrder orderLoad) \ { \ - assert((unsigned int)orderLoad <= (unsigned int)orderSwap); \ - assert(orderSwap == __ATOMIC_RELEASE ? orderLoad == __ATOMIC_RELAXED : true); \ if(type == __ATOMIC_CAS_TYPE_STRONG) \ - return atomic_compare_exchange_strong_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeStrong(atomic, current, future, orderSwap, orderLoad); \ else \ - return atomic_compare_exchange_weak_explicit(&(atomic->a), &(current->tag_ptr), future.tag_ptr, orderSwap, orderLoad); \ + return CAtomicsCompareAndExchangeWeak(atomic, current, future, orderSwap, orderLoad); \ } \ static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ @@ -387,6 +453,8 @@ CLANG_ATOMICS_POINTER_GENERATE(AtomicOptionalOpaquePointer, atomic_uintptr_t, st CLANG_ATOMICS_TAGGED_POINTER_LOAD(atomicType, structType) \ CLANG_ATOMICS_TAGGED_POINTER_STORE(atomicType, structType) \ CLANG_ATOMICS_TAGGED_POINTER_SWAP(atomicType, structType) \ + CLANG_ATOMICS_TAGGED_POINTER_STRONG_CAS(atomicType, structType) \ + CLANG_ATOMICS_TAGGED_POINTER_WEAK_CAS(atomicType, structType) \ CLANG_ATOMICS_TAGGED_POINTER_CAS(atomicType, structType) CLANG_ATOMICS_TAGGED_POINTER_GENERATE(TaggedRawPointer, const void*, _Nonnull) @@ -419,17 +487,23 @@ CLANG_ATOMICS_POINTER_SWAP(OpaqueUnmanagedHelper, const void*, _Nullable) // this should only be used for debugging and testing CLANG_ATOMICS_POINTER_LOAD(OpaqueUnmanagedHelper, const void*, _Nullable) +static __inline__ __attribute__((__always_inline__)) \ +__attribute__((overloadable)) \ +_Bool CAtomicsCompareAndExchangeStrong(OpaqueUnmanagedHelper *_Nonnull atomic, + const void *_Nullable current, const void *_Nullable future, + enum MemoryOrder order) +{ + uintptr_t pointer = (uintptr_t) current; + return atomic_compare_exchange_strong_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed); +} + static __inline__ __attribute__((__always_inline__)) \ __attribute__((overloadable)) \ _Bool CAtomicsCompareAndExchange(OpaqueUnmanagedHelper *_Nonnull atomic, const void *_Nullable current, const void *_Nullable future, enum CASType type, enum MemoryOrder order) { - uintptr_t pointer = (uintptr_t) current; - if(type == __ATOMIC_CAS_TYPE_WEAK) - return atomic_compare_exchange_weak_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed); - else - return atomic_compare_exchange_strong_explicit(&(atomic->a), &pointer, (uintptr_t)future, order, memory_order_relaxed); + return CAtomicsCompareAndExchangeStrong(atomic, current, future, order); } #endif diff --git a/Sources/SwiftAtomics/atomics-integer.swift b/Sources/SwiftAtomics/atomics-integer.swift index ef2e9f3..11455b0 100644 --- a/Sources/SwiftAtomics/atomics-integer.swift +++ b/Sources/SwiftAtomics/atomics-integer.swift @@ -22,2083 +22,1943 @@ extension AtomicInt @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Int { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ value: Int) { CAtomicsInitialize(&self, value) } -#else - @inline(__always) - public mutating func initialize(_ value: Int) - { - CAtomicsInitialize(&self, value) - } -#endif -#if swift(>=4.2) + @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> Int { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ value: Int, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#else - @inline(__always) - public mutating func store(_ value: Int, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#endif -#if swift(>=4.2) + @inlinable public mutating func swap(_ value: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsExchange(&self, value, order) } -#else - @inline(__always) - public mutating func swap(_ value: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsExchange(&self, value, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func add(_ delta: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsAdd(&self, delta, order) } -#else - @inline(__always) @discardableResult - public mutating func add(_ delta: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsAdd(&self, delta, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func subtract(_ delta: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsSubtract(&self, delta, order) } -#else - @inline(__always) @discardableResult - public mutating func subtract(_ delta: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsSubtract(&self, delta, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func bitwiseOr(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseOr(&self, bits, order) } -#else - @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func bitwiseXor(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseXor(&self, bits, order) } -#else - @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func bitwiseAnd(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseAnd(&self, bits, order) } -#else - @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: Int, order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func increment(order: MemoryOrder = .acqrel) -> Int { return CAtomicsAdd(&self, 1, order) } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsAdd(&self, 1, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func decrement(order: MemoryOrder = .acqrel) -> Int { return CAtomicsSubtract(&self, 1, order) } -#else - @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int - { - return CAtomicsSubtract(&self, 1, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout Int, future: Int, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Int, future: Int, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func CAS(current: Int, future: Int, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Int, future: Int, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif -} - -@_exported import struct CAtomics.AtomicUInt - -extension AtomicUInt -{ -#if swift(>=4.2) - public var value: UInt { - @inlinable - mutating get { return CAtomicsLoad(&self, .relaxed) } + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else - public var value: UInt { + public var value: Int { @inline(__always) mutating get { return CAtomicsLoad(&self, .relaxed) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: UInt) - { - CAtomicsInitialize(&self, value) - } -#else @inline(__always) - public mutating func initialize(_ value: UInt) + public mutating func initialize(_ value: Int) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt - { - return CAtomicsLoad(&self, order) - } -#else + @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ value: UInt, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else @inline(__always) - public mutating func store(_ value: UInt, order: StoreMemoryOrder = .release) + public mutating func store(_ value: Int, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsExchange(&self, value, order) - } -#else + @inline(__always) - public mutating func swap(_ value: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func swap(_ value: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func add(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsAdd(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func add(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func add(_ delta: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func subtract(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsSubtract(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func subtract(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func subtract(_ delta: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func bitwiseOr(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func bitwiseXor(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt + public mutating func bitwiseAnd(_ bits: Int, order: MemoryOrder = .acqrel) -> Int { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsAdd(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt + public mutating func increment(order: MemoryOrder = .acqrel) -> Int { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt - { - return CAtomicsSubtract(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout UInt, future: UInt, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult - public mutating func loadCAS(current: inout UInt, future: UInt, - type: CASType = .weak, + public mutating func loadCAS(current: inout Int, future: Int, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UInt, future: UInt, + @inline(__always) @discardableResult + public mutating func CAS(current: Int, future: Int, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: UInt, future: UInt, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } -@_exported import struct CAtomics.AtomicInt8 +@_exported import struct CAtomics.AtomicUInt -extension AtomicInt8 +extension AtomicUInt { #if swift(>=4.2) - public var value: Int8 { + public var value: UInt { @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Int8 { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable - public mutating func initialize(_ value: Int8) - { - CAtomicsInitialize(&self, value) - } -#else - @inline(__always) - public mutating func initialize(_ value: Int8) + public mutating func initialize(_ value: UInt) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int8 - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int8 + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) @inlinable - public mutating func store(_ value: Int8, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else - @inline(__always) - public mutating func store(_ value: Int8, order: StoreMemoryOrder = .release) + public mutating func store(_ value: UInt, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func swap(_ value: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsExchange(&self, value, order) - } -#else - @inline(__always) - public mutating func swap(_ value: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func swap(_ value: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func add(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsAdd(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func add(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func add(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func subtract(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsSubtract(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func subtract(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func subtract(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func bitwiseOr(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func bitwiseXor(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + public mutating func bitwiseAnd(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsAdd(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int8 + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int8 - { - return CAtomicsSubtract(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int8 + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func loadCAS(current: inout Int8, future: Int8, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Int8, future: Int8, - type: CASType = .weak, + public mutating func loadCAS(current: inout UInt, future: UInt, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func CAS(current: Int8, future: Int8, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Int8, future: Int8, + public mutating func CAS(current: UInt, future: UInt, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif -} - -@_exported import struct CAtomics.AtomicUInt8 - -extension AtomicUInt8 -{ -#if swift(>=4.2) - public var value: UInt8 { - @inlinable - mutating get { return CAtomicsLoad(&self, .relaxed) } + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else - public var value: UInt8 { + public var value: UInt { @inline(__always) mutating get { return CAtomicsLoad(&self, .relaxed) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: UInt8) - { - CAtomicsInitialize(&self, value) - } -#else @inline(__always) - public mutating func initialize(_ value: UInt8) + public mutating func initialize(_ value: UInt) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt8 - { - return CAtomicsLoad(&self, order) - } -#else + @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt8 + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ value: UInt8, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else @inline(__always) - public mutating func store(_ value: UInt8, order: StoreMemoryOrder = .release) + public mutating func store(_ value: UInt, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsExchange(&self, value, order) - } -#else + @inline(__always) - public mutating func swap(_ value: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + public mutating func swap(_ value: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func add(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsAdd(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func add(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + public mutating func add(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func subtract(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsSubtract(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func subtract(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + public mutating func subtract(_ delta: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + public mutating func bitwiseOr(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + public mutating func bitwiseXor(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: UInt, order: MemoryOrder = .acqrel) -> UInt { return CAtomicsBitwiseAnd(&self, bits, order) } -#else + @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#endif - -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsAdd(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt8 + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt8 - { - return CAtomicsSubtract(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt8 + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout UInt8, future: UInt8, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult - public mutating func loadCAS(current: inout UInt8, future: UInt8, - type: CASType = .weak, + public mutating func loadCAS(current: inout UInt, future: UInt, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UInt8, future: UInt8, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult - public mutating func CAS(current: UInt8, future: UInt8, + public mutating func CAS(current: UInt, future: UInt, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } -@_exported import struct CAtomics.AtomicInt16 +@_exported import struct CAtomics.AtomicInt8 -extension AtomicInt16 +extension AtomicInt8 { #if swift(>=4.2) - public var value: Int16 { + public var value: Int8 { @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Int16 { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable - public mutating func initialize(_ value: Int16) - { - CAtomicsInitialize(&self, value) - } -#else - @inline(__always) - public mutating func initialize(_ value: Int16) + public mutating func initialize(_ value: Int8) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int16 - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int16 + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int8 { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) @inlinable - public mutating func store(_ value: Int16, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else - @inline(__always) - public mutating func store(_ value: Int16, order: StoreMemoryOrder = .release) + public mutating func store(_ value: Int8, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func swap(_ value: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsExchange(&self, value, order) - } -#else - @inline(__always) - public mutating func swap(_ value: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func swap(_ value: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func add(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsAdd(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func add(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func add(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func subtract(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsSubtract(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func subtract(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func subtract(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func bitwiseOr(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func bitwiseXor(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + public mutating func bitwiseAnd(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsAdd(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int16 + public mutating func increment(order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int16 - { - return CAtomicsSubtract(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int16 + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func loadCAS(current: inout Int16, future: Int16, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Int16, future: Int16, - type: CASType = .weak, + public mutating func loadCAS(current: inout Int8, future: Int8, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func CAS(current: Int16, future: Int16, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Int16, future: Int16, + public mutating func CAS(current: Int8, future: Int8, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif -} - -@_exported import struct CAtomics.AtomicUInt16 - -extension AtomicUInt16 -{ -#if swift(>=4.2) - public var value: UInt16 { - @inlinable - mutating get { return CAtomicsLoad(&self, .relaxed) } + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else - public var value: UInt16 { + public var value: Int8 { @inline(__always) mutating get { return CAtomicsLoad(&self, .relaxed) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: UInt16) - { - CAtomicsInitialize(&self, value) - } -#else @inline(__always) - public mutating func initialize(_ value: UInt16) + public mutating func initialize(_ value: Int8) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt16 - { - return CAtomicsLoad(&self, order) - } -#else + @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt16 + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int8 { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ value: UInt16, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else @inline(__always) - public mutating func store(_ value: UInt16, order: StoreMemoryOrder = .release) + public mutating func store(_ value: Int8, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: UInt16, order: MemoryOrder = .acqrel) -> UInt16 - { - return CAtomicsExchange(&self, value, order) - } -#else + @inline(__always) - public mutating func swap(_ value: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func swap(_ value: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func add(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + @inline(__always) @discardableResult + public mutating func add(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 { return CAtomicsAdd(&self, delta, order) } -#else + + @inline(__always) @discardableResult + public mutating func subtract(_ delta: Int8, order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseXor(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: Int8, order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsAdd(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int8 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout Int8, future: Int8, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inline(__always) @discardableResult + public mutating func CAS(current: Int8, future: Int8, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#endif +} + +@_exported import struct CAtomics.AtomicUInt8 + +extension AtomicUInt8 +{ +#if swift(>=4.2) + public var value: UInt8 { + @inlinable + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inlinable + public mutating func initialize(_ value: UInt8) + { + CAtomicsInitialize(&self, value) + } + + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt8 + { + return CAtomicsLoad(&self, order) + } + + @inlinable + public mutating func store(_ value: UInt8, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inlinable + public mutating func swap(_ value: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsExchange(&self, value, order) + } + + @inlinable @discardableResult + public mutating func add(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsAdd(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func subtract(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func bitwiseOr(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseXor(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseAnd(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsAdd(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout UInt8, future: UInt8, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UInt8, future: UInt8, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#else + public var value: UInt8 { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: UInt8) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt8 + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: UInt8, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsExchange(&self, value, order) + } + + @inline(__always) @discardableResult + public mutating func add(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsAdd(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func subtract(_ delta: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseXor(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: UInt8, order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsAdd(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt8 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout UInt8, future: UInt8, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inline(__always) @discardableResult + public mutating func CAS(current: UInt8, future: UInt8, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#endif +} + +@_exported import struct CAtomics.AtomicInt16 + +extension AtomicInt16 +{ +#if swift(>=4.2) + public var value: Int16 { + @inlinable + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inlinable + public mutating func initialize(_ value: Int16) + { + CAtomicsInitialize(&self, value) + } + + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int16 + { + return CAtomicsLoad(&self, order) + } + + @inlinable + public mutating func store(_ value: Int16, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inlinable + public mutating func swap(_ value: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsExchange(&self, value, order) + } + + @inlinable @discardableResult + public mutating func add(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsAdd(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func subtract(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func bitwiseOr(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseXor(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseAnd(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsAdd(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout Int16, future: Int16, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: Int16, future: Int16, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#else + public var value: Int16 { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: Int16) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int16 + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: Int16, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsExchange(&self, value, order) + } + + @inline(__always) @discardableResult + public mutating func add(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsAdd(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func subtract(_ delta: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseXor(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: Int16, order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsAdd(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int16 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout Int16, future: Int16, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inline(__always) @discardableResult + public mutating func CAS(current: Int16, future: Int16, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#endif +} + +@_exported import struct CAtomics.AtomicUInt16 + +extension AtomicUInt16 +{ +#if swift(>=4.2) + public var value: UInt16 { + @inlinable + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inlinable + public mutating func initialize(_ value: UInt16) + { + CAtomicsInitialize(&self, value) + } + + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt16 + { + return CAtomicsLoad(&self, order) + } + + @inlinable + public mutating func store(_ value: UInt16, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inlinable + public mutating func swap(_ value: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsExchange(&self, value, order) + } + + @inlinable @discardableResult + public mutating func add(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsAdd(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func subtract(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func bitwiseOr(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseXor(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseAnd(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsAdd(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout UInt16, future: UInt16, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UInt16, future: UInt16, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#else + public var value: UInt16 { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: UInt16) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt16 + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: UInt16, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsExchange(&self, value, order) + } + @inline(__always) @discardableResult public mutating func add(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) + @inline(__always) @discardableResult + public mutating func subtract(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseXor(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsAdd(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt16 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout UInt16, future: UInt16, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inline(__always) @discardableResult + public mutating func CAS(current: UInt16, future: UInt16, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#endif +} + +@_exported import struct CAtomics.AtomicInt32 + +extension AtomicInt32 +{ +#if swift(>=4.2) + public var value: Int32 { + @inlinable + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inlinable + public mutating func initialize(_ value: Int32) + { + CAtomicsInitialize(&self, value) + } + + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int32 + { + return CAtomicsLoad(&self, order) + } + + @inlinable + public mutating func store(_ value: Int32, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inlinable + public mutating func swap(_ value: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsExchange(&self, value, order) + } + + @inlinable @discardableResult + public mutating func add(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsAdd(&self, delta, order) + } + @inlinable @discardableResult - public mutating func subtract(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func subtract(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 { return CAtomicsSubtract(&self, delta, order) } + + @inlinable @discardableResult + public mutating func bitwiseOr(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseXor(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseAnd(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsAdd(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout Int32, future: Int32, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: Int32, future: Int32, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var value: Int32 { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: Int32) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int32 + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: Int32, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsExchange(&self, value, order) + } + @inline(__always) @discardableResult - public mutating func subtract(_ delta: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func add(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsAdd(&self, delta, order) + } + + @inline(__always) @discardableResult + public mutating func subtract(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 { return CAtomicsSubtract(&self, delta, order) } + + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseOr(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseXor(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func bitwiseAnd(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inline(__always) @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsAdd(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int32 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout Int32, future: Int32, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inline(__always) @discardableResult + public mutating func CAS(current: Int32, future: Int32, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #endif +} +@_exported import struct CAtomics.AtomicUInt32 + +extension AtomicUInt32 +{ #if swift(>=4.2) + public var value: UInt32 { + @inlinable + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inlinable + public mutating func initialize(_ value: UInt32) + { + CAtomicsInitialize(&self, value) + } + + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt32 + { + return CAtomicsLoad(&self, order) + } + + @inlinable + public mutating func store(_ value: UInt32, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inlinable + public mutating func swap(_ value: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsExchange(&self, value, order) + } + @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func add(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsAdd(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func subtract(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsSubtract(&self, delta, order) + } + + @inlinable @discardableResult + public mutating func bitwiseOr(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 { return CAtomicsBitwiseOr(&self, bits, order) } -#else + + @inlinable @discardableResult + public mutating func bitwiseXor(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsBitwiseXor(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func bitwiseAnd(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsBitwiseAnd(&self, bits, order) + } + + @inlinable @discardableResult + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsAdd(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsSubtract(&self, 1, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout UInt32, future: UInt32, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UInt32, future: UInt32, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#else + public var value: UInt32 { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: UInt32) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt32 + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: UInt32, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsExchange(&self, value, order) + } + + @inline(__always) @discardableResult + public mutating func add(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + { + return CAtomicsAdd(&self, delta, order) + } + @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func subtract(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 { - return CAtomicsBitwiseOr(&self, bits, order) + return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + @inline(__always) @discardableResult + public mutating func bitwiseOr(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 { - return CAtomicsBitwiseXor(&self, bits, order) + return CAtomicsBitwiseOr(&self, bits, order) } -#else + @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func bitwiseXor(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: UInt16, order: MemoryOrder = .acqrel) -> UInt16 + public mutating func bitwiseAnd(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt16 - { - return CAtomicsAdd(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt16 + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt32 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt16 - { - return CAtomicsSubtract(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt16 + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt32 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout UInt16, future: UInt16, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult - public mutating func loadCAS(current: inout UInt16, future: UInt16, - type: CASType = .weak, + public mutating func loadCAS(current: inout UInt32, future: UInt32, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UInt16, future: UInt16, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult - public mutating func CAS(current: UInt16, future: UInt16, + public mutating func CAS(current: UInt32, future: UInt32, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } -@_exported import struct CAtomics.AtomicInt32 +@_exported import struct CAtomics.AtomicInt64 -extension AtomicInt32 +extension AtomicInt64 { #if swift(>=4.2) - public var value: Int32 { + public var value: Int64 { @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Int32 { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable - public mutating func initialize(_ value: Int32) - { - CAtomicsInitialize(&self, value) - } -#else - @inline(__always) - public mutating func initialize(_ value: Int32) + public mutating func initialize(_ value: Int64) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int32 - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int32 + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int64 { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) @inlinable - public mutating func store(_ value: Int32, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else - @inline(__always) - public mutating func store(_ value: Int32, order: StoreMemoryOrder = .release) + public mutating func store(_ value: Int64, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func swap(_ value: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsExchange(&self, value, order) - } -#else - @inline(__always) - public mutating func swap(_ value: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func swap(_ value: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func add(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsAdd(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func add(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func add(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func subtract(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsSubtract(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func subtract(_ delta: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func subtract(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func bitwiseOr(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func bitwiseXor(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: Int32, order: MemoryOrder = .acqrel) -> Int32 + public mutating func bitwiseAnd(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsAdd(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int32 + public mutating func increment(order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int32 - { - return CAtomicsSubtract(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int32 + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func loadCAS(current: inout Int32, future: Int32, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Int32, future: Int32, - type: CASType = .weak, + public mutating func loadCAS(current: inout Int64, future: Int64, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func CAS(current: Int32, future: Int32, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Int32, future: Int32, + public mutating func CAS(current: Int64, future: Int64, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif -} - -@_exported import struct CAtomics.AtomicUInt32 - -extension AtomicUInt32 -{ -#if swift(>=4.2) - public var value: UInt32 { - @inlinable - mutating get { return CAtomicsLoad(&self, .relaxed) } + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else - public var value: UInt32 { + public var value: Int64 { @inline(__always) mutating get { return CAtomicsLoad(&self, .relaxed) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: UInt32) - { - CAtomicsInitialize(&self, value) - } -#else @inline(__always) - public mutating func initialize(_ value: UInt32) + public mutating func initialize(_ value: Int64) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt32 - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt32 - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ value: UInt32, order: StoreMemoryOrder = .release) + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> Int64 { - CAtomicsStore(&self, value, order) + return CAtomicsLoad(&self, order) } -#else + @inline(__always) - public mutating func store(_ value: UInt32, order: StoreMemoryOrder = .release) + public mutating func store(_ value: Int64, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsExchange(&self, value, order) - } -#else + @inline(__always) - public mutating func swap(_ value: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func swap(_ value: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func add(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsAdd(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func add(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func add(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func subtract(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsSubtract(&self, delta, order) - } -#else @inline(__always) @discardableResult - public mutating func subtract(_ delta: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func subtract(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func bitwiseOr(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func bitwiseXor(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: UInt32, order: MemoryOrder = .acqrel) -> UInt32 + public mutating func bitwiseAnd(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsAdd(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt32 + public mutating func increment(order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt32 - { - return CAtomicsSubtract(&self, 1, order) - } -#else @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt32 + public mutating func decrement(order: MemoryOrder = .acqrel) -> Int64 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout UInt32, future: UInt32, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult - public mutating func loadCAS(current: inout UInt32, future: UInt32, - type: CASType = .weak, + public mutating func loadCAS(current: inout Int64, future: Int64, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UInt32, future: UInt32, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult - public mutating func CAS(current: UInt32, future: UInt32, + public mutating func CAS(current: Int64, future: Int64, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } -@_exported import struct CAtomics.AtomicInt64 +@_exported import struct CAtomics.AtomicUInt64 -extension AtomicInt64 +extension AtomicUInt64 { #if swift(>=4.2) - public var value: Int64 { + public var value: UInt64 { @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Int64 { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable - public mutating func initialize(_ value: Int64) - { - CAtomicsInitialize(&self, value) - } -#else - @inline(__always) - public mutating func initialize(_ value: Int64) + public mutating func initialize(_ value: UInt64) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int64 - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Int64 + public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt64 { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) @inlinable - public mutating func store(_ value: Int64, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else - @inline(__always) - public mutating func store(_ value: Int64, order: StoreMemoryOrder = .release) + public mutating func store(_ value: UInt64, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) + @inlinable - public mutating func swap(_ value: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsExchange(&self, value, order) - } -#else - @inline(__always) - public mutating func swap(_ value: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func swap(_ value: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func add(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsAdd(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func add(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func add(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func subtract(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsSubtract(&self, delta, order) - } -#else - @inline(__always) @discardableResult - public mutating func subtract(_ delta: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func subtract(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseOr(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func bitwiseOr(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseXor(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func bitwiseXor(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else - @inline(__always) @discardableResult - public mutating func bitwiseAnd(_ bits: Int64, order: MemoryOrder = .acqrel) -> Int64 + public mutating func bitwiseAnd(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsAdd(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> Int64 + public mutating func increment(order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int64 - { - return CAtomicsSubtract(&self, 1, order) - } -#else - @inline(__always) @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> Int64 + public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func loadCAS(current: inout Int64, future: Int64, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Int64, future: Int64, - type: CASType = .weak, + public mutating func loadCAS(current: inout UInt64, future: UInt64, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult - public mutating func CAS(current: Int64, future: Int64, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Int64, future: Int64, + public mutating func CAS(current: UInt64, future: UInt64, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif -} - -@_exported import struct CAtomics.AtomicUInt64 - -extension AtomicUInt64 -{ -#if swift(>=4.2) - public var value: UInt64 { - @inlinable - mutating get { return CAtomicsLoad(&self, .relaxed) } + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else public var value: UInt64 { @inline(__always) mutating get { return CAtomicsLoad(&self, .relaxed) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: UInt64) - { - CAtomicsInitialize(&self, value) - } -#else @inline(__always) public mutating func initialize(_ value: UInt64) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt64 - { - return CAtomicsLoad(&self, order) - } -#else + @inline(__always) public mutating func load(order: LoadMemoryOrder = .acquire) -> UInt64 { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ value: UInt64, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#else @inline(__always) public mutating func store(_ value: UInt64, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsExchange(&self, value, order) - } -#else + @inline(__always) public mutating func swap(_ value: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsExchange(&self, value, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func add(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsAdd(&self, delta, order) - } -#else @inline(__always) @discardableResult public mutating func add(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsAdd(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func subtract(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsSubtract(&self, delta, order) - } -#else @inline(__always) @discardableResult public mutating func subtract(_ delta: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsSubtract(&self, delta, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseOr(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsBitwiseOr(&self, bits, order) - } -#else @inline(__always) @discardableResult public mutating func bitwiseOr(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseOr(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseXor(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsBitwiseXor(&self, bits, order) - } -#else @inline(__always) @discardableResult public mutating func bitwiseXor(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseXor(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func bitwiseAnd(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsBitwiseAnd(&self, bits, order) - } -#else @inline(__always) @discardableResult public mutating func bitwiseAnd(_ bits: UInt64, order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsBitwiseAnd(&self, bits, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func increment(order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsAdd(&self, 1, order) - } -#else @inline(__always) @discardableResult public mutating func increment(order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsAdd(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt64 - { - return CAtomicsSubtract(&self, 1, order) - } -#else @inline(__always) @discardableResult public mutating func decrement(order: MemoryOrder = .acqrel) -> UInt64 { return CAtomicsSubtract(&self, 1, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout UInt64, future: UInt64, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult public mutating func loadCAS(current: inout UInt64, future: UInt64, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UInt64, future: UInt64, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UInt64, future: UInt64, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -2112,146 +1972,146 @@ extension AtomicBool @inlinable mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: Bool { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ value: Bool) { CAtomicsInitialize(&self, value) } -#else - @inline(__always) - public mutating func initialize(_ value: Bool) - { - CAtomicsInitialize(&self, value) - } -#endif -#if swift(>=4.2) + @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> Bool { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ value: Bool, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#else - @inline(__always) - public mutating func store(_ value: Bool, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#endif -#if swift(>=4.2) + @inlinable public mutating func swap(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool { return CAtomicsExchange(&self, value, order) } -#else - @inline(__always) - public mutating func swap(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsExchange(&self, value, order) - } -#endif -#if swift(>=4.2) + @inlinable @discardableResult public mutating func or(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool { return CAtomicsOr(&self, value, order) } -#else - @inline(__always) @discardableResult - public mutating func or(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsOr(&self, value, order) - } -#endif -#if swift(>=4.2) + @inlinable @discardableResult public mutating func xor(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool { return CAtomicsXor(&self, value, order) } -#else - @inline(__always) @discardableResult - public mutating func xor(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsXor(&self, value, order) - } -#endif -#if swift(>=4.2) + @inlinable @discardableResult public mutating func and(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool { return CAtomicsAnd(&self, value, order) } -#else - @inline(__always) @discardableResult - public mutating func and(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsAnd(&self, value, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout Bool, future: Bool, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Bool, future: Bool, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func CAS(current: Bool, future: Bool, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var value: Bool { + @inline(__always) + mutating get { return CAtomicsLoad(&self, .relaxed) } + } + + @inline(__always) + public mutating func initialize(_ value: Bool) + { + CAtomicsInitialize(&self, value) + } + + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> Bool + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ value: Bool, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, value, order) + } + + + @inline(__always) + public mutating func swap(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool + { + return CAtomicsExchange(&self, value, order) + } + + + @inline(__always) @discardableResult + public mutating func or(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool + { + return CAtomicsOr(&self, value, order) + } + + + @inline(__always) @discardableResult + public mutating func xor(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool + { + return CAtomicsXor(&self, value, order) + } + + + @inline(__always) @discardableResult + public mutating func and(_ value: Bool, order: MemoryOrder = .acqrel) -> Bool + { + return CAtomicsAnd(&self, value, order) + } + + @inline(__always) @discardableResult + public mutating func loadCAS(current: inout Bool, future: Bool, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + @inline(__always) @discardableResult public mutating func CAS(current: Bool, future: Bool, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } diff --git a/Sources/SwiftAtomics/atomics-integer.swift.gyb b/Sources/SwiftAtomics/atomics-integer.swift.gyb index 377201b..a2398d8 100644 --- a/Sources/SwiftAtomics/atomics-integer.swift.gyb +++ b/Sources/SwiftAtomics/atomics-integer.swift.gyb @@ -20,160 +20,89 @@ import CAtomics extension ${AtomicType} { #if swift(>=4.2) +% for inlinable in ['@inlinable', '@inline(__always)']: +% usableFromInline = '@usableFromInline' if inlinable == '@inlinable' else '@_versioned' +% end = '#else' if inlinable == '@inlinable' else '#endif' public var value: ${IntType} { - @inlinable + ${inlinable} mutating get { return CAtomicsLoad(&self, .relaxed) } } -#else - public var value: ${IntType} { - @inline(__always) - mutating get { return CAtomicsLoad(&self, .relaxed) } - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ value: ${IntType}) - { - CAtomicsInitialize(&self, value) - } -#else - @inline(__always) + ${inlinable} public mutating func initialize(_ value: ${IntType}) { CAtomicsInitialize(&self, value) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> ${IntType} - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) + + ${inlinable} public mutating func load(order: LoadMemoryOrder = .acquire) -> ${IntType} { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func store(_ value: ${IntType}, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, value, order) } -#else - @inline(__always) - public mutating func store(_ value: ${IntType}, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, value, order) - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ value: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} - { - return CAtomicsExchange(&self, value, order) - } -#else - @inline(__always) + + ${inlinable} public mutating func swap(_ value: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} { return CAtomicsExchange(&self, value, order) } -#endif % if IntType == 'Bool': % for (rmwMethod, rmwFunc, rmwParam) in [('or', 'Or', 'value'), ('xor', 'Xor', 'value'), ('and', 'And', 'value')]: -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func ${rmwMethod}(_ ${rmwParam}: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} - { - return CAtomics${rmwFunc}(&self, ${rmwParam}, order) - } -#else - @inline(__always) @discardableResult + + ${inlinable} @discardableResult public mutating func ${rmwMethod}(_ ${rmwParam}: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} { return CAtomics${rmwFunc}(&self, ${rmwParam}, order) } -#endif % end # for % else: % for (rmwMethod, rmwFunc, rmwParam) in [('add', 'Add', 'delta'), ('subtract', 'Subtract', 'delta'), ('bitwiseOr', 'BitwiseOr', 'bits'), ('bitwiseXor', 'BitwiseXor', 'bits'), ('bitwiseAnd', 'BitwiseAnd', 'bits')]: -#if swift(>=4.2) - @inlinable @discardableResult + ${inlinable} @discardableResult public mutating func ${rmwMethod}(_ ${rmwParam}: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} { return CAtomics${rmwFunc}(&self, ${rmwParam}, order) } -#else - @inline(__always) @discardableResult - public mutating func ${rmwMethod}(_ ${rmwParam}: ${IntType}, order: MemoryOrder = .acqrel) -> ${IntType} - { - return CAtomics${rmwFunc}(&self, ${rmwParam}, order) - } -#endif % end # for % for (inc, op) in [('increment', 'Add'), ('decrement', 'Subtract')]: -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func ${inc}(order: MemoryOrder = .acqrel) -> ${IntType} - { - return CAtomics${op}(&self, 1, order) - } -#else - @inline(__always) @discardableResult + ${inlinable} @discardableResult public mutating func ${inc}(order: MemoryOrder = .acqrel) -> ${IntType} { return CAtomics${op}(&self, 1, order) } -#endif % end # for % end # if IntType == 'Bool' -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout ${IntType}, future: ${IntType}, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult + ${inlinable} @discardableResult public mutating func loadCAS(current: inout ${IntType}, future: ${IntType}, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: ${IntType}, future: ${IntType}, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else - @inline(__always) @discardableResult + ${inlinable} @discardableResult public mutating func CAS(current: ${IntType}, future: ${IntType}, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } -#endif +${end} +% end # inlinable } % end # for AtomicType diff --git a/Sources/SwiftAtomics/atomics-orderings.swift b/Sources/SwiftAtomics/atomics-orderings.swift new file mode 100644 index 0000000..6143661 --- /dev/null +++ b/Sources/SwiftAtomics/atomics-orderings.swift @@ -0,0 +1,39 @@ +// +// atomics-orderings.swift +// +// +// Created by Guillaume Lessard on 4/4/20. +// + +import CAtomics + +extension MemoryOrder +{ +#if swift(>=4.2) + @usableFromInline + internal func asLoadOrdering() -> LoadMemoryOrder + { + switch self { + case .relaxed: return .relaxed + case .acquire: return .acquire + case .release: return .relaxed + case .acqrel : return .acquire + case .sequential: return .sequential + default: return LoadMemoryOrder(rawValue: rawValue)! + } + } +#else + @_versioned + internal func asLoadOrdering() -> LoadMemoryOrder + { + switch self { + case .relaxed: return .relaxed + case .acquire: return .acquire + case .release: return .relaxed + case .acqrel : return .acquire + case .sequential: return .sequential + default: return LoadMemoryOrder(rawValue: rawValue)! + } + } +#endif +} diff --git a/Sources/SwiftAtomics/atomics-pointer.swift b/Sources/SwiftAtomics/atomics-pointer.swift index b4f0199..40aeace 100644 --- a/Sources/SwiftAtomics/atomics-pointer.swift +++ b/Sources/SwiftAtomics/atomics-pointer.swift @@ -38,100 +38,98 @@ public struct AtomicPointer return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) } } -#else - public var pointer: UnsafePointer { - @inline(__always) - mutating get { - return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer { return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer - { - return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafePointer, order: StoreMemoryOrder = .release) { CAtomicsStore(&ptr, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafePointer, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&ptr, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafePointer, order: MemoryOrder = .acqrel) -> UnsafePointer { return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafePointer, order: MemoryOrder = .acqrel) -> UnsafePointer - { - return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafePointer, future: UnsafePointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c.assumingMemoryBound(to: Pointee.self) return s } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafePointer, future: UnsafePointer, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var pointer: UnsafePointer { + @inline(__always) + mutating get { + return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) + } + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer + { + return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafePointer, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&ptr, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafePointer, order: MemoryOrder = .acqrel) -> UnsafePointer + { + return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafePointer, future: UnsafePointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c.assumingMemoryBound(to: Pointee.self) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafePointer, future: UnsafePointer, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafePointer, future: UnsafePointer, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -161,100 +159,98 @@ public struct AtomicMutablePointer return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) } } -#else - public var pointer: UnsafeMutablePointer { - @inline(__always) - mutating get { - return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer { return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer - { - return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeMutablePointer, order: StoreMemoryOrder = .release) { CAtomicsStore(&ptr, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeMutablePointer, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&ptr, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeMutablePointer, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer { return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeMutablePointer, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer - { - return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeMutablePointer, future: UnsafeMutablePointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeMutableRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c.assumingMemoryBound(to: Pointee.self) return s } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeMutablePointer, future: UnsafeMutablePointer, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var pointer: UnsafeMutablePointer { + @inline(__always) + mutating get { + return CAtomicsLoad(&ptr, .acquire).assumingMemoryBound(to: Pointee.self) + } + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer + { + return CAtomicsLoad(&ptr, order).assumingMemoryBound(to: Pointee.self) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeMutablePointer, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&ptr, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeMutablePointer, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer + { + return CAtomicsExchange(&ptr, pointer, order).assumingMemoryBound(to: Pointee.self) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeMutablePointer, future: UnsafeMutablePointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeMutableRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c.assumingMemoryBound(to: Pointee.self) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeMutablePointer, future: UnsafeMutablePointer, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeMutablePointer, future: UnsafeMutablePointer, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -289,100 +285,98 @@ public struct AtomicOptionalPointer return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) } } -#else - public var pointer: UnsafePointer? { - @inline(__always) - mutating get { - return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer? { return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer? - { - return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafePointer?, order: StoreMemoryOrder = .release) { CAtomicsStore(&ptr, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafePointer?, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&ptr, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafePointer?, order: MemoryOrder = .acqrel) -> UnsafePointer? { return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafePointer?, order: MemoryOrder = .acqrel) -> UnsafePointer? - { - return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafePointer?, future: UnsafePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c?.assumingMemoryBound(to: Pointee.self) return s } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafePointer?, future: UnsafePointer?, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var pointer: UnsafePointer? { + @inline(__always) + mutating get { + return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) + } + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafePointer? + { + return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafePointer?, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&ptr, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafePointer?, order: MemoryOrder = .acqrel) -> UnsafePointer? + { + return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafePointer?, future: UnsafePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c?.assumingMemoryBound(to: Pointee.self) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafePointer?, future: UnsafePointer?, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafePointer?, future: UnsafePointer?, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -417,100 +411,98 @@ public struct AtomicOptionalMutablePointer return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) } } -#else - public var pointer: UnsafeMutablePointer? { - @inline(__always) - mutating get { - return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer? { return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer? - { - return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeMutablePointer?, order: StoreMemoryOrder = .release) { CAtomicsStore(&ptr, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeMutablePointer?, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&ptr, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeMutablePointer?, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer? { return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeMutablePointer?, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer? - { - return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeMutablePointer?, future: UnsafeMutablePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeMutableRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c?.assumingMemoryBound(to: Pointee.self) return s } -#else + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeMutablePointer?, future: UnsafeMutablePointer?, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } +#else + public var pointer: UnsafeMutablePointer? { + @inline(__always) + mutating get { + return CAtomicsLoad(&ptr, .acquire)?.assumingMemoryBound(to: Pointee.self) + } + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutablePointer? + { + return CAtomicsLoad(&ptr, order)?.assumingMemoryBound(to: Pointee.self) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeMutablePointer?, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&ptr, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeMutablePointer?, order: MemoryOrder = .acqrel) -> UnsafeMutablePointer? + { + return CAtomicsExchange(&ptr, pointer, order)?.assumingMemoryBound(to: Pointee.self) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeMutablePointer?, future: UnsafeMutablePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = UnsafeMutableRawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c?.assumingMemoryBound(to: Pointee.self) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeMutablePointer?, future: UnsafeMutablePointer?, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeMutablePointer?, future: UnsafeMutablePointer?, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -526,108 +518,104 @@ extension AtomicRawPointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: UnsafeRawPointer { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: UnsafeRawPointer) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: UnsafeRawPointer) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeRawPointer, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeRawPointer, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeRawPointer, order: MemoryOrder = .acqrel) -> UnsafeRawPointer { return CAtomicsExchange(&self, pointer, order) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeRawPointer, order: MemoryOrder = .acqrel) -> UnsafeRawPointer - { - return CAtomicsExchange(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeRawPointer, future: UnsafeRawPointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeRawPointer, future: UnsafeRawPointer, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var pointer: UnsafeRawPointer { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + + @inline(__always) + public mutating func initialize(_ pointer: UnsafeRawPointer) + { + CAtomicsInitialize(&self, pointer) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeRawPointer, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeRawPointer, order: MemoryOrder = .acqrel) -> UnsafeRawPointer + { + return CAtomicsExchange(&self, pointer, order) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeRawPointer, future: UnsafeRawPointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeRawPointer, future: UnsafeRawPointer, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeRawPointer, future: UnsafeRawPointer, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -643,108 +631,104 @@ extension AtomicOptionalRawPointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: UnsafeRawPointer? { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: UnsafeRawPointer?) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: UnsafeRawPointer?) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer? { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer? - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeRawPointer?, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeRawPointer?, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeRawPointer? { return CAtomicsExchange(&self, pointer, order) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeRawPointer? - { - return CAtomicsExchange(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeRawPointer?, future: UnsafeRawPointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeRawPointer?, future: UnsafeRawPointer?, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var pointer: UnsafeRawPointer? { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + + @inline(__always) + public mutating func initialize(_ pointer: UnsafeRawPointer?) + { + CAtomicsInitialize(&self, pointer) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeRawPointer? + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeRawPointer?, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeRawPointer? + { + return CAtomicsExchange(&self, pointer, order) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeRawPointer?, future: UnsafeRawPointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeRawPointer?, future: UnsafeRawPointer?, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeRawPointer?, future: UnsafeRawPointer?, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -760,108 +744,104 @@ extension AtomicMutableRawPointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: UnsafeMutableRawPointer { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: UnsafeMutableRawPointer) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: UnsafeMutableRawPointer) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeMutableRawPointer, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeMutableRawPointer, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeMutableRawPointer, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer { return CAtomicsExchange(&self, pointer, order) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeMutableRawPointer, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer - { - return CAtomicsExchange(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeMutableRawPointer, future: UnsafeMutableRawPointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeMutableRawPointer, future: UnsafeMutableRawPointer, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var pointer: UnsafeMutableRawPointer { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + + @inline(__always) + public mutating func initialize(_ pointer: UnsafeMutableRawPointer) + { + CAtomicsInitialize(&self, pointer) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeMutableRawPointer, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeMutableRawPointer, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer + { + return CAtomicsExchange(&self, pointer, order) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeMutableRawPointer, future: UnsafeMutableRawPointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeMutableRawPointer, future: UnsafeMutableRawPointer, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeMutableRawPointer, future: UnsafeMutableRawPointer, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -877,108 +857,104 @@ extension AtomicOptionalMutableRawPointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: UnsafeMutableRawPointer? { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: UnsafeMutableRawPointer?) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: UnsafeMutableRawPointer?) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer? { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer? - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: UnsafeMutableRawPointer?, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: UnsafeMutableRawPointer?, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: UnsafeMutableRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer? { return CAtomicsExchange(&self, pointer, order) } -#else - @inline(__always) - public mutating func swap(_ pointer: UnsafeMutableRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer? - { - return CAtomicsExchange(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout UnsafeMutableRawPointer?, future: UnsafeMutableRawPointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: UnsafeMutableRawPointer?, future: UnsafeMutableRawPointer?, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var pointer: UnsafeMutableRawPointer? { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + + @inline(__always) + public mutating func initialize(_ pointer: UnsafeMutableRawPointer?) + { + CAtomicsInitialize(&self, pointer) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> UnsafeMutableRawPointer? + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: UnsafeMutableRawPointer?, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: UnsafeMutableRawPointer?, order: MemoryOrder = .acqrel) -> UnsafeMutableRawPointer? + { + return CAtomicsExchange(&self, pointer, order) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout UnsafeMutableRawPointer?, future: UnsafeMutableRawPointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: UnsafeMutableRawPointer?, future: UnsafeMutableRawPointer?, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: UnsafeMutableRawPointer?, future: UnsafeMutableRawPointer?, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -994,108 +970,104 @@ extension AtomicOpaquePointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: OpaquePointer { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: OpaquePointer) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: OpaquePointer) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: OpaquePointer, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } + + @inlinable + public mutating func swap(_ pointer: OpaquePointer, order: MemoryOrder = .acqrel) -> OpaquePointer + { + return CAtomicsExchange(&self, pointer, order) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout OpaquePointer, + future: OpaquePointer, + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: OpaquePointer, future: OpaquePointer, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var pointer: OpaquePointer { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + @inline(__always) - public mutating func store(_ pointer: OpaquePointer, order: StoreMemoryOrder = .release) + public mutating func initialize(_ pointer: OpaquePointer) { - CAtomicsStore(&self, pointer, order) + CAtomicsInitialize(&self, pointer) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ pointer: OpaquePointer, order: MemoryOrder = .acqrel) -> OpaquePointer + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: OpaquePointer, order: StoreMemoryOrder = .release) { - return CAtomicsExchange(&self, pointer, order) + CAtomicsStore(&self, pointer, order) } -#else + @inline(__always) public mutating func swap(_ pointer: OpaquePointer, order: MemoryOrder = .acqrel) -> OpaquePointer { return CAtomicsExchange(&self, pointer, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout OpaquePointer, - future: OpaquePointer, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else @inline(__always) @discardableResult public mutating func loadCAS(current: inout OpaquePointer, future: OpaquePointer, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: OpaquePointer, future: OpaquePointer, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: OpaquePointer, future: OpaquePointer, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -1111,108 +1083,104 @@ extension AtomicOptionalOpaquePointer return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: OpaquePointer? { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ pointer: OpaquePointer?) { CAtomicsInitialize(&self, pointer) } -#else - @inline(__always) - public mutating func initialize(_ pointer: OpaquePointer?) - { - CAtomicsInitialize(&self, pointer) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer? { return CAtomicsLoad(&self, order) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer? - { - return CAtomicsLoad(&self, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ pointer: OpaquePointer?, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: OpaquePointer?, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ pointer: OpaquePointer?, order: MemoryOrder = .acqrel) -> OpaquePointer? { return CAtomicsExchange(&self, pointer, order) } -#else - @inline(__always) - public mutating func swap(_ pointer: OpaquePointer?, order: MemoryOrder = .acqrel) -> OpaquePointer? - { - return CAtomicsExchange(&self, pointer, order) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout OpaquePointer?, future: OpaquePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) + } + + @inlinable @discardableResult + public mutating func CAS(current: OpaquePointer?, future: OpaquePointer?, + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #else + public var pointer: OpaquePointer? { + @inline(__always) + mutating get { + return CAtomicsLoad(&self, .acquire) + } + } + + @inline(__always) + public mutating func initialize(_ pointer: OpaquePointer?) + { + CAtomicsInitialize(&self, pointer) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> OpaquePointer? + { + return CAtomicsLoad(&self, order) + } + + @inline(__always) + public mutating func store(_ pointer: OpaquePointer?, order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, pointer, order) + } + + @inline(__always) + public mutating func swap(_ pointer: OpaquePointer?, order: MemoryOrder = .acqrel) -> OpaquePointer? + { + return CAtomicsExchange(&self, pointer, order) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout OpaquePointer?, future: OpaquePointer?, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: OpaquePointer?, future: OpaquePointer?, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: OpaquePointer?, future: OpaquePointer?, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -1234,127 +1202,119 @@ extension AtomicTaggedRawPointer return (t.ptr, t.tag) } } -#else - public var value: (pointer: UnsafeRawPointer, tag: Int) { - @inline(__always) - mutating get { - let t = CAtomicsLoad(&self, .acquire) - return (t.ptr, t.tag) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ p: (pointer: UnsafeRawPointer, tag: Int)) { CAtomicsInitialize(&self, TaggedRawPointer(p.pointer, tag: p.tag)) } -#else - @inline(__always) - public mutating func initialize(_ p: (pointer: UnsafeRawPointer, tag: Int)) - { - CAtomicsInitialize(&self, TaggedRawPointer(p.pointer, tag: p.tag)) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer, tag: Int) { let t = CAtomicsLoad(&self, order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer, tag: Int) - { - let t = CAtomicsLoad(&self, order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ p: (pointer: UnsafeRawPointer, tag: Int), order: StoreMemoryOrder = .release) { CAtomicsStore(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) } -#else - @inline(__always) - public mutating func store(_ p: (pointer: UnsafeRawPointer, tag: Int), order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ p: (pointer: UnsafeRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer, tag: Int) { let t = CAtomicsExchange(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func swap(_ p: (pointer: UnsafeRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer, tag: Int) - { - let t = CAtomicsExchange(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeRawPointer, tag: Int), future: (pointer: UnsafeRawPointer, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedRawPointer(current.0, tag: current.1) let f = TaggedRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } + + @inlinable @discardableResult + public mutating func CAS(current: (pointer: UnsafeRawPointer, tag: Int), + future: (pointer: UnsafeRawPointer, tag: Int), + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var value: (pointer: UnsafeRawPointer, tag: Int) { + @inline(__always) + mutating get { + let t = CAtomicsLoad(&self, .acquire) + return (t.ptr, t.tag) + } + } + + @inline(__always) + public mutating func initialize(_ p: (pointer: UnsafeRawPointer, tag: Int)) + { + CAtomicsInitialize(&self, TaggedRawPointer(p.pointer, tag: p.tag)) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer, tag: Int) + { + let t = CAtomicsLoad(&self, order) + return (t.ptr, t.tag) + } + + @inline(__always) + public mutating func store(_ p: (pointer: UnsafeRawPointer, tag: Int), order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) + } + + @inline(__always) + public mutating func swap(_ p: (pointer: UnsafeRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer, tag: Int) + { + let t = CAtomicsExchange(&self, TaggedRawPointer(p.pointer, tag: p.tag), order) + return (t.ptr, t.tag) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeRawPointer, tag: Int), future: (pointer: UnsafeRawPointer, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedRawPointer(current.0, tag: current.1) let f = TaggedRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: (pointer: UnsafeRawPointer, tag: Int), - future: (pointer: UnsafeRawPointer, tag: Int), - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - let c = TaggedRawPointer(current.0, tag: current.1) - let f = TaggedRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: (pointer: UnsafeRawPointer, tag: Int), future: (pointer: UnsafeRawPointer, tag: Int), type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - let c = TaggedRawPointer(current.0, tag: current.1) - let f = TaggedRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -1376,6 +1336,59 @@ extension AtomicTaggedOptionalRawPointer return (t.ptr, t.tag) } } + + @inlinable + public mutating func initialize(_ p: (pointer: UnsafeRawPointer?, tag: Int)) + { + CAtomicsInitialize(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag)) + } + + @inlinable + public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer?, tag: Int) + { + let t = CAtomicsLoad(&self, order) + return (t.ptr, t.tag) + } + + @inlinable + public mutating func store(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) + } + + @inlinable + public mutating func swap(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer?, tag: Int) + { + let t = CAtomicsExchange(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) + return (t.ptr, t.tag) + } + + @inlinable @discardableResult + public mutating func loadCAS(current: inout (pointer: UnsafeRawPointer?, tag: Int), + future: (pointer: UnsafeRawPointer?, tag: Int), + type: CASType = .strong, + orderSwap: MemoryOrder = .acqrel, + orderLoad: LoadMemoryOrder = .acquire) -> Bool + { + var c = TaggedOptionalRawPointer(current.0, tag: current.1) + let f = TaggedOptionalRawPointer(future.0, tag: future.1) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) + current = (c.ptr, c.tag) + return s + } + + @inlinable @discardableResult + public mutating func CAS(current: (pointer: UnsafeRawPointer?, tag: Int), + future: (pointer: UnsafeRawPointer?, tag: Int), + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else public var value: (pointer: UnsafeRawPointer?, tag: Int) { @inline(__always) @@ -1384,119 +1397,58 @@ extension AtomicTaggedOptionalRawPointer return (t.ptr, t.tag) } } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ p: (pointer: UnsafeRawPointer?, tag: Int)) - { - CAtomicsInitialize(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag)) - } -#else @inline(__always) public mutating func initialize(_ p: (pointer: UnsafeRawPointer?, tag: Int)) { CAtomicsInitialize(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag)) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer?, tag: Int) - { - let t = CAtomicsLoad(&self, order) - return (t.ptr, t.tag) - } -#else @inline(__always) public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeRawPointer?, tag: Int) { let t = CAtomicsLoad(&self, order) return (t.ptr, t.tag) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) - } -#else @inline(__always) public mutating func store(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: StoreMemoryOrder = .release) { CAtomicsStore(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer?, tag: Int) - { - let t = CAtomicsExchange(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) - return (t.ptr, t.tag) - } -#else @inline(__always) public mutating func swap(_ p: (pointer: UnsafeRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeRawPointer?, tag: Int) { let t = CAtomicsExchange(&self, TaggedOptionalRawPointer(p.pointer, tag: p.tag), order) return (t.ptr, t.tag) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout (pointer: UnsafeRawPointer?, tag: Int), - future: (pointer: UnsafeRawPointer?, tag: Int), - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - var c = TaggedOptionalRawPointer(current.0, tag: current.1) - let f = TaggedOptionalRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) - current = (c.ptr, c.tag) - return s - } -#else @inline(__always) @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeRawPointer?, tag: Int), future: (pointer: UnsafeRawPointer?, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedOptionalRawPointer(current.0, tag: current.1) let f = TaggedOptionalRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: (pointer: UnsafeRawPointer?, tag: Int), - future: (pointer: UnsafeRawPointer?, tag: Int), - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - let c = TaggedOptionalRawPointer(current.0, tag: current.1) - let f = TaggedOptionalRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: (pointer: UnsafeRawPointer?, tag: Int), future: (pointer: UnsafeRawPointer?, tag: Int), type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - let c = TaggedOptionalRawPointer(current.0, tag: current.1) - let f = TaggedOptionalRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -1518,127 +1470,119 @@ extension AtomicTaggedMutableRawPointer return (t.ptr, t.tag) } } -#else - public var value: (pointer: UnsafeMutableRawPointer, tag: Int) { - @inline(__always) - mutating get { - let t = CAtomicsLoad(&self, .acquire) - return (t.ptr, t.tag) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer, tag: Int)) { CAtomicsInitialize(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag)) } -#else - @inline(__always) - public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer, tag: Int)) - { - CAtomicsInitialize(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag)) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer, tag: Int) { let t = CAtomicsLoad(&self, order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer, tag: Int) - { - let t = CAtomicsLoad(&self, order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: StoreMemoryOrder = .release) { CAtomicsStore(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) } -#else - @inline(__always) - public mutating func store(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer, tag: Int) { let t = CAtomicsExchange(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer, tag: Int) - { - let t = CAtomicsExchange(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeMutableRawPointer, tag: Int), future: (pointer: UnsafeMutableRawPointer, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedMutableRawPointer(current.0, tag: current.1) let f = TaggedMutableRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } + + @inlinable @discardableResult + public mutating func CAS(current: (pointer: UnsafeMutableRawPointer, tag: Int), + future: (pointer: UnsafeMutableRawPointer, tag: Int), + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var value: (pointer: UnsafeMutableRawPointer, tag: Int) { + @inline(__always) + mutating get { + let t = CAtomicsLoad(&self, .acquire) + return (t.ptr, t.tag) + } + } + + @inline(__always) + public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer, tag: Int)) + { + CAtomicsInitialize(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag)) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer, tag: Int) + { + let t = CAtomicsLoad(&self, order) + return (t.ptr, t.tag) + } + + @inline(__always) + public mutating func store(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) + } + + @inline(__always) + public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer, tag: Int) + { + let t = CAtomicsExchange(&self, TaggedMutableRawPointer(p.pointer, tag: p.tag), order) + return (t.ptr, t.tag) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeMutableRawPointer, tag: Int), future: (pointer: UnsafeMutableRawPointer, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedMutableRawPointer(current.0, tag: current.1) let f = TaggedMutableRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: (pointer: UnsafeMutableRawPointer, tag: Int), - future: (pointer: UnsafeMutableRawPointer, tag: Int), - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - let c = TaggedMutableRawPointer(current.0, tag: current.1) - let f = TaggedMutableRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: (pointer: UnsafeMutableRawPointer, tag: Int), future: (pointer: UnsafeMutableRawPointer, tag: Int), type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - let c = TaggedMutableRawPointer(current.0, tag: current.1) - let f = TaggedMutableRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } @@ -1660,127 +1604,119 @@ extension AtomicTaggedOptionalMutableRawPointer return (t.ptr, t.tag) } } -#else - public var value: (pointer: UnsafeMutableRawPointer?, tag: Int) { - @inline(__always) - mutating get { - let t = CAtomicsLoad(&self, .acquire) - return (t.ptr, t.tag) - } - } -#endif -#if swift(>=4.2) @inlinable public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int)) { CAtomicsInitialize(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag)) } -#else - @inline(__always) - public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int)) - { - CAtomicsInitialize(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag)) - } -#endif -#if swift(>=4.2) @inlinable public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer?, tag: Int) { let t = CAtomicsLoad(&self, order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer?, tag: Int) - { - let t = CAtomicsLoad(&self, order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable public mutating func store(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: StoreMemoryOrder = .release) { CAtomicsStore(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) } -#else - @inline(__always) - public mutating func store(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) - } -#endif -#if swift(>=4.2) @inlinable public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer?, tag: Int) { let t = CAtomicsExchange(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer?, tag: Int) - { - let t = CAtomicsExchange(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) @inlinable @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeMutableRawPointer?, tag: Int), future: (pointer: UnsafeMutableRawPointer?, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedOptionalMutableRawPointer(current.0, tag: current.1) let f = TaggedOptionalMutableRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } + + @inlinable @discardableResult + public mutating func CAS(current: (pointer: UnsafeMutableRawPointer?, tag: Int), + future: (pointer: UnsafeMutableRawPointer?, tag: Int), + type: CASType = .strong, + order: MemoryOrder = .acqrel) -> Bool + { + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) + } #else + public var value: (pointer: UnsafeMutableRawPointer?, tag: Int) { + @inline(__always) + mutating get { + let t = CAtomicsLoad(&self, .acquire) + return (t.ptr, t.tag) + } + } + + @inline(__always) + public mutating func initialize(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int)) + { + CAtomicsInitialize(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag)) + } + + @inline(__always) + public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: UnsafeMutableRawPointer?, tag: Int) + { + let t = CAtomicsLoad(&self, order) + return (t.ptr, t.tag) + } + + @inline(__always) + public mutating func store(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: StoreMemoryOrder = .release) + { + CAtomicsStore(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) + } + + @inline(__always) + public mutating func swap(_ p: (pointer: UnsafeMutableRawPointer?, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: UnsafeMutableRawPointer?, tag: Int) + { + let t = CAtomicsExchange(&self, TaggedOptionalMutableRawPointer(p.pointer, tag: p.tag), order) + return (t.ptr, t.tag) + } + @inline(__always) @discardableResult public mutating func loadCAS(current: inout (pointer: UnsafeMutableRawPointer?, tag: Int), future: (pointer: UnsafeMutableRawPointer?, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = TaggedOptionalMutableRawPointer(current.0, tag: current.1) let f = TaggedOptionalMutableRawPointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func CAS(current: (pointer: UnsafeMutableRawPointer?, tag: Int), - future: (pointer: UnsafeMutableRawPointer?, tag: Int), - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - let c = TaggedOptionalMutableRawPointer(current.0, tag: current.1) - let f = TaggedOptionalMutableRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) - } -#else @inline(__always) @discardableResult public mutating func CAS(current: (pointer: UnsafeMutableRawPointer?, tag: Int), future: (pointer: UnsafeMutableRawPointer?, tag: Int), type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - let c = TaggedOptionalMutableRawPointer(current.0, tag: current.1) - let f = TaggedOptionalMutableRawPointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } #endif } diff --git a/Sources/SwiftAtomics/atomics-pointer.swift.gyb b/Sources/SwiftAtomics/atomics-pointer.swift.gyb index b50bf64..b1e229a 100644 --- a/Sources/SwiftAtomics/atomics-pointer.swift.gyb +++ b/Sources/SwiftAtomics/atomics-pointer.swift.gyb @@ -20,10 +20,12 @@ import CAtomics public struct Atomic${Nullable}${Mutable}Pointer { #if swift(>=4.2) - @usableFromInline var ptr = Atomic${Nullable}${Mutable}RawPointer() -#else - @_versioned var ptr = Atomic${Nullable}${Mutable}RawPointer() -#endif +% for inlinable in ['@inlinable', '@inline(__always)']: +% usableFromInline = '@usableFromInline' if inlinable == '@inlinable' else '@_versioned' +% end = '#else' if inlinable == '@inlinable' else '#endif' + ${usableFromInline} var ptr = Atomic${Nullable}${Mutable}RawPointer() +${end} +% end # inlinable % if Nullable == 'Optional': public init() @@ -43,108 +45,60 @@ public struct Atomic${Nullable}${Mutable}Pointer } #if swift(>=4.2) +% for inlinable in ['@inlinable', '@inline(__always)']: +% usableFromInline = '@usableFromInline' if inlinable == '@inlinable' else '@_versioned' +% end = '#else' if inlinable == '@inlinable' else '#endif' public var pointer: Unsafe${Mutable}${Pointer} { - @inlinable + ${inlinable} mutating get { return CAtomicsLoad(&ptr, .acquire)${optional}.assumingMemoryBound(to: Pointee.self) } } -#else - public var pointer: Unsafe${Mutable}${Pointer} { - @inline(__always) - mutating get { - return CAtomicsLoad(&ptr, .acquire)${optional}.assumingMemoryBound(to: Pointee.self) - } - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> Unsafe${Mutable}${Pointer} - { - return CAtomicsLoad(&ptr, order)${optional}.assumingMemoryBound(to: Pointee.self) - } -#else - @inline(__always) + ${inlinable} public mutating func load(order: LoadMemoryOrder = .acquire) -> Unsafe${Mutable}${Pointer} { return CAtomicsLoad(&ptr, order)${optional}.assumingMemoryBound(to: Pointee.self) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func store(_ pointer: Unsafe${Mutable}${Pointer}, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&ptr, pointer, order) - } -#else - @inline(__always) + ${inlinable} public mutating func store(_ pointer: Unsafe${Mutable}${Pointer}, order: StoreMemoryOrder = .release) { CAtomicsStore(&ptr, pointer, order) } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func swap(_ pointer: Unsafe${Mutable}${Pointer}, order: MemoryOrder = .acqrel) -> Unsafe${Mutable}${Pointer} { return CAtomicsExchange(&ptr, pointer, order)${optional}.assumingMemoryBound(to: Pointee.self) } -#else - @inline(__always) - public mutating func swap(_ pointer: Unsafe${Mutable}${Pointer}, order: MemoryOrder = .acqrel) -> Unsafe${Mutable}${Pointer} - { - return CAtomicsExchange(&ptr, pointer, order)${optional}.assumingMemoryBound(to: Pointee.self) - } -#endif -#if swift(>=4.2) - @inlinable @discardableResult + ${inlinable} @discardableResult public mutating func loadCAS(current: inout Unsafe${Mutable}${Pointer}, future: Unsafe${Mutable}${Pointer}, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = Unsafe${Mutable}RawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&ptr, &c, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&ptr, &c, future, orderSwap, orderLoad) current = c${optional}.assumingMemoryBound(to: Pointee.self) return s } -#else - @inline(__always) @discardableResult - public mutating func loadCAS(current: inout Unsafe${Mutable}${Pointer}, - future: Unsafe${Mutable}${Pointer}, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - var c = Unsafe${Mutable}RawPointer(current) - let s = CAtomicsCompareAndExchange(&ptr, &c, future, type, orderSwap, orderLoad) - current = c${optional}.assumingMemoryBound(to: Pointee.self) - return s - } -#endif -#if swift(>=4.2) - @inlinable @discardableResult + ${inlinable} @discardableResult public mutating func CAS(current: Unsafe${Mutable}${Pointer}, future: Unsafe${Mutable}${Pointer}, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: Unsafe${Mutable}${Pointer}, future: Unsafe${Mutable}${Pointer}, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&ptr, current, future, type, order) - } -#endif +${end} +% end # inlinable } % end % end @@ -158,116 +112,63 @@ public struct Atomic${Nullable}${Mutable}Pointer extension Atomic${optional}${type}Pointer { #if swift(>=4.2) +% for inlinable in ['@inlinable', '@inline(__always)']: +% usableFromInline = '@usableFromInline' if inlinable == '@inlinable' else '@_versioned' +% end = '#else' if inlinable == '@inlinable' else '#endif' public var pointer: ${prefix}${type}Pointer${opt} { - @inlinable + ${inlinable} mutating get { return CAtomicsLoad(&self, .acquire) } } -#else - public var pointer: ${prefix}${type}Pointer${opt} { - @inline(__always) - mutating get { - return CAtomicsLoad(&self, .acquire) - } - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ pointer: ${prefix}${type}Pointer${opt}) - { - CAtomicsInitialize(&self, pointer) - } -#else - @inline(__always) + ${inlinable} public mutating func initialize(_ pointer: ${prefix}${type}Pointer${opt}) { CAtomicsInitialize(&self, pointer) } -#endif -#if swift(>=4.2) - @inlinable - public mutating func load(order: LoadMemoryOrder = .acquire) -> ${prefix}${type}Pointer${opt} - { - return CAtomicsLoad(&self, order) - } -#else - @inline(__always) + ${inlinable} public mutating func load(order: LoadMemoryOrder = .acquire) -> ${prefix}${type}Pointer${opt} { return CAtomicsLoad(&self, order) } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func store(_ pointer: ${prefix}${type}Pointer${opt}, order: StoreMemoryOrder = .release) { CAtomicsStore(&self, pointer, order) } -#else - @inline(__always) - public mutating func store(_ pointer: ${prefix}${type}Pointer${opt}, order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, pointer, order) - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func swap(_ pointer: ${prefix}${type}Pointer${opt}, order: MemoryOrder = .acqrel) -> ${prefix}${type}Pointer${opt} - { - return CAtomicsExchange(&self, pointer, order) - } -#else - @inline(__always) + ${inlinable} public mutating func swap(_ pointer: ${prefix}${type}Pointer${opt}, order: MemoryOrder = .acqrel) -> ${prefix}${type}Pointer${opt} { return CAtomicsExchange(&self, pointer, order) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout ${prefix}${type}Pointer${opt}, - future: ${prefix}${type}Pointer${opt}, - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) - } -#else - @inline(__always) @discardableResult + ${inlinable} @discardableResult public mutating func loadCAS(current: inout ${prefix}${type}Pointer${opt}, future: ${prefix}${type}Pointer${opt}, - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { - return CAtomicsCompareAndExchange(&self, ¤t, future, type, orderSwap, orderLoad) + return type == .weak + ? CAtomicsCompareAndExchangeWeak(&self, ¤t, future, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, ¤t, future, orderSwap, orderLoad) } -#endif -#if swift(>=4.2) - @inlinable @discardableResult + ${inlinable} @discardableResult public mutating func CAS(current: ${prefix}${type}Pointer${opt}, future: ${prefix}${type}Pointer${opt}, type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - return CAtomicsCompareAndExchange(&self, current, future, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: ${prefix}${type}Pointer${opt}, future: ${prefix}${type}Pointer${opt}, - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - return CAtomicsCompareAndExchange(&self, current, future, type, order) - } -#endif +${end} +% end # inlinable } % end % end @@ -285,136 +186,71 @@ extension AtomicTagged${optional}${type}Pointer } #if swift(>=4.2) +% for inlinable in ['@inlinable', '@inline(__always)']: +% usableFromInline = '@usableFromInline' if inlinable == '@inlinable' else '@_versioned' +% end = '#else' if inlinable == '@inlinable' else '#endif' public var value: (pointer: Unsafe${type}Pointer${opt}, tag: Int) { - @inlinable + ${inlinable} mutating get { let t = CAtomicsLoad(&self, .acquire) return (t.ptr, t.tag) } } -#else - public var value: (pointer: Unsafe${type}Pointer${opt}, tag: Int) { - @inline(__always) - mutating get { - let t = CAtomicsLoad(&self, .acquire) - return (t.ptr, t.tag) - } - } -#endif -#if swift(>=4.2) - @inlinable - public mutating func initialize(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int)) - { - CAtomicsInitialize(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag)) - } -#else - @inline(__always) + ${inlinable} public mutating func initialize(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int)) { CAtomicsInitialize(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag)) } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: Unsafe${type}Pointer${opt}, tag: Int) { let t = CAtomicsLoad(&self, order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func load(order: LoadMemoryOrder = .acquire) -> (pointer: Unsafe${type}Pointer${opt}, tag: Int) - { - let t = CAtomicsLoad(&self, order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func store(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int), order: StoreMemoryOrder = .release) { CAtomicsStore(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag), order) } -#else - @inline(__always) - public mutating func store(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int), order: StoreMemoryOrder = .release) - { - CAtomicsStore(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag), order) - } -#endif -#if swift(>=4.2) - @inlinable + ${inlinable} public mutating func swap(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: Unsafe${type}Pointer${opt}, tag: Int) { let t = CAtomicsExchange(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag), order) return (t.ptr, t.tag) } -#else - @inline(__always) - public mutating func swap(_ p: (pointer: Unsafe${type}Pointer${opt}, tag: Int), order: MemoryOrder = .acqrel) -> (pointer: Unsafe${type}Pointer${opt}, tag: Int) - { - let t = CAtomicsExchange(&self, Tagged${optional}${type}Pointer(p.pointer, tag: p.tag), order) - return (t.ptr, t.tag) - } -#endif -#if swift(>=4.2) - @inlinable @discardableResult - public mutating func loadCAS(current: inout (pointer: Unsafe${type}Pointer${opt}, tag: Int), - future: (pointer: Unsafe${type}Pointer${opt}, tag: Int), - type: CASType = .weak, - orderSwap: MemoryOrder = .acqrel, - orderLoad: LoadMemoryOrder = .acquire) -> Bool - { - var c = Tagged${optional}${type}Pointer(current.0, tag: current.1) - let f = Tagged${optional}${type}Pointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) - current = (c.ptr, c.tag) - return s - } -#else - @inline(__always) @discardableResult + ${inlinable} @discardableResult public mutating func loadCAS(current: inout (pointer: Unsafe${type}Pointer${opt}, tag: Int), future: (pointer: Unsafe${type}Pointer${opt}, tag: Int), - type: CASType = .weak, + type: CASType = .strong, orderSwap: MemoryOrder = .acqrel, orderLoad: LoadMemoryOrder = .acquire) -> Bool { var c = Tagged${optional}${type}Pointer(current.0, tag: current.1) let f = Tagged${optional}${type}Pointer(future.0, tag: future.1) - let s = CAtomicsCompareAndExchange(&self, &c, f, type, orderSwap, orderLoad) + let s = (type == .weak) + ? CAtomicsCompareAndExchangeWeak(&self, &c, f, orderSwap, orderLoad) + : CAtomicsCompareAndExchangeStrong(&self, &c, f, orderSwap, orderLoad) current = (c.ptr, c.tag) return s } -#endif -#if swift(>=4.2) - @inlinable @discardableResult + ${inlinable} @discardableResult public mutating func CAS(current: (pointer: Unsafe${type}Pointer${opt}, tag: Int), future: (pointer: Unsafe${type}Pointer${opt}, tag: Int), type: CASType = .strong, order: MemoryOrder = .acqrel) -> Bool { - let c = Tagged${optional}${type}Pointer(current.0, tag: current.1) - let f = Tagged${optional}${type}Pointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) - } -#else - @inline(__always) @discardableResult - public mutating func CAS(current: (pointer: Unsafe${type}Pointer${opt}, tag: Int), - future: (pointer: Unsafe${type}Pointer${opt}, tag: Int), - type: CASType = .strong, - order: MemoryOrder = .acqrel) -> Bool - { - let c = Tagged${optional}${type}Pointer(current.0, tag: current.1) - let f = Tagged${optional}${type}Pointer(future.0, tag: future.1) - return CAtomicsCompareAndExchange(&self, c, f, type, order) + var current = current + return loadCAS(current: ¤t, future: future, type: type, + orderSwap: order, orderLoad: order.asLoadOrdering()) } -#endif +${end} +% end # inlinable } % end diff --git a/Sources/SwiftAtomics/atomics-reference.swift b/Sources/SwiftAtomics/atomics-reference.swift index 6f66ddc..64fcf87 100644 --- a/Sources/SwiftAtomics/atomics-reference.swift +++ b/Sources/SwiftAtomics/atomics-reference.swift @@ -148,7 +148,7 @@ extension AtomicReference let c = current.map(Unmanaged.passUnretained) let f = future.map(Unmanaged.passRetained) - if CAtomicsCompareAndExchange(&ptr, c?.toOpaque(), f?.toOpaque(), .strong, order) + if CAtomicsCompareAndExchangeStrong(&ptr, c?.toOpaque(), f?.toOpaque(), order) { c?.release() return true @@ -165,7 +165,7 @@ extension AtomicReference let c = current.map(Unmanaged.passUnretained) let f = future.map(Unmanaged.passRetained) - if CAtomicsCompareAndExchange(&ptr, c?.toOpaque(), f?.toOpaque(), .strong, order) + if CAtomicsCompareAndExchangeStrong(&ptr, c?.toOpaque(), f?.toOpaque(), order) { c?.release() return true diff --git a/Tests/CAtomicsTests/CAtomicsTests.swift b/Tests/CAtomicsTests/CAtomicsTests.swift index caa08f9..6acfae9 100644 --- a/Tests/CAtomicsTests/CAtomicsTests.swift +++ b/Tests/CAtomicsTests/CAtomicsTests.swift @@ -61,12 +61,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -120,12 +120,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -179,12 +179,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -238,12 +238,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -297,12 +297,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -356,12 +356,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -415,12 +415,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -474,12 +474,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -533,12 +533,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -592,12 +592,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -642,12 +642,13 @@ public class CAtomicsBasicTests: XCTestCase var current = true XCTAssertEqual(CAtomicsLoad(&b, .relaxed), current) - CAtomicsCompareAndExchange(&b, ¤t, false, .strong, .relaxed, .relaxed) + CAtomicsCompareAndExchangeStrong(&b, ¤t, false, .relaxed, .relaxed) XCTAssertEqual(CAtomicsLoad(&b, .relaxed), false) - XCTAssertEqual(CAtomicsCompareAndExchange(&b, false, true, .strong, .relaxed), true) - while !CAtomicsCompareAndExchange(&b, ¤t, false, .weak, .relaxed, .relaxed) {} - while !CAtomicsCompareAndExchange(&b, !current, true, .weak, .relaxed) {} + current = false + XCTAssertEqual(CAtomicsCompareAndExchangeStrong(&b, ¤t, true, .relaxed, .relaxed), true) + while !CAtomicsCompareAndExchangeWeak(&b, ¤t, false, .relaxed, .relaxed) {} + while !CAtomicsCompareAndExchangeWeak(&b, ¤t, true, .relaxed, .relaxed) {} } } @@ -674,14 +675,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -707,14 +710,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -740,14 +745,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -773,14 +780,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -806,14 +815,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -839,14 +850,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -947,14 +960,15 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -1011,14 +1025,15 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -1075,14 +1090,15 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -1139,14 +1155,15 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } diff --git a/Tests/CAtomicsTests/CAtomicsTests.swift.gyb b/Tests/CAtomicsTests/CAtomicsTests.swift.gyb index 9aeeb4b..0c59a2a 100644 --- a/Tests/CAtomicsTests/CAtomicsTests.swift.gyb +++ b/Tests/CAtomicsTests/CAtomicsTests.swift.gyb @@ -64,12 +64,12 @@ public class CAtomicsBasicTests: XCTestCase j = r1 CAtomicsStore(&i, r1, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&i, &j, r2, .strong, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&i, &j, r2, .relaxed, .relaxed)) XCTAssertEqual(r2, CAtomicsLoad(&i, .relaxed)) j = r2 CAtomicsStore(&i, r1, .relaxed) - while(!CAtomicsCompareAndExchange(&i, &j, r3, .weak, .relaxed, .relaxed)) {} + while(!CAtomicsCompareAndExchangeWeak(&i, &j, r3, .relaxed, .relaxed)) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&i, .relaxed)) } @@ -115,12 +115,13 @@ public class CAtomicsBasicTests: XCTestCase var current = true XCTAssertEqual(CAtomicsLoad(&b, .relaxed), current) - CAtomicsCompareAndExchange(&b, ¤t, false, .strong, .relaxed, .relaxed) + CAtomicsCompareAndExchangeStrong(&b, ¤t, false, .relaxed, .relaxed) XCTAssertEqual(CAtomicsLoad(&b, .relaxed), false) - XCTAssertEqual(CAtomicsCompareAndExchange(&b, false, true, .strong, .relaxed), true) - while !CAtomicsCompareAndExchange(&b, ¤t, false, .weak, .relaxed, .relaxed) {} - while !CAtomicsCompareAndExchange(&b, !current, true, .weak, .relaxed) {} + current = false + XCTAssertEqual(CAtomicsCompareAndExchangeStrong(&b, ¤t, true, .relaxed, .relaxed), true) + while !CAtomicsCompareAndExchangeWeak(&b, ¤t, false, .relaxed, .relaxed) {} + while !CAtomicsCompareAndExchangeWeak(&b, ¤t, true, .relaxed, .relaxed) {} } } @@ -153,14 +154,16 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + j = r3 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) } @@ -264,14 +267,15 @@ extension CAtomicsBasicTests XCTAssertEqual(r1, j) XCTAssertEqual(r2, CAtomicsLoad(&p, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r3, .strong, .relaxed)) + j = r2 + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r3, .relaxed, .relaxed)) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) - XCTAssertFalse(CAtomicsCompareAndExchange(&p, j, r2, .strong, .relaxed)) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r3, r2, .strong, .relaxed)) + XCTAssertFalse(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r2, .relaxed, .relaxed)) j = CAtomicsLoad(&p, .relaxed) - XCTAssertTrue(CAtomicsCompareAndExchange(&p, r2, r1, .strong, .relaxed)) - while !CAtomicsCompareAndExchange(&p, &j, r3, .weak, .relaxed, .relaxed) {} + XCTAssertTrue(CAtomicsCompareAndExchangeStrong(&p, &j, r1, .relaxed, .relaxed)) + while !CAtomicsCompareAndExchangeWeak(&p, &j, r3, .relaxed, .relaxed) {} XCTAssertEqual(r1, j) XCTAssertEqual(r3, CAtomicsLoad(&p, .relaxed)) }