🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

my old eventcount ported to Relacy...

#878
From: Chris M. Thomasson
Date: Sun, 15 Nov 2026 23:30
168 lines
5488 bytes
I wrote about this a long time ago back in 2005. I need to find my old 
post on c.p.t. Joe mentioned the damn memory barriers, uggg. But, it 
should work with a distributed version of my futex stack I posted here 
yesterday. Passes tests:

_____________________
#pragma once

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


namespace ct
{
     namespace eventcount_test
     {
         constexpr unsigned long CT_THREADS = 4;
         constexpr unsigned long CT_ITERS = 3;

         // debug
         static unsigned long g_debug_wait_calls = 0;
         static unsigned long g_debug_notify_slow = 0;


         struct eventcount
         {
             enum : unsigned long
             {
                 WAITERS = 0x80000000ul,
                 COUNT = 0x7FFFFFFFul
             };

             rl::atomic<unsigned long> m_ec = { 0 };

             unsigned long
             get_key()
             {
                 // slow path anyway...
                 unsigned long prev = m_ec.fetch_or(WAITERS, 
rl::mo_seq_cst, $);
                 return prev & COUNT;
             }

             void
             wait_key(unsigned long key)
             {
                 m_ec.wait(key | WAITERS, rl::mo_relaxed, $);
                 ++g_debug_wait_calls;
             }

             void
             notify_all()
             {
                 // GRRRRRR!!!!!
                 // humm... might be ripe for async...
                 // or help from the user algo state...
                 unsigned long local = m_ec.load(rl::mo_seq_cst, $);

                 if (local & WAITERS)
                 {
                     while (! m_ec.compare_exchange_strong(
                         local, (local + 1) & COUNT, rl::mo_relaxed, $))
                     {

                     }

                     m_ec.notify_all($); // grrr! from my old code.
                     ++g_debug_notify_slow;
                 }
             }
         };

         struct ec_mutex
         {
             rl::atomic<unsigned long> m_state = { 0 };
             eventcount m_eventcount;


             // Test the eventcount version...
             void
             ec_lock()
             {
                 while (m_state.exchange(1, rl::mo_acquire, $))
                 {
                     unsigned long key = m_eventcount.get_key();

                     if (! m_state.exchange(1, rl::mo_acquire, $))
                     {
                         break;
                     }

                     m_eventcount.wait_key(key);
                 }
             }

             void
             ec_unlock()
             {
                 m_state.store(0, rl::mo_release, $);
                 m_eventcount.notify_all();
             }
         };


         // the actual test unit...
         struct eventcount_relacy_test_v000
             : rl::test_suite<eventcount_relacy_test_v000, CT_THREADS>
         {
             ec_mutex m_mutex;

             VAR_T(unsigned long) m_state = { 0 };

             void before()
             {
                 RL_ASSERT(VAR(m_state) == 0);
             }

             void after()
             {
                 RL_ASSERT(VAR(m_state) == CT_THREADS * CT_ITERS);
             }

             void
             thread(
                 unsigned int tidx
             ) {
                 for (unsigned long i = 0; i < CT_ITERS; ++i)
                 {
                     m_mutex.ec_lock();

                     {
                         VAR(m_state) += 1;
                     }

                     m_mutex.ec_unlock();
                 }
             }
         };
     }
}


namespace ct
{
     namespace eventcount_test
     {
         namespace test
         {
             inline void
             manifest(
                 rl::test_params& p
             ) {
  
rl::simulate<ct::eventcount_test::eventcount_relacy_test_v000>(p);

                 std::cout << "g_debug_wait_calls = " << 
g_debug_wait_calls << "\n";
                 std::cout << "g_debug_notify_slow = " << 
g_debug_notify_slow << "\n";
             }
         }
     }
}
_____________________
Technical Details
Message-ID: <11ddfba$35kg6$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!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: my old eventcount ported to Relacy...
Date: Sun, 15 Nov 2026 15:30:49 -0800
Organization: A noiseless patient Spider
Lines: 168
Message-ID: <11ddfba$35kg6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 15 Nov 2026 23:30:51 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3330566"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+SHm16sW73OwUxm5Woo2rk/KzJTq9GZNI="; posting-host="8d4bfe82b7f7bce740ab6d5ca121f681"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:vPH8bcUcVCuOeUomCX/HcGhVlsA= sha256:yoGKD7qN7dBHTiGrzIIOuE396hdYl+F42S7zSdCEAFg=
	sha1:UvVdJXJpOvYl7N32wYkBjG77xOE= sha256:Ny5v9W89Gt7al2Dpn/xMLnOMaYO+XXACySUQz7XYCzI=
Content-Language: en-US