1//===----------------------------------------------------------------------===//
2//
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
9#include <barrier>
10#include <thread>
11
12_LIBCPP_BEGIN_NAMESPACE_STD
13_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
14
15class __barrier_algorithm_base {
16public:
17 struct alignas(64) /* naturally-align the heap state */ __state_t {
18 struct {
19 atomic<__barrier_phase_t> __phase{0};
20 } __tickets[64];
21 };
22
23 ptrdiff_t& __expected_;
24 unique_ptr<__state_t[]> __state_;
25
26 _LIBCPP_HIDDEN __barrier_algorithm_base(ptrdiff_t& __expected) : __expected_(__expected) {
27 size_t const __count = (__expected + 1) >> 1;
28 __state_ = unique_ptr<__state_t[]>(new __state_t[__count]);
29 }
30 _LIBCPP_HIDDEN bool __arrive(__barrier_phase_t __old_phase) {
31 __barrier_phase_t const __half_step = __old_phase + 1, __full_step = __old_phase + 2;
32 size_t __current_expected = __expected_,
33 __current = hash<thread::id>()(this_thread::get_id()) % ((__expected_ + 1) >> 1);
34 for (int __round = 0;; ++__round) {
35 if (__current_expected <= 1)
36 return true;
37 size_t const __end_node = ((__current_expected + 1) >> 1), __last_node = __end_node - 1;
38 for (;; ++__current) {
39 if (__current == __end_node)
40 __current = 0;
41 __barrier_phase_t expect = __old_phase;
42 if (__current == __last_node && (__current_expected & 1)) {
43 if (__state_[__current].__tickets[__round].__phase.compare_exchange_strong(
44 e&: expect, d: __full_step, m: memory_order_acq_rel))
45 break; // I'm 1 in 1, go to next __round
46 } else if (__state_[__current].__tickets[__round].__phase.compare_exchange_strong(
47 e&: expect, d: __half_step, m: memory_order_acq_rel)) {
48 return false; // I'm 1 in 2, done with arrival
49 } else if (expect == __half_step) {
50 if (__state_[__current].__tickets[__round].__phase.compare_exchange_strong(
51 e&: expect, d: __full_step, m: memory_order_acq_rel))
52 break; // I'm 2 in 2, go to next __round
53 }
54 }
55 __current_expected = __last_node + 1;
56 __current >>= 1;
57 }
58 }
59};
60
61_LIBCPP_EXPORTED_FROM_ABI __barrier_algorithm_base* __construct_barrier_algorithm_base(ptrdiff_t& __expected) {
62 return new __barrier_algorithm_base(__expected);
63}
64_LIBCPP_EXPORTED_FROM_ABI bool __arrive_barrier_algorithm_base(
65 _LIBCPP_NOESCAPE __barrier_algorithm_base* __barrier, __barrier_phase_t __old_phase) noexcept {
66 return __barrier->__arrive(__old_phase);
67}
68_LIBCPP_EXPORTED_FROM_ABI void
69__destroy_barrier_algorithm_base(_LIBCPP_NOESCAPE __barrier_algorithm_base* __barrier) noexcept {
70 delete __barrier;
71}
72
73_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
74_LIBCPP_END_NAMESPACE_STD
75