6 #ifndef XENIUM_CHASE_WORK_STEALING_DEQUE_HPP
7 #define XENIUM_CHASE_WORK_STEALING_DEQUE_HPP
9 #include <xenium/detail/fixed_size_circular_array.hpp>
10 #include <xenium/detail/growing_circular_array.hpp>
11 #include <xenium/parameter.hpp>
12 #include <xenium/policy.hpp>
37 template <
class T,
class... Policies>
39 using value_type = T*;
40 static constexpr std::size_t capacity =
41 parameter::value_param_t<std::size_t,
policy::capacity, 128, Policies...>::value;
43 parameter::type_param_t<policy::container, detail::growing_circular_array<T, capacity>, Policies...>;
53 bool try_push(value_type item);
54 [[nodiscard]]
bool try_pop(value_type& result);
55 [[nodiscard]]
bool try_steal(value_type& result);
58 auto t = _top.load(std::memory_order_relaxed);
59 return _bottom.load(std::memory_order_relaxed) - t;
64 std::atomic<std::size_t> _bottom;
65 std::atomic<std::size_t> _top;
68 template <
class T,
class... Policies>
71 template <
class T,
class... Policies>
72 bool chase_work_stealing_deque<T, Policies...>::try_push(value_type item) {
73 auto b = _bottom.load(std::memory_order_relaxed);
74 auto t = _top.load(std::memory_order_relaxed);
76 if (size >= _items.capacity()) {
77 if (_items.can_grow()) {
79 assert(size < _items.capacity());
86 _items.put(b, item, std::memory_order_relaxed);
89 _bottom.store(b + 1, std::memory_order_release);
93 template <
class T,
class... Policies>
94 bool chase_work_stealing_deque<T, Policies...>::try_pop(value_type& result) {
95 auto b = _bottom.load(std::memory_order_relaxed);
96 auto t = _top.load(std::memory_order_relaxed);
107 _bottom.store(b, std::memory_order_seq_cst);
109 auto* item = _items.get(b, std::memory_order_relaxed);
111 t = _top.load(std::memory_order_seq_cst);
118 if (_top.compare_exchange_strong(t, t + 1, std::memory_order_relaxed)) {
119 _bottom.store(t + 1, std::memory_order_relaxed);
123 _bottom.store(t, std::memory_order_relaxed);
128 _bottom.store(t, std::memory_order_relaxed);
132 template <
class T,
class... Policies>
133 bool chase_work_stealing_deque<T, Policies...>::try_steal(value_type& result) {
134 auto t = _top.load(std::memory_order_relaxed);
138 auto b = _bottom.load(std::memory_order_seq_cst);
139 auto size =
static_cast<std::intptr_t
>(b) -
static_cast<std::intptr_t
>(t);
144 auto* item = _items.get(t, std::memory_order_relaxed);
146 if (_top.compare_exchange_strong(t, t + 1, std::memory_order_seq_cst, std::memory_order_relaxed)) {