barrier 10 KB

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