|
Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
|
Epoch-based memory reclamation for lock-free data structures. More...
#include <epoch_manager.h>

Public Member Functions | |
| void | enter_critical () noexcept |
| Enter critical section (pin to current epoch) | |
| void | exit_critical () noexcept |
| Exit critical section. | |
| bool | in_critical_section () const noexcept |
| Check if current thread is in critical section. | |
| void | defer_delete (void *ptr, std::function< void(void *)> deleter) |
| Defer deletion of a pointer. | |
| template<typename T > | |
| void | defer_delete (T *ptr) |
| Defer deletion of a typed pointer. | |
| size_t | try_gc () |
| Attempt garbage collection. | |
| size_t | force_gc () |
| Force garbage collection of all epochs. | |
| uint64_t | current_epoch () const noexcept |
| Get the current global epoch. | |
| size_t | gc_count () const noexcept |
| Get the number of GC cycles performed. | |
| size_t | reclaimed_count () const noexcept |
| Get the total number of objects reclaimed. | |
| size_t | pending_count () const |
| Get the number of objects pending deletion. | |
| epoch_manager (const epoch_manager &)=delete | |
| epoch_manager & | operator= (const epoch_manager &)=delete |
| epoch_manager (epoch_manager &&)=delete | |
| epoch_manager & | operator= (epoch_manager &&)=delete |
Static Public Member Functions | |
| static epoch_manager & | instance () |
| Get the singleton instance. | |
Static Public Attributes | |
| static constexpr uint64_t | INACTIVE = UINT64_MAX |
| Sentinel value indicating thread is not in critical section. | |
| static constexpr size_t | NUM_EPOCHS = 3 |
| Number of epochs in rotation (must be at least 3 for safety) | |
Private Member Functions | |
| epoch_manager ()=default | |
| Private constructor for singleton. | |
| ~epoch_manager () | |
| Destructor - clean up any remaining deferred deletions. | |
Static Private Member Functions | |
| static std::atomic< uint64_t > & | thread_epoch () |
| Get thread-local epoch reference. | |
Private Attributes | |
| std::atomic< uint64_t > | global_epoch_ {0} |
| std::atomic< size_t > | gc_count_ {0} |
| std::atomic< size_t > | reclaimed_count_ {0} |
| std::array< std::mutex, NUM_EPOCHS > | retire_mutex_ |
| std::array< std::vector< std::pair< void *, std::function< void(void *)> > >, NUM_EPOCHS > | retired_ |
Epoch-based memory reclamation for lock-free data structures.
This class implements the epoch-based reclamation (EBR) algorithm for safe memory deallocation in lock-free data structures. It uses a three-epoch system where memory is deferred until all readers from two epochs ago have completed.
The algorithm:
Properties:
Definition at line 46 of file epoch_manager.h.
|
delete |
|
delete |
|
privatedefault |
Private constructor for singleton.
|
inlineprivate |
Destructor - clean up any remaining deferred deletions.
Definition at line 264 of file epoch_manager.h.
References force_gc().

|
inlinenodiscardnoexcept |
Get the current global epoch.
Definition at line 216 of file epoch_manager.h.
References global_epoch_.
|
inline |
Defer deletion of a typed pointer.
Convenience overload that automatically creates the deleter.
| T | Type of the pointer |
| ptr | Pointer to defer deletion for |
Definition at line 131 of file epoch_manager.h.
References defer_delete().

|
inline |
Defer deletion of a pointer.
The deleter will be called when it's safe to reclaim the memory (when all readers from the current epoch have exited).
| ptr | Pointer to defer deletion for |
| deleter | Function to call to delete the pointer |
Thread safety: Uses mutex for list append
Definition at line 110 of file epoch_manager.h.
References global_epoch_, NUM_EPOCHS, retire_mutex_, and retired_.
Referenced by defer_delete().

|
inlinenoexcept |
Enter critical section (pin to current epoch)
Must be called before accessing any lock-free data structure. Must be paired with exit_critical().
Thread safety: Lock-free
Definition at line 76 of file epoch_manager.h.
References global_epoch_, and thread_epoch().
Referenced by kcenon::container::epoch_guard::epoch_guard().


|
inlinenoexcept |
Exit critical section.
Must be called after finishing access to lock-free data structures.
Thread safety: Lock-free
Definition at line 87 of file epoch_manager.h.
References INACTIVE, and thread_epoch().
Referenced by kcenon::container::epoch_guard::~epoch_guard().


|
inline |
Force garbage collection of all epochs.
This should only be called when no threads are accessing lock-free data structures (e.g., during shutdown).
Definition at line 188 of file epoch_manager.h.
References gc_count_, NUM_EPOCHS, reclaimed_count_, retire_mutex_, and retired_.
Referenced by ~epoch_manager().

|
inlinenodiscardnoexcept |
Get the number of GC cycles performed.
Definition at line 224 of file epoch_manager.h.
References gc_count_.
|
inlinenodiscardnoexcept |
Check if current thread is in critical section.
Definition at line 95 of file epoch_manager.h.
References INACTIVE, and thread_epoch().

|
inlinestatic |
Get the singleton instance.
Definition at line 63 of file epoch_manager.h.
References instance().
Referenced by kcenon::container::epoch_guard::epoch_guard(), instance(), and kcenon::container::epoch_guard::~epoch_guard().


|
delete |
|
delete |
|
inlinenodiscard |
Get the number of objects pending deletion.
Definition at line 240 of file epoch_manager.h.
References NUM_EPOCHS, retire_mutex_, and retired_.
|
inlinenodiscardnoexcept |
Get the total number of objects reclaimed.
Definition at line 232 of file epoch_manager.h.
References reclaimed_count_.
|
inlinestaticprivate |
Get thread-local epoch reference.
Definition at line 272 of file epoch_manager.h.
References INACTIVE.
Referenced by enter_critical(), exit_critical(), and in_critical_section().

|
inline |
Attempt garbage collection.
Advances the global epoch and reclaims memory from epochs that are safe to collect (all threads have exited that epoch).
Definition at line 145 of file epoch_manager.h.
References gc_count_, global_epoch_, NUM_EPOCHS, reclaimed_count_, retire_mutex_, and retired_.
|
private |
Definition at line 278 of file epoch_manager.h.
Referenced by force_gc(), gc_count(), and try_gc().
|
private |
Definition at line 277 of file epoch_manager.h.
Referenced by current_epoch(), defer_delete(), enter_critical(), and try_gc().
|
staticconstexpr |
Sentinel value indicating thread is not in critical section.
Definition at line 52 of file epoch_manager.h.
Referenced by exit_critical(), in_critical_section(), and thread_epoch().
|
staticconstexpr |
Number of epochs in rotation (must be at least 3 for safety)
Definition at line 57 of file epoch_manager.h.
Referenced by defer_delete(), force_gc(), pending_count(), and try_gc().
|
private |
Definition at line 279 of file epoch_manager.h.
Referenced by force_gc(), reclaimed_count(), and try_gc().
|
mutableprivate |
Definition at line 281 of file epoch_manager.h.
Referenced by defer_delete(), force_gc(), pending_count(), and try_gc().
|
private |
Definition at line 282 of file epoch_manager.h.
Referenced by defer_delete(), force_gc(), pending_count(), and try_gc().