semaphore 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_SEMAPHORE
  10. #define _LIBCPP_SEMAPHORE
  11. /*
  12. semaphore synopsis
  13. namespace std {
  14. template<ptrdiff_t least_max_value = implementation-defined>
  15. class counting_semaphore
  16. {
  17. public:
  18. static constexpr ptrdiff_t max() noexcept;
  19. constexpr explicit counting_semaphore(ptrdiff_t desired);
  20. ~counting_semaphore();
  21. counting_semaphore(const counting_semaphore&) = delete;
  22. counting_semaphore& operator=(const counting_semaphore&) = delete;
  23. void release(ptrdiff_t update = 1);
  24. void acquire();
  25. bool try_acquire() noexcept;
  26. template<class Rep, class Period>
  27. bool try_acquire_for(const chrono::duration<Rep, Period>& rel_time);
  28. template<class Clock, class Duration>
  29. bool try_acquire_until(const chrono::time_point<Clock, Duration>& abs_time);
  30. private:
  31. ptrdiff_t counter; // exposition only
  32. };
  33. using binary_semaphore = counting_semaphore<1>;
  34. }
  35. */
  36. #include <__config>
  37. #ifdef _LIBCPP_HAS_NO_THREADS
  38. # error "<semaphore> is not supported since libc++ has been configured without support for threads."
  39. #endif
  40. #include <__assert>
  41. #include <__atomic/atomic_base.h>
  42. #include <__atomic/atomic_sync.h>
  43. #include <__atomic/memory_order.h>
  44. #include <__availability>
  45. #include <__chrono/time_point.h>
  46. #include <__thread/poll_with_backoff.h>
  47. #include <__thread/support.h>
  48. #include <__thread/timed_backoff_policy.h>
  49. #include <cstddef>
  50. #include <limits>
  51. #include <version>
  52. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  53. # pragma GCC system_header
  54. #endif
  55. _LIBCPP_PUSH_MACROS
  56. #include <__undef_macros>
  57. #if _LIBCPP_STD_VER >= 14
  58. _LIBCPP_BEGIN_NAMESPACE_STD
  59. /*
  60. __atomic_semaphore_base is the general-case implementation.
  61. It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
  62. functions. It avoids contention against users' own use of those facilities.
  63. */
  64. # define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
  65. class __atomic_semaphore_base {
  66. __atomic_base<ptrdiff_t> __a_;
  67. public:
  68. _LIBCPP_HIDE_FROM_ABI constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count) {}
  69. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) {
  70. auto __old = __a_.fetch_add(__update, memory_order_release);
  71. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
  72. __update <= _LIBCPP_SEMAPHORE_MAX - __old, "update is greater than the expected value");
  73. if (__old == 0) {
  74. __a_.notify_all();
  75. }
  76. }
  77. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() {
  78. std::__atomic_wait_unless(
  79. __a_, [this](ptrdiff_t& __old) { return __try_acquire_impl(__old); }, memory_order_relaxed);
  80. }
  81. template <class _Rep, class _Period>
  82. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
  83. try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
  84. if (__rel_time == chrono::duration<_Rep, _Period>::zero())
  85. return try_acquire();
  86. auto const __poll_fn = [this]() { return try_acquire(); };
  87. return std::__libcpp_thread_poll_with_backoff(__poll_fn, __libcpp_timed_backoff_policy(), __rel_time);
  88. }
  89. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() {
  90. auto __old = __a_.load(memory_order_relaxed);
  91. return __try_acquire_impl(__old);
  92. }
  93. private:
  94. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool __try_acquire_impl(ptrdiff_t& __old) {
  95. while (true) {
  96. if (__old == 0)
  97. return false;
  98. if (__a_.compare_exchange_weak(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
  99. return true;
  100. }
  101. }
  102. };
  103. template <ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
  104. class counting_semaphore {
  105. __atomic_semaphore_base __semaphore_;
  106. public:
  107. static_assert(__least_max_value >= 0, "The least maximum value must be a positive number");
  108. static constexpr ptrdiff_t max() noexcept { return __least_max_value; }
  109. _LIBCPP_HIDE_FROM_ABI constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) {
  110. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
  111. __count >= 0,
  112. "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
  113. "initialized with a negative value");
  114. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
  115. __count <= max(),
  116. "counting_semaphore::counting_semaphore(ptrdiff_t): counting_semaphore cannot be "
  117. "initialized with a value greater than max()");
  118. }
  119. ~counting_semaphore() = default;
  120. counting_semaphore(const counting_semaphore&) = delete;
  121. counting_semaphore& operator=(const counting_semaphore&) = delete;
  122. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void release(ptrdiff_t __update = 1) {
  123. _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__update >= 0, "counting_semaphore:release called with a negative value");
  124. __semaphore_.release(__update);
  125. }
  126. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI void acquire() { __semaphore_.acquire(); }
  127. template <class _Rep, class _Period>
  128. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
  129. try_acquire_for(chrono::duration<_Rep, _Period> const& __rel_time) {
  130. return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
  131. }
  132. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool try_acquire() { return __semaphore_.try_acquire(); }
  133. template <class _Clock, class _Duration>
  134. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_HIDE_FROM_ABI bool
  135. try_acquire_until(chrono::time_point<_Clock, _Duration> const& __abs_time) {
  136. auto const __current = _Clock::now();
  137. if (__current >= __abs_time)
  138. return try_acquire();
  139. else
  140. return try_acquire_for(__abs_time - __current);
  141. }
  142. };
  143. using binary_semaphore = counting_semaphore<1>;
  144. _LIBCPP_END_NAMESPACE_STD
  145. #endif // _LIBCPP_STD_VER >= 14
  146. _LIBCPP_POP_MACROS
  147. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  148. # include <atomic>
  149. #endif
  150. #endif //_LIBCPP_SEMAPHORE