barrier 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_BARRIER
  10. #define _LIBCPP_BARRIER
  11. /*
  12. barrier synopsis
  13. namespace std
  14. {
  15. template<class CompletionFunction = see below>
  16. class barrier
  17. {
  18. public:
  19. using arrival_token = see below;
  20. static constexpr ptrdiff_t max() noexcept;
  21. constexpr explicit barrier(ptrdiff_t phase_count,
  22. CompletionFunction f = CompletionFunction());
  23. ~barrier();
  24. barrier(const barrier&) = delete;
  25. barrier& operator=(const barrier&) = delete;
  26. [[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);
  27. void wait(arrival_token&& arrival) const;
  28. void arrive_and_wait();
  29. void arrive_and_drop();
  30. private:
  31. CompletionFunction completion; // exposition only
  32. };
  33. }
  34. */
  35. #include <__assert> // all public C++ headers provide the assertion handler
  36. #include <__availability>
  37. #include <__config>
  38. #include <__memory/unique_ptr.h>
  39. #include <__thread/timed_backoff_policy.h>
  40. #include <__utility/move.h>
  41. #include <atomic>
  42. #include <limits>
  43. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  44. # pragma GCC system_header
  45. #endif
  46. #ifdef _LIBCPP_HAS_NO_THREADS
  47. # error "<barrier> is not supported since libc++ has been configured without support for threads."
  48. #endif
  49. _LIBCPP_PUSH_MACROS
  50. #include <__undef_macros>
  51. #if _LIBCPP_STD_VER >= 14
  52. _LIBCPP_BEGIN_NAMESPACE_STD
  53. struct __empty_completion
  54. {
  55. inline _LIBCPP_INLINE_VISIBILITY
  56. void operator()() noexcept
  57. {
  58. }
  59. };
  60. #ifndef _LIBCPP_HAS_NO_TREE_BARRIER
  61. /*
  62. The default implementation of __barrier_base is a classic tree barrier.
  63. It looks different from literature pseudocode for two main reasons:
  64. 1. Threads that call into std::barrier functions do not provide indices,
  65. so a numbering step is added before the actual barrier algorithm,
  66. appearing as an N+1 round to the N rounds of the tree barrier.
  67. 2. A great deal of attention has been paid to avoid cache line thrashing
  68. by flattening the tree structure into cache-line sized arrays, that
  69. are indexed in an efficient way.
  70. */
  71. using __barrier_phase_t = uint8_t;
  72. class __barrier_algorithm_base;
  73. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI
  74. __barrier_algorithm_base* __construct_barrier_algorithm_base(ptrdiff_t& __expected);
  75. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI
  76. bool __arrive_barrier_algorithm_base(__barrier_algorithm_base* __barrier,
  77. __barrier_phase_t __old_phase);
  78. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_EXPORTED_FROM_ABI
  79. void __destroy_barrier_algorithm_base(__barrier_algorithm_base* __barrier);
  80. template<class _CompletionF>
  81. class __barrier_base {
  82. ptrdiff_t __expected_;
  83. unique_ptr<__barrier_algorithm_base,
  84. void (*)(__barrier_algorithm_base*)> __base_;
  85. __atomic_base<ptrdiff_t> __expected_adjustment_;
  86. _CompletionF __completion_;
  87. __atomic_base<__barrier_phase_t> __phase_;
  88. public:
  89. using arrival_token = __barrier_phase_t;
  90. static constexpr ptrdiff_t max() noexcept {
  91. return numeric_limits<ptrdiff_t>::max();
  92. }
  93. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  94. __barrier_base(ptrdiff_t __expected, _CompletionF __completion = _CompletionF())
  95. : __expected_(__expected), __base_(std::__construct_barrier_algorithm_base(this->__expected_),
  96. &__destroy_barrier_algorithm_base),
  97. __expected_adjustment_(0), __completion_(std::move(__completion)), __phase_(0)
  98. {
  99. }
  100. [[nodiscard]] _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  101. arrival_token arrive(ptrdiff_t __update)
  102. {
  103. auto const __old_phase = __phase_.load(memory_order_relaxed);
  104. for(; __update; --__update)
  105. if(__arrive_barrier_algorithm_base(__base_.get(), __old_phase)) {
  106. __completion_();
  107. __expected_ += __expected_adjustment_.load(memory_order_relaxed);
  108. __expected_adjustment_.store(0, memory_order_relaxed);
  109. __phase_.store(__old_phase + 2, memory_order_release);
  110. __phase_.notify_all();
  111. }
  112. return __old_phase;
  113. }
  114. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  115. void wait(arrival_token&& __old_phase) const
  116. {
  117. auto const __test_fn = [this, __old_phase]() -> bool {
  118. return __phase_.load(memory_order_acquire) != __old_phase;
  119. };
  120. std::__libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy());
  121. }
  122. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  123. void arrive_and_drop()
  124. {
  125. __expected_adjustment_.fetch_sub(1, memory_order_relaxed);
  126. (void)arrive(1);
  127. }
  128. };
  129. #else
  130. /*
  131. The alternative implementation of __barrier_base is a central barrier.
  132. Two versions of this algorithm are provided:
  133. 1. A fairly straightforward implementation of the litterature for the
  134. general case where the completion function is not empty.
  135. 2. An optimized implementation that exploits 2's complement arithmetic
  136. and well-defined overflow in atomic arithmetic, to handle the phase
  137. roll-over for free.
  138. */
  139. template<class _CompletionF>
  140. class __barrier_base {
  141. __atomic_base<ptrdiff_t> __expected;
  142. __atomic_base<ptrdiff_t> __arrived;
  143. _CompletionF __completion;
  144. __atomic_base<bool> __phase;
  145. public:
  146. using arrival_token = bool;
  147. static constexpr ptrdiff_t max() noexcept {
  148. return numeric_limits<ptrdiff_t>::max();
  149. }
  150. _LIBCPP_INLINE_VISIBILITY
  151. __barrier_base(ptrdiff_t __expected, _CompletionF __completion = _CompletionF())
  152. : __expected(__expected), __arrived(__expected), __completion(std::move(__completion)), __phase(false)
  153. {
  154. }
  155. [[nodiscard]] _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  156. arrival_token arrive(ptrdiff_t update)
  157. {
  158. auto const __old_phase = __phase.load(memory_order_relaxed);
  159. auto const __result = __arrived.fetch_sub(update, memory_order_acq_rel) - update;
  160. auto const new_expected = __expected.load(memory_order_relaxed);
  161. if(0 == __result) {
  162. __completion();
  163. __arrived.store(new_expected, memory_order_relaxed);
  164. __phase.store(!__old_phase, memory_order_release);
  165. __phase.notify_all();
  166. }
  167. return __old_phase;
  168. }
  169. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  170. void wait(arrival_token&& __old_phase) const
  171. {
  172. __phase.wait(__old_phase, memory_order_acquire);
  173. }
  174. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  175. void arrive_and_drop()
  176. {
  177. __expected.fetch_sub(1, memory_order_relaxed);
  178. (void)arrive(1);
  179. }
  180. };
  181. template<>
  182. class __barrier_base<__empty_completion> {
  183. static constexpr uint64_t __expected_unit = 1ull;
  184. static constexpr uint64_t __arrived_unit = 1ull << 32;
  185. static constexpr uint64_t __expected_mask = __arrived_unit - 1;
  186. static constexpr uint64_t __phase_bit = 1ull << 63;
  187. static constexpr uint64_t __arrived_mask = (__phase_bit - 1) & ~__expected_mask;
  188. __atomic_base<uint64_t> __phase_arrived_expected;
  189. static _LIBCPP_INLINE_VISIBILITY
  190. constexpr uint64_t __init(ptrdiff_t __count) _NOEXCEPT
  191. {
  192. return ((uint64_t(1u << 31) - __count) << 32)
  193. | (uint64_t(1u << 31) - __count);
  194. }
  195. public:
  196. using arrival_token = uint64_t;
  197. static constexpr ptrdiff_t max() noexcept {
  198. return ptrdiff_t(1u << 31) - 1;
  199. }
  200. _LIBCPP_INLINE_VISIBILITY
  201. explicit inline __barrier_base(ptrdiff_t __count, __empty_completion = __empty_completion())
  202. : __phase_arrived_expected(__init(__count))
  203. {
  204. }
  205. [[nodiscard]] inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  206. arrival_token arrive(ptrdiff_t update)
  207. {
  208. auto const __inc = __arrived_unit * update;
  209. auto const __old = __phase_arrived_expected.fetch_add(__inc, memory_order_acq_rel);
  210. if((__old ^ (__old + __inc)) & __phase_bit) {
  211. __phase_arrived_expected.fetch_add((__old & __expected_mask) << 32, memory_order_relaxed);
  212. __phase_arrived_expected.notify_all();
  213. }
  214. return __old & __phase_bit;
  215. }
  216. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  217. void wait(arrival_token&& __phase) const
  218. {
  219. auto const __test_fn = [=]() -> bool {
  220. uint64_t const __current = __phase_arrived_expected.load(memory_order_acquire);
  221. return ((__current & __phase_bit) != __phase);
  222. };
  223. __libcpp_thread_poll_with_backoff(__test_fn, __libcpp_timed_backoff_policy());
  224. }
  225. inline _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  226. void arrive_and_drop()
  227. {
  228. __phase_arrived_expected.fetch_add(__expected_unit, memory_order_relaxed);
  229. (void)arrive(1);
  230. }
  231. };
  232. #endif // !_LIBCPP_HAS_NO_TREE_BARRIER
  233. template<class _CompletionF = __empty_completion>
  234. class barrier {
  235. __barrier_base<_CompletionF> __b_;
  236. public:
  237. using arrival_token = typename __barrier_base<_CompletionF>::arrival_token;
  238. static constexpr ptrdiff_t max() noexcept {
  239. return __barrier_base<_CompletionF>::max();
  240. }
  241. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  242. barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF())
  243. : __b_(__count, _VSTD::move(__completion)) {
  244. }
  245. barrier(barrier const&) = delete;
  246. barrier& operator=(barrier const&) = delete;
  247. [[nodiscard]] _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  248. arrival_token arrive(ptrdiff_t __update = 1)
  249. {
  250. return __b_.arrive(__update);
  251. }
  252. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  253. void wait(arrival_token&& __phase) const
  254. {
  255. __b_.wait(_VSTD::move(__phase));
  256. }
  257. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  258. void arrive_and_wait()
  259. {
  260. wait(arrive());
  261. }
  262. _LIBCPP_AVAILABILITY_SYNC _LIBCPP_INLINE_VISIBILITY
  263. void arrive_and_drop()
  264. {
  265. __b_.arrive_and_drop();
  266. }
  267. };
  268. _LIBCPP_END_NAMESPACE_STD
  269. #endif // _LIBCPP_STD_VER >= 14
  270. _LIBCPP_POP_MACROS
  271. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  272. # include <concepts>
  273. # include <iterator>
  274. # include <memory>
  275. # include <stdexcept>
  276. # include <variant>
  277. #endif
  278. #endif //_LIBCPP_BARRIER