Thread View: comp.lang.c++
1 messages
1 total messages
Started by Chris M. Thomasson
Sun, 15 Nov 2026 23:30
my old eventcount ported to Relacy...
Author: Chris M. Thomasson
Date: Sun, 15 Nov 2026 23:30
Date: Sun, 15 Nov 2026 23:30
168 lines
5488 bytes
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";
}
}
}
}
_____________________
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