std::atomic_flag::test

Ulrich Drepper drepper@redhat.com
Fri May 8 15:05:16 GMT 2020


This is not yet implemented.  Here is a patch.

2020-05-08  Ulrich Drepper  <drepper@redhat.com>

        * include/bits/atomic_base.h (atomic_flag): Implement test
memeber function.
        * include/std/version: Define __cpp_lib_atomic_flag_test.
        * testsuite/29_atomics/atomic_flag/test/explicit.cc: New file.
        * testsuite/29_atomics/atomic_flag/test/implicit.cc: New file.



libatomic does not have a function 'test' so I implemented it with
__atomic_load (which takes care of memory ordering) and then compare
with the set-value.

The code generated at least for x86-64 looks good, it's a
straight-forward load, nothing else.
-------------- next part --------------
2020-05-08  Ulrich Drepper  <drepper@redhat.com>

	* include/bits/atomic_base.h (atomic_flag): Implement test memeber function.
	* include/std/version: Define __cpp_lib_atomic_flag_test.
	* testsuite/29_atomics/atomic_flag/test/explicit.cc: New file.
	* testsuite/29_atomics/atomic_flag/test/implicit.cc: New file.

diff --git libstdc++-v3/include/bits/atomic_base.h libstdc++-v3/include/bits/atomic_base.h
index 87fe0bd6000..3b66b040976 100644
--- libstdc++-v3/include/bits/atomic_base.h
+++ libstdc++-v3/include/bits/atomic_base.h
@@ -208,6 +208,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       return __atomic_test_and_set (&_M_i, int(__m));
     }
 
+#if __cplusplus > 201703L
+#define __cpp_lib_atomic_flag_test 201907L
+
+    _GLIBCXX_ALWAYS_INLINE bool
+    test(memory_order __m = memory_order_seq_cst) noexcept
+    {
+      __atomic_flag_data_type __v;
+      __atomic_load(&_M_i, &__v, int(__m));
+      return __v == __GCC_ATOMIC_TEST_AND_SET_TRUEVAL;
+    }
+
+    _GLIBCXX_ALWAYS_INLINE bool
+    test(memory_order __m = memory_order_seq_cst) volatile noexcept
+    {
+      __atomic_flag_data_type __v;
+      __atomic_load(&_M_i, &__v, int(__m));
+      return __v == __GCC_ATOMIC_TEST_AND_SET_TRUEVAL;
+    }
+#endif // C++20
+
     _GLIBCXX_ALWAYS_INLINE void
     clear(memory_order __m = memory_order_seq_cst) noexcept
     {
diff --git libstdc++-v3/include/std/version libstdc++-v3/include/std/version
index c3a5bd26e63..c6bde2cfbda 100644
--- libstdc++-v3/include/std/version
+++ libstdc++-v3/include/std/version
@@ -164,6 +164,7 @@
 
 #if __cplusplus > 201703L
 // c++2a
+#define __cpp_lib_atomic_flag_test 201907L
 #define __cpp_lib_atomic_float 201711L
 #define __cpp_lib_atomic_ref 201806L
 #define __cpp_lib_atomic_value_initialization 201911L
--- /dev/null	2020-05-07 16:14:59.793169510 +0200
+++ libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc	2020-05-08 12:53:14.134152671 +0200
@@ -0,0 +1,32 @@
+// { dg-do run { target c++2a } }
+// { dg-require-thread-fence "" }
+
+// Copyright (C) 2008-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/>.
+
+#include <atomic>
+
+int main()
+{
+  using namespace std;
+  atomic_flag af = ATOMIC_FLAG_INIT;
+
+  if (af.test(memory_order_acquire))
+    af.clear(memory_order_release);
+
+  return 0;
+}
--- /dev/null	2020-05-07 16:14:59.793169510 +0200
+++ libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc	2020-05-08 12:54:48.608014474 +0200
@@ -0,0 +1,32 @@
+// { dg-do run { target c++2a } }
+// { dg-require-thread-fence "" }
+
+// Copyright (C) 2008-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/>.
+
+#include <atomic>
+
+int main()
+{
+  using namespace std;
+  atomic_flag af = ATOMIC_FLAG_INIT;
+
+  if (af.test())
+    af.clear();
+
+  return 0;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 195 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-patches/attachments/20200508/19055135/attachment.sig>


More information about the Gcc-patches mailing list