From patchwork Tue Jan 18 16:32:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 50179 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A0B83858032 for ; Tue, 18 Jan 2022 16:48:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A0B83858032 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642524508; bh=W4suGQxCjGPkYY+2RPKXmyuu9fSM0oe4leOVkUxcP5Y=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=X/4CkTs0v1EjDKyXu6hyxqbcTLmNyr9/BkshneVZ4jEd3IwVrqiCLs0WvFlSXE9GZ i97jxg4UOaOhgsy1/Qxeebs5KRuoe+V7hPYNXQ4YbRZWydBTXm8m5NnExFoOSzbAjp pGExTIgN1OET956o4ItOo0BT4FPSJIXZFEB8TFsU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 149293857C51 for ; Tue, 18 Jan 2022 16:43:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 149293857C51 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-221-kCBDQRinPhWPfKNwqnpzRQ-1; Tue, 18 Jan 2022 11:43:23 -0500 X-MC-Unique: kCBDQRinPhWPfKNwqnpzRQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 944F1112BBFE; Tue, 18 Jan 2022 16:32:37 +0000 (UTC) Received: from localhost (unknown [10.33.37.93]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0DB4410A48AF; Tue, 18 Jan 2022 16:32:36 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Define for C++23 Date: Tue, 18 Jan 2022 16:32:35 +0000 Message-Id: <20220118163235.3799725-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested powerpc64le-linux, pushed to trunk. This adds the C++23 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. --- libstdc++-v3/include/Makefile.am | 1 + libstdc++-v3/include/Makefile.in | 1 + .../include/c_compatibility/stdatomic.h | 124 +++++++++++++++++ .../headers/stdatomic.h/c_compat.cc | 130 ++++++++++++++++++ 4 files changed, 256 insertions(+) create mode 100644 libstdc++-v3/include/c_compatibility/stdatomic.h create mode 100644 libstdc++-v3/testsuite/29_atomics/headers/stdatomic.h/c_compat.cc diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index d1431926cdd..43f7f9f240d 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -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 diff --git a/libstdc++-v3/include/c_compatibility/stdatomic.h b/libstdc++-v3/include/c_compatibility/stdatomic.h new file mode 100644 index 00000000000..852574bd87c --- /dev/null +++ b/libstdc++-v3/include/c_compatibility/stdatomic.h @@ -0,0 +1,124 @@ +// C compatibility header -*- 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 +// . + +/** @file include/stdatomic.h + * This is a Standard C++ Library header. + */ + +#ifndef _GLIBCXX_STDATOMIC_H +#define _GLIBCXX_STDATOMIC_H + +#if __cplusplus > 202002L +#include + +#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 diff --git a/libstdc++-v3/testsuite/29_atomics/headers/stdatomic.h/c_compat.cc b/libstdc++-v3/testsuite/29_atomics/headers/stdatomic.h/c_compat.cc new file mode 100644 index 00000000000..98c1102a974 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/headers/stdatomic.h/c_compat.cc @@ -0,0 +1,130 @@ +// { dg-options "-std=gnu++23" } +// { dg-do compile { target c++23 } } + +#include + +#ifndef ATOMIC_BOOL_LOCK_FREE +#error ATOMIC_BOOL_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_CHAR_LOCK_FREE +#error ATOMIC_CHAR_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_CHAR16_T_LOCK_FREE +#error ATOMIC_CHAR16_T_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_CHAR32_T_LOCK_FREE +#error ATOMIC_CHAR32_T_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_WCHAR_T_LOCK_FREE +#error ATOMIC_WCHAR_T_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_SHORT_LOCK_FREE +#error ATOMIC_SHORT_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_INT_LOCK_FREE +#error ATOMIC_INT_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_LONG_LOCK_FREE +#error ATOMIC_LONG_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_LLONG_LOCK_FREE +#error ATOMIC_LLONG_LOCK_FREE is not defined in +#endif +#ifndef ATOMIC_POINTER_LOCK_FREE +#error ATOMIC_POINTER_LOCK_FREE is not defined in +#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 constexpr bool is_same = false; +template constexpr bool is_same = true; + +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +#ifdef _GLIBCXX_USE_CHAR8_T +static_assert(is_same); +#endif +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); + +#include +#ifdef _GLIBCXX_USE_C99_STDINT_TR1 +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +#endif +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +static_assert(is_same); +#ifdef _GLIBCXX_USE_C99_STDINT_TR1 +static_assert(is_same); +static_assert(is_same); +#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); +} );