7#include "wfree_idle_count.hpp"
9#include "../utility/module.hpp"
15namespace hi::inline
v1 {
22template<
typename T,
typename Allocator = std::allocator<T>>
26 using allocator_type = Allocator;
33 constexpr rcu(allocator_type allocator = allocator_type{})
noexcept : _allocator(allocator) {}
36 rcu(rcu
const&) =
delete;
38 rcu& operator=(rcu
const&) =
delete;
39 rcu& operator=(rcu&&) =
delete;
60 value_type
const *
get() const noexcept
62 return _ptr.load(std::memory_order::acquire);
74#if defined(__alpha__) or defined(__alpha) or defined(_M_ALPHA)
75 return _ptr.load(std::memory_order::acquire);
77 return _ptr.load(std::memory_order::relaxed);
86 [[nodiscard]] uint64_t
version() const noexcept
97 hilet lock = std::scoped_lock(_old_ptrs_mutex);
98 return _old_ptrs.size() + not empty();
107 [[nodiscard]] value_type *
exchange(value_type *ptr)
noexcept
109 return _ptr.exchange(ptr, std::memory_order::release);
119 [[nodiscard]] value_type *
copy(value_type
const *ptr)
const noexcept
124 return std::launder(new_ptr);
133 [[nodiscard]] value_type *
copy() const noexcept
137 value_type
const *
const ptr = get();
141 return std::launder(new_ptr);
148 void abort(value_type *ptr)
const noexcept
161 auto *old_ptr = exchange(ptr);
162 auto old_version = version();
164 add_old_copy(old_version, old_ptr);
183 auto *
const old_ptr = exchange(new_ptr);
184 hilet old_version = version();
187 add_old_copy(old_version, old_ptr);
190 [[nodiscard]]
bool empty() const noexcept
192 return not to_bool(_ptr.load(std::memory_order::relaxed));
195 explicit operator bool() const noexcept
200 void reset() noexcept
203 auto *
const old_ptr = _ptr.exchange(
nullptr, std::memory_order::release);
204 hilet old_version = *_idle_count;
207 add_old_copy(old_version, old_ptr);
210 rcu& operator=(nullptr_t)
noexcept
231 hilet new_version = version();
233 hilet lock = std::scoped_lock(_old_ptrs_mutex);
234 _old_ptrs.emplace_back(old_version, old_ptr);
237 auto it = _old_ptrs.begin();
238 while (it != _old_ptrs.end() and it->first < new_version) {
243 _old_ptrs.erase(_old_ptrs.begin(), it);
250 mutable allocator_type _allocator;
251 mutable unfair_mutex _old_ptrs_mutex;
Definition of the unfair_mutex.
#define hi_assert_not_null(x,...)
Assert if an expression is not nullptr.
Definition assert.hpp:223
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
Read-copy-update.
Definition rcu.hpp:23
constexpr rcu(allocator_type allocator=allocator_type{}) noexcept
Construct a new rcu object.
Definition rcu.hpp:33
void add_old_copy(uint64_t old_version, value_type *old_ptr) noexcept
Add an old copy.
Definition rcu.hpp:225
void unlock() const noexcept
Unlock the rcu pointer for reading.
Definition rcu.hpp:50
void commit(value_type *ptr) noexcept
Commit the copied value.
Definition rcu.hpp:158
value_type * copy() const noexcept
Create a copy of the value.
Definition rcu.hpp:133
value_type const * get() const noexcept
get the rcu-pointer.
Definition rcu.hpp:60
void lock() const noexcept
Lock the rcu pointer for reading.
Definition rcu.hpp:43
value_type * exchange(value_type *ptr) noexcept
Exchange the rcu-pointers.
Definition rcu.hpp:107
value_type const * unsafe_get() noexcept
get the rcu-pointer.
Definition rcu.hpp:72
void emplace(auto &&...args) noexcept
Emplace a new value.
Definition rcu.hpp:177
size_t capacity() const noexcept
Number of objects that are currently allocated.
Definition rcu.hpp:95
uint64_t version() const noexcept
The version of the lock.
Definition rcu.hpp:86
void abort(value_type *ptr) const noexcept
Abort a copy.
Definition rcu.hpp:148
value_type * copy(value_type const *ptr) const noexcept
Create a copy of the value at the given pointer.
Definition rcu.hpp:119
Counts how many times a critical section was idle.
Definition wfree_idle_count.hpp:37