🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Relacy verification: Asymmetric SMR / Hazard Pointer with mo_consume and zero seq_cst...

#554
From: Chris M. Thomasson
Date: Wed, 14 Oct 2026 21:14
323 lines
11133 bytes
On 10/13/2026 4:12 PM, Chris M. Thomasson wrote:
> Just wanted to share a quick update on a Safe Memory Reclamation (SMR) 
> hazard pointer engine I've been putting through its paces using Relacy. 
> I managed to design an asymmetric, poller-driven deferred reclamation 
> model that successfully eliminates heavy mo_seq_cst operations entirely 
> from the runtime path.[...]

Oh my... I don't even need to use the SMR loop on smr_load! This works 
fine. All relaxed, no consume for the smr_load. Here is a relacy test unit:
____________________
#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_relaxed, $);
                 }

                 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!
                         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
             ) {
                 per_node* node0 = m_node.load(rl::mo_relaxed, $);
                 m_threads[tidx].m_haz_0.store(node0, rl::mo_relaxed, $);
                 return node0;
             }


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

         // 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)
                     {
                         unsigned long data = VAR(node->m_data);
                         RL_ASSERT(data == 123 || data == 456);

                         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: <11aorci$1rrdk$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: <11amdsd$108d7$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: Relacy verification: Asymmetric SMR / Hazard Pointer with
 mo_consume and zero seq_cst...
Date: Wed, 14 Oct 2026 14:14:56 -0700
Organization: A noiseless patient Spider
Lines: 323
Message-ID: <11aorci$1rrdk$1@dont-email.me>
References: <11amdsd$108d7$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 14 Oct 2026 21:14:59 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1961396"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+IoDBuOmtBb7oS+7gcQT5n3wL+T6wo6OY="; posting-host="643801f06158b8bfbdcc975916b62ee0"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:9UkTiwblNEmHkNMVsA2aeHB049U= sha256:GWqcueRC9STL+fWbMsxPgUwMYJljnt/tg/6TdmJxKX0=
	sha1:hzBBSZIgBeAHu8CvuVebNQszgvc= sha256:9Sjxi7vmlSSgXznpbVaMjjhoPaY3wZNK0Kaz+//r6LA=
Content-Language: en-US
In-Reply-To: <11amdsd$108d7$1@dont-email.me>