Go to main content
Oracle Developer Studio 12.6 Man Pages

Exit Print View

Updated: June 2017
 
 

stdatomic (3A)

Name

stdatomic.h, stdatomic - atomics header

Synopsis

#include <stdatomic.h>

Description

The <stdatomic.h> header defines the following macros:

ATOMIC_BOOL_LOCK_FREE
ATOMIC_CHAR_LOCK_FREE
ATOMIC_CHAR16_T_LOCK_FREE
ATOMIC_CHAR32_T_LOCK_FREE
ATOMIC_WCHAR_T_LOCK_FREE
ATOMIC_SHORT_LOCK_FREE
ATOMIC_INT_LOCK_FREE
ATOMIC_LONG_LOCK_FREE
ATOMIC_LLONG_LOCK_FREE
ATOMIC_POINTER_LOCK_FREE

The above 10 macros indicate the lock-free property of the corresponding atomic types. For the non-pointer types, the results apply to both signed and unsigned types.

ATOMIC_FLAG_INIT

The ATOMIC_FLAG_INIT macro expands to an intializer for an object of type atomic_flag. It may be used to initialize an atomic_flag to the clear state. An atomic_flag that is not explicitly initialized with ATOMIC_FLAG_INIT is initially in an indeterminate state.

Example:

atomic_flag guard = ATOMIC_FLAG_INIT;
ATOMIC_VAR_INIT(value)

The ATOMIC_VAR_INIT macro expands to a token sequence suitable for initializing an atomic object of a type that is initialization-compatible with value.

Example:

atomic_int guide = ATOMIC_VAR_INIT(42);
kill_dependency(y)

Returns the value of y and terminates a dependency chain on variable y. Refer to C Standard for the meaning of terminating a dependency chain.

The following data types are defined through typedef:

atomic_flag

A structure type representing a lock-free, primitive atomic flag. An object of type atomic_flag can have two states: set and clear. It provides the classic test-and-set functionality.

memory_order

An enumarated type whose enumerators identify memory ordering constraints. It specifies the detailed regular (non-atomic) memory synchronization operations as defined in C standard 5.1.2.4 and may provide for operation ordering. Its enumeration constants are as follows:

memory_order_relaxed
memory_order_consume
memory_order_acquire
memory_order_release
memory_order_acq_rel
memory_order_seq_cst

For memory_order_relaxed, no operation orders memory.

For memory_order_release, memory_order_acq_rel, and memory_order_seq_cst, a store operation performs a release operation on the affected memory location.

For memory_order_acquire, memory_order_acq_rel, and memory_order_seq_cst, a load operation performs an acquire operation on the affected memory location.

For memory_order_consume, a load operation performs a consume operation on the affected memory location.

There will be a single total order S on all memory_order_seq_cst operations, consistent with the “happens before” order and modification orders for all affected locations, such that each memory_order_seq_cst operation B that loads a value from an atomic object M observes one of the following values:

  • the result of the last modification A of M that precedes B in S, if it exists, or

  • if A exists, the result of some modification of M in the visible sequence of side effects with respect to B that is not memory_order_seq_cst and that does not happen before A, or

  • if A does not exist, the result of some modification of M in the visible sequence of side effects with respect to B that is not memory_order_seq_cst.

For an atomic operation B that reads the value of an atomic object M, if there is a memory_order_seq_cst fence X sequenced before B, then B observes either the last memory_order_seq_cst modification of M preceding X in the total order S or a later modification of M in its modification order.

For atomic operations A and B on an atomic object M, where A modifies M and B takes its value, if there is a memory_order_seq_cst fence X such that A is sequenced before X and B follows X in S, then B observes either the effects of A or a later modification of M in its modification order.

For atomic operations A and B on an atomic object M, where A modifies M and B takes its value, if there are memory_order_seq_cst fences X and Y such that A is sequenced before X, Y is sequenced before B, and X precedes Y in S, then B observes either the effects of A or a later modification of M in its modification order.

Atomic read-modify-write operations always read the last value (in the modification order) stored before the write associated with the read-modify-write operation.

An atomic store only stores a value that has been computed from constants and program input values by a finite sequence of program evaluations, such that each evaluation observes the values of variables as computed by the last prior assignment in the sequence. The ordering of evaluations in this sequence will be such that

  • If an evaluation B observes a value computed by A in a different thread, then B does not happen before A.

  • If an evaluation A is included in the sequence, then all evaluations that assign to the same variable and happen before A are also included.

The following data types are defined through typedef. They are equivalent to the corresponding _Atomic qualified types listed on their right.

atomic_bool
_Atomic _Bool
atomic_char
_Atomic char
atomic_schar
_Atomic signed char
atomic_uchar
_Atomic unsigned char
atomic_short
_Atomic short
atomic_ushort
_Atomic unsigned short
atomic_int
_Atomic int
atomic_uint
_Atomic unsigned int
atomic_long
_Atomic long
atomic_ulong
_Atomic unsigned long
atomic_llong
_Atomic long long
atomic_ullong
_Atomic unsigned long long
atomic_char16_t
_Atomic char16_t
atomic_char32_t
_Atomic char32_t
atomic_wchar_t
_Atomic wchar_t
atomic_int_least8_t
_Atomic int_least8_t
atomic_uint_least8_t
_Atomic uint_least8_t
atomic_int_least16_t
_Atomic int_least16_t
atomic_uint_least16_t
_Atomic uint_least16_t
atomic_int_least32_t
_Atomic int_least32_t
atomic_uint_least32_t
_Atomic uint_least32_t
atomic_int_least64_t
_Atomic int_least64_t
atomic_uint_least64_t
_Atomic uint_least64_t
atomic_int_fast8_t
_Atomic int_fast8_t
atomic_uint_fast8_t
_Atomic uint_fast8_t
atomic_int_fast16_t
_Atomic int_fast16_t
atomic_uint_fast16_t
_Atomic uint_fast16_t
atomic_int_fast32_t
_Atomic int_fast32_t
atomic_uint_fast32_t
_Atomic uint_fast32_t
atomic_int_fast64_t
_Atomic int_fast64_t
atomic_uint_fast64_t
_Atomic uint_fast64_t
atomic_intptr_t
_Atomic intptr_t
atomic_uintptr_t
_Atomic uintptr_t
atomic_size_t
_Atomic size_t
atomic_ptrdiff_t
_Atomic ptrdiff_t
atomic_intmax_t
_Atomic intmax_t
atomic_uintmax_t
_Atomic uintmax_t

The following is a list of generic functions defined in the header. Generic functions are type generic macros, which expand to compiler intrinsics.

In the following description:

  • An A refers to an atomic type,

  • A C refers to A's corresponding non-atomic type.

  • An M refers to the type of the other argument for arithmetic operations. For atomic integer types, M is C. For atomic pointer types, M is ptrdiff_t.

void atomic_init (volatile A *obj, C value);

Initializes the atomic object pointed to by obj to value. If A is a locked atomic type, the lock associated with obj will also have been initialized after atomic_init() call.

_Bool atomic_is_lock_free (const volatile A *obj);

Returns whether the object pointed to by obj is lock-free.

In the following description, the generic functions not ending in _explicit have the same semantics as the corresponding _explicit function with memory_order_seq_cst for the memory_order argument. If there is no further description, for each generic function, memory is affected according to the value of memory_order argument. Refer to memory_order and C standard 5.1.2.4 for details about memory order.

void atomic_store (volatile A *object, C desired);
void atomic_store_explicit (volatile A *object, C desired,
memory_order order);

Atomically replace the value pointed to by object with the value of desired.

The order argument cannot be memory_order_acquire, memory_order_consume, nor memory_order_acq_rel.

C atomic_load (volatile A *object);
C atomic_load_explicit (volatile A *object, memory_order order);

Atomically return the value pointed to by object.

The order argument cannot be memory_order_release nor memory_order_acq_rel.

C atomic_exchange (volatile A *object, C desired);
C atomic_exchange_explicit (volatile A *object, C desired,
memory_order order);

Atomically replace the value pointed to by object with the value of desired. The return value is the value pointed to by object immediately before the effect. These operations are read-modify-write operations.

_Bool atomic_compare_exchange_strong (volatile A *object,
C *expected, C desired);
_Bool atomic_compare_exchange_strong_explicit (
volatile A *object, C *expected, C desired,
memory_order success, memory_order failure);
_Bool atomic_compare_exchange_weak (volatile A *object,
C *expected, C desired);
_Bool atomic_compare_exchange_weak_explicit (
volatile A *object, C *expected, C desired,
memory_order success, memory_order failure);

Atomically, compare if the value pointed to by object equal to the value pointed to by expected, and if true, replace the value pointed to by object with desired, and if false, update the value in expected with the value pointed to by object. Further, if the comparison is true, memory is affected according to the value of success, and if the comparison is false, memory is affected according to the value of failure. The return value is the result of the comparison.

These operations are atomic read-modify-write operations.

A weak compare-and-exchange operation may fail spuriously. That is, even when the contents of memory referred to by expected and object are equal, it may return zero and store back to expected the same memory contents that were originally there.

The following operations perform arithmetic and bitwise computations. All of these operations are applicable to an object of any atomic integer type. None of these operations is applicable to atomic_bool.

C atomic_fetch_add (volatile A *object, M operand);
C atomic_fetch_add_explicit (volatile A *object, M operand,
memory_order order);
C atomic_fetch_sub (volatile A *object, M operand);
C atomic_fetch_sub_explicit (volatile A *object, M operand,
memory_order order);
C atomic_fetch_or (volatile A *object, M operand);
C atomic_fetch_or_explicit (volatile A *object, M operand,
memory_order order);
C atomic_fetch_xor (volatile A *object, M operand);
C atomic_fetch_xor_explicit (volatile A *object, M operand,
memory_order order);
C atomic_fetch_and (volatile A *object, M operand);
C atomic_fetch_and_explicit (volatile A *object, M operand,
memory_order order);

Atomically replace the value pointed to by object with the result of the computation applied to the value pointed to by object and the given operand. Atomically, return the value pointed to by object immediately before the effects.

The operator of the computation is indicated by the key in the name of the generic function, the following table shows the key, operator, and computation correspondence

key
op
computation
add
+
addition
sub
-
subtraction
or
|
bitwise inclusive or
xor
^
bitwise exclusive or
and
&
bitwise and

These operations are atomic read-modify-write operations.

For signed integer types, arithmetic is defined to use two's complement representation with silent wrap-around on overflow. There is no undefinied behavior on signed integer overflow for these atomic operations.

For address types, the result may be an undefined address, but the operations otherwise have no undefined behavior for these atomic operations.

Attributes

See attributes(5) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
Standard

See Also

atomic_flag_clear(3A), atomic_flag_clear_explicit(3A), atomic_flag_test_and_set(3A), atomic_flag_test_and_set_explicit(3A), atomic_signal_fence(3A), atomic_thread_fence(3A)