From: Thomas Rodgers <trodgers@redhat.com>
To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org
Subject: [PATCH] Add c++2a binary_semaphore
Date: Tue, 18 Feb 2020 06:46:00 -0000 [thread overview]
Message-ID: <1291008602.5930779.1582008369882.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <381004109.5930741.1582008200673.JavaMail.zimbra@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 713 bytes --]
This patch adds the c++2a semaphore header and binary_semaphore type. The implementation is not complete, this patch is just to solicit initial feedback.
* include/std/semaphore: New file.
* include/std/version (__cpp_lib_semaphore): Add feature test macro.
* include/Makefile.am (std_headers): add semaphore.
* include/Makefile.in: Regenerate.
* testsuite/30_threads/semaphore/1.cc: New test.
* testsuite/30_threads/semaphore/2.cc: New test.
* testsuite/30_threads/semaphore/binary_semaphore.cc: New test.
* testsuite/30_threads/semaphore/try_acquire.cc: New test.
* testsuite/30_threads/semaphore/try_acquire_for.cc: New test.
* testsuite/30_threads/semaphore/try_acquire_until.cc: New test.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-c-2a-binary_semaphore.patch --]
[-- Type: text/x-patch; name=0001-Add-c-2a-binary_semaphore.patch, Size: 17351 bytes --]
From 8ce4252605361ed33b389a91fb7e3aeec529d9ac Mon Sep 17 00:00:00 2001
From: Thomas Rodgers <rodgert@thufir.appliantology.local>
Date: Mon, 17 Feb 2020 19:58:57 -0800
Subject: [PATCH] Add c++2a binary_semaphore
* include/std/semaphore: New file.
* include/std/version (__cpp_lib_semaphore): Add feature test macro.
* include/Makefile.am (std_headers): add semaphore.
* include/Makefile.in: Regenerate.
* testsuite/30_threads/semaphore/1.cc: New test.
* testsuite/30_threads/semaphore/2.cc: New test.
* testsuite/30_threads/semaphore/binary_semaphore.cc: New test.
* testsuite/30_threads/semaphore/try_acquire.cc: New test.
* testsuite/30_threads/semaphore/try_acquire_for.cc: New test.
* testsuite/30_threads/semaphore/try_acquire_until.cc: New test.
---
libstdc++-v3/include/Makefile.am | 1 +
libstdc++-v3/include/Makefile.in | 1 +
libstdc++-v3/include/std/semaphore | 131 ++++++++++++++++++
libstdc++-v3/include/std/version | 1 +
.../testsuite/30_threads/semaphore/1.cc | 27 ++++
.../testsuite/30_threads/semaphore/2.cc | 27 ++++
.../30_threads/semaphore/binary_semaphore.cc | 47 +++++++
.../30_threads/semaphore/try_acquire.cc | 36 +++++
.../30_threads/semaphore/try_acquire_for.cc | 72 ++++++++++
.../30_threads/semaphore/try_acquire_until.cc | 74 ++++++++++
10 files changed, 417 insertions(+)
create mode 100644 libstdc++-v3/include/std/semaphore
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/1.cc
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/2.cc
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/binary_semaphore.cc
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/try_acquire.cc
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 89835759069..2dbb7d3a6b1 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -69,6 +69,7 @@ std_headers = \
${std_srcdir}/ratio \
${std_srcdir}/regex \
${std_srcdir}/scoped_allocator \
+ ${std_srcdir}/semaphore \
${std_srcdir}/set \
${std_srcdir}/shared_mutex \
${std_srcdir}/span \
diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in
index 123d24bb1c6..8f2780c86ce 100644
--- a/libstdc++-v3/include/Makefile.in
+++ b/libstdc++-v3/include/Makefile.in
@@ -414,6 +414,7 @@ std_headers = \
${std_srcdir}/ratio \
${std_srcdir}/regex \
${std_srcdir}/scoped_allocator \
+ ${std_srcdir}/semaphore \
${std_srcdir}/set \
${std_srcdir}/shared_mutex \
${std_srcdir}/span \
diff --git a/libstdc++-v3/include/std/semaphore b/libstdc++-v3/include/std/semaphore
new file mode 100644
index 00000000000..e3e88a50eec
--- /dev/null
+++ b/libstdc++-v3/include/std/semaphore
@@ -0,0 +1,131 @@
+// <stop_token> -*- C++ -*-
+
+// Copyright (C) 2019-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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file include/stop_token
+ * This is a Standard C++ Library header.
+ */
+
+#ifndef _GLIBCXX_SEMAPHORE
+#define _GLIBCXX_SEMAPHORE
+
+#if __cplusplus > 201703L
+#define __cpp_lib_semaphore 201907L
+
+#include <cstddef>
+#include <atomic>
+#include <chrono>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+ // TODO: replace this with a real implementation of std::binary_semaphore
+ struct binary_semaphore
+{
+ explicit binary_semaphore(int __d) : _M_counter(__d > 0) { }
+
+ static constexpr std::ptrdiff_t
+ max() noexcept
+ {
+ return 1;
+ }
+
+ void release() { _M_counter.fetch_add(1, memory_order::release); }
+
+ void acquire()
+ {
+ while (!_M_try_acquire())
+ {
+ _S_yield();
+ }
+ }
+
+ bool
+ try_acquire() noexcept
+ {
+ return _M_try_acquire(1u);
+ }
+
+ template<class _Rep, class _Period>
+ bool try_acquire_for(const std::chrono::duration<_Rep, _Period>& __rel_time)
+ {
+ auto __abst = std::chrono::steady_clock::now() + __rel_time;
+ return try_acquire_until(__abst);
+ }
+
+ template<class _Clock, class _Duration>
+ bool try_acquire_until(const std::chrono::time_point<_Clock, _Duration>& __abs_time)
+ {
+ do
+ {
+ if (_M_try_acquire())
+ {
+ return true;
+ }
+ } while (std::chrono::steady_clock::now() < __abs_time);
+ return false;
+ }
+
+private:
+ static void
+ _S_yield() noexcept
+ {
+#if defined __i386__ || defined __x86_64__
+ __builtin_ia32_pause();
+#elif defined _GLIBCXX_USE_SCHED_YIELD
+ __gthread_yield();
+#endif
+ }
+
+ bool
+ _M_try_acquire(unsigned __spin_ct)
+ {
+ int __old = 1;
+ while (!_M_counter.compare_exchange_weak(__old, 0,
+ memory_order::acquire,
+ memory_order::relaxed))
+ {
+ if (--__spin_ct == 0)
+ {
+ return false;
+ }
+ __old = 1;
+ }
+ return true;
+ }
+
+ static constexpr unsigned _S_spin_ct = 64u;
+ bool
+ _M_try_acquire()
+ {
+ return _M_try_acquire(1);
+ }
+
+ atomic<int> _M_counter;
+};
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
+#endif // __cplusplus > 201703L
+#endif // _GLIBCXX_STOP_TOKEN
diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version
index d8a97767453..4654830b614 100644
--- a/libstdc++-v3/include/std/version
+++ b/libstdc++-v3/include/std/version
@@ -188,6 +188,7 @@
#define __cpp_lib_interpolate 201902L
#ifdef _GLIBCXX_HAS_GTHREADS
# define __cpp_lib_jthread 201907L
+# define __cpp_lib_semaphore 201907L
#endif
#define __cpp_lib_list_remove_return_type 201806L
#define __cpp_lib_math_constants 201907L
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/1.cc b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc
new file mode 100644
index 00000000000..1bbca687fc3
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/1.cc
@@ -0,0 +1,27 @@
+// 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 compile { target c++2a } }
+
+#include <semaphore>
+
+#ifndef __cpp_lib_semaphore
+# error "Feature-test macro for semaphore missing in <semaphore>"
+#elif __cpp_lib_semaphore != 201907L
+# error "Feature-test macro for semaphore has wrong value in <semaphore>"
+#endif
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/2.cc b/libstdc++-v3/testsuite/30_threads/semaphore/2.cc
new file mode 100644
index 00000000000..b96b8a59c64
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/2.cc
@@ -0,0 +1,27 @@
+// Copyright (C) 2019-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 compile { target c++2a } }
+
+#include <version>
+
+#ifndef __cpp_lib_semaphore
+# error "Feature-test macro for semaphore missing in <version>"
+#elif __cpp_lib_semaphore != 201907L
+# error "Feature-test macro for semaphore has wrong value in <version>"
+#endif
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/binary_semaphore.cc b/libstdc++-v3/testsuite/30_threads/semaphore/binary_semaphore.cc
new file mode 100644
index 00000000000..1a42d323ca0
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/binary_semaphore.cc
@@ -0,0 +1,47 @@
+// Copyright (C) 2019-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 -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+
+#include <semaphore>
+#include <chrono>
+#include <thread>
+
+#include <testsuite_hooks.h>
+
+int main()
+{
+ std::binary_semaphore s(1);
+
+ auto ar = [&]() {
+ for(auto i = 0u; i < 512; ++i) {
+ s.acquire();
+ std::this_thread::sleep_for(std::chrono::microseconds(1));
+ s.release();
+ }
+ };
+
+ std::thread t(ar);
+ ar();
+
+ t.join();
+
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire.cc
new file mode 100644
index 00000000000..295cfa9bca8
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire.cc
@@ -0,0 +1,36 @@
+// Copyright (C) 2019-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 -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+
+#include <semaphore>
+
+int main()
+{
+ std::binary_semaphore s(1);
+
+ s.acquire();
+ if (s.try_acquire())
+ return -1;
+ s.release();
+ if (!s.try_acquire())
+ return -1;
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
new file mode 100644
index 00000000000..a04b39b5401
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_for.cc
@@ -0,0 +1,72 @@
+// Copyright (C) 2019-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 -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+
+#include <semaphore>
+#include <chrono>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+
+int main()
+{
+ using lock_type = std::unique_lock<std::mutex>;
+
+ bool ready = false;
+ lock_type::mutex_type m;
+ std::condition_variable cv;
+
+ std::binary_semaphore s(1);
+ auto ar =[&] {
+ s.acquire();
+ {
+ lock_type l(m);
+ ready = true;
+ }
+ cv.notify_one();
+ std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ s.release();
+ };
+
+ std::thread t1(ar);
+ {
+ lock_type l(m, std::defer_lock);
+ cv.wait(l, [&] { return ready; });
+
+ if (s.try_acquire_for(std::chrono::milliseconds(100)))
+ return -1;
+ t1.join();
+ if (!s.try_acquire())
+ return -1;
+ s.release();
+ ready = false;
+ }
+
+ std::thread t2(ar);
+ {
+ lock_type l(m, std::defer_lock);
+ cv.wait(l, [&] { return ready; });
+ if (!s.try_acquire_for(std::chrono::milliseconds(1009)))
+ return -1;
+ t2.join();
+ }
+ return 0;
+}
diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
new file mode 100644
index 00000000000..987b655a6da
--- /dev/null
+++ b/libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_until.cc
@@ -0,0 +1,74 @@
+// Copyright (C) 2019-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 -pthread" }
+// { dg-do run { target c++2a } }
+// { dg-require-effective-target pthread }
+// { dg-require-gthreads "" }
+
+#include <semaphore>
+#include <chrono>
+#include <thread>
+#include <mutex>
+#include <condition_variable>
+
+int main()
+{
+ using lock_type = std::unique_lock<std::mutex>;
+
+ bool ready = false;
+ lock_type::mutex_type m;
+ std::condition_variable cv;
+
+ auto abst = std::chrono::steady_clock::now();
+
+ std::binary_semaphore s(1);
+ auto ar =[&] {
+ s.acquire();
+ {
+ lock_type l(m);
+ ready = true;
+ }
+ cv.notify_one();
+ std::this_thread::sleep_until(abst + std::chrono::milliseconds(500));
+ s.release();
+ };
+
+ std::thread t1(ar);
+ {
+ lock_type l(m, std::defer_lock);
+ cv.wait(l, [&] { return ready; });
+
+ if (s.try_acquire_until(abst + std::chrono::milliseconds(100)))
+ return -1;
+ t1.join();
+ if (!s.try_acquire())
+ return -1;
+ s.release();
+ ready = false;
+ }
+
+ std::thread t2(ar);
+ {
+ lock_type l(m, std::defer_lock);
+ cv.wait(l, [&] { return ready; });
+ if (!s.try_acquire_until(abst + std::chrono::milliseconds(1009)))
+ return -1;
+ t2.join();
+ }
+ return 0;
+}
--
2.24.1
next parent reply other threads:[~2020-02-18 6:46 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <381004109.5930741.1582008200673.JavaMail.zimbra@redhat.com>
2020-02-18 6:46 ` Thomas Rodgers [this message]
2020-02-18 13:49 ` Sebastian Huber
2020-02-18 14:31 ` Jonathan Wakely
2020-02-21 12:33 ` Sebastian Huber
2020-02-18 14:25 ` Jonathan Wakely
2020-02-20 3:19 ` Thomas Rodgers
2020-02-25 2:53 ` Thomas Rodgers
2020-02-25 8:53 ` Sebastian Huber
2020-02-25 12:33 ` Jonathan Wakely
2020-02-25 12:39 ` Jonathan Wakely
2020-02-26 16:07 ` Jonathan Wakely
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1291008602.5930779.1582008369882.JavaMail.zimbra@redhat.com \
--to=trodgers@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).