cycle_ptr
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cycle_ptr::detail::hazard< T > Class Template Reference

Hazard pointer algorithm. More...

#include <hazard.h>

Public Types

using pointer = intrusive_ptr< T >
 Pointer used by this algorithm.
 

Public Member Functions

 hazard (const hazard &)=delete
 
 hazard () noexcept
 Create hazard context. More...
 
auto operator() (const std::atomic< T * > &ptr) noexcept -> pointer
 Load value in ptr. More...
 

Static Public Member Functions

static auto release (T *&&ptr) noexcept -> void
 Release pointer, granting it to a hazard operation, if possible. More...
 
static auto reset (std::atomic< T * > &ptr) noexcept -> void
 Reset the pointer. More...
 
static auto reset (std::atomic< T * > &ptr, pointer &&new_value) noexcept -> void
 Reset the pointer to the given new value. More...
 
static auto reset (std::atomic< T * > &ptr, const pointer &new_value) noexcept -> void
 Reset the pointer to the given new value. More...
 
static auto exchange (std::atomic< T * > &ptr, std::nullptr_t new_value) noexcept -> pointer
 Exchange the pointer. More...
 
static auto exchange (std::atomic< T * > &ptr, pointer &&new_value) noexcept -> pointer
 Exchange the pointer. More...
 
static auto exchange (std::atomic< T * > &ptr, const pointer &new_value) noexcept -> pointer
 Exchange the pointer. More...
 
static auto compare_exchange_weak (std::atomic< T * > &ptr, pointer &expected, pointer desired) noexcept -> bool
 Compare-exchange operation. More...
 
static auto compare_exchange_strong (std::atomic< T * > &ptr, pointer &expected, pointer desired) noexcept -> bool
 Compare-exchange operation. More...
 

Detailed Description

template<typename T>
class cycle_ptr::detail::hazard< T >

Hazard pointer algorithm.

While hazard algorithm is in theory lock-free and wait-free, since we cannot create an infinite amount of hazard global storage, we do have some spinning operations, that may strike when thread contention is high.

Template Parameters
TThe element type of the pointer.

Constructor & Destructor Documentation

◆ hazard()

template<typename T >
cycle_ptr::detail::hazard< T >::hazard ( )
inlineexplicitnoexcept

Create hazard context.

Used for reading hazard pointers.

Member Function Documentation

◆ compare_exchange_strong()

template<typename T >
static auto cycle_ptr::detail::hazard< T >::compare_exchange_strong ( std::atomic< T * > &  ptr,
pointer expected,
pointer  desired 
) -> bool
inlinestaticnoexcept

Compare-exchange operation.

Replaces ptr with desired, if it is equal to expected.

If this fails, expected is updated with the value stored in ptr.

Parameters
ptrThe atomic pointer to change.
expectedThe expected value of ptr.
desiredThe value to assign to ptr, if ptr holds expected.

◆ compare_exchange_weak()

template<typename T >
static auto cycle_ptr::detail::hazard< T >::compare_exchange_weak ( std::atomic< T * > &  ptr,
pointer expected,
pointer  desired 
) -> bool
inlinestaticnoexcept

Compare-exchange operation.

Replaces ptr with desired, if it is equal to expected.

If this fails, expected is updated with the value stored in ptr.

This weak operation may fail despite ptr holding expected.

Parameters
ptrThe atomic pointer to change.
expectedThe expected value of ptr.
desiredThe value to assign to ptr, if ptr holds expected.

◆ exchange() [1/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::exchange ( std::atomic< T * > &  ptr,
std::nullptr_t  new_value 
) -> pointer
inlinestaticnoexcept

Exchange the pointer.

Clears the store pointer and returns the previous value.

◆ exchange() [2/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::exchange ( std::atomic< T * > &  ptr,
pointer &&  new_value 
) -> pointer
inlinestaticnoexcept

Exchange the pointer.

Stores the pointer new_value in the hazard and returns the previous value.

◆ exchange() [3/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::exchange ( std::atomic< T * > &  ptr,
const pointer new_value 
) -> pointer
inlinestaticnoexcept

Exchange the pointer.

Stores the pointer new_value in the hazard and returns the previous value.

◆ operator()()

template<typename T >
auto cycle_ptr::detail::hazard< T >::operator() ( const std::atomic< T * > &  ptr) -> pointer
inlinenoexcept

Load value in ptr.

Returns
The value of ptr. Returned value has ownership.

◆ release()

template<typename T >
static auto cycle_ptr::detail::hazard< T >::release ( T *&&  ptr) -> void
inlinestaticnoexcept

Release pointer, granting it to a hazard operation, if possible.

Releases the pointer in a way that another hazard may be able to take ownership of ptr.

Not needed in normal operation. Only required during atomic ptr resets.

Because we only require this for pointers participating in the hazards (i.e., atomic pointers), we have to fill in all hazards at release.

If we wanted to assign to only one, we would require all pointer resets to go through the hazards. Which would be potentially error prone, not to mention cause a lot of overhead which could be entirely avoided in unshared cases.

◆ reset() [1/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::reset ( std::atomic< T * > &  ptr) -> void
inlinestaticnoexcept

Reset the pointer.

Assigns a nullptr value to ptr.

Does the correct thing to ensure the life time invariant of hazard is maintained.

◆ reset() [2/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::reset ( std::atomic< T * > &  ptr,
pointer &&  new_value 
) -> void
inlinestaticnoexcept

Reset the pointer to the given new value.

Assigns new_value to ptr.

Does the correct thing to ensure the life time invariant of hazard is maintained.

Parameters
[in,out]ptrThe atomic pointer that is to be assigned to.
[in]new_valueThe newly assigned pointer value. Ownership is transferred to ptr.

◆ reset() [3/3]

template<typename T >
static auto cycle_ptr::detail::hazard< T >::reset ( std::atomic< T * > &  ptr,
const pointer new_value 
) -> void
inlinestaticnoexcept

Reset the pointer to the given new value.

Assigns new_value to ptr.

Does the correct thing to ensure the life time invariant of hazard is maintained.

Parameters
[in,out]ptrThe atomic pointer that is to be assigned to.
[in]new_valueThe newly assigned pointer value.

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