🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #571

Re: Consequential RCU

#571
From: Chris M. Thomasson
Date: Thu, 15 Oct 2026 18:23
412 lines
13703 bytes
On 10/13/2026 4:34 PM, Joseph Seigh wrote:
> On 10/13/26 3:36 PM, Chris M. Thomasson wrote:
>> On 10/13/2026 10:01 AM, Joseph Seigh wrote:
> 
>>> This is what the code I did a quick and dirty test with looked like
>>>
>>> using csequence_t = uint64_t;
>>>
>>> inline bool cseq_retry(csequence_t mark, std::atomic<csequence_t> & 
>>> sequence)
>>> {
>>>      std::atomic_thread_fence(std::memory_order_acquire);
>>>      return mark != sequence.load(std::memory_order_acquire);
>>> }
>>>
>>> csequence_t mark;
>>>
>>> retry:
>>> do {
>>>
>>>      mark = cseq_count.load(std::memory_order_acquire);
>>>      node_t * item = list.load(std::memory_order_acquire);
>>>
>>>      for (int ndx2 = 0; ndx2 < depth; ndx2++) {
>>>          item = item->next.load(std::memory_order_acquire);
>>>          if (cseq_retry(mark, cseq_count)) [[unlikely]]
>>>              goto retry;
>>>      }
>>>
>>> } while (cseq_retry(mark, cseq_count));
>>>
>>>
>>> As you can see, not a heck of lot of room in that loop
>>> for the cpu pipeline to deal with the extra load and
>>> conditional logic.
[...]


std::atomic_thread_fence(std::memory_order_acquire);
return mark != sequence.load(std::memory_order_acquire);

Sometimes I get nervous wrt how c++ deals with integrating the 
standalone fence with the per atomic fences. Humm... As you know I 
recently got back into relacy. Its interesting to work with. When you 
get really bored, and have nothing to do, give it a go! Sigh. Might be 
fun for you?

I should try to model your cseq_retry. Anyway, after talking to some 
people about the most recent Relacy, here is what we came up with. 
Apparently this is the most "relaxed" version of my async SMR test. I am 
not sure how to make it any more relaxed! Notice smr_load, smr_unload, 
and the prv_poll functions if you have the time and the interest.

smr_unload cannot use relaxed, grrr!!!. It needs the release to allow 
C++ mem model to get into the second pass of the smr polling logic. So, 
pass 1, relaxed sample, systemwide_fence, then the second sample needs 
acquire:


____________________
#pragma once

#include <relacy/relacy.hpp>
#include <cstdio>
#include <cstddef>
#include <cstdlib>

#include <array>
#include <string>
#include <vector>

namespace ct
{
     namespace smr_test
     {
         constexpr unsigned long CT_THREADS_READERS = 5;
         constexpr unsigned long CT_THREADS_WRITERS = 3;
         constexpr unsigned long CT_THREADS = CT_THREADS_READERS + 
CT_THREADS_WRITERS;
         constexpr unsigned long CT_THREADS_READER_ITER_N = 12;
         constexpr unsigned long CT_THREADS_WRITER_ITER_N = 8;

         struct per_node
         {
             VAR_T(per_node*) m_defer = { nullptr };
             VAR_T(unsigned long) m_data = { 0 };
         };

         struct per_thread
         {
             rl::atomic<per_node*> m_haz_0 = { nullptr };
         };

         struct poller
         {
             rl::mutex m_poll;
             VAR_T(per_node*) m_defer0 = nullptr;

             void
             prv_poll(
                 per_node*& defer,
                 std::array<per_thread, CT_THREADS>& threads
             ) {
                 per_node* head = defer;
                 defer = nullptr; // Take ownership of the caller's 
local batch

                 // Merge any previously deferred nodes from m_defer0 
into the processing batch
                 per_node* curr = VAR(m_defer0);
                 VAR(m_defer0) = nullptr;
                 while (curr)
                 {
                     per_node* next_node = VAR(curr->m_defer);
                     VAR(curr->m_defer) = head;
                     head = curr;
                     curr = next_node;
                 }

                 // Two-Pass Hazard Pointer Scan:
                 // Pass 1: Capture all thread hazard slots
                 std::vector<per_node*> haz_pass1(threads.size());
                 for (size_t i = 0; i < threads.size(); ++i)
                 {
                     haz_pass1[i] = 
threads[i].m_haz_0.load(rl::mo_relaxed, $);
                 }

                 // Synchronization barrier between scans
                 rl::systemwide_fence($);

                 // Pass 2: Capture all thread hazard slots again
                 std::vector<per_node*> haz_pass2(threads.size());
                 for (size_t i = 0; i < threads.size(); ++i)
                 {
                     haz_pass2[i] = 
threads[i].m_haz_0.load(rl::mo_acquire, $);
                 }

                 per_node* remaining = nullptr;

                 while (head)
                 {
                     per_node* next = VAR(head->m_defer);
                     bool protected_node = false;

                     // Check if the node is protected in either pass 
snapshot
                     for (auto haz : haz_pass1)
                     {
                         if (haz == head)
                         {
                             protected_node = true;
                             break;
                         }
                     }

                     if (! protected_node)
                     {
                         for (auto haz : haz_pass2)
                         {
                             if (haz == head)
                             {
                                 protected_node = true;
                                 break;
                             }
                         }
                     }

                     if (protected_node)
                     {
                         // Keep it in the deferred list
                         VAR(head->m_defer) = remaining;
                         remaining = head;
                     }
                     else
                     {
                         // Safe to reclaim!
                         VAR(head->m_data) = 0xDEADBEEF;
                         delete head;
                     }

                     head = next;
                 }

                 // Store any still-protected nodes back into the 
central poller queue
                 VAR(m_defer0) = remaining;
             }

             bool
             try_poll(
                 per_node*& defer,
                 std::array<per_thread, CT_THREADS>& threads
             ) {
                 if (m_poll.try_lock($))
                 {
                     prv_poll(defer, threads);
                     m_poll.unlock($);
                     return true;
                 }

                 return false;
             }

             void
             poll(
                 per_node*& defer,
                 std::array<per_thread, CT_THREADS>& threads
             ) {
                 m_poll.lock($);
                 prv_poll(defer, threads);
                 m_poll.unlock($);
             }
         };

         struct state
         {
             std::array<per_thread, CT_THREADS> m_threads;
             poller m_poller = { };
             rl::atomic<per_node*> m_node = { };

             // relacy hooks, so to speak... ;^o
             // relaxed because everything either has not
             // started yet, or everything is already done...
             void
             before()
             {
                 per_node* node = new per_node();
                 VAR(node->m_data) = 456;
                 VAR(node->m_defer) = nullptr;

                 m_node.store(node, rl::mo_relaxed, $);
             }


             void
             after()
             {
                 per_node* node = m_node.load(rl::mo_relaxed, $);

                 if (node)
                 {
                     delete node;
                 }

                 // Also clean up any lingering nodes left in the 
poller's m_defer0 at teardown
                 per_node* poll_defer = VAR(m_poller.m_defer0);
                 while (poll_defer)
                 {
                     per_node* next = VAR(poll_defer->m_defer);
                     delete poll_defer;
                     poll_defer = next;
                 }
             }

             per_node*
             smr_load(
                 unsigned long tidx
             ) {
                 for (;;)
                 {
                     per_node* node0 = m_node.load(rl::mo_relaxed, $);

                     if (! node0)
                     {
                         RL_ASSERT(0); // hould never happen in this test!
                         m_threads[tidx].m_haz_0.store(nullptr, 
rl::mo_relaxed, $);
                         break;
                     }

                     m_threads[tidx].m_haz_0.store(node0, 
rl::mo_relaxed, $);

                     // change this to relaxed and it fails...
                     per_node* node1 = m_node.load(rl::mo_consume, $);

                     if (node0 == node1)
                     {
                         return node0;
                     }
                 }

                 return nullptr;
             }


             void
             smr_unload(
                 unsigned long tidx
             ) {
                 // humm...
                 m_threads[tidx].m_haz_0.store(nullptr, rl::mo_release, $);
             }
         };

         // the actual test unit...
         struct smr_test_v000
         :   rl::test_suite<smr_test_v000, CT_THREADS>
         {
             state m_state = { };

             void before()
             {
                 m_state.before();
             }

             void after()
             {
                 m_state.after();
             }

             void
             thread_reader(
                 unsigned int tidx
             ) {
                 for (unsigned long i = 0; i < CT_THREADS_READER_ITER_N; 
++i)
                 {
                     per_node* node = m_state.smr_load(tidx);

                     if (node)
                     {
                         // dummy load to poke Relacy... ;^)
  
(void)m_state.m_threads[tidx].m_haz_0.load(rl::mo_relaxed, $);

                         unsigned long data = VAR(node->m_data);
                         RL_ASSERT((data == 123 || data == 456) && (data 
!= 0xDEADBEEF));

                         m_state.smr_unload(tidx);
                     }
                 }
             }

             void
             thread_writer(
                 unsigned int tidx
             ) {
                 per_node* my_defer = nullptr;

                 for (unsigned long i = 0; i < CT_THREADS_WRITER_ITER_N; 
++i)
                 {
                     per_node* node = new per_node();
                     VAR(node->m_data) = 456;
                     VAR(node->m_defer) = nullptr;
                     per_node* old = m_state.m_node.exchange(node, 
rl::mo_acq_rel, $);

                     if (old)
                     {
                         VAR(old->m_defer) = my_defer;
                         my_defer = old;
                     }

                     // Opportunistically try to poll and sweep nodes
                     m_state.m_poller.try_poll(my_defer, m_state.m_threads);
                 }

                 // Drain remaining deferred nodes upon thread exit
                 m_state.m_poller.poll(my_defer, m_state.m_threads);
             }

             void
             thread(
                 unsigned int tidx
             ) {
                 if (tidx < CT_THREADS_READERS)
                 {
                     thread_reader(tidx);
                 }

                 else
                 {
                     thread_writer(tidx);
                 }
             }
         };
     }
}

namespace ct
{
     namespace smr_test
     {
         namespace test
         {
             inline void
             manifest(
                 rl::test_params& p
             ) {
                 rl::simulate<ct::smr_test::smr_test_v000>(p);
             }
         }
     }
}




____________________






Technical Details
Message-ID: <11ar5mm$2lg07$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <118f4vv$3plsv$1@dont-email.me> <11ag8ue$2nvjl$1@dont-email.me> <11alo51$nmur$1@dont-email.me> <11am17q$rl8q$1@dont-email.me> <11amf6v$10g9m$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups: comp.lang.c++
Subject: Re: Consequential RCU
Date: Thu, 15 Oct 2026 11:23:15 -0700
Organization: A noiseless patient Spider
Lines: 412
Message-ID: <11ar5mm$2lg07$1@dont-email.me>
References: <118f4vv$3plsv$1@dont-email.me> <11ag8ue$2nvjl$1@dont-email.me>
 <11alo51$nmur$1@dont-email.me> <11am17q$rl8q$1@dont-email.me>
 <11amf6v$10g9m$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 15 Oct 2026 18:23:19 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2801671"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/ZB1AaD8NKIibD7Dmgh3itulqArIO4L3w="; posting-host="979f69778276c51b653f13045ccea5e5"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:xZtHC9/zDuf8hv6YifVsn2mIh78= sha256:B9mk1z5FUCoDCVFWgAyVaU6RRy0p9V5/KX/9xmz/5uk=
	sha1:SSaRznYNScS1ot8Ofa1FIg8jFr8= sha256:SEMXyGt9JPsrvNExMyKIab0PyJgCtnCerUW6CL8N3UU=
In-Reply-To: <11amf6v$10g9m$1@dont-email.me>
Content-Language: en-US