1 // <barrier> -*- C++ -*-
3 // Copyright (C) 2020-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
20 // This implementation is based on libcxx/include/barrier
21 //===-- barrier.h --------------------------------------------------===//
23 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
24 // See https://llvm.org/LICENSE.txt for license information.
25 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
27 //===---------------------------------------------------------------===//
29 /** @file include/barrier
30 * This is a Standard C++ Library header.
33 #ifndef _GLIBCXX_BARRIER
34 #define _GLIBCXX_BARRIER 1
36 #pragma GCC system_header
38 #if __cplusplus > 201703L
39 #include <bits/atomic_base.h>
40 #if __cpp_lib_atomic_wait && __cpp_aligned_new
41 #include <bits/std_thread.h>
42 #include <bits/unique_ptr.h>
46 #define __cpp_lib_barrier 201907L
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 struct __empty_completion
54 _GLIBCXX_ALWAYS_INLINE void
61 The default implementation of __tree_barrier 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.
73 enum class __barrier_phase_t : unsigned char { };
75 template<typename _CompletionF>
78 using __atomic_phase_ref_t = std::__atomic_ref<__barrier_phase_t>;
79 using __atomic_phase_const_ref_t = std::__atomic_ref<const __barrier_phase_t>;
80 static constexpr auto __phase_alignment =
81 __atomic_phase_ref_t::required_alignment;
83 using __tickets_t = std::array<__barrier_phase_t, 64>;
84 struct alignas(64) /* naturally-align the heap state */ __state_t
86 alignas(__phase_alignment) __tickets_t __tickets;
89 ptrdiff_t _M_expected;
90 unique_ptr<__state_t[]> _M_state;
91 __atomic_base<ptrdiff_t> _M_expected_adjustment;
92 _CompletionF _M_completion;
94 alignas(__phase_alignment) __barrier_phase_t _M_phase;
97 _M_arrive(__barrier_phase_t __old_phase, size_t __current)
99 const auto __old_phase_val = static_cast<unsigned char>(__old_phase);
100 const auto __half_step =
101 static_cast<__barrier_phase_t>(__old_phase_val + 1);
102 const auto __full_step =
103 static_cast<__barrier_phase_t>(__old_phase_val + 2);
105 size_t __current_expected = _M_expected;
106 std::hash<std::thread::id> __hasher;
107 __current %= ((_M_expected + 1) >> 1);
109 for (int __round = 0; ; ++__round)
111 if (__current_expected <= 1)
113 size_t const __end_node = ((__current_expected + 1) >> 1),
114 __last_node = __end_node - 1;
115 for ( ; ; ++__current)
117 if (__current == __end_node)
119 auto __expect = __old_phase;
120 __atomic_phase_ref_t __phase(_M_state[__current]
121 .__tickets[__round]);
122 if (__current == __last_node && (__current_expected & 1))
124 if (__phase.compare_exchange_strong(__expect, __full_step,
125 memory_order_acq_rel))
126 break; // I'm 1 in 1, go to next __round
128 else if (__phase.compare_exchange_strong(__expect, __half_step,
129 memory_order_acq_rel))
131 return false; // I'm 1 in 2, done with arrival
133 else if (__expect == __half_step)
135 if (__phase.compare_exchange_strong(__expect, __full_step,
136 memory_order_acq_rel))
137 break; // I'm 2 in 2, go to next __round
140 __current_expected = __last_node + 1;
146 using arrival_token = __barrier_phase_t;
148 static constexpr ptrdiff_t
150 { return __PTRDIFF_MAX__; }
152 __tree_barrier(ptrdiff_t __expected, _CompletionF __completion)
153 : _M_expected(__expected), _M_expected_adjustment(0),
154 _M_completion(move(__completion)),
155 _M_phase(static_cast<__barrier_phase_t>(0))
157 size_t const __count = (_M_expected + 1) >> 1;
159 _M_state = std::make_unique<__state_t[]>(__count);
162 [[nodiscard]] arrival_token
163 arrive(ptrdiff_t __update)
165 std::hash<std::thread::id> __hasher;
166 size_t __current = __hasher(std::this_thread::get_id());
167 __atomic_phase_ref_t __phase(_M_phase);
168 const auto __old_phase = __phase.load(memory_order_relaxed);
169 const auto __cur = static_cast<unsigned char>(__old_phase);
170 for(; __update; --__update)
172 if(_M_arrive(__old_phase, __current))
175 _M_expected += _M_expected_adjustment.load(memory_order_relaxed);
176 _M_expected_adjustment.store(0, memory_order_relaxed);
177 auto __new_phase = static_cast<__barrier_phase_t>(__cur + 2);
178 __phase.store(__new_phase, memory_order_release);
179 __phase.notify_all();
186 wait(arrival_token&& __old_phase) const
188 __atomic_phase_const_ref_t __phase(_M_phase);
189 auto const __test_fn = [=]
191 return __phase.load(memory_order_acquire) != __old_phase;
193 std::__atomic_wait_address(&_M_phase, __test_fn);
199 _M_expected_adjustment.fetch_sub(1, memory_order_relaxed);
204 template<typename _CompletionF = __empty_completion>
207 // Note, we may introduce a "central" barrier algorithm at some point
208 // for more space constrained targets
209 using __algorithm_t = __tree_barrier<_CompletionF>;
213 class arrival_token final
216 arrival_token(arrival_token&&) = default;
217 arrival_token& operator=(arrival_token&&) = default;
218 ~arrival_token() = default;
221 friend class barrier;
222 using __token = typename __algorithm_t::arrival_token;
223 explicit arrival_token(__token __tok) noexcept : _M_tok(__tok) { }
227 static constexpr ptrdiff_t
229 { return __algorithm_t::max(); }
232 barrier(ptrdiff_t __count, _CompletionF __completion = _CompletionF())
233 : _M_b(__count, std::move(__completion))
236 barrier(barrier const&) = delete;
237 barrier& operator=(barrier const&) = delete;
239 [[nodiscard]] arrival_token
240 arrive(ptrdiff_t __update = 1)
241 { return arrival_token{_M_b.arrive(__update)}; }
244 wait(arrival_token&& __phase) const
245 { _M_b.wait(std::move(__phase._M_tok)); }
253 { _M_b.arrive_and_drop(); }
256 _GLIBCXX_END_NAMESPACE_VERSION
258 #endif // __cpp_lib_atomic_wait && __cpp_aligned_new
259 #endif // __cplusplus > 201703L
260 #endif // _GLIBCXX_BARRIER