Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::rcu_value< T > Class Template Reference

Lock-free value wrapper using Read-Copy-Update (RCU) pattern. More...

#include <rcu_value.h>

Collaboration diagram for kcenon::container::rcu_value< T >:
Collaboration graph

Public Member Functions

 rcu_value ()
 Default constructor - initializes with default-constructed T.
 
 rcu_value (T initial)
 Construct with initial value.
 
 rcu_value (const rcu_value &other)
 Copy constructor.
 
 rcu_value (rcu_value &&other) noexcept
 Move constructor.
 
rcu_valueoperator= (const rcu_value &other)
 Copy assignment operator.
 
rcu_valueoperator= (rcu_value &&other) noexcept
 Move assignment operator.
 
 ~rcu_value ()=default
 Destructor.
 
std::shared_ptr< const T > read () const noexcept
 Lock-free read - returns current snapshot.
 
void update (T new_value)
 Update the value atomically.
 
bool compare_and_update (const std::shared_ptr< const T > &expected, T new_value)
 Compare-and-swap update.
 
size_t update_count () const noexcept
 Get the number of updates performed.
 
bool has_value () const noexcept
 Check if the value has been initialized.
 

Private Attributes

std::shared_ptr< const T > current_
 
std::atomic< size_t > update_count_ {0}
 

Detailed Description

template<typename T>
class kcenon::container::rcu_value< T >

Lock-free value wrapper using Read-Copy-Update (RCU) pattern.

This template provides truly lock-free reads using atomic shared_ptr operations. Writers create new versions, and readers access the current version atomically. Old versions are automatically reclaimed when the last reader releases them.

Properties:

  • Read: Wait-free O(1) - no blocking, no spinning
  • Update: Lock-free - may require retry under contention
  • Memory: Automatic reclamation via shared_ptr reference counting
Template Parameters
TThe value type to store (must be copy-constructible or move-constructible)

Example usage:

rcu_value<int> counter{0};
// Lock-free read from any thread
auto snapshot = counter.read();
int value = *snapshot;
// Update from any thread
counter.update(42);
Lock-free value wrapper using Read-Copy-Update (RCU) pattern.
Definition rcu_value.h:41
std::shared_ptr< const T > read() const noexcept
Lock-free read - returns current snapshot.
Definition rcu_value.h:115
Enhanced type-safe value with perfect legacy compatibility.
Definition value.h:129

Definition at line 40 of file rcu_value.h.

Constructor & Destructor Documentation

◆ rcu_value() [1/4]

template<typename T >
kcenon::container::rcu_value< T >::rcu_value ( )
inline

Default constructor - initializes with default-constructed T.

Definition at line 49 of file rcu_value.h.

50 : current_(std::make_shared<const T>()) {}
std::shared_ptr< const T > current_
Definition rcu_value.h:181

◆ rcu_value() [2/4]

template<typename T >
kcenon::container::rcu_value< T >::rcu_value ( T initial)
inlineexplicit

Construct with initial value.

Parameters
initialThe initial value

Definition at line 56 of file rcu_value.h.

57 : current_(std::make_shared<const T>(std::move(initial))) {}

◆ rcu_value() [3/4]

template<typename T >
kcenon::container::rcu_value< T >::rcu_value ( const rcu_value< T > & other)
inline

Copy constructor.

Parameters
otherThe rcu_value to copy from

Definition at line 63 of file rcu_value.h.

64 : current_(std::atomic_load_explicit(&other.current_, std::memory_order_acquire)) {}

◆ rcu_value() [4/4]

template<typename T >
kcenon::container::rcu_value< T >::rcu_value ( rcu_value< T > && other)
inlinenoexcept

Move constructor.

Parameters
otherThe rcu_value to move from

Definition at line 70 of file rcu_value.h.

71 : current_(std::atomic_load_explicit(&other.current_, std::memory_order_acquire)) {}

◆ ~rcu_value()

template<typename T >
kcenon::container::rcu_value< T >::~rcu_value ( )
default

Destructor.

Member Function Documentation

◆ compare_and_update()

template<typename T >
bool kcenon::container::rcu_value< T >::compare_and_update ( const std::shared_ptr< const T > & expected,
T new_value )
inline

Compare-and-swap update.

Atomically updates the value only if the current value equals expected. This is useful for implementing lock-free algorithms that need to detect concurrent modifications.

Parameters
expectedShared pointer to expected current value
new_valueThe new value to store if current matches expected
Returns
true if update succeeded, false if current value changed

Thread safety: Safe to call concurrently with any other operation

Definition at line 148 of file rcu_value.h.

148 {
149 auto new_ptr = std::make_shared<const T>(std::move(new_value));
150 auto expected_copy = expected;
151 bool success = std::atomic_compare_exchange_strong_explicit(
152 &current_,
153 &expected_copy,
154 new_ptr,
155 std::memory_order_acq_rel,
156 std::memory_order_acquire
157 );
158 if (success) {
159 update_count_.fetch_add(1, std::memory_order_relaxed);
160 }
161 return success;
162 }
std::atomic< size_t > update_count_
Definition rcu_value.h:182

References kcenon::container::rcu_value< T >::current_, and kcenon::container::rcu_value< T >::update_count_.

◆ has_value()

template<typename T >
bool kcenon::container::rcu_value< T >::has_value ( ) const
inlinenodiscardnoexcept

Check if the value has been initialized.

Returns
true if a value is stored, false if null

Definition at line 176 of file rcu_value.h.

176 {
177 return std::atomic_load_explicit(&current_, std::memory_order_acquire) != nullptr;
178 }

References kcenon::container::rcu_value< T >::current_.

◆ operator=() [1/2]

template<typename T >
rcu_value & kcenon::container::rcu_value< T >::operator= ( const rcu_value< T > & other)
inline

Copy assignment operator.

Parameters
otherThe rcu_value to copy from
Returns
Reference to this

Definition at line 78 of file rcu_value.h.

78 {
79 if (this != &other) {
80 auto new_ptr = std::atomic_load_explicit(&other.current_, std::memory_order_acquire);
81 std::atomic_store_explicit(&current_, new_ptr, std::memory_order_release);
82 }
83 return *this;
84 }

References kcenon::container::rcu_value< T >::current_.

◆ operator=() [2/2]

template<typename T >
rcu_value & kcenon::container::rcu_value< T >::operator= ( rcu_value< T > && other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe rcu_value to move from
Returns
Reference to this

Definition at line 91 of file rcu_value.h.

91 {
92 if (this != &other) {
93 auto new_ptr = std::atomic_load_explicit(&other.current_, std::memory_order_acquire);
94 std::atomic_store_explicit(&current_, new_ptr, std::memory_order_release);
95 }
96 return *this;
97 }

References kcenon::container::rcu_value< T >::current_.

◆ read()

template<typename T >
std::shared_ptr< const T > kcenon::container::rcu_value< T >::read ( ) const
inlinenodiscardnoexcept

Lock-free read - returns current snapshot.

This operation is wait-free: it completes in O(1) time regardless of what other threads are doing. The returned shared_ptr keeps the snapshot alive even if the value is updated by another thread.

Returns
Shared pointer to immutable snapshot of current value

Thread safety: Safe to call concurrently with any other operation

Definition at line 115 of file rcu_value.h.

115 {
116 return std::atomic_load_explicit(&current_, std::memory_order_acquire);
117 }

References kcenon::container::rcu_value< T >::current_.

◆ update()

template<typename T >
void kcenon::container::rcu_value< T >::update ( T new_value)
inline

Update the value atomically.

Creates a new immutable version and publishes it atomically. The old version is automatically reclaimed when the last reader releases it.

Parameters
new_valueThe new value to store

Thread safety: Safe to call concurrently with any other operation

Definition at line 129 of file rcu_value.h.

129 {
130 auto new_ptr = std::make_shared<const T>(std::move(new_value));
131 std::atomic_store_explicit(&current_, new_ptr, std::memory_order_release);
132 update_count_.fetch_add(1, std::memory_order_relaxed);
133 }

References kcenon::container::rcu_value< T >::current_, and kcenon::container::rcu_value< T >::update_count_.

◆ update_count()

template<typename T >
size_t kcenon::container::rcu_value< T >::update_count ( ) const
inlinenodiscardnoexcept

Get the number of updates performed.

Returns
Update count since construction

Definition at line 168 of file rcu_value.h.

168 {
169 return update_count_.load(std::memory_order_relaxed);
170 }

References kcenon::container::rcu_value< T >::update_count_.

Member Data Documentation

◆ current_

◆ update_count_

template<typename T >
std::atomic<size_t> kcenon::container::rcu_value< T >::update_count_ {0}
private

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