latch 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_LATCH
  10. #define _LIBCPP_LATCH
  11. /*
  12. latch synopsis
  13. namespace std
  14. {
  15. class latch
  16. {
  17. public:
  18. static constexpr ptrdiff_t max() noexcept;
  19. constexpr explicit latch(ptrdiff_t __expected);
  20. ~latch();
  21. latch(const latch&) = delete;
  22. latch& operator=(const latch&) = delete;
  23. void count_down(ptrdiff_t __update = 1);
  24. bool try_wait() const noexcept;
  25. void wait() const;
  26. void arrive_and_wait(ptrdiff_t __update = 1);
  27. private:
  28. ptrdiff_t __counter; // exposition only
  29. };
  30. }
  31. */
  32. #include <__assert> // all public C++ headers provide the assertion handler
  33. #include <__availability>
  34. #include <__config>
  35. #include <atomic>
  36. #include <limits>
  37. #include <version>
  38. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  39. # pragma GCC system_header
  40. #endif
  41. #ifdef _LIBCPP_HAS_NO_THREADS
  42. # error "<latch> is not supported since libc++ has been configured without support for threads."
  43. #endif
  44. _LIBCPP_PUSH_MACROS
  45. #include <__undef_macros>
  46. #if _LIBCPP_STD_VER >= 14
  47. _LIBCPP_BEGIN_NAMESPACE_STD
  48. class latch
  49. {
  50. __atomic_base<ptrdiff_t> __a_;
  51. public:
  52. static constexpr ptrdiff_t max() noexcept {
  53. return numeric_limits<ptrdiff_t>::max();
  54. }
  55. inline _LIBCPP_INLINE_VISIBILITY
  56. constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected) { }
  57. ~latch() = default;
  58. latch(const latch&) = delete;
  59. latch& operator=(const latch&) = delete;
  60. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  61. void count_down(ptrdiff_t __update = 1)
  62. {
  63. auto const __old = __a_.fetch_sub(__update, memory_order_release);
  64. if(__old == __update)
  65. __a_.notify_all();
  66. }
  67. inline _LIBCPP_INLINE_VISIBILITY
  68. bool try_wait() const noexcept
  69. {
  70. return 0 == __a_.load(memory_order_acquire);
  71. }
  72. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  73. void wait() const
  74. {
  75. __cxx_atomic_wait(&__a_.__a_, [&]() -> bool {
  76. return try_wait();
  77. });
  78. }
  79. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  80. void arrive_and_wait(ptrdiff_t __update = 1)
  81. {
  82. count_down(__update);
  83. wait();
  84. }
  85. };
  86. _LIBCPP_END_NAMESPACE_STD
  87. #endif // _LIBCPP_STD_VER >= 14
  88. _LIBCPP_POP_MACROS
  89. #endif //_LIBCPP_LATCH