* [PATCH] Add support for atomic_flag::wait/notify_one/notify_all
@ 2020-09-08 18:06 Thomas Rodgers
0 siblings, 0 replies; only message in thread
From: Thomas Rodgers @ 2020-09-08 18:06 UTC (permalink / raw)
To: gcc-patches, libstdc++; +Cc: trodgers, Thomas Rodgers
* include/bits/atomic_base.h (__atomic_flag::wait): Define.
(__atomic_flag::notify_one): Likewise.
(__atomic_flag::notify_all): Likewise.
* testsuite/29_atomics/atomic_flag/wait_notify/1.cc: New test.
---
libstdc++-v3/include/bits/atomic_base.h | 23 +++++++
.../29_atomics/atomic_flag/wait_notify/1.cc | 61 +++++++++++++++++++
2 files changed, 84 insertions(+)
create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h
index c121d993fee..a7ddd03d544 100644
--- a/libstdc++-v3/include/bits/atomic_base.h
+++ b/libstdc++-v3/include/bits/atomic_base.h
@@ -229,6 +229,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
__atomic_load(&_M_i, &__v, int(__m));
return __v == __GCC_ATOMIC_TEST_AND_SET_TRUEVAL;
}
+
+ _GLIBCXX_ALWAYS_INLINE void
+ wait(bool __old,
+ memory_order __m = memory_order_seq_cst) const noexcept
+ {
+ std::__atomic_wait(&_M_i, __old,
+ [__m, this, __old]()
+ { return this->test(__m) != __old; });
+ }
+
+ // TODO add const volatile overload
+
+ _GLIBCXX_ALWAYS_INLINE void
+ notify_one() const noexcept
+ { std::__atomic_notify(&_M_i, false); }
+
+ // TODO add const volatile overload
+
+ _GLIBCXX_ALWAYS_INLINE void
+ notify_all() const noexcept
+ { std::__atomic_notify(&_M_i, true); }
+
+ // TODO add const volatile overload
#endif // C++20
_GLIBCXX_ALWAYS_INLINE void
diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
new file mode 100644
index 00000000000..6de7873ecc2
--- /dev/null
+++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc
@@ -0,0 +1,61 @@
+// { dg-options "-std=gnu++2a -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+
+// 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/>.
+
+#include <atomic>
+#include <chrono>
+#include <condition_variable>
+#include <concepts>
+#include <mutex>
+#include <thread>
+
+#include <testsuite_hooks.h>
+
+int
+main()
+{
+ using namespace std::literals::chrono_literals;
+
+ std::mutex m;
+ std::condition_variable cv;
+
+ std::atomic_flag a;
+ std::atomic_flag b;
+ std::thread t([&]
+ {
+ cv.notify_one();
+ a.wait(false);
+ b.test_and_set();
+ b.notify_one();
+ });
+
+ std::unique_lock<std::mutex> l(m);
+ cv.wait(l);
+ std::this_thread::sleep_for(100ms);
+ a.test_and_set();
+ a.notify_one();
+ b.wait(false);
+ t.join();
+
+ VERIFY( a.test() );
+ VERIFY( b.test() );
+ return 0;
+}
--
2.26.2
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2020-09-08 18:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-08 18:06 [PATCH] Add support for atomic_flag::wait/notify_one/notify_all Thomas Rodgers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).