Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::hazard_pointer_domain< T > Class Template Reference

Domain managing hazard pointers and retirement for a specific type. More...

#include <hazard_pointer.h>

Collaboration diagram for kcenon::thread::hazard_pointer_domain< T >:
Collaboration graph

Classes

struct  stats
 Get statistics. More...
 
struct  thread_retire_list
 

Public Member Functions

hazard_pointer acquire ()
 Acquire a hazard pointer for this domain.
 
void retire (T *ptr)
 Retire an object for later reclamation.
 
size_t reclaim ()
 Force reclamation scan (optional, for testing)
 
stats get_stats () const
 
 ~hazard_pointer_domain ()
 Destructor - ensures all retire objects are reclaimed.
 

Static Public Member Functions

static hazard_pointer_domainglobal ()
 Get the global domain instance for type T.
 

Private Member Functions

 hazard_pointer_domain ()=default
 

Static Private Member Functions

static thread_retire_listget_thread_retire_list ()
 

Private Attributes

std::atomic< size_t > objects_retired_ {0}
 
std::atomic< size_t > objects_reclaimed_ {0}
 
std::atomic< size_t > scan_count_ {0}
 

Detailed Description

template<typename T>
class kcenon::thread::hazard_pointer_domain< T >

Domain managing hazard pointers and retirement for a specific type.

Template Parameters
TType of objects protected by this domain

Definition at line 165 of file hazard_pointer.h.

Constructor & Destructor Documentation

◆ ~hazard_pointer_domain()

Destructor - ensures all retire objects are reclaimed.

Definition at line 292 of file hazard_pointer.h.

292 {
293 // Ensure all objects are reclaimed before domain destruction
294 auto& retire_list = get_thread_retire_list();
295 retire_list.reclaim_all();
296}
static thread_retire_list & get_thread_retire_list()

◆ hazard_pointer_domain()

template<typename T >
kcenon::thread::hazard_pointer_domain< T >::hazard_pointer_domain ( )
privatedefault

Member Function Documentation

◆ acquire()

template<typename T >
hazard_pointer kcenon::thread::hazard_pointer_domain< T >::acquire ( )
inline

Acquire a hazard pointer for this domain.

Returns
Hazard pointer that will automatically release on destruction

Definition at line 175 of file hazard_pointer.h.

175 {
176 return hazard_pointer();
177 }

References kcenon::thread::hazard_pointer::hazard_pointer().

Here is the call graph for this function:

◆ get_stats()

template<typename T >
auto kcenon::thread::hazard_pointer_domain< T >::get_stats ( ) const

Definition at line 281 of file hazard_pointer.h.

281 {
283
284 return stats{.hazard_pointers_allocated = registry.get_active_thread_count() *
286 .objects_retired = objects_retired_.load(std::memory_order_relaxed),
287 .objects_reclaimed = objects_reclaimed_.load(std::memory_order_relaxed),
288 .scan_count = scan_count_.load(std::memory_order_relaxed)};
289}
static hazard_pointer_registry & instance()
static constexpr size_t MAX_HAZARDS_PER_THREAD

References kcenon::thread::detail::hazard_pointer_registry::instance(), and kcenon::thread::detail::thread_hazard_list::MAX_HAZARDS_PER_THREAD.

Here is the call graph for this function:

◆ get_thread_retire_list()

template<typename T >
static thread_retire_list & kcenon::thread::hazard_pointer_domain< T >::get_thread_retire_list ( )
inlinestaticprivate

Definition at line 230 of file hazard_pointer.h.

230 {
231 static thread_local thread_retire_list list;
232 return list;
233 }

◆ global()

template<typename T >
static hazard_pointer_domain & kcenon::thread::hazard_pointer_domain< T >::global ( )
inlinestatic

Get the global domain instance for type T.

Definition at line 168 of file hazard_pointer.h.

168 {
169 static hazard_pointer_domain instance;
170 return instance;
171 }

◆ reclaim()

template<typename T >
size_t kcenon::thread::hazard_pointer_domain< T >::reclaim ( )

Force reclamation scan (optional, for testing)

Returns
Number of objects reclaimed

Definition at line 261 of file hazard_pointer.h.

261 {
262 scan_count_.fetch_add(1, std::memory_order_relaxed);
263
264 // Scan all hazard pointers to get protected set
266 auto protected_ptrs = registry.scan_hazard_pointers();
267
268 // Reclaim objects not in protected set
269 auto& retire_list = get_thread_retire_list();
270 size_t reclaimed = retire_list.scan_and_reclaim(protected_ptrs);
271
272 // Also try to reclaim from global orphanage
273 // We can do this every time or periodically. Doing it every time ensures faster cleanup.
274 reclaimed += detail::global_reclamation_manager::instance().reclaim(protected_ptrs);
275
276 objects_reclaimed_.fetch_add(reclaimed, std::memory_order_relaxed);
277 return reclaimed;
278}
static global_reclamation_manager & instance()
size_t reclaim(const std::vector< void * > &protected_ptrs)
Reclaim orphaned nodes that are no longer protected.

References kcenon::thread::detail::global_reclamation_manager::instance(), kcenon::thread::detail::hazard_pointer_registry::instance(), and kcenon::thread::detail::global_reclamation_manager::reclaim().

Here is the call graph for this function:

◆ retire()

template<typename T >
void kcenon::thread::hazard_pointer_domain< T >::retire ( T * ptr)

Retire an object for later reclamation.

Parameters
ptrPointer to object to retire
Note
Object will be deleted when no hazard pointers protect it

Definition at line 244 of file hazard_pointer.h.

244 {
245 if (!ptr) {
246 return;
247 }
248
249 auto& retire_list = get_thread_retire_list();
250 retire_list.add(ptr);
251 objects_retired_.fetch_add(1, std::memory_order_relaxed);
252
253 // Check if we should run reclamation using adaptive threshold
254 // Threshold scales with active thread count to prevent excessive memory
255 if (retire_list.count >= retire_list.get_adaptive_threshold()) {
256 reclaim();
257 }
258}
size_t reclaim()
Force reclamation scan (optional, for testing)

Member Data Documentation

◆ objects_reclaimed_

template<typename T >
std::atomic<size_t> kcenon::thread::hazard_pointer_domain< T >::objects_reclaimed_ {0}
mutableprivate

Definition at line 237 of file hazard_pointer.h.

237{0};

◆ objects_retired_

template<typename T >
std::atomic<size_t> kcenon::thread::hazard_pointer_domain< T >::objects_retired_ {0}
mutableprivate

Definition at line 236 of file hazard_pointer.h.

236{0};

◆ scan_count_

template<typename T >
std::atomic<size_t> kcenon::thread::hazard_pointer_domain< T >::scan_count_ {0}
mutableprivate

Definition at line 238 of file hazard_pointer.h.

238{0};

The documentation for this class was generated from the following file: