Thread View: comp.lang.c++
31 messages
31 total messages
Started by Chris M. Thomasson
Wed, 07 Oct 2026 04:33
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 04:33
Date: Wed, 07 Oct 2026 04:33
27 lines
3002 bytes
3002 bytes
On 10/5/2026 12:23 PM, Bonita Montero wrote: > Am 05.10.2026 um 19:57 schrieb Chris M. Thomasson: > >> Not sure what you mean. Traditional RCU reads do not need any atomic >> RMW's and/or memory barriers (alpha aside for a moment). > > My idea is that a thread periodically polls for updates on shared data. > Between these updates the data may become stale. The thread-local poin- > ter is inside a shared_obj<T>. The central threaded analogy for an > atomic<shared_ptr<T>> is my tshared_obj<T>. > As long as the shared tshared_obj<T> doesn't change the updates are > nearly for free. If the object changes the access is locked, but not > with a separate futex; the pointer itself is the futex, meaning that > it goes to -1 while there's a deferred update of the reference coun- > ter the object points to. The pointer changes back to its initial > value once the change has completed. > The following diagram shows the micro-benchmark erformance of updating > a shared_obj<T> periodically from a tshred_obj<T>, which itself doesn't > change. The scaling is logarithmic. As you see the update is nearly for > free. The atomic shared_ptr's update is up to more than 10.000 times > slower: Bench mark it against Joes atomic ptr that uses DWCAS. Keep in mind that we can use it to make a proxy collector that has RCU like performance as well. Keep in mind that it involves a thread being able to take a reference to something it does no already own. No locks. Once a local_ptr is gained its basically equal to basic thread saftey normal atomic refcount.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 04:34
Date: Wed, 07 Oct 2026 04:34
30 lines
3125 bytes
3125 bytes
On 10/6/2026 9:33 PM, Chris M. Thomasson wrote: > On 10/5/2026 12:23 PM, Bonita Montero wrote: >> Am 05.10.2026 um 19:57 schrieb Chris M. Thomasson: >> >>> Not sure what you mean. Traditional RCU reads do not need any atomic >>> RMW's and/or memory barriers (alpha aside for a moment). >> >> My idea is that a thread periodically polls for updates on shared data. >> Between these updates the data may become stale. The thread-local poin- >> ter is inside a shared_obj<T>. The central threaded analogy for an >> atomic<shared_ptr<T>> is my tshared_obj<T>. >> As long as the shared tshared_obj<T> doesn't change the updates are >> nearly for free. If the object changes the access is locked, but not >> with a separate futex; the pointer itself is the futex, meaning that >> it goes to -1 while there's a deferred update of the reference coun- >> ter the object points to. The pointer changes back to its initial >> value once the change has completed. >> The following diagram shows the micro-benchmark erformance of updating >> a shared_obj<T> periodically from a tshred_obj<T>, which itself doesn't >> change. The scaling is logarithmic. As you see the update is nearly for >> free. The atomic shared_ptr's update is up to more than 10.000 times >> slower: > Bench mark it against Joes atomic ptr that uses DWCAS. Keep in mind that > we can use it to make a proxy collector that has RCU like performance as > well. Keep in mind that it involves a thread being able to take a > reference to something it does no already own. No locks. Once a > local_ptr is gained its basically equal to basic thread saftey normal > atomic refcount. No locks, no futex, etc...
Re: Consequential RCU
Author: Bonita Montero
Date: Wed, 07 Oct 2026 13:00
Date: Wed, 07 Oct 2026 13:00
23 lines
2769 bytes
2769 bytes
Am 07.10.2026 um 06:33 schrieb Chris M. Thomasson: > Bench mark it against Joes atomic ptr that uses DWCAS. ... You still dont' understand my idea. Imagine you've got a shared_ptr<X> and you constantly poll an atomic<shared_ptr<X>> with this to ger a up to date version of the shared data. With the current atomic impl. the at<sp<X>> is converted to a sp<X> and then this value is move -assigned to the sp<X>. This has a big overhad through the atomic increments of the reference counter. Imagine now that the at<sp<X>> has an assignment operator that takes an sp<X> reference directly and not only for it's own type. This ope- rator could simply compare its own value with the at<sp<X>>'s value and if this value hasn't changed it does nothing further. This would mean as long as there are no updates of the at<sp<X>> this operation would proceed in a nanosecond since the shared data and it's reference counter's cache lines would remain in shared mode and there would be no additional cachline loads. With RCU you take a snapshot of some shared data, read it, mark it as discadable and the cleanup would take place asynchronously. With my idea that's not much different since polling and updating the thread-local "sp<X>" nearly costs nothing.
Re: Consequential RCU
Author: Bonita Montero
Date: Wed, 07 Oct 2026 13:26
Date: Wed, 07 Oct 2026 13:26
29 lines
2679 bytes
2679 bytes
This is an example where I used my shared_obj<T>, which is constantly
poll-updated from a tshared_obj<T>. As long as the latter doesn't change
this is nearly for free.
template<bool Utc, bool AlsoEqual>
auto date_1601::lookUpLeap( int64_t ts ) noexcept -> lulp_ret
{
thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps;
pLeaps = g_leapSeconds; // update
span leaps = pLeaps->m_leaps;
auto
itBegin = leaps.rbegin(),
itEnd = leaps.rend(),
itBefore = itBegin;
if constexpr( AlsoEqual )
itBefore = lower_bound( itBegin, itEnd, ts, []( const dual_deaps
&leap, int64_t ref ) { return leap[Utc] > ref; } );
else
itBefore = upper_bound( itBegin, itEnd, ts, []( int64_t ref, const
dual_deaps &leap ) { return ref > leap[Utc]; } );
if( itBefore == itEnd )
return lulp_ret( -1, 0 );
return lulp_ret( leaps.rend() - itBefore - 1, (*itBefore)[Utc] );
}
In this example I'm dealing with a central storage of leap seconds which
may been updated asynchronously. This updates woudln't happen so often,
meaning the commented update assingment is nearly for free.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 20:13
Date: Wed, 07 Oct 2026 20:13
33 lines
3080 bytes
3080 bytes
On 10/7/2026 6:00 AM, Bonita Montero wrote: > Am 07.10.2026 um 06:33 schrieb Chris M. Thomasson: > >> Bench mark it against Joes atomic ptr that uses DWCAS. ... > > You still dont' understand my idea. Imagine you've got a shared_ptr<X> > and you constantly poll an atomic<shared_ptr<X>> with this to ger a > up to date version of the shared data. With the current atomic impl. > the at<sp<X>> is converted to a sp<X> and then this value is move > -assigned to the sp<X>. This has a big overhad through the atomic > increments of the reference counter. > Imagine now that the at<sp<X>> has an assignment operator that takes > an sp<X> reference directly and not only for it's own type. This ope- > rator could simply compare its own value with the at<sp<X>>'s value > and if this value hasn't changed it does nothing further. This would > mean as long as there are no updates of the at<sp<X>> this operation > would proceed in a nanosecond since the shared data and it's reference > counter's cache lines would remain in shared mode and there would be > no additional cachline loads. > With RCU you take a snapshot of some shared data, read it, mark it > as discadable and the cleanup would take place asynchronously. Huh? A snapshot? What are you talking about? I don't think you know what a read side iteration of say, a single linked list looks like. With > my idea that's not much different since polling and updating the > thread-local "sp<X>" nearly costs nothing. > Huh? You are either making a strong atomic refcount or not.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Wed, 07 Oct 2026 20:17
Date: Wed, 07 Oct 2026 20:17
39 lines
3229 bytes
3229 bytes
On 10/7/2026 6:26 AM, Bonita Montero wrote:
> This is an example where I used my shared_obj<T>, which is constantly
> poll-updated from a tshared_obj<T>. As long as the latter doesn't change
> this is nearly for free.
>
> template<bool Utc, bool AlsoEqual>
> auto date_1601::lookUpLeap( int64_t ts ) noexcept -> lulp_ret
> {
> thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps;
> pLeaps = g_leapSeconds; // update
> span leaps = pLeaps->m_leaps;
> auto
> itBegin = leaps.rbegin(),
> itEnd = leaps.rend(),
> itBefore = itBegin;
> if constexpr( AlsoEqual )
> itBefore = lower_bound( itBegin, itEnd, ts, []( const
> dual_deaps &leap, int64_t ref ) { return leap[Utc] > ref; } );
> else
> itBefore = upper_bound( itBegin, itEnd, ts, []( int64_t ref,
> const dual_deaps &leap ) { return ref > leap[Utc]; } );
> if( itBefore == itEnd )
> return lulp_ret( -1, 0 );
> return lulp_ret( leaps.rend() - itBefore - 1, (*itBefore)[Utc] );
> }
>
> In this example I'm dealing with a central storage of leap seconds which
> may been updated asynchronously. This updates woudln't happen so often,
> meaning the commented update assingment is nearly for free.
>
Can you iterate a shared single linked list with basically zero overhead?
thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps;
? So, are you taking a reference to something the thread does not own? I
noticed you mentioned futex. But that implies waiting! Or else why use
it to begin with?
Re: Consequential RCU
Author: Bonita Montero
Date: Thu, 08 Oct 2026 03:55
Date: Thu, 08 Oct 2026 03:55
7 lines
1917 bytes
1917 bytes
Am 07.10.2026 um 22:17 schrieb Chris M. Thomasson: > thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps; > ? So, are you taking a reference to something the thread does not own? Like with RCU no thread owns the data once its published since it is read only.
Re: Consequential RCU
Author: Bonita Montero
Date: Thu, 08 Oct 2026 03:56
Date: Thu, 08 Oct 2026 03:56
6 lines
1843 bytes
1843 bytes
Am 07.10.2026 um 22:13 schrieb Chris M. Thomasson: > Huh? You are either making a strong atomic refcount or not. With RCU a newer version of the dater may have been published and a thread is working with an older version. With my idea it's the same.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Thu, 08 Oct 2026 19:19
Date: Thu, 08 Oct 2026 19:19
11 lines
2115 bytes
2115 bytes
On 10/7/2026 8:55 PM, Bonita Montero wrote: > Am 07.10.2026 um 22:17 schrieb Chris M. Thomasson: > >> thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps; > >> ? So, are you taking a reference to something the thread does not own? > Like with RCU no thread owns the data once its published since it is > read only. Well, your work sure seems to act like a proxy. But it uses CAS and futex. Therefore its out of scope with anything lock-free.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Thu, 08 Oct 2026 21:20
Date: Thu, 08 Oct 2026 21:20
116 lines
4578 bytes
4578 bytes
On 10/8/2026 12:19 PM, Chris M. Thomasson wrote:
> On 10/7/2026 8:55 PM, Bonita Montero wrote:
>> Am 07.10.2026 um 22:17 schrieb Chris M. Thomasson:
>>
>>> thread_local shared_obj<leaps_set, sobj_mode::All> pLeaps;
>>
>>> ? So, are you taking a reference to something the thread does not own?
>> Like with RCU no thread owns the data once its published since it is
>> read only.
>
> Well, your work sure seems to act like a proxy. But it uses CAS and
> futex. Therefore its out of scope with anything lock-free.
For instance. Take my most recent post "CAS free Proxy Collector..."
https://pastebin.com/raw/CYZ78gVj
Notice how it can allow lock-free iterations of a lock-free stack that
does not use any DWCAS, no ABA? How can we use yours for the same thing?
No lock, no futex, 100% lock-free etc? Heck on systems that don't use
LL/SC loops for fetch and add, well its wait-free on the fast path. For
instance:
// you're basic lock-free stack...
// well, minus ABA counter and DWCAS of course! ;^)
class ct_stack
{
std::atomic<ct_node*> m_head;
public:
ct_stack() : m_head(nullptr)
{
}
public:
void push(ct_node* n)
{
ct_node* head = m_head.load(std::memory_order_relaxed);
do
{
n->m_next.store(head, std::memory_order_relaxed);
}
while (! m_head.compare_exchange_weak(
head,
n,
std::memory_order_release));
}
ct_node* flush()
{
return m_head.exchange(nullptr, std::memory_order_acquire);
}
ct_node* get_head()
{
return m_head.load(std::memory_order_acquire);
}
ct_node* pop()
{
ct_node* head = m_head.load(std::memory_order_acquire);
ct_node* xchg;
do
{
if (! head) return nullptr;
xchg = head->m_next.load(std::memory_order_relaxed);
}
while (!m_head.compare_exchange_weak(
head,
xchg,
std::memory_order_acquire));
return head;
}
};
With iterations with heavy read and some write activity.
// Reader threads
// Iterates through the lock free stack
void ct_thread_reader(ct_shared& shared)
{
// iterate the lockfree stack
for (unsigned long i = 0; i < ct_reader_iters_n; ++i)
{
ct_proxy_collector::collector& c = shared.m_proxy_gc.acquire();
ct_node* n = shared.m_stack.get_head();
while (n)
{
// need to add in some processing...
// std::this_thread::yield();
n = n->m_next.load(std::memory_order_relaxed);
}
shared.m_proxy_gc.release(c);
}
}
Ideally the shared.m_stack should have a lot of nodes to make it worth
while...
Re: Consequential RCU
Author: Bonita Montero
Date: Fri, 09 Oct 2026 06:14
Date: Fri, 09 Oct 2026 06:14
6 lines
1943 bytes
1943 bytes
Am 08.10.2026 um 21:19 schrieb Chris M. Thomasson: > Well, your work sure seems to act like a proxy. But it uses CAS and > futex. Therefore its out of scope with anything lock-free. You don't understand it.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Fri, 09 Oct 2026 18:05
Date: Fri, 09 Oct 2026 18:05
702 lines
24193 bytes
24193 bytes
On 10/8/2026 11:14 PM, Bonita Montero wrote:
> Am 08.10.2026 um 21:19 schrieb Chris M. Thomasson:
>
>> Well, your work sure seems to act like a proxy. But it uses CAS and
>> futex. Therefore its out of scope with anything lock-free.
>
> You don't understand it.
Oh I do understand it. Actually, afaict, it "should" beat
atomic<shared_ptr>. But, you need to run it through a race detector.
Actually, the damn AI seemingly found some issues. Found no issues in my
proxy collector. I already knew that because I had it running and
passing various tests in Relacy way back in 2010. And for you to make
claims that RCU isn't necessary is rather wild!
Therefore, I say that you don't really understand RCU...
Fwiw, here is a test harness it made for your work:
______________
// Leak / lifetime harness for shared_obj / tshared_obj (his posted
code, compile fixes only).
// Every Cfg owns a heap Block; allocation and free counters make leaks
and early frees visible.
#include <atomic>
#include <chrono>
#include <cstdio>
#include <thread>
#include <vector>
#include <csignal>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include "his/shared_obj.hpp"
static std::atomic<long> g_alloc(0), g_free(0);
struct Block { std::uint64_t v; };
struct Cfg
{
Block* p;
explicit Cfg(std::uint64_t k) : p(new Block{k}) {
g_alloc.fetch_add(1); }
Cfg(Cfg&& o) noexcept : p(o.p) { o.p = nullptr; }
Cfg(const Cfg&) = delete;
~Cfg() noexcept { if (p) { delete p; g_free.fetch_add(1); } }
};
using SO = shared_obj<Cfg, 1>;
using TSO = tshared_obj<Cfg, 1>;
static void reset() { g_alloc = 0; g_free = 0; }
static void report(const char* name, bool bug)
{
std::printf(" allocated=%ld freed=%ld leaked=%ld -> %s\n\n",
g_alloc.load(), g_free.load(), g_alloc.load() -
g_free.load(),
bug ? "BUG REPRODUCED" : "ok");
(void)name;
}
// A: a reader that moves to a new published version never drops its
reference to the old one.
static void test_A()
{
std::printf("A. reader follows a republished pointer (slowAssign)\n");
reset();
{
TSO tso( make_shared_obj<Cfg,1>(1) );
SO local;
local = tso; // holds v1
tso = make_shared_obj<Cfg,1>(2); // writer publishes v2,
drops tso's ref on v1
local = tso; // reader moves to v2 --
should release v1 here
} // everything destroyed;
v1 should be freed by now
report("A", g_alloc.load() != g_free.load());
}
// B: assigning an empty shared_obj leaves the destination pointing at
an object it no longer counts.
static void test_B()
{
std::printf("B. a = empty, while another owner still holds the
object (operator=(const shared_obj&))\n");
reset();
SO empty;
SO c;
bool a_nonnull_after = false;
long freed_while_c_alive = 0;
{
SO a = make_shared_obj<Cfg,1>(7);
c = a; // two owners, count == 2
std::printf(" use_count before: %u\n", a.use_count());
a = empty; // should make a empty,
count == 1
a_nonnull_after = (bool)a;
std::printf(" after a = empty: a is %s, c.use_count() = %u
(expected a empty, count 1)\n",
a_nonnull_after ? "STILL NON-NULL" : "null",
c.use_count());
} // ~a runs: decrements again
freed_while_c_alive = g_free.load();
std::printf(" after ~a, c still alive: blocks freed = %ld
(expected 0 -- c still owns it)\n", freed_while_c_alive);
report("B", a_nonnull_after || freed_while_c_alive != 0);
// c now dangles; do not touch it. Leak its (already freed) pointer
deliberately:
// destroying c would double free, so exit this scope by leaking it.
new SO(std::move(c)); // intentional: avoid the
double free for the demo
}
// C: tshared_obj = empty shared_obj dereferences null.
static void test_C()
{
std::printf("C. tshared_obj = empty shared_obj (operator=(const
shared_obj&))\n");
pid_t pid = fork();
if (pid == 0)
{
TSO tso( make_shared_obj<Cfg,1>(1) );
SO empty;
tso = empty; // so.m_pData->incr()
with m_pData == nullptr
_exit(0);
}
int st = 0;
waitpid(pid, &st, 0);
if (WIFSIGNALED(st))
std::printf(" child died with signal %d (%s) -> BUG
REPRODUCED\n\n", WTERMSIG(st), strsignal(WTERMSIG(st)));
else
std::printf(" child exited normally (%d) -> ok\n\n",
WEXITSTATUS(st));
}
// D: concurrent readers polling while a writer republishes.
static void test_D(int readers, int period_us, double secs)
{
std::printf("D. %d readers polling, one writer republishing every
%d us for %.1fs\n", readers, period_us, secs);
reset();
std::atomic<bool> stop(false);
std::atomic<unsigned long> versions(0);
{
TSO tso( make_shared_obj<Cfg,1>(1) );
std::vector<std::thread> th;
for (int i = 0; i < readers; ++i)
th.emplace_back([&]{
SO local;
while (!stop.load(std::memory_order_relaxed)) local = tso;
});
std::thread w([&]{
std::uint64_t k = 2;
while (!stop.load(std::memory_order_relaxed)) {
tso = make_shared_obj<Cfg,1>(k++);
versions.fetch_add(1);
std::this_thread::sleep_for(std::chrono::microseconds(period_us));
}
});
std::this_thread::sleep_for(std::chrono::duration<double>(secs));
stop = true;
for (auto& t : th) t.join();
w.join();
}
std::printf(" versions published=%lu\n", versions.load());
report("D", g_alloc.load() != g_free.load());
}
int main()
{
test_A();
test_B();
test_C();
test_D(3, 1000, 1.5);
return 0;
}
______________
______________
#pragma once
#include <algorithm> // added: std::max
#include <cstdint> // added: uint32_t
#include <cstddef> // added: size_t
#include <atomic>
#include <concepts>
#include <type_traits>
#include <cassert>
#include "cacheline.hpp"
#include "inline.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdangling-else"
#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
#endif
template<typename T, int ThreadMode>
struct shared_obj;
template<typename T, int ThreadMode, typename ... Args>
shared_obj<T, ThreadMode> make_shared_obj( Args &&... args );
template<typename T, int ThreadMode>
struct tshared_obj;
template<typename T, int ThreadMode>
struct shared_obj
{
shared_obj() noexcept = default;
shared_obj( const tshared_obj<T, ThreadMode> & ) noexcept;
shared_obj( tshared_obj<T, ThreadMode> && ) noexcept;
template<typename T2>
shared_obj( const shared_obj<T2, ThreadMode> &so ) noexcept;
template<typename T2>
shared_obj( shared_obj<T2, ThreadMode> &&so ) noexcept;
shared_obj &operator =( const shared_obj & ) noexcept;
shared_obj &operator =( shared_obj && ) noexcept;
shared_obj &operator =( const tshared_obj<T, ThreadMode> & ) noexcept;
shared_obj &operator =( tshared_obj<T, ThreadMode> && ) noexcept;
~shared_obj();
T *get() const noexcept;
operator bool() const noexcept;
uint32_t use_count() const noexcept;
T &operator *() const noexcept;
T *operator ->() const noexcept;
void reset() noexcept;
private:
static_assert( std::is_nothrow_destructible_v<T> );
template<typename T2, int ThreadMode2, typename ... Args>
friend shared_obj<T2, ThreadMode2> make_shared_obj( Args &&... args );
template<typename T2, int ThreadMode2>
friend struct tshared_obj;
template<typename T2, int ThreadMode2>
friend struct shared_obj;
static constexpr size_t
RcAlign = ThreadMode > 0 ? CL_SIZE : alignof( uint32_t ),
TotalAlign = std::max( ThreadMode < 0 ? CL_SIZE : alignof( T ),
RcAlign );
struct alignas( RcAlign ) ref_count
{
uint32_t m_refCount;
template<std::memory_order Order = std::memory_order_relaxed>
void incr() noexcept;
bool decr() noexcept;
};
struct non_class_value_type
{
T m_value;
};
static constexpr bool ValueIsClass = std::is_class_v<T>;
using value_type = std::conditional_t<ValueIsClass, T,
non_class_value_type>;
struct alignas( TotalAlign ) data : ref_count, value_type
{
template<typename ... Args>
data( Args &&... args );
};
shared_obj( data *obj ) noexcept;
template<typename T2>
data *assign( const shared_obj<T2, ThreadMode> & ) noexcept;
data *m_pData = nullptr;
void slowAssign( const tshared_obj<T, ThreadMode> &tso, data
*pDtaRef ) noexcept;
};
template<typename T, int ThreadMode>
template<std::memory_order Order>
FORCEINLINE void shared_obj<T, ThreadMode>::ref_count::incr() noexcept
{
uint32_t before;
if constexpr( ThreadMode )
before = std::atomic_ref( m_refCount ).fetch_add( 1, Order );
else
before = m_refCount++;
assert(before && before != (unsigned)-1);
return (void)before;
}
template<typename T, int ThreadMode>
FORCEINLINE bool shared_obj<T, ThreadMode>::ref_count::decr() noexcept
{
if constexpr( ThreadMode )
return std::atomic_ref( m_refCount ).fetch_sub( 1,
std::memory_order_relaxed ) == 1;
else
return !--m_refCount;
}
template<typename T, int ThreadMode>
template<typename T2>
inline shared_obj<T, ThreadMode>::shared_obj( const shared_obj<T2,
ThreadMode> &so ) noexcept :
m_pData( assign( so ) )
{
if( m_pData )
m_pData->incr();
}
template<typename T, int ThreadMode>
template<typename T2>
inline shared_obj<T, ThreadMode>::shared_obj( shared_obj<T2, ThreadMode>
&&so ) noexcept :
m_pData( assign( so ) )
{
so.m_pData = nullptr;
}
template<typename T, int ThreadMode>
template<typename T2>
FORCEINLINE auto shared_obj<T, ThreadMode>::assign( const shared_obj<T2,
ThreadMode> &so ) noexcept -> data *
{
using namespace std;
constexpr bool Polymorphic = !same_as<T, T2> && derived_from<T, T2>
&& is_polymorphic_v<T2>;
static_assert( same_as<T, T2> || derived_from<T2, T> || Polymorphic );
if( !so.m_pData )
return nullptr;
if constexpr( Polymorphic )
if( !dynamic_cast<T *>( &so.m_pData->m_value ) )
return nullptr;
return (data *)so.m_pData;
}
template<typename T, int ThreadMode>
template<typename ... Args>
inline shared_obj<T, ThreadMode>::data::data( Args &&... args ) :
ref_count( 1 ),
value_type( T( std::forward<Args>( args ) ... ) )
{
}
template<typename T, int ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =(
shared_obj &&other ) noexcept
{
if( m_pData == other.m_pData ) [[unlikely]]
{
other.m_pData = nullptr;
if( m_pData ) [[likely]]
m_pData->decr();
return *this;
}
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = other.m_pData;
other.m_pData = nullptr;
return *this;
}
template<typename T, int ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =( const
shared_obj &other ) noexcept
{
if( m_pData == other.m_pData )
return *this;
if( m_pData && m_pData->decr() )
{
delete m_pData;
m_pData = nullptr;
}
if( !other.m_pData )
return *this;
m_pData = other.m_pData;
m_pData->incr();
return *this;
}
template<typename T, int ThreadMode>
shared_obj<T, ThreadMode>::~shared_obj()
{
if( m_pData && m_pData->decr() )
delete m_pData;
}
template<typename T, int ThreadMode>
FORCEINLINE T *shared_obj<T, ThreadMode>::get() const noexcept
{
if constexpr( ValueIsClass )
return static_cast<T *>( m_pData );
else
return &m_pData->m_value;
}
template<typename T, int ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::operator bool() const noexcept
{
return (bool)m_pData;
}
template<typename T, int ThreadMode>
uint32_t shared_obj<T, ThreadMode>::use_count() const noexcept
{
if( !m_pData )
return 0;
if constexpr( ThreadMode )
return std::atomic_ref( m_pData->m_refCount ).load(
std::memory_order_relaxed );
else
return m_pData->m_refCount;
}
template<typename T, int ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( data *pData ) noexcept :
m_pData( pData )
{
assert(!m_pData || m_pData->m_refCount);
}
template<typename T, int ThreadMode>
FORCEINLINE T &shared_obj<T, ThreadMode>::operator *() const noexcept
{
assert(m_pData);
return *get();
}
template<typename T, int ThreadMode>
FORCEINLINE T *shared_obj<T, ThreadMode>::operator ->() const noexcept
{
assert(m_pData);
return get();
}
template<typename T, int ThreadMode>
void shared_obj<T, ThreadMode>::reset() noexcept
{
if( !m_pData )
return;
if( m_pData->decr() )
delete m_pData;
m_pData = nullptr;
}
template<typename T, int ThreadMode, typename ... Args>
shared_obj<T, ThreadMode> make_shared_obj( Args &&... args )
{
using data = shared_obj<T, ThreadMode>::data;
return shared_obj<T, ThreadMode>( new data( std::forward<Args>(
args ) ... ) );
}
template<typename T, int ThreadMode>
struct tshared_obj
{
tshared_obj() noexcept = default;
tshared_obj( const shared_obj<T, ThreadMode> & ) noexcept;
tshared_obj( shared_obj<T, ThreadMode> && ) noexcept;
~tshared_obj();
tshared_obj &operator =( const shared_obj<T, ThreadMode> & ) noexcept;
tshared_obj &operator =( shared_obj<T, ThreadMode> && ) noexcept;
void exchange( shared_obj<T, ThreadMode> &so );
void reset() noexcept;
private:
static_assert( ThreadMode );
using data = shared_obj<T, ThreadMode>::data;
template<typename T2, int ThreadMode2>
friend struct shared_obj;
mutable std::atomic<data *> m_pAtmData = nullptr;
};
template<typename T, int ThreadMode>
FORCEINLINE auto shared_obj<T, ThreadMode>::operator =( const
tshared_obj<T, ThreadMode> &tso ) noexcept -> shared_obj &
{
data *pDtaRef = tso.m_pAtmData.load( std::memory_order_relaxed );
if( m_pData != pDtaRef ) [[unlikely]]
slowAssign( tso, pDtaRef );
return *this;
}
template<typename T, int ThreadMode>
NOINLINE void shared_obj<T, ThreadMode>::slowAssign( const
tshared_obj<T, ThreadMode> &tso, data *pDtaRef ) noexcept
{
using namespace std;
for( ; ; )
{
if( !pDtaRef )
{
data *pData = m_pData;
if( pData && pData->decr() )
delete pData;
m_pData = nullptr;
return;
}
if( pDtaRef == (data *)-1 )
{
tso.m_pAtmData.wait( pDtaRef );
pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
}
else if( tso.m_pAtmData.compare_exchange_strong( pDtaRef, (data
*)-1, memory_order_relaxed, memory_order_relaxed ) ) [[likely]]
break;
if( m_pData == pDtaRef )
return;
}
m_pData = pDtaRef;
m_pData->template incr<memory_order_acquire>(); // FIX (GCC):
needs `template`
tso.m_pAtmData.store( pDtaRef, memory_order_relaxed );
tso.m_pAtmData.notify_all();
}
template<typename T, int ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =(
tshared_obj<T, ThreadMode> &&tso ) noexcept
{
using namespace std;
data *pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( !pDtaRef )
{
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = nullptr;
return *this;
}
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
tso.m_pAtmData.wait( pDtaRef );
pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
}
else if( tso.m_pAtmData.compare_exchange_strong( pDtaRef,
nullptr, memory_order_acquire, memory_order_relaxed ) )
break;
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = pDtaRef;
return *this;
}
template<typename T, int ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( const tshared_obj<T,
ThreadMode> &tso ) noexcept :
m_pData( nullptr )
{
*this = tso;
}
template<typename T, int ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( tshared_obj<T,
ThreadMode> &&tso ) noexcept :
m_pData( nullptr )
{
*this = std::move( tso );
}
template<typename T, int ThreadMode>
tshared_obj<T, ThreadMode>::tshared_obj( const shared_obj<T, ThreadMode>
&so ) noexcept :
m_pAtmData( so.m_pData )
{
if( so.m_pData )
so.m_pData->template incr<std::memory_order_release>(); //
FIX: posted as incr( std::memory_order_release )
}
template<typename T, int ThreadMode>
inline tshared_obj<T, ThreadMode>::tshared_obj( shared_obj<T,
ThreadMode> &&so ) noexcept :
m_pAtmData( so.m_pData )
{
so.m_pData = nullptr;
}
template<typename T, int ThreadMode>
tshared_obj<T, ThreadMode>::~tshared_obj()
{
data *pAtmData = m_pAtmData.load( std::memory_order_relaxed );
if( pAtmData && pAtmData->decr() )
delete pAtmData;
}
template<typename T, int ThreadMode>
NOINLINE auto tshared_obj<T, ThreadMode>::operator =( const
shared_obj<T, ThreadMode> &so ) noexcept -> tshared_obj &
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData ) [[likely]]
return *this;
else if( pDtaRef == (data *)-1 )
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, (data
*)-1, memory_order_relaxed, memory_order_relaxed ) )
break;
if( pDtaRef && pDtaRef->decr() )
delete pDtaRef;
so.m_pData->incr();
m_pAtmData.store( so.m_pData, memory_order_release );
m_pAtmData.notify_all();
return *this;
}
template<typename T, int ThreadMode>
auto tshared_obj<T, ThreadMode>::operator =( shared_obj<T, ThreadMode>
&&so ) noexcept -> tshared_obj &
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData )
{
if( !so.m_pData )
return *this;
so.m_pData->decr();
so.m_pData = nullptr;
return *this;
}
else if( m_pAtmData == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef,
so.m_pData, memory_order_release, memory_order_relaxed ) )
break;
if( pDtaRef && pDtaRef->decr() )
delete pDtaRef;
so.m_pData = nullptr;
return *this;
}
template<typename T, int ThreadMode>
void tshared_obj<T, ThreadMode>::exchange( shared_obj<T, ThreadMode> &so )
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData )
return;
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef,
so.m_pData, memory_order_acq_rel, memory_order_relaxed ) )
break;
so.m_pData = pDtaRef;
}
template<typename T, int ThreadMode>
void tshared_obj<T, ThreadMode>::reset() noexcept
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( !pDtaRef )
return;
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, nullptr,
memory_order_relaxed, memory_order_relaxed ) )
break;
if( pDtaRef->decr() )
delete pDtaRef;
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
_____________
Well, perhaps you can take a look at it and make sure to have some
grains of salt. Actually, you should just port it over to relacy and run
it.
Actually, I am wondering. Can you run the AI test on your end? I don't
have access to you missing code anyway. cacheline, inline, etc...
The harness is leak_demo.cpp, and each test reproduced its bug on the
first run.
Test What it does Result
A A reader polls, the writer republishes, the reader polls again, then
everything is destroyed. 2 allocated, 1 freed, 1 leaked. The reader
never released the old version.
B a and c both own an object, then a = empty. a is still non-null
afterward, and when a is destroyed the object is freed while c still
owns it. This is a use-after-free setup.
C tshared_obj = empty shared_obj, run in a forked child. Segmentation
fault, from so.m_pData->incr() on null.
D 3 readers polling while a writer republishes every 1 ms for 1.5s.
1,330 allocated, 1 freed, 1,329 leaked. Essentially every version is
lost once readers have seen it.
Well, did it find something?
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Sat, 10 Oct 2026 21:20
Date: Sat, 10 Oct 2026 21:20
9 lines
1950 bytes
1950 bytes
On 10/7/2026 8:56 PM, Bonita Montero wrote: > Am 07.10.2026 um 22:13 schrieb Chris M. Thomasson: > >> Huh? You are either making a strong atomic refcount or not. > > With RCU a newer version of the dater may have been published and a > thread is working with an older version. With my idea it's the same. Afaict, you idea is ridden with bugs.
Re: Consequential RCU
Author: Bonita Montero
Date: Sun, 11 Oct 2026 04:59
Date: Sun, 11 Oct 2026 04:59
6 lines
1818 bytes
1818 bytes
Am 10.10.2026 um 23:20 schrieb Chris M. Thomasson: > Afaict, you idea is ridden with bugs. I've written unit- and performance-tests.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Sun, 11 Oct 2026 21:07
Date: Sun, 11 Oct 2026 21:07
9 lines
1933 bytes
1933 bytes
On 10/10/2026 9:59 PM, Bonita Montero wrote: > Am 10.10.2026 um 23:20 schrieb Chris M. Thomasson: > >> Afaict, you idea is ridden with bugs. > > I've written unit- and performance-tests. > Have you ran them against any race detectors?
Re: Consequential RCU
Author: Bonita Montero
Date: Mon, 12 Oct 2026 03:18
Date: Mon, 12 Oct 2026 03:18
6 lines
1926 bytes
1926 bytes
Am 11.10.2026 um 23:07 schrieb Chris M. Thomasson: > Have you ran them against any race detectors? I've ran the code with a competition of 32 threads.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Mon, 12 Oct 2026 03:31
Date: Mon, 12 Oct 2026 03:31
14 lines
2463 bytes
2463 bytes
On 10/11/2026 8:18 PM, Bonita Montero wrote: > > I've ran the code with a competition of 32 threads. Nice, 32 threads is a good load! Actually, can you tell me a bit more about what the test did? Mainly... did the shared pointer actually change during the run (and how often), and did anything check the results at the end, like total allocated vs. total freed? The reason I ask is that the trouble spots are in the paths that only run when the pointer changes, and a run that never changes it can come out clean without exercising them. Also curious whether you tried it under ThreadSanitizer or ASan/LeakSanitizer, since even one run there would tell us a lot.
Re: Consequential RCU
Author: Bonita Montero
Date: Mon, 12 Oct 2026 04:32
Date: Mon, 12 Oct 2026 04:32
113 lines
4980 bytes
4980 bytes
Am 12.10.2026 um 05:31 schrieb Chris M. Thomasson:
> Nice, 32 threads is a good load! Actually, can you tell me a bit more
> about what the test did? Mainly... did the shared pointer actually
> change during the run (and how often), and did anything check the
> results at the end, like total allocated vs. total freed?
static void timedTest( char const *what, auto fn, bool forTbl );
static void soTest( int type, bool forTbl )
{
constexpr sobj_mode Mode = sobj_mode::Hdr;
tshared_obj<string, Mode> tsoStr( make_shared_obj<string, Mode>( "hello
world" ) );
shared_obj<string, Mode> altStrs[2];
if( type == StoreAlt )
{
altStrs[0] = make_shared_obj<string, Mode>( "alt1" );
altStrs[1] = make_shared_obj<string, Mode>( "alt2" );
}
constexpr const char *Descr = "shared_obj: ";
switch( type )
{
case Load:
timedTest( Descr, [&]
{
thread_local shared_obj<string, Mode> soStr;
soStr = tsoStr;
}, forTbl );
break;
case Store:
timedTest( Descr, [&]
{
thread_local shared_obj<string, Mode> soStr = tsoStr;
tsoStr = soStr;
}, forTbl );
break;
case StoreAlt:
timedTest( Descr, [&]
{
thread_local bool flip = true;
tsoStr = altStrs[flip = !flip];
}, forTbl );
break;
case Xchg:
timedTest( Descr, [&]
{
thread_local shared_obj<string, Mode> soStr = tsoStr;
tsoStr.exchange( soStr );
}, forTbl );
break;
case Move:
timedTest( Descr, [&]
{
thread_local shared_obj<string, Mode> soStr;
if( !soStr )
soStr = move( tsoStr );
else
tsoStr = move( soStr );
}, forTbl );
}
}
static void timedTest( char const *what, auto fn, bool forTbl )
{
cout << what << endl;
vector<jthread> threads;
unsigned hc = jthread::hardware_concurrency();
threads.reserve( hc );
for( unsigned nThreads = 1; nThreads <= hc; ++nThreads )
{
latch lat( nThreads );
atomic_int64_t tSum( 0 ), roundsSum = 0;
atomic_bool stop( false );
for( unsigned t = 1; t <= nThreads; ++t )
{
threads.emplace_back( [&, fn = fn]()
{
lat.arrive_and_wait();
auto start = high_resolution_clock::now();
int64_t rnd = 0;
for( ; !stop.load( memory_order_relaxed ); ++rnd )
fn();
tSum += duration_cast<nanoseconds>( high_resolution_clock::now() -
start ).count();
roundsSum += rnd;
} );
#if defined(_WIN32)
SetThreadAffinityMask( threads.back().native_handle(), (DWORD_PTR)1
<< (t - 1) );
#endif
}
#if defined(NDEBUG)
constexpr duration TIME = 100ms;
#else
constexpr duration TIME = 100ms;
#endif
this_thread::sleep_for( TIME );
stop = true;
threads.resize( 0 );
double nsOp = (double)tSum / (double)roundsSum;
string result = (ostringstream() << nsOp).str();
replace_if( result.begin(), result.end(), []( char c ) { return c ==
'.'; }, ',' );
if( !forTbl )
cout << "\t" << nThreads << ": " << nsOp << endl;
else
cout << "\"" << result<< "\"" << endl;
}
}
But I'm pretty sure you don't understand the code since you didn't
understand the basic idea.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Mon, 12 Oct 2026 20:14
Date: Mon, 12 Oct 2026 20:14
17 lines
2511 bytes
2511 bytes
On 10/11/2026 9:32 PM, Bonita Montero wrote: > static void timedTest( char const *what, auto fn, bool forTbl ); > [...] > But I'm pretty sure you don't understand the code since you didn't > understand the basic idea. Sigh. Your Load, Store and Xchg tests never change the pointer, so they only measure the fast path. Could you add one where a writer keeps swapping the slot while the others poll it, and print allocations vs. frees at the end? one thread republishing through StoreAlt while the others run Load, plus an allocated-versus-freed check at the end. That shows the herd on the lock and any leak in one run.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Mon, 12 Oct 2026 20:19
Date: Mon, 12 Oct 2026 20:19
11 lines
2348 bytes
2348 bytes
On 10/11/2026 9:32 PM, Bonita Montero wrote: > Am 12.10.2026 um 05:31 schrieb Chris M. Thomasson: > >> Nice, 32 threads is a good load! Actually, can you tell me a bit more >> about what the test did? Mainly... did the shared pointer actually >> change during the run (and how often), and did anything check the >> results at the end, like total allocated vs. total freed? [...] Where is sobj_mode defined, and is that the same shared_obj header you posted before?
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Mon, 12 Oct 2026 20:24
Date: Mon, 12 Oct 2026 20:24
17 lines
2672 bytes
2672 bytes
On 10/12/2026 1:19 PM, Chris M. Thomasson wrote: > On 10/11/2026 9:32 PM, Bonita Montero wrote: >> Am 12.10.2026 um 05:31 schrieb Chris M. Thomasson: >> >>> Nice, 32 threads is a good load! Actually, can you tell me a bit more >>> about what the test did? Mainly... did the shared pointer actually >>> change during the run (and how often), and did anything check the >>> results at the end, like total allocated vs. total freed? > [...] > > Where is sobj_mode defined, and is that the same shared_obj header you > posted before? Just use your thing to allow iterations of a lock-free linked list not have to use DWCAS, ABA. Make it single word. Let reader threads iterate it while writer threads are mutating it. Make it all lock-free, no loops. Can you do it as is?
Re: Consequential RCU
Author: Bonita Montero
Date: Tue, 13 Oct 2026 06:14
Date: Tue, 13 Oct 2026 06:14
11 lines
2252 bytes
2252 bytes
Am 12.10.2026 um 22:14 schrieb Chris M. Thomasson: > Your Load, Store and Xchg tests never change the pointer, so they only > measure the fast path. ... For some tests, yes, but not all. > one thread republishing through StoreAlt while the others run Load, ... No, all threads do the same.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Tue, 13 Oct 2026 07:08
Date: Tue, 13 Oct 2026 07:08
16 lines
2500 bytes
2500 bytes
On 10/12/2026 11:14 PM, Bonita Montero wrote: > Am 12.10.2026 um 22:14 schrieb Chris M. Thomasson: > >> Your Load, Store and Xchg tests never change the pointer, so they only >> measure the fast path. ... > > For some tests, yes, but not all. > >> one thread republishing through StoreAlt while the others run Load, ... > > No, all threads do the same. > Try to make it uses say, reader and writer threads. Multiple readers iterating linked dynamic data structures while the writers are mutating them all at the same time.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Tue, 13 Oct 2026 07:10
Date: Tue, 13 Oct 2026 07:10
20 lines
2664 bytes
2664 bytes
On 10/13/2026 12:08 AM, Chris M. Thomasson wrote: > On 10/12/2026 11:14 PM, Bonita Montero wrote: >> Am 12.10.2026 um 22:14 schrieb Chris M. Thomasson: >> >>> Your Load, Store and Xchg tests never change the pointer, so they >>> only measure the fast path. ... >> >> For some tests, yes, but not all. >> >>> one thread republishing through StoreAlt while the others run Load, ... >> >> No, all threads do the same. >> > > Try to make it uses say, reader and writer threads. Multiple readers > iterating linked dynamic data structures while the writers are mutating > them all at the same time. You may find that your futex is going be put under some stress in that scenario?
Re: Consequential RCU
Author: Bonita Montero
Date: Tue, 13 Oct 2026 07:43
Date: Tue, 13 Oct 2026 07:43
7 lines
2316 bytes
2316 bytes
Am 13.10.2026 um 09:10 schrieb Chris M. Thomasson: > You may find that your futex is going be put under some stress in that > scenario? My code is optimized for RCU-like patterns. For all other operations futex'd synchronization is inevitable.
Re: Consequential RCU
Author: Bonita Montero
Date: Tue, 13 Oct 2026 09:33
Date: Tue, 13 Oct 2026 09:33
500 lines
16172 bytes
16172 bytes
Am 12.10.2026 um 22:19 schrieb Chris M. Thomasson:
> Where is sobj_mode defined, and is that the same shared_obj header you
> posted before?
No, the ThreadMode was < 0 if the compete object including the
reference counter was cacheline aligned, > 0 if the reference
counter itself is the only aligned object and == 0 if there is
no aligned. The latter case is if you want to use the shared_obj
class itself for thread local shared objects. The tshared_obj
requires "ThreadMode" be != 0.
Now I have a separate sobj_mode enum:
enum class sobj_mode : int { All = -1, No = 0, Hdr = 1 };
---
#pragma once
#include <atomic>
#include <concepts>
#include <type_traits>
#include <cassert>
#include "cacheline.hpp"
#include "inline.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdangling-else"
#pragma clang diagnostic ignored "-Wlogical-op-parentheses"
#endif
enum class sobj_mode : int { All = -1, No = 0, Hdr = 1 };
template<typename T, sobj_mode ThreadMode>
struct shared_obj;
template<typename T, sobj_mode ThreadMode, typename ... Args>
shared_obj<T, ThreadMode> make_shared_obj( Args &&... args );
template<typename T, sobj_mode ThreadMode>
struct tshared_obj;
template<typename T, sobj_mode ThreadMode>
struct shared_obj
{
shared_obj() noexcept = default;
shared_obj( const tshared_obj<T, ThreadMode> & ) noexcept;
shared_obj( tshared_obj<T, ThreadMode> && ) noexcept;
template<typename T2>
shared_obj( const shared_obj<T2, ThreadMode> &so ) noexcept;
template<typename T2>
shared_obj( shared_obj<T2, ThreadMode> &&so ) noexcept;
shared_obj &operator =( const shared_obj & ) noexcept;
shared_obj &operator =( shared_obj && ) noexcept;
shared_obj &operator =( const tshared_obj<T, ThreadMode> & ) noexcept;
shared_obj &operator =( tshared_obj<T, ThreadMode> && ) noexcept;
~shared_obj();
T *get() const noexcept;
operator bool() const noexcept;
uint32_t use_count() const noexcept;
T &operator *() const noexcept;
T *operator ->() const noexcept;
void reset() noexcept;
private:
static_assert( std::is_nothrow_destructible_v<T> );
template<typename T2, sobj_mode ThreadMode2, typename ... Args>
friend shared_obj<T2, ThreadMode2> make_shared_obj( Args &&... args );
template<typename T2, sobj_mode ThreadMode2>
friend struct tshared_obj;
static constexpr size_t
RcAlign = (int)ThreadMode > 0 ? CL_SIZE : alignof( uint32_t ),
TotalAlign = std::max( (int)ThreadMode < 0 ? CL_SIZE : alignof( T ),
RcAlign );
struct alignas( RcAlign ) ref_count
{
uint32_t m_refCount;
template<std::memory_order Order = std::memory_order_relaxed>
void incr() noexcept;
bool decr() noexcept;
};
struct non_class_value_type
{
T m_value;
};
static constexpr bool ValueIsClass = std::is_class_v<T>;
using value_type = std::conditional_t<ValueIsClass, T,
non_class_value_type>;
struct alignas( TotalAlign ) data : ref_count, value_type
{
template<typename ... Args>
data( Args &&... args );
};
shared_obj( data *obj ) noexcept;
template<typename T2>
data *assign( const shared_obj<T2, ThreadMode> & ) noexcept;
data *m_pData = nullptr;
void slowAssign( const tshared_obj<T, ThreadMode> &tso, data *pDtaRef )
noexcept;
};
template<typename T, sobj_mode ThreadMode>
template<std::memory_order Order>
FORCEINLINE void shared_obj<T, ThreadMode>::ref_count::incr() noexcept
{
uint32_t before;
if constexpr( (int)ThreadMode != 0 )
before = std::atomic_ref( m_refCount ).fetch_add( 1, Order );
else
before = m_refCount++;
assert(before && before != (unsigned)-1);
return (void)before;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE bool shared_obj<T, ThreadMode>::ref_count::decr() noexcept
{
if constexpr( (int)ThreadMode != 0 )
return std::atomic_ref( m_refCount ).fetch_sub( 1,
std::memory_order_relaxed ) == 1;
else
return !--m_refCount;
}
template<typename T, sobj_mode ThreadMode>
template<typename T2>
inline shared_obj<T, ThreadMode>::shared_obj( const shared_obj<T2,
ThreadMode> &so ) noexcept :
m_pData( assign( so ) )
{
if( m_pData )
m_pData->incr();
}
template<typename T, sobj_mode ThreadMode>
template<typename T2>
inline shared_obj<T, ThreadMode>::shared_obj( shared_obj<T2, ThreadMode>
&&so ) noexcept :
m_pData( assign( so ) )
{
so.m_pData = nullptr;
}
template<typename T, sobj_mode ThreadMode>
template<typename T2>
FORCEINLINE auto shared_obj<T, ThreadMode>::assign( const shared_obj<T2,
ThreadMode> &so ) noexcept -> data *
{
using namespace std;
constexpr bool Polymorphic = !same_as<T, T2> && derived_from<T, T2> &&
is_polymorphic_v<T2>;
static_assert( same_as<T, T2> || derived_from<T2, T> || Polymorphic );
if( !so.m_pData )
return nullptr;
if constexpr( Polymorphic )
if( !dynamic_cast<T *>( &so.m_pData->m_value ) )
return nullptr;
return (data *)so.m_pData;
}
template<typename T, sobj_mode ThreadMode>
template<typename ... Args>
inline shared_obj<T, ThreadMode>::data::data( Args &&... args ) :
ref_count( 1 ),
value_type( T( std::forward<Args>( args ) ... ) )
{
}
template<typename T, sobj_mode ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =(
shared_obj &&other ) noexcept
{
if( m_pData == other.m_pData ) [[unlikely]]
{
other.m_pData = nullptr;
if( m_pData ) [[likely]]
m_pData->decr();
return *this;
}
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = other.m_pData;
other.m_pData = nullptr;
return *this;
}
template<typename T, sobj_mode ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =( const
shared_obj &other ) noexcept
{
if( m_pData == other.m_pData )
return *this;
if( m_pData && m_pData->decr() )
{
delete m_pData;
m_pData = nullptr;
}
if( !other.m_pData )
return *this;
m_pData = other.m_pData;
m_pData->incr();
return *this;
}
template<typename T, sobj_mode ThreadMode>
shared_obj<T, ThreadMode>::~shared_obj()
{
if( m_pData && m_pData->decr() )
delete m_pData;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE T *shared_obj<T, ThreadMode>::get() const noexcept
{
if constexpr( ValueIsClass )
return static_cast<T *>( m_pData );
else
return &m_pData->m_value;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::operator bool() const noexcept
{
return (bool)m_pData;
}
template<typename T, sobj_mode ThreadMode>
uint32_t shared_obj<T, ThreadMode>::use_count() const noexcept
{
if( !m_pData )
return 0;
if constexpr( (int)ThreadMode != 0 )
return std::atomic_ref( m_pData->m_refCount ).load(
std::memory_order_relaxed );
else
return m_pData->m_refCount;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( data *pData ) noexcept :
m_pData( pData )
{
assert(!m_pData || m_pData->m_refCount);
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE T &shared_obj<T, ThreadMode>::operator *() const noexcept
{
assert(m_pData);
return *get();
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE T *shared_obj<T, ThreadMode>::operator ->() const noexcept
{
assert(m_pData);
return get();
}
template<typename T, sobj_mode ThreadMode>
void shared_obj<T, ThreadMode>::reset() noexcept
{
if( !m_pData )
return;
if( m_pData->decr() )
delete m_pData;
m_pData = nullptr;
}
template<typename T, sobj_mode ThreadMode, typename ... Args>
shared_obj<T, ThreadMode> make_shared_obj( Args &&... args )
{
using data = shared_obj<T, ThreadMode>::data;
return shared_obj<T, ThreadMode>( new data( std::forward<Args>( args )
... ) );
}
template<typename T, sobj_mode ThreadMode>
struct tshared_obj
{
tshared_obj() noexcept = default;
tshared_obj( const shared_obj<T, ThreadMode> & ) noexcept;
tshared_obj( shared_obj<T, ThreadMode> && ) noexcept;
~tshared_obj();
tshared_obj &operator =( const shared_obj<T, ThreadMode> & ) noexcept;
tshared_obj &operator =( shared_obj<T, ThreadMode> && ) noexcept;
void exchange( shared_obj<T, ThreadMode> &so );
void reset() noexcept;
private:
static_assert( (int)ThreadMode != 0 );
using data = shared_obj<T, ThreadMode>::data;
template<typename T2, sobj_mode ThreadMode2>
friend struct shared_obj;
mutable std::atomic<data *> m_pAtmData = nullptr;
};
template<typename T, sobj_mode ThreadMode>
FORCEINLINE auto shared_obj<T, ThreadMode>::operator =( const
tshared_obj<T, ThreadMode> &tso ) noexcept -> shared_obj &
{
data *pDtaRef = tso.m_pAtmData.load( std::memory_order_relaxed );
if( m_pData != pDtaRef ) [[unlikely]]
slowAssign( tso, pDtaRef );
return *this;
}
template<typename T, sobj_mode ThreadMode>
NOINLINE void shared_obj<T, ThreadMode>::slowAssign( const
tshared_obj<T, ThreadMode> &tso, data *pDtaRef ) noexcept
{
using namespace std;
for( ; ; )
{
if( !pDtaRef )
{
data *pData = m_pData;
if( pData && pData->decr() )
delete pData;
m_pData = nullptr;
return;
}
if( pDtaRef == (data *)-1 )
{
tso.m_pAtmData.wait( pDtaRef );
pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
}
else if( tso.m_pAtmData.compare_exchange_strong( pDtaRef, (data *)-1,
memory_order_relaxed, memory_order_relaxed ) ) [[likely]]
break;
if( m_pData == pDtaRef )
return;
}
m_pData = pDtaRef;
m_pData->template incr<memory_order_acquire>();
tso.m_pAtmData.store( pDtaRef, memory_order_relaxed );
tso.m_pAtmData.notify_all();
}
template<typename T, sobj_mode ThreadMode>
shared_obj<T, ThreadMode> &shared_obj<T, ThreadMode>::operator =(
tshared_obj<T, ThreadMode> &&tso ) noexcept
{
using namespace std;
data *pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( !pDtaRef )
{
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = nullptr;
return *this;
}
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
tso.m_pAtmData.wait( pDtaRef );
pDtaRef = tso.m_pAtmData.load( memory_order_relaxed );
}
else if( tso.m_pAtmData.compare_exchange_strong( pDtaRef, nullptr,
memory_order_acquire, memory_order_relaxed ) )
break;
if( m_pData && m_pData->decr() )
delete m_pData;
m_pData = pDtaRef;
return *this;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( const tshared_obj<T,
ThreadMode> &tso ) noexcept :
m_pData( nullptr )
{
*this = tso;
}
template<typename T, sobj_mode ThreadMode>
FORCEINLINE shared_obj<T, ThreadMode>::shared_obj( tshared_obj<T,
ThreadMode> &&tso ) noexcept :
m_pData( nullptr )
{
*this = std::move( tso );
}
template<typename T, sobj_mode ThreadMode>
tshared_obj<T, ThreadMode>::tshared_obj( const shared_obj<T, ThreadMode>
&so ) noexcept :
m_pAtmData( so.m_pData )
{
if( so.m_pData )
so.m_pData->template incr<std::memory_order_release>();
}
template<typename T, sobj_mode ThreadMode>
inline tshared_obj<T, ThreadMode>::tshared_obj( shared_obj<T,
ThreadMode> &&so ) noexcept :
m_pAtmData( so.m_pData )
{
so.m_pData = nullptr;
}
template<typename T, sobj_mode ThreadMode>
tshared_obj<T, ThreadMode>::~tshared_obj()
{
data *pAtmData = m_pAtmData.load( std::memory_order_relaxed );
if( pAtmData && pAtmData->decr() )
delete pAtmData;
}
template<typename T, sobj_mode ThreadMode>
auto tshared_obj<T, ThreadMode>::operator =( const shared_obj<T,
ThreadMode> &so ) noexcept -> tshared_obj &
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData ) [[likely]]
return *this;
else if( pDtaRef == (data *)-1 )
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, (data *)-1,
memory_order_relaxed, memory_order_relaxed ) )
break;
so.m_pData->template incr<memory_order_release>();
m_pAtmData.store( so.m_pData, memory_order_relaxed );
m_pAtmData.notify_all();
if( pDtaRef && pDtaRef->decr() )
delete pDtaRef;
return *this;
}
template<typename T, sobj_mode ThreadMode>
auto tshared_obj<T, ThreadMode>::operator =( shared_obj<T, ThreadMode>
&&so ) noexcept -> tshared_obj &
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData )
{
if( !so.m_pData )
return *this;
so.m_pData->decr();
so.m_pData = nullptr;
return *this;
}
else if( m_pAtmData == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, so.m_pData,
memory_order_release, memory_order_relaxed ) )
break;
if( pDtaRef && pDtaRef->decr() )
delete pDtaRef;
so.m_pData = nullptr;
return *this;
}
template<typename T, sobj_mode ThreadMode>
void tshared_obj<T, ThreadMode>::exchange( shared_obj<T, ThreadMode> &so )
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( pDtaRef == so.m_pData )
return;
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, so.m_pData,
memory_order_acq_rel, memory_order_relaxed ) )
break;
so.m_pData = pDtaRef;
}
template<typename T, sobj_mode ThreadMode>
void tshared_obj<T, ThreadMode>::reset() noexcept
{
using namespace std;
data *pDtaRef = m_pAtmData.load( memory_order_relaxed );
for( ; ; )
if( !pDtaRef )
return;
else if( pDtaRef == (data *)-1 ) [[unlikely]]
{
m_pAtmData.wait( pDtaRef );
pDtaRef = m_pAtmData.load( memory_order_relaxed );
}
else if( m_pAtmData.compare_exchange_strong( pDtaRef, nullptr,
memory_order_relaxed, memory_order_relaxed ) )
break;
if( pDtaRef->decr() )
delete pDtaRef;
}
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Tue, 13 Oct 2026 19:29
Date: Tue, 13 Oct 2026 19:29
20 lines
2726 bytes
2726 bytes
On 10/13/2026 12:43 AM, Bonita Montero wrote: > Am 13.10.2026 um 09:10 schrieb Chris M. Thomasson: > >> You may find that your futex is going be put under some stress in that >> scenario? > > My code is optimized for RCU-like patterns. So it should work fine with readers iterating a shared lock-free linked list while writers are mutating said list. > For all other operations futex'd synchronization is inevitable. I think you might have a problem in your following logic: m_pData = pDtaRef; m_pData->template incr<memory_order_acquire>(); tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); tso.m_pAtmData.notify_all();
Re: Consequential RCU
Author: Bonita Montero
Date: Wed, 14 Oct 2026 03:56
Date: Wed, 14 Oct 2026 03:56
9 lines
2396 bytes
2396 bytes
Am 13.10.2026 um 21:29 schrieb Chris M. Thomasson: > m_pData = pDtaRef; > m_pData->template incr<memory_order_acquire>(); > tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); > tso.m_pAtmData.notify_all(); No, the code is fine.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Wed, 14 Oct 2026 21:02
Date: Wed, 14 Oct 2026 21:02
16 lines
2643 bytes
2643 bytes
On 10/13/2026 8:56 PM, Bonita Montero wrote: > Am 13.10.2026 um 21:29 schrieb Chris M. Thomasson: > >> m_pData = pDtaRef; >> m_pData->template incr<memory_order_acquire>(); >> tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); >> tso.m_pAtmData.notify_all(); > > No, the code is fine. > It does not need to dec any prior reference before: tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); I must be missing something here. Thanks for your patience.
Re: Consequential RCU
Author: Bonita Montero
Date: Fri, 16 Oct 2026 03:28
Date: Fri, 16 Oct 2026 03:28
8 lines
2509 bytes
2509 bytes
Am 14.10.2026 um 23:02 schrieb Chris M. Thomasson: > It does not need to dec any prior reference before: > tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); > I must be missing something here. Thanks for your patience. You don't understand the code.
Re: Consequential RCU
Author: Chris M. Thomasson
Date: Fri, 16 Oct 2026 20:28
Date: Fri, 16 Oct 2026 20:28
19 lines
2975 bytes
2975 bytes
On 10/15/2026 8:28 PM, Bonita Montero wrote: > Am 14.10.2026 um 23:02 schrieb Chris M. Thomasson: > >> It does not need to dec any prior reference before: >> tso.m_pAtmData.store( pDtaRef, memory_order_relaxed ); >> I must be missing something here. Thanks for your patience. > > You don't understand the code. > I think I do. Btw, why don't you sort of, "give in", a bit and make a Relacy test unit out of it? I kind of got back into it recently. Its quite interesting wrt what it can find... For me personally, its a fairly decent detour from my fractal, vector field work... :^) Also, brings back some memories of working with Relacy back when it was in pre-alpha stage. Oh how time flies.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads