Olivier Giroux | 54fa9ec | 2020-02-18 14:58:34 | [diff] [blame^] | 1 | //===------------------------- barrier.cpp ---------------------------------===// |
| 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 <__config> |
| 10 | |
| 11 | #ifndef _LIBCPP_HAS_NO_THREADS |
| 12 | |
| 13 | #include <barrier> |
| 14 | #include <thread> |
| 15 | |
| 16 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 17 | |
| 18 | #if !defined(_LIBCPP_HAS_NO_TREE_BARRIER) && (_LIBCPP_STD_VER > 11) |
| 19 | |
| 20 | class __barrier_algorithm_base { |
| 21 | public: |
| 22 | struct alignas(64) /* naturally-align the heap state */ __state_t |
| 23 | { |
| 24 | struct { |
| 25 | __atomic_base<__barrier_phase_t> __phase = ATOMIC_VAR_INIT(0); |
| 26 | } __tickets[64]; |
| 27 | }; |
| 28 | |
| 29 | ptrdiff_t& __expected; |
| 30 | unique_ptr<__state_t[]> __state; |
| 31 | |
| 32 | _LIBCPP_HIDDEN |
| 33 | __barrier_algorithm_base(ptrdiff_t& __expected) |
| 34 | : __expected(__expected), __state(new __barrier_algorithm_base::__state_t[(__expected + 1) >> 1]) |
| 35 | { |
| 36 | } |
| 37 | _LIBCPP_HIDDEN |
| 38 | bool __arrive(__barrier_phase_t __old_phase) |
| 39 | { |
| 40 | __barrier_phase_t const __half_step = __old_phase + 1, |
| 41 | __full_step = __old_phase + 2; |
| 42 | size_t __current_expected = __expected, |
| 43 | __current = hash<thread::id>()(this_thread::get_id()) % ((__expected + 1) >> 1); |
| 44 | for(int __round = 0;; ++__round) { |
| 45 | if(__current_expected <= 1) |
| 46 | return true; |
| 47 | size_t const __end_node = ((__current_expected + 1) >> 1), |
| 48 | __last_node = __end_node - 1; |
| 49 | for(;;++__current) { |
| 50 | if(__current == __end_node) |
| 51 | __current = 0; |
| 52 | __barrier_phase_t expect = __old_phase; |
| 53 | if(__current == __last_node && (__current_expected & 1)) |
| 54 | { |
| 55 | if(__state[__current].__tickets[__round].__phase.compare_exchange_strong(expect, __full_step, memory_order_acq_rel)) |
| 56 | break; // I'm 1 in 1, go to next __round |
| 57 | } |
| 58 | else if(__state[__current].__tickets[__round].__phase.compare_exchange_strong(expect, __half_step, memory_order_acq_rel)) |
| 59 | { |
| 60 | return false; // I'm 1 in 2, done with arrival |
| 61 | } |
| 62 | else if(expect == __half_step) |
| 63 | { |
| 64 | if(__state[__current].__tickets[__round].__phase.compare_exchange_strong(expect, __full_step, memory_order_acq_rel)) |
| 65 | break; // I'm 2 in 2, go to next __round |
| 66 | } |
| 67 | } |
| 68 | __current_expected = __last_node + 1; |
| 69 | __current >>= 1; |
| 70 | } |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | _LIBCPP_EXPORTED_FROM_ABI |
| 75 | __barrier_algorithm_base * __construct_barrier_algorithm_base(ptrdiff_t& __expected) |
| 76 | { |
| 77 | return new __barrier_algorithm_base(__expected); |
| 78 | } |
| 79 | _LIBCPP_EXPORTED_FROM_ABI |
| 80 | bool __arrive_barrier_algorithm_base(__barrier_algorithm_base* __barrier, |
| 81 | __barrier_phase_t __old_phase) |
| 82 | { |
| 83 | return __barrier->__arrive(__old_phase); |
| 84 | } |
| 85 | _LIBCPP_EXPORTED_FROM_ABI |
| 86 | void __destroy_barrier_algorithm_base(__barrier_algorithm_base* __barrier) |
| 87 | { |
| 88 | delete __barrier; |
| 89 | } |
| 90 | |
| 91 | #endif //!defined(_LIBCPP_HAS_NO_TREE_BARRIER) && (_LIBCPP_STD_VER >= 11) |
| 92 | |
| 93 | _LIBCPP_END_NAMESPACE_STD |
| 94 | |
| 95 | #endif //_LIBCPP_HAS_NO_THREADS |