Skip to content

Commit

Permalink
libstdc++: Define <stdatomic.h> for C++23
Browse files Browse the repository at this point in the history
This adds the C++23 <stdatomic.h> header, as proposed by P0943R6, for
compatibility with C code.

There are still some ABI differences between atomic_xxx in C and C++
std::atomic_xxx in C++, so this only provides source compatibility, not
binary compatibility.

libstdc++-v3/ChangeLog:

	* include/Makefile.am: Install new header.
	* include/Makefile.in: Regenerate.
	* include/c_compatibility/stdatomic.h: New file.
	* testsuite/29_atomics/headers/stdatomic.h/c_compat.cc: New test.
  • Loading branch information
jwakely committed Jan 18, 2022
1 parent 58385f6 commit d7f2a09
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 0 deletions.
1 change: 1 addition & 0 deletions libstdc++-v3/include/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ c_compatibility_headers = \
${c_compatibility_srcdir}/fenv.h \
${c_compatibility_srcdir}/tgmath.h \
${c_compatibility_srcdir}/math.h \
${c_compatibility_srcdir}/stdatomic.h \
${c_compatibility_srcdir}/stdlib.h
endif

Expand Down
1 change: 1 addition & 0 deletions libstdc++-v3/include/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,7 @@ c_compatibility_builddir = .
@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/fenv.h \
@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/tgmath.h \
@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/math.h \
@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdatomic.h \
@GLIBCXX_C_HEADERS_C_GLOBAL_TRUE@ ${c_compatibility_srcdir}/stdlib.h

@GLIBCXX_C_HEADERS_C_STD_TRUE@c_compatibility_headers =
Expand Down
124 changes: 124 additions & 0 deletions libstdc++-v3/include/c_compatibility/stdatomic.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// C compatibility header <stdatomic.h> -*- C++ -*-

// Copyright (C) 2022 The GCC developers
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/stdatomic.h
* This is a Standard C++ Library header.
*/

#ifndef _GLIBCXX_STDATOMIC_H
#define _GLIBCXX_STDATOMIC_H

#if __cplusplus > 202002L
#include <atomic>

#define _Atomic(_Tp) std::atomic<_Tp>

using std::memory_order;
using std::memory_order_relaxed;
using std::memory_order_consume;
using std::memory_order_acquire;
using std::memory_order_release;
using std::memory_order_acq_rel;
using std::memory_order_seq_cst;
using std::atomic_flag;
using std::atomic_bool;
using std::atomic_char;
using std::atomic_schar;
using std::atomic_uchar;
using std::atomic_short;
using std::atomic_ushort;
using std::atomic_int;
using std::atomic_uint;
using std::atomic_long;
using std::atomic_ulong;
using std::atomic_llong;
using std::atomic_ullong;
#ifdef _GLIBCXX_USE_CHAR8_T
using std::atomic_char8_t;
#endif
using std::atomic_char16_t;
using std::atomic_char32_t;
using std::atomic_wchar_t;
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
using std::atomic_int8_t;
using std::atomic_uint8_t;
using std::atomic_int16_t;
using std::atomic_uint16_t;
using std::atomic_int32_t;
using std::atomic_uint32_t;
using std::atomic_int64_t;
using std::atomic_uint64_t;
using std::atomic_int_least8_t;
using std::atomic_uint_least8_t;
using std::atomic_int_least16_t;
using std::atomic_uint_least16_t;
using std::atomic_int_least32_t;
using std::atomic_uint_least32_t;
using std::atomic_int_least64_t;
using std::atomic_uint_least64_t;
using std::atomic_int_fast8_t;
using std::atomic_uint_fast8_t;
using std::atomic_int_fast16_t;
using std::atomic_uint_fast16_t;
using std::atomic_int_fast32_t;
using std::atomic_uint_fast32_t;
using std::atomic_int_fast64_t;
using std::atomic_uint_fast64_t;
#endif
using std::atomic_intptr_t;
using std::atomic_uintptr_t;
using std::atomic_size_t;
using std::atomic_ptrdiff_t;
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
using std::atomic_intmax_t;
using std::atomic_uintmax_t;
#endif
using std::atomic_is_lock_free;
using std::atomic_load;
using std::atomic_load_explicit;
using std::atomic_store;
using std::atomic_store_explicit;
using std::atomic_exchange;
using std::atomic_exchange_explicit;
using std::atomic_compare_exchange_strong;
using std::atomic_compare_exchange_strong_explicit;
using std::atomic_compare_exchange_weak;
using std::atomic_compare_exchange_weak_explicit;
using std::atomic_fetch_add;
using std::atomic_fetch_add_explicit;
using std::atomic_fetch_sub;
using std::atomic_fetch_sub_explicit;
using std::atomic_fetch_or;
using std::atomic_fetch_or_explicit;
using std::atomic_fetch_and;
using std::atomic_fetch_and_explicit;
using std::atomic_flag_test_and_set;
using std::atomic_flag_test_and_set_explicit;
using std::atomic_flag_clear;
using std::atomic_flag_clear_explicit;
using std::atomic_thread_fence;
using std::atomic_signal_fence;

#endif // C++23
#endif // _GLIBCXX_STDATOMIC_H
130 changes: 130 additions & 0 deletions libstdc++-v3/testsuite/29_atomics/headers/stdatomic.h/c_compat.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// { dg-options "-std=gnu++23" }
// { dg-do compile { target c++23 } }

#include <stdatomic.h>

#ifndef ATOMIC_BOOL_LOCK_FREE
#error ATOMIC_BOOL_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_CHAR_LOCK_FREE
#error ATOMIC_CHAR_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_CHAR16_T_LOCK_FREE
#error ATOMIC_CHAR16_T_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_CHAR32_T_LOCK_FREE
#error ATOMIC_CHAR32_T_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_WCHAR_T_LOCK_FREE
#error ATOMIC_WCHAR_T_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_SHORT_LOCK_FREE
#error ATOMIC_SHORT_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_INT_LOCK_FREE
#error ATOMIC_INT_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_LONG_LOCK_FREE
#error ATOMIC_LONG_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_LLONG_LOCK_FREE
#error ATOMIC_LLONG_LOCK_FREE is not defined in <stdatomic.h>
#endif
#ifndef ATOMIC_POINTER_LOCK_FREE
#error ATOMIC_POINTER_LOCK_FREE is not defined in <stdatomic.h>
#endif

constexpr const memory_order* orders[] = {
&memory_order_relaxed, &memory_order_consume, &memory_order_acquire,
&memory_order_release, &memory_order_acq_rel, &memory_order_seq_cst
};

constexpr atomic_flag flag{};

template<typename A, typename B> constexpr bool is_same = false;
template<typename A> constexpr bool is_same<A, A> = true;

static_assert(is_same<atomic_bool, _Atomic(bool)>);
static_assert(is_same<atomic_char, _Atomic(char)>);
static_assert(is_same<atomic_schar, _Atomic(signed char)>);
static_assert(is_same<atomic_uchar, _Atomic(unsigned char)>);
static_assert(is_same<atomic_short, _Atomic(short)>);
static_assert(is_same<atomic_ushort, _Atomic(unsigned short)>);
static_assert(is_same<atomic_int, _Atomic(int)>);
static_assert(is_same<atomic_uint, _Atomic(unsigned int)>);
static_assert(is_same<atomic_long, _Atomic(long)>);
static_assert(is_same<atomic_ulong, _Atomic(unsigned long)>);
static_assert(is_same<atomic_llong, _Atomic(long long)>);
static_assert(is_same<atomic_ullong, _Atomic(unsigned long long)>);
#ifdef _GLIBCXX_USE_CHAR8_T
static_assert(is_same<atomic_char8_t, _Atomic(char8_t)>);
#endif
static_assert(is_same<atomic_char16_t, _Atomic(char16_t)>);
static_assert(is_same<atomic_char32_t, _Atomic(char32_t)>);
static_assert(is_same<atomic_wchar_t, _Atomic(wchar_t)>);

#include <stdint.h>
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
static_assert(is_same<atomic_int8_t, _Atomic(int8_t)>);
static_assert(is_same<atomic_uint8_t, _Atomic(uint8_t)>);
static_assert(is_same<atomic_int16_t, _Atomic(int16_t)>);
static_assert(is_same<atomic_uint16_t, _Atomic(uint16_t)>);
static_assert(is_same<atomic_int32_t, _Atomic(int32_t)>);
static_assert(is_same<atomic_uint32_t, _Atomic(uint32_t)>);
static_assert(is_same<atomic_int64_t, _Atomic(int64_t)>);
static_assert(is_same<atomic_uint64_t, _Atomic(uint64_t)>);
static_assert(is_same<atomic_int_least8_t, _Atomic(int_least8_t)>);
static_assert(is_same<atomic_uint_least8_t, _Atomic(uint_least8_t)>);
static_assert(is_same<atomic_int_least16_t, _Atomic(int_least16_t)>);
static_assert(is_same<atomic_uint_least16_t, _Atomic(uint_least16_t)>);
static_assert(is_same<atomic_int_least32_t, _Atomic(int_least32_t)>);
static_assert(is_same<atomic_uint_least32_t, _Atomic(uint_least32_t)>);
static_assert(is_same<atomic_int_least64_t, _Atomic(int_least64_t)>);
static_assert(is_same<atomic_uint_least64_t, _Atomic(uint_least64_t)>);
static_assert(is_same<atomic_int_fast8_t, _Atomic(int_fast8_t)>);
static_assert(is_same<atomic_uint_fast8_t, _Atomic(uint_fast8_t)>);
static_assert(is_same<atomic_int_fast16_t, _Atomic(int_fast16_t)>);
static_assert(is_same<atomic_uint_fast16_t, _Atomic(uint_fast16_t)>);
static_assert(is_same<atomic_int_fast32_t, _Atomic(int_fast32_t)>);
static_assert(is_same<atomic_uint_fast32_t, _Atomic(uint_fast32_t)>);
static_assert(is_same<atomic_int_fast64_t, _Atomic(int_fast64_t)>);
static_assert(is_same<atomic_uint_fast64_t, _Atomic(uint_fast64_t)>);
#endif
static_assert(is_same<atomic_intptr_t, _Atomic(intptr_t)>);
static_assert(is_same<atomic_uintptr_t, _Atomic(uintptr_t)>);
static_assert(is_same<atomic_size_t, _Atomic(size_t)>);
static_assert(is_same<atomic_ptrdiff_t, _Atomic(ptrdiff_t)>);
#ifdef _GLIBCXX_USE_C99_STDINT_TR1
static_assert(is_same<atomic_intmax_t, _Atomic(intmax_t)>);
static_assert(is_same<atomic_uintmax_t, _Atomic(uintmax_t)>);
#endif

static_assert( requires (::atomic_int* i, int* e) {
::atomic_is_lock_free(i);
::atomic_load(i);
::atomic_load_explicit(i, memory_order_relaxed);
::atomic_store(i, 2);
::atomic_store_explicit(i, 3, memory_order_release);
::atomic_exchange(i, 2);
::atomic_exchange_explicit(i, 3, memory_order_acq_rel);
::atomic_compare_exchange_strong(i, e, 2);
::atomic_compare_exchange_strong_explicit(i, e, 3,
memory_order_acq_rel,
memory_order_relaxed);
::atomic_compare_exchange_weak(i, e, 2);
::atomic_compare_exchange_weak_explicit(i, e, 3,
memory_order_acq_rel,
memory_order_relaxed);
} );

static_assert( requires (::atomic_flag* f) {
::atomic_flag_test_and_set(f);
::atomic_flag_test_and_set_explicit(f, memory_order_relaxed);
::atomic_flag_clear(f);
::atomic_flag_clear_explicit(f, memory_order_acq_rel);
} );

static_assert( requires (::memory_order o) {
::atomic_thread_fence(o);
::atomic_signal_fence(o);
} );

0 comments on commit d7f2a09

Please sign in to comment.