latch 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // <latch> -*- C++ -*-
  2. // Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/latch
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_LATCH
  24. #define _GLIBCXX_LATCH 1
  25. #pragma GCC system_header
  26. #if __cplusplus > 201703L
  27. #include <bits/atomic_base.h>
  28. #include <ext/numeric_traits.h>
  29. #if __cpp_lib_atomic_wait
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. #define __cpp_lib_latch 201907L
  34. class latch
  35. {
  36. public:
  37. static constexpr ptrdiff_t
  38. max() noexcept
  39. { return __gnu_cxx::__int_traits<__detail::__platform_wait_t>::__max; }
  40. constexpr explicit latch(ptrdiff_t __expected) noexcept
  41. : _M_a(__expected) { }
  42. ~latch() = default;
  43. latch(const latch&) = delete;
  44. latch& operator=(const latch&) = delete;
  45. _GLIBCXX_ALWAYS_INLINE void
  46. count_down(ptrdiff_t __update = 1)
  47. {
  48. auto const __old = __atomic_impl::fetch_sub(&_M_a,
  49. __update, memory_order::release);
  50. if (__old == __update)
  51. __atomic_impl::notify_all(&_M_a);
  52. }
  53. _GLIBCXX_ALWAYS_INLINE bool
  54. try_wait() const noexcept
  55. { return __atomic_impl::load(&_M_a, memory_order::acquire) == 0; }
  56. _GLIBCXX_ALWAYS_INLINE void
  57. wait() const noexcept
  58. {
  59. auto const __pred = [this] { return this->try_wait(); };
  60. std::__atomic_wait_address(&_M_a, __pred);
  61. }
  62. _GLIBCXX_ALWAYS_INLINE void
  63. arrive_and_wait(ptrdiff_t __update = 1) noexcept
  64. {
  65. count_down(__update);
  66. wait();
  67. }
  68. private:
  69. alignas(__alignof__(__detail::__platform_wait_t)) __detail::__platform_wait_t _M_a;
  70. };
  71. _GLIBCXX_END_NAMESPACE_VERSION
  72. } // namespace
  73. #endif // __cpp_lib_atomic_wait
  74. #endif // __cplusplus > 201703L
  75. #endif // _GLIBCXX_LATCH