![]() |
![]()
| ![]() |
![]()
NAME
SYNOPSIS
void
void
int
int
<type>
void
<type>
<type>
void
void
void
<type>
int
int
void
DESCRIPTIONAtomic operations are commonly used to implement reference counts and as building blocks for synchronization primitives, such as mutexes. All of these operations are performed atomically across multiple threads and in the presence of interrupts, meaning that they are performed in an indivisible manner from the perspective of concurrently running threads and interrupt handlers. On all architectures supported by FreeBSD, ordinary loads and stores of integers in cache-coherent memory are inherently atomic if the integer is naturally aligned and its size does not exceed the processor's word size. However, such loads and stores may be elided from the program by the compiler, whereas atomic operations are always performed. When atomic operations are performed on cache-coherent memory, all operations on the same location are totally ordered. When an atomic load is performed on a location in cache-coherent memory, it reads the entire value that was defined by the last atomic store to each byte of the location. An atomic load will never return a value out of thin air. When an atomic store is performed on a location, no other thread or interrupt handler will observe a torn write, or partial modification of the location. Except as noted below, the semantics of these operations are almost identical to the semantics of similarly named C11 atomic operations. TypesMost atomic operations act upon a specific type. That type is indicated in the function name. In contrast to C11 atomic operations, FreeBSD's atomic operations are performed on ordinary integer types. The available types are: For example, the function to atomically add
two integers is called
Certain architectures also provide operations for types smaller
than “ These types must not be used in machine-independent code. Acquire and Release OperationsBy default, a thread's accesses to different memory locations might not be performed in program order, that is, the order in which the accesses appear in the source code. To optimize the program's execution, both the compiler and processor might reorder the thread's accesses. However, both ensure that their reordering of the accesses is not visible to the thread. Otherwise, the traditional memory model that is expected by single-threaded programs would be violated. Nonetheless, other threads in a multithreaded program, such as the FreeBSD kernel, might observe the reordering. Moreover, in some cases, such as the implementation of synchronization between threads, arbitrary reordering might result in the incorrect execution of the program. To constrain the reordering that both the compiler and processor might perform on a thread's accesses, a programmer can use atomic operations with acquire and release semantics. Atomic operations on memory have up to three variants. The first, or relaxed variant, performs the operation without imposing any ordering constraints on accesses to other memory locations. This variant is the default. The second variant has acquire semantics, and the third variant has release semantics. When an atomic operation has acquire
semantics, the operation must have completed before any subsequent load or
store (by program order) is performed. Conversely, acquire semantics do not
require that prior loads or stores have completed before the atomic
operation is performed. An atomic operation can only have acquire semantics
if it performs a load from memory. To denote acquire semantics, the suffix
“ When an atomic operation has release
semantics, all prior loads or stores (by program order) must have completed
before the operation is performed. Conversely, release semantics do not
require that the atomic operation must have completed before any subsequent
load or store is performed. An atomic operation can only have release
semantics if it performs a store to memory. To denote release semantics, the
suffix “ When a release operation by one thread synchronizes with an acquire operation by another thread, usually meaning that the acquire operation reads the value written by the release operation, then the effects of all prior stores by the releasing thread must become visible to subsequent loads by the acquiring thread. Moreover, the effects of all stores (by other threads) that were visible to the releasing thread must also become visible to the acquiring thread. These rules only apply to the synchronizing threads. Other threads might observe these stores in a different order. In effect, atomic operations with acquire and release semantics establish one-way barriers to reordering that enable the implementations of synchronization primitives to express their ordering requirements without also imposing unnecessary ordering. For example, for a critical section guarded by a mutex, an acquire operation when the mutex is locked and a release operation when the mutex is unlocked will prevent any loads or stores from moving outside of the critical section. However, they will not prevent the compiler or processor from moving loads or stores into the critical section, which does not violate the semantics of a mutex. Thread Fence OperationsAlternatively, a programmer can use atomic thread fence operations to constrain the reordering of accesses. In contrast to other atomic operations, fences do not, themselves, access memory. When a fence has acquire semantics,
all prior loads (by program order) must have completed before any subsequent
load or store is performed. Thus, an acquire fence is a two-way barrier for
load operations. To denote acquire semantics, the suffix
“ When a fence has release semantics,
all prior loads or stores (by program order) must have completed before any
subsequent store operation is performed. Thus, a release fence is a two-way
barrier for store operations. To denote release semantics, the suffix
“ Although
In C11, a release fence by one thread synchronizes with an acquire fence by another thread when an atomic load that is prior to the acquire fence (by program order) reads the value written by an atomic store that is subsequent to the release fence. In constrast, in FreeBSD, because of the atomicity of ordinary, naturally aligned loads and stores, fences can also be synchronized by ordinary loads and stores. This simplifies the implementation and use of some synchronization primitives in FreeBSD. Since neither a compiler nor a processor can foresee which (atomic) load will read the value written by an (atomic) store, the ordering constraints imposed by fences must be more restrictive than acquire loads and release stores. Essentially, this is why fences are two-way barriers. Although fences impose more restrictive ordering than acquire loads and release stores, by separating access from ordering, they can sometimes facilitate more efficient implementations of synchronization primitives. For example, they can be used to avoid executing a memory barrier until a memory access shows that some condition is satisfied. Interrupt Fence OperationsThe
Multiple ProcessorsIn multiprocessor systems, the atomicity of the atomic operations
on memory depends on support for cache coherence in the underlying
architecture. In general, cache coherence on the default memory type,
SemanticsThis section describes the semantics of each operation using a C like notation.
Some architectures do not implement the
On architectures implementing Compare And Swap operation in hardware, the functionality can be described as if (*dst == *old) { *dst = new; return (1); } else { *old = *dst; return (0); } *dst might
also fail for several reasons, most important of which is a parallel write to
*dst cache line by other CPU. In this case
atomic_fcmpset ()
function also returns false , despite
*old == *dst Some architectures do not implement the
The
The
The
The
The type “ RETURN VALUESThe EXAMPLESThis example uses the
/* Try to obtain mtx_lock once. */ #define _obtain_lock(mp, tid) \ atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid)) /* Get a sleep lock, deal with recursion inline. */ #define _get_sleep_lock(mp, tid, opts, file, line) do { \ uintptr_t _tid = (uintptr_t)(tid); \ \ if (!_obtain_lock(mp, tid)) { \ if (((mp)->mtx_lock & MTX_FLAGMASK) != _tid) \ _mtx_lock_sleep((mp), _tid, (opts), (file), (line));\ else { \ atomic_set_ptr(&(mp)->mtx_lock, MTX_RECURSE); \ (mp)->mtx_recurse++; \ } \ } \ } while (0) HISTORYThe The The The The The relaxed variants of The
|