Adjusting C++ Atomics for C Compatibility

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]

Introduction

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:

Wording

This wording is relative to N3126 Working Draft, Standard for Programming Language C++.

29.1 General [atomics.general]

Edit table 141 as follows.

Table 141 — Atomics library summary
SubclauseHeader(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>

29.2 Header <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);

29.5 Atomic Types [atomics.types]

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.

29.5.1 Integral Types [atomics.types.integral]

Remove this section.

29.5.2 Address Type [atomics.types.address]

Remove this section.

29.5.3 Generic Type [atomics.types.generic]

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 integral in the second column of Table 142 or Table 143, the specialization atomic<integral> shall be publicly derived from the corresponding atomic integral type in the first column of the table. In addition, the specialization atomic<bool> shall be publicly derived from atomic_bool. provide additional atomic operations appropriate to integral types. 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 the atomic 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]

29.6 Operations on Atomic Types [atomics.types.operations]

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.2 29.5.1, 29.5.2, and 29.5.3 29.5.

Edit paragraph 5 as follows.

Remarks: A macro that expands to a token sequence suitable for initializing constant initialization of an atomic variable of static storage duration of a type that is initializion-compatible initialization-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_int atomic<int> v = ATOMIC_VAR_INIT(5);

end example]

Edit paragraph paragraph 7 as follows.

Effects: Dynamically initializes an atomic variable. Non-atomically That 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.

29.9 C Compatibility [atomics.compatibility]

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>