CNum 0.2.1
CPU-optimized ML library for C++
Loading...
Searching...
No Matches
CNum::DataStructs::HazardPointer Namespace Reference

Tools for protecting "hazerdous" objects in memory. More...

Classes

struct  HazardSlot
 A slot in which to store a protected pointer. More...
struct  Retire
 A retired protected pointer. More...
class  HazardPtrDomain
 The domain of all hazard pointers. More...
class  HazardPtrGuard
 Thread local guards for hazard pointers. More...

Functions

template<typename T>
void default_hazard_ptr_delete (void *ptr)
 Default delete function for pointers.
template<typename T>
T * protect (::std::atomic< T * > &src, HazardSlot *slot)
 Protect an atomic pointer.
template<typename T>
void protect (T *src, HazardSlot *slot)
 Protect a raw pointer.
template<typename T>
void retire (T *ptr)
 Retire a pointer.
void scan ()
 Free memory of objects pointed to by pointers that are no longer hazardous.
void flush ()
 Force a scan.

Variables

constexpr size_t MAX_HP_SLOTS = 1024
 The maximum amount of hazard pointer slots (globally) Add more as needed.
constexpr size_t SLOTS_PER_THREAD = 2
 The amount of slots 1 thread can have.
constexpr size_t RETIRED_SCAN_THRESHOLD = 64
 The threshold of Retire objects in the tls_retire at which we call scan().
thread_local ::std::vector< Retiretls_retire
 The thread local storage of retired objects.

Detailed Description

Tools for protecting "hazerdous" objects in memory.

Function Documentation

◆ default_hazard_ptr_delete()

template<typename T>
void CNum::DataStructs::HazardPointer::default_hazard_ptr_delete ( void * ptr)

Default delete function for pointers.

Parameters
ptrThe pointer to free

◆ flush()

void CNum::DataStructs::HazardPointer::flush ( )

Force a scan.

◆ protect() [1/2]

template<typename T>
T * CNum::DataStructs::HazardPointer::protect ( ::std::atomic< T * > & src,
HazardSlot * slot )

Protect an atomic pointer.

Used for ensuring that a pointer shared by multiple threads does not get deleted (freed) when another is actively using it

Parameters
srcAtomic pointer to protect
slotThe slot in which to store the pointer
Returns
The raw pointer

◆ protect() [2/2]

template<typename T>
void CNum::DataStructs::HazardPointer::protect ( T * src,
HazardSlot * slot )

Protect a raw pointer.

Used for ensuring that a pointer shared by multiple threads does not get deleted (freed) when another is actively using it

Parameters
srcThe raw pointer to protect
slotThe slot in which to store the pointer

◆ retire()

template<typename T>
void CNum::DataStructs::HazardPointer::retire ( T * ptr)

Retire a pointer.

Mark protected pointer as ready to free

Parameters
ptrThe raw pointer to free

◆ scan()

void CNum::DataStructs::HazardPointer::scan ( )

Free memory of objects pointed to by pointers that are no longer hazardous.

Iterate over the tls_retire and check if the pointers are hazardous by checking if it is active in the global domain (i.e. marked hazardous by thread)

Variable Documentation

◆ MAX_HP_SLOTS

size_t CNum::DataStructs::HazardPointer::MAX_HP_SLOTS = 1024
constexpr

The maximum amount of hazard pointer slots (globally) Add more as needed.

◆ RETIRED_SCAN_THRESHOLD

size_t CNum::DataStructs::HazardPointer::RETIRED_SCAN_THRESHOLD = 64
constexpr

The threshold of Retire objects in the tls_retire at which we call scan().

◆ SLOTS_PER_THREAD

size_t CNum::DataStructs::HazardPointer::SLOTS_PER_THREAD = 2
constexpr

The amount of slots 1 thread can have.

I assigned a value of 2 because 2 is all that is required for my lock-free concurrent queue. But for your own implementations of lock-free data structures you may need to increase this value.

◆ tls_retire

thread_local ::std::vector<Retire> CNum::DataStructs::HazardPointer::tls_retire
inline

The thread local storage of retired objects.