From 8ce4252605361ed33b389a91fb7e3aeec529d9ac Mon Sep 17 00:00:00 2001 From: Thomas Rodgers 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 @@ +// -*- 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 +// . + +/** @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 +#include +#include + +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 + 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 + 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 _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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +#ifndef __cpp_lib_semaphore +# error "Feature-test macro for semaphore missing in " +#elif __cpp_lib_semaphore != 201907L +# error "Feature-test macro for semaphore has wrong value in " +#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 +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +#ifndef __cpp_lib_semaphore +# error "Feature-test macro for semaphore missing in " +#elif __cpp_lib_semaphore != 201907L +# error "Feature-test macro for semaphore has wrong value in " +#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 +// . + +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +#include +#include +#include + +#include + +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 +// . + +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +#include + +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 +// . + +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +#include +#include +#include +#include +#include + +int main() +{ + using lock_type = std::unique_lock; + + 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 +// . + +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +#include +#include +#include +#include +#include + +int main() +{ + using lock_type = std::unique_lock; + + 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