7#include "wfree_idle_count.hpp"
9#include "../utility/utility.hpp"
10#include "../macros.hpp"
25template<
typename T,
typename Allocator = std::allocator<T>>
29 using allocator_type = Allocator;
36 constexpr rcu(allocator_type allocator = allocator_type{})
noexcept : _allocator(allocator) {}
39 rcu(rcu
const&) =
delete;
41 rcu& operator=(rcu
const&) =
delete;
42 rcu& operator=(rcu&&) =
delete;
63 value_type
const *
get() const noexcept
65 return _ptr.load(std::memory_order::acquire);
77#if defined(__alpha__) or defined(__alpha) or defined(_M_ALPHA)
78 return _ptr.load(std::memory_order::acquire);
80 return _ptr.load(std::memory_order::relaxed);
89 [[nodiscard]] uint64_t
version() const noexcept
100 hilet lock = std::scoped_lock(_old_ptrs_mutex);
101 return _old_ptrs.size() + not empty();
110 [[nodiscard]] value_type *
exchange(value_type *ptr)
noexcept
112 return _ptr.exchange(ptr, std::memory_order::release);
122 [[nodiscard]] value_type *
copy(value_type
const *ptr)
const noexcept
124 hi_assert_not_null(ptr);
127 return std::launder(new_ptr);
136 [[nodiscard]] value_type *
copy() const noexcept
140 value_type
const *
const ptr = get();
141 hi_assert_not_null(ptr);
144 return std::launder(new_ptr);
151 void abort(value_type *ptr)
const noexcept
164 auto *old_ptr = exchange(ptr);
165 auto old_version = version();
167 add_old_copy(old_version, old_ptr);
186 auto *
const old_ptr = exchange(new_ptr);
187 hilet old_version = version();
190 add_old_copy(old_version, old_ptr);
193 [[nodiscard]]
bool empty() const noexcept
195 return not to_bool(_ptr.load(std::memory_order::relaxed));
198 explicit operator bool() const noexcept
203 void reset() noexcept
206 auto *
const old_ptr = _ptr.exchange(
nullptr, std::memory_order::release);
207 hilet old_version = *_idle_count;
210 add_old_copy(old_version, old_ptr);
213 rcu& operator=(nullptr_t)
noexcept
234 hilet new_version = version();
236 hilet lock = std::scoped_lock(_old_ptrs_mutex);
237 _old_ptrs.emplace_back(old_version, old_ptr);
240 auto it = _old_ptrs.begin();
241 while (it != _old_ptrs.end() and it->first < new_version) {
246 _old_ptrs.erase(_old_ptrs.begin(), it);
253 mutable allocator_type _allocator;
254 mutable unfair_mutex _old_ptrs_mutex;
Definition of the unfair_mutex.
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Read-copy-update.
Definition rcu.hpp:26
constexpr rcu(allocator_type allocator=allocator_type{}) noexcept
Construct a new rcu object.
Definition rcu.hpp:36
void add_old_copy(uint64_t old_version, value_type *old_ptr) noexcept
Add an old copy.
Definition rcu.hpp:228
void unlock() const noexcept
Unlock the rcu pointer for reading.
Definition rcu.hpp:53
void commit(value_type *ptr) noexcept
Commit the copied value.
Definition rcu.hpp:161
value_type * copy() const noexcept
Create a copy of the value.
Definition rcu.hpp:136
value_type const * get() const noexcept
get the rcu-pointer.
Definition rcu.hpp:63
void lock() const noexcept
Lock the rcu pointer for reading.
Definition rcu.hpp:46
value_type * exchange(value_type *ptr) noexcept
Exchange the rcu-pointers.
Definition rcu.hpp:110
value_type const * unsafe_get() noexcept
get the rcu-pointer.
Definition rcu.hpp:75
void emplace(auto &&...args) noexcept
Emplace a new value.
Definition rcu.hpp:180
size_t capacity() const noexcept
Number of objects that are currently allocated.
Definition rcu.hpp:98
uint64_t version() const noexcept
The version of the lock.
Definition rcu.hpp:89
void abort(value_type *ptr) const noexcept
Abort a copy.
Definition rcu.hpp:151
value_type * copy(value_type const *ptr) const noexcept
Create a copy of the value at the given pointer.
Definition rcu.hpp:122
Counts how many times a critical section was idle.
Definition wfree_idle_count.hpp:40