latch 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <__atomic/atomic_base.h>
  34. #include <__atomic/atomic_sync.h>
  35. #include <__atomic/memory_order.h>
  36. #include <__availability>
  37. #include <__config>
  38. #include <cstddef>
  39. #include <limits>
  40. #include <version>
  41. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  42. # pragma GCC system_header
  43. #endif
  44. #ifdef _LIBCPP_HAS_NO_THREADS
  45. # error "<latch> is not supported since libc++ has been configured without support for threads."
  46. #endif
  47. _LIBCPP_PUSH_MACROS
  48. #include <__undef_macros>
  49. #if _LIBCPP_STD_VER >= 14
  50. _LIBCPP_BEGIN_NAMESPACE_STD
  51. class latch
  52. {
  53. __atomic_base<ptrdiff_t> __a_;
  54. public:
  55. static _LIBCPP_HIDE_FROM_ABI constexpr ptrdiff_t max() noexcept {
  56. return numeric_limits<ptrdiff_t>::max();
  57. }
  58. inline _LIBCPP_INLINE_VISIBILITY
  59. constexpr explicit latch(ptrdiff_t __expected) : __a_(__expected)
  60. {
  61. _LIBCPP_ASSERT_UNCATEGORIZED(__expected >= 0,
  62. "latch::latch(ptrdiff_t): latch cannot be "
  63. "initialized with a negative value");
  64. _LIBCPP_ASSERT_UNCATEGORIZED(__expected <= max(),
  65. "latch::latch(ptrdiff_t): latch cannot be "
  66. "initialized with a value greater than max()");
  67. }
  68. _LIBCPP_HIDE_FROM_ABI ~latch() = default;
  69. latch(const latch&) = delete;
  70. latch& operator=(const latch&) = delete;
  71. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  72. void count_down(ptrdiff_t __update = 1)
  73. {
  74. _LIBCPP_ASSERT_UNCATEGORIZED(
  75. __update >= 0, "latch::count_down called with a negative value");
  76. auto const __old = __a_.fetch_sub(__update, memory_order_release);
  77. _LIBCPP_ASSERT_UNCATEGORIZED(
  78. __update <= __old, "latch::count_down called with a value greater "
  79. "than the internal counter");
  80. if (__old == __update)
  81. __a_.notify_all();
  82. }
  83. inline _LIBCPP_INLINE_VISIBILITY
  84. bool try_wait() const noexcept
  85. {
  86. return 0 == __a_.load(memory_order_acquire);
  87. }
  88. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  89. void wait() const
  90. {
  91. __cxx_atomic_wait(&__a_.__a_, [this]() -> bool {
  92. return try_wait();
  93. });
  94. }
  95. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  96. void arrive_and_wait(ptrdiff_t __update = 1)
  97. {
  98. _LIBCPP_ASSERT_UNCATEGORIZED(
  99. __update >= 0, "latch::arrive_and_wait called with a negative value");
  100. // other preconditions on __update are checked in count_down()
  101. count_down(__update);
  102. wait();
  103. }
  104. };
  105. _LIBCPP_END_NAMESPACE_STD
  106. #endif // _LIBCPP_STD_VER >= 14
  107. _LIBCPP_POP_MACROS
  108. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  109. # include <atomic>
  110. #endif
  111. #endif //_LIBCPP_LATCH