semaphore 5.3 KB

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