semaphore 5.4 KB

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