AtomicOrdering
public enum AtomicOrdering : Comparable
AtomicOrdering
enumerates available memory ordering semantics.
Atomic instructions (cmpxchg
, atomicrmw
, fence
, atomic load
, and
atomic store
) take ordering parameters that determine which other atomic
instructions on the same address they synchronize with. These semantics are
borrowed from Java and C++0x, but are somewhat more colloquial. If these
descriptions aren’t precise enough, check those specs (see spec references
in the atomics guide). fence instructions treat these orderings somewhat
differently since they don’t take an address. See that instruction’s
documentation for details.
-
A load or store which is not atomic
Declaration
Swift
case notAtomic
-
Lowest level of atomicity, guarantees somewhat sane results, lock free.
The set of values that can be read is governed by the happens-before partial order. A value cannot be read unless some operation wrote it. This is intended to provide a guarantee strong enough to model Java’s non-volatile shared variables. This ordering cannot be specified for read-modify-write operations; it is not strong enough to make them atomic in any interesting way.
Declaration
Swift
case unordered
-
Guarantees that if you take all the operations affecting a specific address, a consistent ordering exists.
In addition to the guarantees of unordered, there is a single total order for modifications by monotonic operations on each address. All modification orders must be compatible with the happens-before order. There is no guarantee that the modification orders can be combined to a global total order for the whole program (and this often will not be possible). The read in an atomic read-modify-write operation (cmpxchg and atomicrmw) reads the value in the modification order immediately before the value it writes. If one atomic read happens before another atomic read of the same address, the later read must see the same value or a later value in the address’s modification order. This disallows reordering of monotonic (or stronger) operations on the same address. If an address is written monotonic-ally by one thread, and other threads monotonic-ally read that address repeatedly, the other threads must eventually see the write. This corresponds to the C++0x/C1x memory_order_relaxed.
Declaration
Swift
case monotonic
-
Acquire provides a barrier of the sort necessary to acquire a lock to access other memory with normal loads and stores.
In addition to the guarantees of monotonic, a synchronizes-with edge may be formed with a release operation. This is intended to model C++’s
memory_order_acquire
.Declaration
Swift
case acquire
-
Release is similar to Acquire, but with a barrier of the sort necessary to release a lock.
In addition to the guarantees of monotonic, if this operation writes a value which is subsequently read by an acquire operation, it synchronizes-with that operation. (This isn’t a complete description; see the C++0x definition of a release sequence.) This corresponds to the C++0x/C1x memory_order_release.
Declaration
Swift
case release
-
provides both an Acquire and a Release barrier (for fences and operations which both read and write memory).
This corresponds to the C++0x/C1x memory_order_acq_rel.
Declaration
Swift
case acquireRelease
-
Provides Acquire semantics for loads and Release semantics for stores.
In addition to the guarantees of acq_rel (acquire for an operation that only reads, release for an operation that only writes), there is a global total order on all sequentially-consistent operations on all addresses, which is consistent with the happens-before partial order and with the modification orders of all the affected addresses. Each sequentially-consistent read sees the last preceding write to the same address in this global order. This corresponds to the C++0x/C1x
memory_order_seq_cst
and Java volatile.Declaration
Swift
case sequentiallyConsistent
-
Returns whether the left atomic ordering is strictly weaker than the right atomic order.
Declaration
Swift
public static func < (lhs: AtomicOrdering, rhs: AtomicOrdering) -> Bool