cycle_ptr
|
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... | |
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.
T | The element type of the pointer. |
|
inlineexplicitnoexcept |
Create hazard context.
Used for reading hazard pointers.
|
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
.
ptr | The atomic pointer to change. |
expected | The expected value of ptr . |
desired | The value to assign to ptr , if ptr holds expected . |
|
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
.
ptr | The atomic pointer to change. |
expected | The expected value of ptr . |
desired | The value to assign to ptr , if ptr holds expected . |
|
inlinestaticnoexcept |
Exchange the pointer.
Clears the store pointer and returns the previous value.
|
inlinestaticnoexcept |
Exchange the pointer.
Stores the pointer new_value
in the hazard and returns the previous value.
|
inlinestaticnoexcept |
Exchange the pointer.
Stores the pointer new_value
in the hazard and returns the previous value.
|
inlinenoexcept |
Load value in ptr.
|
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.
|
inlinestaticnoexcept |
Reset the pointer.
Assigns a nullptr value to ptr
.
Does the correct thing to ensure the life time invariant of hazard is maintained.
|
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.
[in,out] | ptr | The atomic pointer that is to be assigned to. |
[in] | new_value | The newly assigned pointer value. Ownership is transferred to ptr . |
|
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.
[in,out] | ptr | The atomic pointer that is to be assigned to. |
[in] | new_value | The newly assigned pointer value. |