semaphore 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <__assert> // all public C++ headers provide the assertion handler
  37. #include <__availability>
  38. #include <__chrono/time_point.h>
  39. #include <__config>
  40. #include <__thread/timed_backoff_policy.h>
  41. #include <__threading_support>
  42. #include <atomic>
  43. #include <limits>
  44. #include <version>
  45. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  46. # pragma GCC system_header
  47. #endif
  48. #ifdef _LIBCPP_HAS_NO_THREADS
  49. # error "<semaphore> is not supported since libc++ has been configured without support for threads."
  50. #endif
  51. _LIBCPP_PUSH_MACROS
  52. #include <__undef_macros>
  53. #if _LIBCPP_STD_VER >= 14
  54. _LIBCPP_BEGIN_NAMESPACE_STD
  55. /*
  56. __atomic_semaphore_base is the general-case implementation.
  57. It is a typical Dijkstra semaphore algorithm over atomics, wait and notify
  58. functions. It avoids contention against users' own use of those facilities.
  59. */
  60. class __atomic_semaphore_base
  61. {
  62. __atomic_base<ptrdiff_t> __a_;
  63. public:
  64. _LIBCPP_INLINE_VISIBILITY
  65. constexpr explicit __atomic_semaphore_base(ptrdiff_t __count) : __a_(__count)
  66. {
  67. }
  68. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  69. void release(ptrdiff_t __update = 1)
  70. {
  71. if(0 < __a_.fetch_add(__update, memory_order_release))
  72. ;
  73. else if(__update > 1)
  74. __a_.notify_all();
  75. else
  76. __a_.notify_one();
  77. }
  78. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  79. void acquire()
  80. {
  81. auto const __test_fn = [this]() -> bool {
  82. auto __old = __a_.load(memory_order_relaxed);
  83. return (__old != 0) && __a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed);
  84. };
  85. __cxx_atomic_wait(&__a_.__a_, __test_fn);
  86. }
  87. template <class Rep, class Period>
  88. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  89. bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
  90. {
  91. if (__rel_time == chrono::duration<Rep, Period>::zero())
  92. return try_acquire();
  93. auto const __test_fn = [this]() { return try_acquire(); };
  94. return std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy(), __rel_time);
  95. }
  96. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  97. bool try_acquire()
  98. {
  99. auto __old = __a_.load(memory_order_acquire);
  100. while (true) {
  101. if (__old == 0)
  102. return false;
  103. if (__a_.compare_exchange_strong(__old, __old - 1, memory_order_acquire, memory_order_relaxed))
  104. return true;
  105. }
  106. }
  107. };
  108. #define _LIBCPP_SEMAPHORE_MAX (numeric_limits<ptrdiff_t>::max())
  109. template<ptrdiff_t __least_max_value = _LIBCPP_SEMAPHORE_MAX>
  110. class counting_semaphore
  111. {
  112. __atomic_semaphore_base __semaphore_;
  113. public:
  114. static constexpr ptrdiff_t max() noexcept {
  115. return __least_max_value;
  116. }
  117. _LIBCPP_INLINE_VISIBILITY
  118. constexpr explicit counting_semaphore(ptrdiff_t __count) : __semaphore_(__count) { }
  119. ~counting_semaphore() = default;
  120. counting_semaphore(const counting_semaphore&) = delete;
  121. counting_semaphore& operator=(const counting_semaphore&) = delete;
  122. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  123. void release(ptrdiff_t __update = 1)
  124. {
  125. __semaphore_.release(__update);
  126. }
  127. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  128. void acquire()
  129. {
  130. __semaphore_.acquire();
  131. }
  132. template<class Rep, class Period>
  133. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  134. bool try_acquire_for(chrono::duration<Rep, Period> const& __rel_time)
  135. {
  136. return __semaphore_.try_acquire_for(chrono::duration_cast<chrono::nanoseconds>(__rel_time));
  137. }
  138. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  139. bool try_acquire()
  140. {
  141. return __semaphore_.try_acquire();
  142. }
  143. template <class Clock, class Duration>
  144. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  145. bool try_acquire_until(chrono::time_point<Clock, Duration> const& __abs_time)
  146. {
  147. auto const current = Clock::now();
  148. if (current >= __abs_time)
  149. return try_acquire();
  150. else
  151. return try_acquire_for(__abs_time - current);
  152. }
  153. };
  154. using binary_semaphore = counting_semaphore<1>;
  155. _LIBCPP_END_NAMESPACE_STD
  156. #endif // _LIBCPP_STD_VER >= 14
  157. _LIBCPP_POP_MACROS
  158. #endif //_LIBCPP_SEMAPHORE