libstdc++: Make std::atomic_flag::test members const
authorJonathan Wakely <jwakely@redhat.com>
Fri, 12 Jun 2020 09:22:05 +0000 (10:22 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 12 Jun 2020 09:31:07 +0000 (10:31 +0100)
Also fix the tests so they run without an explicit -std=gnu++2a in the
RUNTESTFLAGS, and test the new function on const-qualified objects.

* include/bits/atomic_base.h (atomic_flag::test): Add missing
const qualifiers.
* testsuite/29_atomics/atomic_flag/test/explicit.cc: Add
dg-options and verify results of test function.
* testsuite/29_atomics/atomic_flag/test/implicit.cc: Likewise.

libstdc++-v3/include/bits/atomic_base.h
libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc
libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc

index 01f77a0..ebcfeb4 100644 (file)
@@ -212,7 +212,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #define __cpp_lib_atomic_flag_test 201907L
 
     _GLIBCXX_ALWAYS_INLINE bool
-    test(memory_order __m = memory_order_seq_cst) noexcept
+    test(memory_order __m = memory_order_seq_cst) const noexcept
     {
       __atomic_flag_data_type __v;
       __atomic_load(&_M_i, &__v, int(__m));
@@ -220,7 +220,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     }
 
     _GLIBCXX_ALWAYS_INLINE bool
-    test(memory_order __m = memory_order_seq_cst) volatile noexcept
+    test(memory_order __m = memory_order_seq_cst) const volatile noexcept
     {
       __atomic_flag_data_type __v;
       __atomic_load(&_M_i, &__v, int(__m));
index 539656b..4f2a10d 100644 (file)
@@ -1,3 +1,4 @@
+// { dg-options "-std=gnu++2a" }
 // { dg-do run { target c++2a } }
 // { dg-require-thread-fence "" }
 
 // <http://www.gnu.org/licenses/>.
 
 #include <atomic>
+#include <testsuite_hooks.h>
 
 int main()
 {
   using namespace std;
-  atomic_flag af = ATOMIC_FLAG_INIT;
 
-  if (af.test(memory_order_acquire))
-    af.clear(memory_order_release);
+  atomic_flag af0 = ATOMIC_FLAG_INIT;
+  VERIFY( ! af0.test(memory_order_acquire) );
 
-  return 0;
+  atomic_flag af{true};
+  const atomic_flag& caf = af;
+
+  VERIFY( af.test(memory_order_acquire) );
+  VERIFY( caf.test(memory_order_acquire) );
+  af.clear(memory_order_release);
+  VERIFY( ! af.test(memory_order_acquire) );
+  VERIFY( ! caf.test(memory_order_acquire) );
 }
index 11d29ac..3889f32 100644 (file)
@@ -1,3 +1,4 @@
+// { dg-options "-std=gnu++2a" }
 // { dg-do run { target c++2a } }
 // { dg-require-thread-fence "" }
 
 // <http://www.gnu.org/licenses/>.
 
 #include <atomic>
+#include <testsuite_hooks.h>
 
 int main()
 {
   using namespace std;
-  atomic_flag af = ATOMIC_FLAG_INIT;
 
-  if (af.test())
-    af.clear();
+  atomic_flag af0 = ATOMIC_FLAG_INIT;
+  VERIFY( ! af0.test(memory_order_acquire) );
 
-  return 0;
+  atomic_flag af{true};
+  const atomic_flag& caf = af;
+
+  VERIFY( af.test() );
+  VERIFY( caf.test() );
+  af.clear();
+  VERIFY( ! af.test() );
+  VERIFY( ! caf.test() );
 }