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

Thread-local hazard pointer record with explicit memory ordering. More...

#include <safe_hazard_pointer.h>

Collaboration diagram for kcenon::thread::safe_hazard_pointer_record:
Collaboration graph

Public Member Functions

 safe_hazard_pointer_record ()
 
void protect (void *p, size_t slot=0) noexcept
 Protect a pointer from reclamation.
 
void clear (size_t slot=0) noexcept
 Clear hazard pointer protection.
 
bool contains (void *p) const noexcept
 Check if a pointer is protected by this record.
 
void * get (size_t slot=0) const noexcept
 Get protected pointer at slot.
 

Public Attributes

std::atomic< safe_hazard_pointer_record * > next {nullptr}
 
std::atomic< bool > active {false}
 

Static Public Attributes

static constexpr size_t MAX_HAZARD_POINTERS = 2
 

Private Attributes

std::array< std::atomic< void * >, MAX_HAZARD_POINTERShazard_pointers_
 

Detailed Description

Thread-local hazard pointer record with explicit memory ordering.

Each thread maintains a small array of hazard pointers. All atomic operations use explicit memory_order for correctness on weak memory model architectures (ARM, etc.)

Definition at line 34 of file safe_hazard_pointer.h.

Constructor & Destructor Documentation

◆ safe_hazard_pointer_record()

kcenon::thread::safe_hazard_pointer_record::safe_hazard_pointer_record ( )
inline
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/safe_hazard_pointer.h.

Definition at line 38 of file safe_hazard_pointer.h.

38 {
39 for (auto& hp : hazard_pointers_) {
40 hp.store(nullptr, std::memory_order_relaxed);
41 }
42 next.store(nullptr, std::memory_order_relaxed);
43 active.store(false, std::memory_order_relaxed);
44 }
std::atomic< safe_hazard_pointer_record * > next
std::array< std::atomic< void * >, MAX_HAZARD_POINTERS > hazard_pointers_

References active, hazard_pointers_, and next.

Member Function Documentation

◆ clear()

void kcenon::thread::safe_hazard_pointer_record::clear ( size_t slot = 0)
inlinenoexcept

Clear hazard pointer protection.

Parameters
slotSlot index

Uses memory_order_release to ensure the clear is visible before any subsequent operations.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/safe_hazard_pointer.h.

Definition at line 66 of file safe_hazard_pointer.h.

66 {
67 assert(slot < MAX_HAZARD_POINTERS);
68 hazard_pointers_[slot].store(nullptr, std::memory_order_release);
69 }

References hazard_pointers_, and MAX_HAZARD_POINTERS.

Referenced by kcenon::thread::safe_hazard_pointer_domain::acquire(), and kcenon::thread::safe_hazard_guard::clear().

Here is the caller graph for this function:

◆ contains()

bool kcenon::thread::safe_hazard_pointer_record::contains ( void * p) const
inlinenodiscardnoexcept

Check if a pointer is protected by this record.

Parameters
pPointer to check
Returns
true if protected

Uses memory_order_acquire to synchronize with protect() calls.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/safe_hazard_pointer.h.

Definition at line 78 of file safe_hazard_pointer.h.

78 {
79 for (const auto& hp : hazard_pointers_) {
80 if (hp.load(std::memory_order_acquire) == p) {
81 return true;
82 }
83 }
84 return false;
85 }

References hazard_pointers_.

◆ get()

void * kcenon::thread::safe_hazard_pointer_record::get ( size_t slot = 0) const
inlinenodiscardnoexcept

Get protected pointer at slot.

Parameters
slotSlot index
Returns
Protected pointer or nullptr
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/safe_hazard_pointer.h.

Definition at line 92 of file safe_hazard_pointer.h.

92 {
93 assert(slot < MAX_HAZARD_POINTERS);
94 return hazard_pointers_[slot].load(std::memory_order_acquire);
95 }

References hazard_pointers_, and MAX_HAZARD_POINTERS.

Referenced by kcenon::thread::safe_hazard_pointer_domain::collect_internal(), and kcenon::thread::safe_hazard_guard::get().

Here is the caller graph for this function:

◆ protect()

void kcenon::thread::safe_hazard_pointer_record::protect ( void * p,
size_t slot = 0 )
inlinenoexcept

Protect a pointer from reclamation.

Parameters
pPointer to protect
slotSlot index (0 or 1)

Uses memory_order_release to ensure visibility to other threads scanning hazard pointers.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/safe_hazard_pointer.h.

Definition at line 54 of file safe_hazard_pointer.h.

54 {
55 assert(slot < MAX_HAZARD_POINTERS);
56 hazard_pointers_[slot].store(p, std::memory_order_release);
57 }

References hazard_pointers_, and MAX_HAZARD_POINTERS.

Referenced by kcenon::thread::safe_hazard_guard::protect(), and kcenon::thread::safe_hazard_guard::safe_hazard_guard().

Here is the caller graph for this function:

Member Data Documentation

◆ active

std::atomic<bool> kcenon::thread::safe_hazard_pointer_record::active {false}

◆ hazard_pointers_

std::array<std::atomic<void*>, MAX_HAZARD_POINTERS> kcenon::thread::safe_hazard_pointer_record::hazard_pointers_
private

◆ MAX_HAZARD_POINTERS

size_t kcenon::thread::safe_hazard_pointer_record::MAX_HAZARD_POINTERS = 2
staticconstexpr

◆ next


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