ISO/IEC JTC1 SC22 WG21 N3164 = 10-0154 - 2010-10-14
Lawrence Crowl, crowl@google.com, Lawrence@Crowl.org
Introduction
Wording
29.1 General [atomics.general]
29.2 Header <atomic>
synopsis [atomics.syn]
29.5 Atomic Types [atomics.types]
29.5.1 Integral Types [atomics.types.integral]
29.5.2 Address Type [atomics.types.address]
29.5.3 Generic Type [atomics.types.generic]
29.6 Operations on Atomic Types [atomics.types.operations]
29.9 C Compatibility [atomics.compatibility]
The draft of C1X includes a facility for atomics. The primary change in this facility in the draft N1494 is incorporation of a new atomics proposal for a productive syntax for declaring atomic types and operators for atomic types. (The latest C1X draft is N1516.) C++0x FCD national body comment CA 23 (and more generally US 1) requests compatibility between C and C++ with respect to atomics.
This paper provides normative wording changes to reflect the choices in N3137 C and C++ Liaison: Compatibility for Atomics.
In summary, the changes are:
atomic_
.... types and typdefs,
except for atomic_flag
.
Now that C has a productive syntax for naming atomic types,
these names are no longer required.
The atomic_flag
type is special,
and is not part of the normal productive atomic syntax.
atomic_
.... named types
and the corresponding specializations
of the atomic
template class.
This change ensures compatibility with C.
Some member functions and operators,
that were formerly inherited from the base class,
must be hoisted from the named types to the specializations.
atomic_
.... freestanding functions to templates,
as they must now cover arbitrary atomic types
rather than just the named types.
Some specializations are required.
ATOMIC_VAR_INIT
and atomic_init
.
<stdatomic.h>
,
the macro __STDC_NO_THREADS__
,
and a new function macro _Atomic
to serve in the syntactic position of C's type specifier for atomics.
Edit table 141 as follows.
Table 141 — Atomics library summary Subclause Header(s) 29.3 Order and Consistency 29.4 Lock-free Property 29.5 Atomic Types <atomic>
29.6 Operations on Atomic Types 29.7 Flag Type and Operations 29.8 Fences 29.9 C Compatibility <stdatomic.h>
<atomic>
synopsis [atomics.syn]
Move the ATOMIC_VAR_INIT
macro declaration
to its proper order with respect to the sections.
Move the atomic_flag
type and function declarations
to their proper order with respect to the sections.
Remove the declaration block for
atomic_bool
and associated functions.
Remove the declaration block for
atomic_itype
and associated functions.
Remove the declaration block for
atomic_address
and associated functions.
Modify the section number for the generic type definitions to 29.5.
For section 29.6, add generic free functions as follows. The functions mirror C type-generic macros.
template <typename Type> bool atomic_is_lock_free(const volatile atomic<Type>*); template <typename Type> bool atomic_is_lock_free(const atomic<Type>*); template <typename Type> void atomic_init(volatile atomic<Type>*, iType); template <typename Type> void atomic_init(atomic<Type>*, iType); template <typename Type> void atomic_store(volatile atomic<Type>*, Type); template <typename Type> void atomic_store(atomic<Type>*, Type); template <typename Type> void atomic_store_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> void atomic_store_explicit(atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_load(const volatile atomic<Type>*); template <typename Type> Type atomic_load(const atomic<Type>*); template <typename Type> Type atomic_load_explicit(const volatile atomic<Type>*, memory_order); template <typename Type> Type atomic_load_explicit(const atomic<Type>*, memory_order); template <typename Type> Type atomic_exchange(volatile atomic<Type>*, Type); template <typename Type> Type atomic_exchange(atomic<Type>*, Type); template <typename Type> Type atomic_exchange_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_exchange_explicit(atomic<Type>*, Type, memory_order); template <typename Type> bool atomic_compare_exchange_weak(volatile atomic<Type>*, Type*, Type); template <typename Type> bool atomic_compare_exchange_weak(atomic<Type>*, Type*, Type); template <typename Type> bool atomic_compare_exchange_strong(volatile atomic<Type>*, Type*, Type); template <typename Type> bool atomic_compare_exchange_strong(atomic<Type>*, Type*, Type); template <typename Type> bool atomic_compare_exchange_weak_explicit(volatile atomic<Type>*, Type*, Type, memory_order, memory_order); template <typename Type> bool atomic_compare_exchange_weak_explicit(atomic<Type>*, Type*, Type, memory_order, memory_order); template <typename Type> bool atomic_compare_exchange_strong_explicit(volatile atomic<Type>*, Type*, Type, memory_order, memory_order); template <typename Type> bool atomic_compare_exchange_strong_explicit(atomic<Type>*, Type*, Type, memory_order, memory_order);
For section 29.6, add generic free functions as follows. These templates are only declared, not defined.
template <typename Type> Type atomic_fetch_add(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_add(atomic<Type>*, Type); template <typename Type> Type atomic_fetch_add_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_add_explicit(atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_sub(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_sub(atomic<Type>*, Type); template <typename Type> Type atomic_fetch_sub_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_sub_explicit(atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_and(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_and(atomic<Type>*, Type); template <typename Type> Type atomic_fetch_and_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_and_explicit(atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_or(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_or(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_or_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_or_explicit(atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_xor(volatile atomic<Type>*, Type); template <typename Type> Type atomic_fetch_xor(atomic<Type>*, Type); template <typename Type> Type atomic_fetch_xor_explicit(volatile atomic<Type>*, Type, memory_order); template <typename Type> Type atomic_fetch_xor_explicit(atomic<Type>*, Type, memory_order);
For section 29.6, add the following integral specializations.
For each integral type
integral
,template <> integral atomic_fetch_add(volatile atomic<integral>*, integral); template <> integral atomic_fetch_add(atomic<integral>*, integral); template <> integral atomic_fetch_add_explicit(volatile atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_add_explicit(atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_sub(volatile atomic<integral>*, integral); template <> integral atomic_fetch_sub(atomic<integral>*, integral); template <> integral atomic_fetch_sub_explicit(volatile atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_sub_explicit(atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_and(volatile atomic<integral>*, integral); template <> integral atomic_fetch_and(atomic<integral>*, integral); template <> integral atomic_fetch_and_explicit(volatile atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_and_explicit(atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_or(volatile atomic<integral>*, integral); template <> integral atomic_fetch_or(volatile atomic<integral>*, integral); template <> integral atomic_fetch_or_explicit(volatile atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_or_explicit(atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_xor(volatile atomic<integral>*, integral); template <> integral atomic_fetch_xor(atomic<integral>*, integral); template <> integral atomic_fetch_xor_explicit(volatile atomic<integral>*, integral, memory_order); template <> integral atomic_fetch_xor_explicit(atomic<integral>*, integral, memory_order);
For section 29.6,
add generic free functions as follows.
These functions correspond to
the atomic
class template partial specialization for pointers.
template <typename Type> Type atomic_fetch_add(volatile atomic<Type*>*, ptrdiff_t); template <typename Type> Type atomic_fetch_add(atomic<Type*>*, ptrdiff_t); template <typename Type> Type atomic_fetch_add_explicit(volatile atomic<Type*>*, ptrdiff_t, memory_order); template <typename Type> Type atomic_fetch_add_explicit(atomic<Type*>*, ptrdiff_t, memory_order); template <typename Type> Type atomic_fetch_sub(volatile atomic<Type*>*, ptrdiff_t); template <typename Type> Type atomic_fetch_sub(atomic<Type*>*, ptrdiff_t); template <typename Type> Type atomic_fetch_sub_explicit(volatile atomic<Type*>*, ptrdiff_t, memory_order); template <typename Type> Type atomic_fetch_sub_explicit(atomic<Type*>*, ptrdiff_t, memory_order);
This section has the bulk of changes, removing the named types in favor of only providing the productive type syntax. The approach is to remove the sections on integral types and address types, and turn the subsection on generic types to the section on types.
Remove this section.
Remove this section.
Rename this subsection to 29.5 Atomic Types [atomics.types].
Edit the synopsis for the integral type specializations as follows.
template <> struct atomic<integral>
: atomic_itype{ bool is_lock_free() const volatile; bool is_lock_free() const; void store(integral, memory_order = memory_order_seq_cst) volatile; void store(integral, memory_order = memory_order_seq_cst); integral load(memory_order = memory_order_seq_cst) const volatile; integral load(memory_order = memory_order_seq_cst) const; operator integral() const volatile; operator integral() const; integral exchange(integral, memory_order = memory_order_seq_cst) volatile; integral exchange(integral, memory_order = memory_order_seq_cst); bool compare_exchange_weak(integral&, integral, memory_order, memory_order) volatile; bool compare_exchange_weak(integral&, integral, memory_order, memory_order); bool compare_exchange_strong(integral&, integral, memory_order, memory_order) volatile; bool compare_exchange_strong(integral&, integral, memory_order, memory_order); bool compare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst) volatile; bool compare_exchange_weak(integral&, integral, memory_order = memory_order_seq_cst); bool compare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst) volatile; bool compare_exchange_strong(integral&, integral, memory_order = memory_order_seq_cst); integral fetch_add(integral, memory_order = memory_order_seq_cst) volatile; integral fetch_add(integral, memory_order = memory_order_seq_cst); integral fetch_sub(integral, memory_order = memory_order_seq_cst) volatile; integral fetch_sub(integral, memory_order = memory_order_seq_cst); integral fetch_and(integral, memory_order = memory_order_seq_cst) volatile; integral fetch_and(integral, memory_order = memory_order_seq_cst); integral fetch_or(integral, memory_order = memory_order_seq_cst) volatile; integral fetch_or(integral, memory_order = memory_order_seq_cst); integral fetch_xor(integral, memory_order = memory_order_seq_cst) volatile; integral fetch_xor(integral, memory_order = memory_order_seq_cst); atomic() = default; constexpr atomic(integral); atomic(const atomic&) = delete; atomic& operator=(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; integral operator=(integral) volatile; integral operator=(integral); integral operator++(int) volatile; integral operator++(int); integral operator--(int) volatile; integral operator--(int); integral operator++() volatile; integral operator++(); integral operator--() volatile; integral operator--(); integral operator+=(integral) volatile; integral operator+=(integral); integral operator-=(integral) volatile; integral operator-=(integral); integral operator&=(integral) volatile; integral operator&=(integral); integral operator|=(integral) volatile; integral operator|=(integral); integral operator^=(integral) volatile; integral operator^=(integral); };
Edit the synopsis for the pointer type specializations as follows.
template <class T> struct atomic<T*>
: atomic_address{ bool is_lock_free() const volatile; bool is_lock_free() const; void store(T*, memory_order = memory_order_seq_cst) volatile; void store(T*, memory_order = memory_order_seq_cst); T* load(memory_order = memory_order_seq_cst) const volatile; T* load(memory_order = memory_order_seq_cst) const; operator T*() const volatile; operator T*() const; T* exchange(T*, memory_order = memory_order_seq_cst) volatile; T* exchange(T*, memory_order = memory_order_seq_cst); bool compare_exchange_weak(T*&, T*, memory_order, memory_order) volatile; bool compare_exchange_weak(T*&, T*, memory_order, memory_order); bool compare_exchange_strong(T*&, T*, memory_order, memory_order) volatile; bool compare_exchange_strong(T*&, T*, memory_order, memory_order); bool compare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst) volatile; bool compare_exchange_weak(T*&, T*, memory_order = memory_order_seq_cst); bool compare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst) volatile; bool compare_exchange_strong(T*&, T*, memory_order = memory_order_seq_cst); T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; T* fetch_add(ptrdiff_t, memory_order = memory_order_seq_cst); T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst) volatile; T* fetch_sub(ptrdiff_t, memory_order = memory_order_seq_cst); atomic() = default; constexpr atomic(T*); atomic(const atomic&) = delete; atomic& operator=(const atomic&) = delete; atomic& operator=(const atomic&) volatile = delete; T* operator=(T*) volatile; T* operator=(T*); T* operator++(int) volatile; T* operator++(int); T* operator--(int) volatile; T* operator--(int); T* operator++() volatile; T* operator++(); T* operator--() volatile; T* operator--(); T* operator+=(ptrdiff_t) volatile; T* operator+=(ptrdiff_t); T* operator-=(ptrdiff_t) volatile; T* operator-=(ptrdiff_t); };
After paragraph 1, add a new paragraph.
The semantics of the operations on atomic
specializations
are defined in 29.6.
Edit paragraph 3 as follows.
There are full specializations over the integral types (char
,signed char
,unsigned char
,short
,unsigned short
,int
,unsigned int
,long
,unsigned long
,long long
,unsigned long long
,char16_t
,char32_t
,wchar_t
, and any other types need by<cstdint>
typedefs.) on the atomic class template. For each integral type integralin the second column of Table 142 or Table 143, the specializationatomic<integral>
shall be publicly derived from the corresponding atomic integral type in the first column of the table. In addition, the specializationprovide additional atomic operations appropriate to integral types.atomic<bool>
shall be publicly derived fromatomic_bool
.These specializations shall have trivial default constructors and trivial destructors.The atomic integral specializations shall have standard layout. They shall each have a trivial default constructor, and a trivial destructor. They shall each support aggregate initialization syntax.
Insert a new paragraph after the one above.
The specialization atomic<bool>
shall have standard layout.
It shall have a trivial default constructor and a trivial destructor.
It shall support aggregate initialization syntax.
Edit paragraph 4 as follows.
There are pointer partial specializations on theatomic
class template.These specializations shall be publicly derived from atomic_address.The unit of addition/subtraction for these specializations shall be the size of the referenced type. These specializations shall have trivial default constructors and trivial destructors.
Insert a new paragraph after the one above.
[Note: The representation of atomic specializations need not have the same size as their corresponding argument types. They should have the same size whenever possible, as it eases effort required to port existing code. —end note]
The free functions in this section become templates or specializations thereof. Technically, this would require adding a template name signature. However, that change seems likely to be more confusing than helpful. So, I have not suggested that change, leaving it to the editor for the final decision.
Edit paragraph 1 as follows.
There are only a few kinds of operations on atomic types, though there are many instances on those kinds. This section specifies each general kind. The specific instances are defined in 29.229.5.1, 29.5.2,and29.5.329.5.
Edit paragraph 5 as follows.
Remarks: A macro that expands to a token sequence suitable for
initializingconstant initialization of an atomic variable of static storage duration of a type that isinitializion-compatibleinitialization-compatible with value. [Note: This operation may need to initialize locks. —end note] Concurrent access to the variable being initialized, even via an atomic operation, constitutes a data race. [Example:
atomic_intatomic<int> v = ATOMIC_VAR_INIT(5);—end example]
Edit paragraph paragraph 7 as follows.
Effects: Dynamically initializes an atomic variable.
Non-atomicallyThat is, non-atomically assigns the value desired to*object
. [Note: This operation may need to initialize locks. —end note] Concurrent access from another thread, even via an atomic operation, constitutes a data race.
Add a new section on C compatibility.
Add a new paragraph as follows.
Note:
Whether or not member operators
are defined when including <stdatomic.h>
was left open by the concurrency subcommittee.
This wording presupposes they are defined.
Including the header
<stdatomic.h>
will place the symbols of this clause into the global namespace.
Add a new paragraph as follows. Note: this issue was left open by the concurrency subcommittee. Note: The C committee may change the name of the macro or remove its applicability to atomics.
Implementations that define the macro
__STDC_NO_THREADS__
need not provide the headers<atomic>
and<stdatomic.h>
nor support any of their facilities.
Add a new paragraph as follows.
The header
<stdatomic.h>
defines a macro_Atomic
as follows.#define _Atomic(T) atomic<T>