blob: bbcfb6001bba1885f1e283943e5803914803ac6b [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Olivier Giroux54fa9ec2020-02-18 14:58:342//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
Olivier Giroux54fa9ec2020-02-18 14:58:349#include <barrier>
10#include <thread>
11
12_LIBCPP_BEGIN_NAMESPACE_STD
13
Mark de Wever0b9b1c82021-12-13 17:30:4114#if !defined(_LIBCPP_HAS_NO_TREE_BARRIER)
Olivier Giroux54fa9ec2020-02-18 14:58:3415
16class __barrier_algorithm_base {
17public:
Louis Dionne9783f282023-12-18 19:01:3318 struct alignas(64) /* naturally-align the heap state */ __state_t {
19 struct {
20 __atomic_base<__barrier_phase_t> __phase{0};
21 } __tickets[64];
22 };
Olivier Giroux54fa9ec2020-02-18 14:58:3423
Louis Dionne9783f282023-12-18 19:01:3324 ptrdiff_t& __expected;
25 unique_ptr<__state_t[]> __state;
Olivier Giroux54fa9ec2020-02-18 14:58:3426
Louis Dionne9783f282023-12-18 19:01:3327 _LIBCPP_HIDDEN __barrier_algorithm_base(ptrdiff_t& __expected) : __expected(__expected) {
28 size_t const __count = (__expected + 1) >> 1;
29 __state = unique_ptr<__state_t[]>(new __state_t[__count]);
30 }
31 _LIBCPP_HIDDEN bool __arrive(__barrier_phase_t __old_phase) {
32 __barrier_phase_t const __half_step = __old_phase + 1, __full_step = __old_phase + 2;
33 size_t __current_expected = __expected,
34 __current = hash<thread::id>()(this_thread::get_id()) % ((__expected + 1) >> 1);
35 for (int __round = 0;; ++__round) {
36 if (__current_expected <= 1)
37 return true;
38 size_t const __end_node = ((__current_expected + 1) >> 1), __last_node = __end_node - 1;
39 for (;; ++__current) {
40 if (__current == __end_node)
41 __current = 0;
42 __barrier_phase_t expect = __old_phase;
43 if (__current == __last_node && (__current_expected & 1)) {
44 if (__state[__current].__tickets[__round].__phase.compare_exchange_strong(
45 expect, __full_step, memory_order_acq_rel))
46 break; // I'm 1 in 1, go to next __round
47 } else if (__state[__current].__tickets[__round].__phase.compare_exchange_strong(
48 expect, __half_step, memory_order_acq_rel)) {
49 return false; // I'm 1 in 2, done with arrival
50 } else if (expect == __half_step) {
51 if (__state[__current].__tickets[__round].__phase.compare_exchange_strong(
52 expect, __full_step, memory_order_acq_rel))
53 break; // I'm 2 in 2, go to next __round
Olivier Giroux54fa9ec2020-02-18 14:58:3454 }
Louis Dionne9783f282023-12-18 19:01:3355 }
56 __current_expected = __last_node + 1;
57 __current >>= 1;
Olivier Giroux54fa9ec2020-02-18 14:58:3458 }
Louis Dionne9783f282023-12-18 19:01:3359 }
Olivier Giroux54fa9ec2020-02-18 14:58:3460};
61
Louis Dionne9783f282023-12-18 19:01:3362_LIBCPP_EXPORTED_FROM_ABI __barrier_algorithm_base* __construct_barrier_algorithm_base(ptrdiff_t& __expected) {
63 return new __barrier_algorithm_base(__expected);
Olivier Giroux54fa9ec2020-02-18 14:58:3464}
Louis Dionne9783f282023-12-18 19:01:3365_LIBCPP_EXPORTED_FROM_ABI bool
Nikolas Klauser4748b492024-06-12 02:18:5166__arrive_barrier_algorithm_base(__barrier_algorithm_base* __barrier, __barrier_phase_t __old_phase) noexcept {
Louis Dionne9783f282023-12-18 19:01:3367 return __barrier->__arrive(__old_phase);
Olivier Giroux54fa9ec2020-02-18 14:58:3468}
Nikolas Klauser4748b492024-06-12 02:18:5169_LIBCPP_EXPORTED_FROM_ABI void __destroy_barrier_algorithm_base(__barrier_algorithm_base* __barrier) noexcept {
Louis Dionne9783f282023-12-18 19:01:3370 delete __barrier;
Olivier Giroux54fa9ec2020-02-18 14:58:3471}
72
Louis Dionne83833512022-05-25 14:04:4773#endif // !defined(_LIBCPP_HAS_NO_TREE_BARRIER)
Olivier Giroux54fa9ec2020-02-18 14:58:3474
75_LIBCPP_END_NAMESPACE_STD