6#ifndef XENIUM_HAZARD_POINTER_HPP
7#define XENIUM_HAZARD_POINTER_HPP
9#include <xenium/reclamation/detail/concurrent_ptr.hpp>
10#include <xenium/reclamation/detail/guard_ptr.hpp>
11#include <xenium/reclamation/detail/deletable_object.hpp>
12#include <xenium/reclamation/detail/thread_block_list.hpp>
13#include <xenium/reclamation/detail/allocation_tracker.hpp>
15#include <xenium/acquire_guard.hpp>
16#include <xenium/parameter.hpp>
17#include <xenium/policy.hpp>
22namespace xenium {
namespace reclamation {
30 using std::runtime_error::runtime_error;
34 template <
class Strategy,
class Derived>
35 struct basic_hp_thread_control_block;
37 template <
size_t K_,
size_t A,
size_t B,
template <
class>
class ThreadControlBlock>
38 struct generic_hp_allocation_strategy
40 static constexpr size_t K = K_;
42 static size_t retired_nodes_threshold() {
43 return A * number_of_active_hazard_pointers() + B;
46 static size_t number_of_active_hazard_pointers() {
47 return number_of_active_hps.load(std::memory_order_relaxed);
50 using thread_control_block = ThreadControlBlock<generic_hp_allocation_strategy>;
52 friend thread_control_block;
53 friend basic_hp_thread_control_block<generic_hp_allocation_strategy, thread_control_block>;
55 inline static std::atomic<size_t> number_of_active_hps{0};
58 template <
class Strategy>
59 struct static_hp_thread_control_block;
61 template <
class Strategy>
62 struct dynamic_hp_thread_control_block;
65 namespace hp_allocation {
76 template <
size_t K = 2,
size_t A = 2,
size_t B = 100>
78 detail::generic_hp_allocation_strategy<K, A, B, detail::static_hp_thread_control_block> {};
101 template <
size_t K = 2,
size_t A = 2,
size_t B = 100>
103 detail::generic_hp_allocation_strategy<K, A, B, detail::dynamic_hp_thread_control_block> {};
106 template <
class AllocationStrategy = hp_allocation::static_strategy<3>>
107 struct hazard_pointer_traits {
108 using allocation_strategy = AllocationStrategy;
110 template <
class... Policies>
111 using with = hazard_pointer_traits<
134 template <
typename Traits = hazard_po
inter_traits<>>
137 using allocation_strategy =
typename Traits::allocation_strategy;
138 using thread_control_block =
typename allocation_strategy::thread_control_block;
139 friend detail::basic_hp_thread_control_block<allocation_strategy, thread_control_block>;
141 template <
class T,
class MarkedPtr>
155 template <
class... Policies>
158 template <
class T, std::
size_t N = 0,
class Deleter = std::default_delete<T>>
159 class enable_concurrent_ptr;
161 class region_guard {};
163 template <
class T, std::
size_t N = T::number_of_mark_bits>
170 inline static detail::thread_block_list<thread_control_block> global_thread_block_list;
171 inline static thread_local thread_data local_thread_data;
173 ALLOCATION_TRACKING_FUNCTIONS;
176 template <
typename Traits>
177 template <
class T, std::
size_t N,
class Deleter>
178 class hazard_pointer<Traits>::enable_concurrent_ptr :
179 private detail::deletable_object_impl<T, Deleter>,
180 private detail::tracked_object<hazard_pointer>
183 static constexpr std::size_t number_of_mark_bits = N;
185 enable_concurrent_ptr() noexcept = default;
186 enable_concurrent_ptr(const enable_concurrent_ptr&) noexcept = default;
187 enable_concurrent_ptr(enable_concurrent_ptr&&) noexcept = default;
188 enable_concurrent_ptr& operator=(const enable_concurrent_ptr&) noexcept = default;
189 enable_concurrent_ptr& operator=(enable_concurrent_ptr&&) noexcept = default;
190 ~enable_concurrent_ptr() noexcept = default;
192 friend detail::deletable_object_impl<T, Deleter>;
194 template <class, class>
195 friend class guard_ptr;
198 template <typename Traits>
199 template <class T, class MarkedPtr>
200 class hazard_pointer<Traits>::guard_ptr : public detail::guard_ptr<T, MarkedPtr, guard_ptr<T, MarkedPtr>>
202 using base = detail::guard_ptr<T, MarkedPtr, guard_ptr>;
203 using Deleter =
typename T::Deleter;
205 guard_ptr() noexcept = default;
208 explicit guard_ptr(const MarkedPtr& p);
209 guard_ptr(const guard_ptr& p);
210 guard_ptr(guard_ptr&& p) noexcept;
212 guard_ptr& operator=(const guard_ptr& p) noexcept;
213 guard_ptr& operator=(guard_ptr&& p) noexcept;
216 void acquire(const concurrent_ptr<T>& p, std::memory_order order = std::memory_order_seq_cst);
219 bool acquire_if_equal(const concurrent_ptr<T>& p,
220 const MarkedPtr& expected,
221 std::memory_order order = std::memory_order_seq_cst);
224 void reset() noexcept;
227 void reclaim(Deleter d = Deleter()) noexcept;
230 using enable_concurrent_ptr = hazard_pointer::enable_concurrent_ptr<T, MarkedPtr::number_of_mark_bits, Deleter>;
233 void do_swap(guard_ptr& g) noexcept;
235 typename thread_control_block::hazard_pointer* hp =
nullptr;
239#define HAZARD_POINTER_IMPL
240#include <xenium/reclamation/impl/hazard_pointer.hpp>
241#undef HAZARD_POINTER_IMPL
This exception is thrown if a thread tries to allocate a new hazard pointer, but the number of availa...
Definition: hazard_pointer.hpp:29
T must be derived from enable_concurrent_ptr<T>. D is a deleter.
Definition: concurrent_ptr.hpp:21
An implementation of the hazard pointers reclamation scheme as proposed by Michael [Mic04].
Definition: hazard_pointer.hpp:136
Policy to configure the allocation strategy.
Definition: policy.hpp:97
Hazard pointer allocation strategy for a dynamic number of hazard pointers per thread.
Definition: hazard_pointer.hpp:103
Hazard pointer allocation strategy for a static number of hazard pointers per thread.
Definition: hazard_pointer.hpp:78