Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 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 | |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 9 | #include <barrier> |
| 10 | #include <thread> |
| 11 | |
| 12 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 13 | |
Mark de Wever | 0b9b1c8 | 2021-12-13 17:30:41 | [diff] [blame] | 14 | #if !defined(_LIBCPP_HAS_NO_TREE_BARRIER) |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 15 | |
| 16 | class __barrier_algorithm_base { |
| 17 | public: |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 18 | 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 Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 23 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 24 | ptrdiff_t& __expected; |
| 25 | unique_ptr<__state_t[]> __state; |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 26 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 27 | _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 Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 54 | } |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 55 | } |
| 56 | __current_expected = __last_node + 1; |
| 57 | __current >>= 1; |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 58 | } |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 59 | } |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 60 | }; |
| 61 | |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 62 | _LIBCPP_EXPORTED_FROM_ABI __barrier_algorithm_base* __construct_barrier_algorithm_base(ptrdiff_t& __expected) { |
| 63 | return new __barrier_algorithm_base(__expected); |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 64 | } |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 65 | _LIBCPP_EXPORTED_FROM_ABI bool |
Nikolas Klauser | 4748b49 | 2024-06-12 02:18:51 | [diff] [blame] | 66 | __arrive_barrier_algorithm_base(__barrier_algorithm_base* __barrier, __barrier_phase_t __old_phase) noexcept { |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 67 | return __barrier->__arrive(__old_phase); |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 68 | } |
Nikolas Klauser | 4748b49 | 2024-06-12 02:18:51 | [diff] [blame] | 69 | _LIBCPP_EXPORTED_FROM_ABI void __destroy_barrier_algorithm_base(__barrier_algorithm_base* __barrier) noexcept { |
Louis Dionne | 9783f28 | 2023-12-18 19:01:33 | [diff] [blame] | 70 | delete __barrier; |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 71 | } |
| 72 | |
Louis Dionne | 8383351 | 2022-05-25 14:04:47 | [diff] [blame] | 73 | #endif // !defined(_LIBCPP_HAS_NO_TREE_BARRIER) |
Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame] | 74 | |
| 75 | _LIBCPP_END_NAMESPACE_STD |