Some atomic functions from Clang in Swift
The atomic functions available in /usr/include/libkern/OSAtomic.h
are quite limiting in Swift, due to the type system. Furthermore, some simple things such as a synchronized load or a synchronized store are not immediately available. On top of that, they have now been deprecated.
Clang, on the other hand, has an implementation of the C11 atomic functions built-in. They're also available on Linux.
This project bridges a subset of Clang's C11 atomics support to Swift.
The following Swift types are implemented:
AtomicPointer
,AtomicMutablePointer
,AtomicRawPointer
,AtomicMutableRawPointer
andAtomicOpaquePointer
;AtomicInt
andAtomicUInt
,AtomicInt32
andAtomicUInt32
,AtomicInt64
andAtomicUInt64
;AtomicBool
The pointer types have the following methods:
load
,store
,swap
andCAS
The integer types have the following methods:
load
,store
,swap
,CAS
,add
,subtract
,increment
,decrement
,bitwiseAnd
,bitwiseOr
andbitwiseXor
The memory order (from <stdatomic.h>
) can be set by using the order
parameter on each method; the default is .relaxed
for the integer types (it is sufficient for counter operations, the most common application,) and .sequential
for pointer types.
The integer types also have a value
property, as a convenient way to perform a .relaxed
load. The pointer types have a pointer
property for the same purpose.
These types should be used as members of reference types, or captured by closures. They are implemented as struct
s so that using them does not automatically incur additional memory allocation. They should not be used members of another struct
, since that would result in violations the law-of-exclusivity.
This library requires Swift 3.0 or later. On Linux, it also requires Clang 3.6 or later.