semaphore 5.3 KB

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