From dec869c95562c0b1255f770e68e7ea72e7e92681 Mon Sep 17 00:00:00 2001
From: Thomas W Rodgers <rodgert@twrodgers.com>
Date: Fri, 10 Feb 2023 09:35:11 -0800
Subject: [PATCH 474/547] libstdc++: Add missing free functions for atomic_flag
 [PR103934]

This patch adds -
  atomic_flag_test
  atomic_flag_test_explicit

Which were missed when commit 491ba6 introduced C++20 atomic flag
test.

libstdc++-v3/ChangeLog:

	PR libstdc++/103934
	* include/std/atomic (atomic_flag_test): Add.
	(atomic_flag_test_explicit): Add.
	* testsuite/29_atomics/atomic_flag/test/explicit.cc: Add
	test case to cover missing atomic_flag free functions.
	* testsuite/29_atomics/atomic_flag/test/implicit.cc:
	Likewise.

(cherry picked from commit a8d769045b43e8509490362865a85cb31a855ccf)
---
 libstdc++-v3/include/std/atomic               | 20 ++++++++++++++
 .../29_atomics/atomic_flag/test/explicit.cc   | 26 ++++++++++++++++++-
 .../29_atomics/atomic_flag/test/implicit.cc   | 26 ++++++++++++++++++-
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/std/atomic b/libstdc++-v3/include/std/atomic
index cf5d7309334..9e042d8ce44 100644
--- a/libstdc++-v3/include/std/atomic
+++ b/libstdc++-v3/include/std/atomic
@@ -1216,6 +1216,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 				    memory_order __m) noexcept
   { return __a->test_and_set(__m); }
 
+#if __cpp_lib_atomic_flag_test
+  inline bool
+  atomic_flag_test(const atomic_flag* __a) noexcept
+  { return __a->test(); }
+
+  inline bool
+  atomic_flag_test(const volatile atomic_flag* __a) noexcept
+  { return __a->test(); }
+
+  inline bool
+  atomic_flag_test_explicit(const atomic_flag* __a,
+			    memory_order __m) noexcept
+  { return __a->test(__m); }
+
+  inline bool
+  atomic_flag_test_explicit(const volatile atomic_flag* __a,
+			    memory_order __m) noexcept
+  { return __a->test(__m); }
+#endif
+
   inline void
   atomic_flag_clear_explicit(atomic_flag* __a, memory_order __m) noexcept
   { __a->clear(__m); }
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc
index f4a42cce930..6cb2d1bfdf8 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/explicit.cc
@@ -22,7 +22,8 @@
 #include <atomic>
 #include <testsuite_hooks.h>
 
-int main()
+void
+test01()
 {
   using namespace std;
 
@@ -38,3 +39,26 @@ int main()
   VERIFY( ! af.test(memory_order_acquire) );
   VERIFY( ! caf.test(memory_order_acquire) );
 }
+
+void
+test02()
+{
+  using namespace std;
+
+  atomic_flag af{true};
+  const atomic_flag& caf = af;
+
+  VERIFY( atomic_flag_test_explicit(&af, memory_order_acquire) );
+  VERIFY( atomic_flag_test_explicit(&caf, memory_order_acquire) );
+  af.clear(memory_order_release);
+  VERIFY( ! atomic_flag_test_explicit(&af, memory_order_acquire) );
+  VERIFY( ! atomic_flag_test_explicit(&caf, memory_order_acquire) );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  return 0;
+}
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc
index 6dc83d16da7..9919732bc8a 100644
--- a/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/test/implicit.cc
@@ -22,7 +22,8 @@
 #include <atomic>
 #include <testsuite_hooks.h>
 
-int main()
+void
+test01()
 {
   using namespace std;
 
@@ -38,3 +39,26 @@ int main()
   VERIFY( ! af.test() );
   VERIFY( ! caf.test() );
 }
+
+void
+test02()
+{
+  using namespace std;
+
+  atomic_flag af{true};
+  const atomic_flag& caf = af;
+
+  VERIFY( atomic_flag_test(&af) );
+  VERIFY( atomic_flag_test(&caf) );
+  af.clear(memory_order_release);
+  VERIFY( ! atomic_flag_test(&af) );
+  VERIFY( ! atomic_flag_test(&caf) );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  return 0;
+}
-- 
2.25.1

