diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2020-01-11 00:11:54 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2020-01-13 13:22:28 +0000 |
commit | e4379a931d140a71b36eaecceace319837fda321 (patch) | |
tree | 014062564f8ba50a3b362f59878320caaea76b38 /libstdc++-v3/testsuite/29_atomics | |
parent | aef85e40e0c5e242f1a1883abc56159dcd90e7b0 (diff) | |
download | gcc-e4379a931d140a71b36eaecceace319837fda321.zip gcc-e4379a931d140a71b36eaecceace319837fda321.tar.gz gcc-e4379a931d140a71b36eaecceace319837fda321.tar.bz2 |
libstdc++: Value-initialize std::atomic for C++20 (P0883R2)
This implements the new requirements for C++20 that std::atomic should
initialize the atomic variable in its default constructor.
This patch does not add the deprecated attribute to atomic_init, but
that should be done at some point as it's deprecated in C++20.
The paper also deprecates the ATOMIC_FLAG_INIT macro, although we can't
apply the deprecated attribute to a macro.
PR libstdc++/58605
* include/bits/atomic_base.h (__cpp_lib_atomic_value_initialization):
Define.
(__atomic_flag_base, __atomic_base, __atomic_base<_PTp*>)
(__atomic_float): Add default member initializer for C++20.
* include/std/atomic (atomic): Likewise.
(atomic::atomic()): Remove noexcept-specifier on default constructor.
* include/std/version (__cpp_lib_atomic_value_initialization): Define.
* testsuite/29_atomics/atomic/cons/assign_neg.cc: Adjust dg-error line
number.
* testsuite/29_atomics/atomic/cons/copy_neg.cc: Likewise.
* testsuite/29_atomics/atomic/cons/value_init.cc: New test.
* testsuite/29_atomics/atomic_flag/cons/value_init.cc: New test.
* testsuite/29_atomics/atomic_flag/requirements/trivial.cc: Adjust
expected result for is_trivially_default_constructible.
* testsuite/29_atomics/atomic_float/requirements.cc: Likewise.
* testsuite/29_atomics/atomic_float/value_init.cc: New test.
* testsuite/29_atomics/atomic_integral/cons/assign_neg.cc: Likewise.
* testsuite/29_atomics/atomic_integral/cons/copy_neg.cc: Likewise.
* testsuite/29_atomics/atomic_integral/cons/value_init.cc
* testsuite/29_atomics/atomic_integral/requirements/trivial.cc: Adjust
expected results for is_trivially_default_constructible.
* testsuite/util/testsuite_common_types.h (has_trivial_dtor): Add
new test generator.
Diffstat (limited to 'libstdc++-v3/testsuite/29_atomics')
11 files changed, 202 insertions, 7 deletions
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/cons/assign_neg.cc b/libstdc++-v3/testsuite/29_atomics/atomic/cons/assign_neg.cc index 306ea5a..d93eacc 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/cons/assign_neg.cc @@ -27,5 +27,5 @@ int main() return 0; } -// { dg-error "deleted" "" { target *-*-* } 639 } +// { dg-error "deleted" "" { target *-*-* } 659 } // { dg-prune-output "include" } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/cons/copy_neg.cc b/libstdc++-v3/testsuite/29_atomics/atomic/cons/copy_neg.cc index 5032912..03ecdd4 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic/cons/copy_neg.cc @@ -27,5 +27,5 @@ int main() return 0; } -// { dg-error "deleted" "" { target *-*-* } 678 } +// { dg-error "deleted" "" { target *-*-* } 698 } // { dg-prune-output "include" } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic/cons/value_init.cc b/libstdc++-v3/testsuite/29_atomics/atomic/cons/value_init.cc new file mode 100644 index 0000000..8620c7c --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic/cons/value_init.cc @@ -0,0 +1,76 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// 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. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include <atomic> + +#ifndef __cpp_lib_atomic_value_initialization +# error "Feature test macro for atomic value-initialization is missing" +#elif __cpp_lib_atomic_value_initialization < 201911L +# error "Feature test macro for atomic value-initialization has wrong value" +#endif + +#include <testsuite_hooks.h> + +struct A +{ + constexpr A() : val(42) { } + int val; +}; + +constexpr std::atomic<A> a; + +void +test01() +{ + VERIFY(a.load().val == 42); + static_assert(!std::is_nothrow_default_constructible_v<std::atomic<A>>); +} + +struct B +{ + constexpr B() noexcept : val(99) { } + int val; +}; + +constexpr std::atomic<B> b; + +void +test02() +{ + VERIFY(b.load().val == 99); + static_assert(std::is_nothrow_default_constructible_v<std::atomic<B>>); +} + +constexpr std::atomic<int*> c; + +void +test03() +{ + VERIFY(c.load() == nullptr); + static_assert(std::is_nothrow_default_constructible_v<std::atomic<int*>>); +} + +int +main() +{ + test01(); + test02(); + test03(); +} diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/cons/value_init.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/cons/value_init.cc new file mode 100644 index 0000000..547e406 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/cons/value_init.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// 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. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include <atomic> +#include <testsuite_hooks.h> + +void +test01() +{ + std::atomic_flag f; + VERIFY(!f.test_and_set()); + VERIFY(f.test_and_set()); + static_assert(std::is_nothrow_default_constructible_v<std::atomic_flag>); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/requirements/trivial.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/requirements/trivial.cc index d08adf8..ecf7ace 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_flag/requirements/trivial.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/requirements/trivial.cc @@ -22,6 +22,10 @@ void test01() { +#if __cplusplus <= 201703L __gnu_test::has_trivial_cons_dtor test; +#else + __gnu_test::has_trivial_dtor test; +#endif test.operator()<std::atomic_flag>(); } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_float/requirements.cc b/libstdc++-v3/testsuite/29_atomics/atomic_float/requirements.cc index 1599655..b8a5562 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_float/requirements.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_float/requirements.cc @@ -25,7 +25,7 @@ test01() { using A = std::atomic<float>; static_assert( std::is_standard_layout_v<A> ); - static_assert( std::is_trivially_default_constructible_v<A> ); + static_assert( !std::is_trivially_default_constructible_v<A> ); static_assert( std::is_trivially_destructible_v<A> ); static_assert( std::is_same_v<A::value_type, float> ); static_assert( std::is_same_v<A::difference_type, A::value_type> ); @@ -41,7 +41,7 @@ test02() { using A = std::atomic<double>; static_assert( std::is_standard_layout_v<A> ); - static_assert( std::is_trivially_default_constructible_v<A> ); + static_assert( !std::is_trivially_default_constructible_v<A> ); static_assert( std::is_trivially_destructible_v<A> ); static_assert( std::is_same_v<A::value_type, double> ); static_assert( std::is_same_v<A::difference_type, A::value_type> ); @@ -57,7 +57,7 @@ test03() { using A = std::atomic<long double>; static_assert( std::is_standard_layout_v<A> ); - static_assert( std::is_trivially_default_constructible_v<A> ); + static_assert( !std::is_trivially_default_constructible_v<A> ); static_assert( std::is_trivially_destructible_v<A> ); static_assert( std::is_same_v<A::value_type, long double> ); static_assert( std::is_same_v<A::difference_type, A::value_type> ); diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_float/value_init.cc b/libstdc++-v3/testsuite/29_atomics/atomic_float/value_init.cc new file mode 100644 index 0000000..237c0dd --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_float/value_init.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// 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. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include <atomic> +#include <testsuite_hooks.h> + +constexpr std::atomic<double> a; + +void +test01() +{ + VERIFY(a.load() == 0); + static_assert(std::is_nothrow_default_constructible_v<std::atomic<double>>); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/assign_neg.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/assign_neg.cc index d842761..3c164b4 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/assign_neg.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/assign_neg.cc @@ -28,5 +28,5 @@ int main() return 0; } -// { dg-error "deleted" "" { target *-*-* } 639 } +// { dg-error "deleted" "" { target *-*-* } 659 } // { dg-prune-output "include" } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/copy_neg.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/copy_neg.cc index a83a214..0131ba2 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/copy_neg.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/copy_neg.cc @@ -28,5 +28,5 @@ int main() return 0; } -// { dg-error "deleted" "" { target *-*-* } 678 } +// { dg-error "deleted" "" { target *-*-* } 698 } // { dg-prune-output "include" } diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/value_init.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/value_init.cc new file mode 100644 index 0000000..fa1eb7a --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/cons/value_init.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// 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. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include <atomic> +#include <testsuite_hooks.h> + +constexpr std::atomic<int> a; + +void +test01() +{ + VERIFY(a.load() == 0); + static_assert(std::is_nothrow_default_constructible_v<std::atomic<int>>); +} + +int +main() +{ + test01(); +} diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_integral/requirements/trivial.cc b/libstdc++-v3/testsuite/29_atomics/atomic_integral/requirements/trivial.cc index aac1218..36aa248 100644 --- a/libstdc++-v3/testsuite/29_atomics/atomic_integral/requirements/trivial.cc +++ b/libstdc++-v3/testsuite/29_atomics/atomic_integral/requirements/trivial.cc @@ -22,7 +22,11 @@ void test01() { +#if __cplusplus <= 201703L __gnu_test::has_trivial_cons_dtor test; +#else + __gnu_test::has_trivial_dtor test; +#endif __gnu_cxx::typelist::apply_generator(test, __gnu_test::atomic_integrals::type()); } |