🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

getting back into Relacy...

#475
From: Chris M. Thomasson
Date: Tue, 13 Oct 2026 07:37
232 lines
6165 bytes
So, I have the most recent version and coded up my old multex algo. It 
100% dead lock free and allows a thread to locks multiple objects in any 
order and lock them. All of the lock ordering is taken care of in the 
finalize function. Basically a thread seeds its per thread lock indices 
with addresses to objects it wants to lock. Calls finalize, then locks 
them. Does its thing and then unlocks them. Fwiw, here is my Relacy test 
output:

struct ct_multex_relacy_test_v000
65% (65536/100000)
iterations: 100000
total time: 9390
throughput: 10649


D:\ct_dev\ct_msvc\projects\ct_relacy_test\x64\Debug\ct_relacy_test.exe 
(process 13356) exited with code 0 (0x0).
Press any key to close this window . . .


Any my code:

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

#if ! defined (NDEBUG)
#   define CT_DBG_PRINTF(e) std::printf e
#else
#   define CT_DBG_PRINTF(e) ((void)0)
#endif


#define CT_THREADS (5)
#define CT_MUTEX_TABLE_N (64)


struct ct_mutex_table
{
     rl::mutex m_mutexs[CT_MUTEX_TABLE_N];

     std::size_t
     hash_ptr(
         void* p
     ) const {
         return rl::hash_ptr(p, CT_MUTEX_TABLE_N);
     }
};


struct ct_thread_mutex_hash
{
     std::vector<std::size_t> m_hashed_idxs;

     // must be before any locks!
     void finalize()
     {
         std::sort(
             m_hashed_idxs.begin(),
             m_hashed_idxs.end());

         m_hashed_idxs.erase(
             std::unique(
                 m_hashed_idxs.begin(),
                 m_hashed_idxs.end()),
             m_hashed_idxs.end());
     }


     // BEFORE WE CALL, WE NEED TO sort and dedup before this.
     void
     lock(
         ct_mutex_table& mutex_table
     ) {
         for (auto& mtx_idx : m_hashed_idxs)
         {
             rl::mutex& mtx = mutex_table.m_mutexs[mtx_idx];
             mtx.lock($);
         }
     }

     // does not really even need to be reversed...
     void
     unlock(
         ct_mutex_table& mutex_table
     ) {
         for (auto& mtx_idx : m_hashed_idxs)
         {
             rl::mutex& mtx = mutex_table.m_mutexs[mtx_idx];
             mtx.unlock($);
         }
     }
};


struct ct_state
{
     ct_mutex_table m_mutex_table;
     VAR_T(int) m_var_0 = 0;
     VAR_T(int) m_var_1 = 1;
     VAR_T(int) m_var_2 = 2;


     void worker_thread_a()
     {
         ct_thread_mutex_hash self_hash;

         self_hash.m_hashed_idxs.push_back(
             m_mutex_table.hash_ptr(&m_var_0));

         self_hash.m_hashed_idxs.push_back(
             m_mutex_table.hash_ptr(&m_var_1));

         self_hash.finalize();

         self_hash.lock(m_mutex_table);

         VAR(m_var_0) = 42;
         VAR(m_var_1) = 128;

         self_hash.unlock(m_mutex_table);
     }

     void worker_thread_b()
     {
         ct_thread_mutex_hash self_hash;

         // Seed!
         {
             self_hash.m_hashed_idxs.push_back(
                 m_mutex_table.hash_ptr(&m_var_1));

             self_hash.m_hashed_idxs.push_back(
                 m_mutex_table.hash_ptr(&m_var_0));
         }


         // Okay... Finalize it...
         {
             self_hash.finalize();
         }


         // Finally, we can lock the bastard!
         {
             self_hash.lock(m_mutex_table);

             {
                 VAR(m_var_0) = 99;
                 VAR(m_var_1) = 77;
             }

             self_hash.unlock(m_mutex_table);
         }
     }
};


struct ct_multex_relacy_test_v000
     : rl::test_suite<ct_multex_relacy_test_v000, CT_THREADS>
{
     ct_state m_state = { };

     void before()
     {
         // not sure to init m_state here. Is it already init?
     }

     void
     thread(
         unsigned tidx
     ) {
         if (tidx == 0)
             m_state.worker_thread_a();
         else
             m_state.worker_thread_b();
     }
};


int main(int argc, char** argv)
{
     {
         rl::test_params p;

         p.iteration_count = argc > 1 ? (unsigned)atoi(argv[1]) : 100000;
         p.execution_depth_limit = 100000;

         if (argc > 2) {
             std::string m = argv[2];
             if (m == "full") p.search_type = rl::sched_full;
             else if (m == "bound") { p.search_type = rl::sched_bound; 
p.context_bound = argc > 3 ? atoi(argv[3]) : 2; }
         }

         rl::simulate<ct_multex_relacy_test_v000>(p);
     }

     return 0;
}



/*
#include <relacy/relacy.hpp>
#include <iostream>


int
main()
{
     std::cout << "ct_relacy_test\n";
     std::cout << "____________________________________________" << 
std::endl;

     {

     }

     std::cout << "____________________________________________" << 
std::endl;
     std::cout << "ct_relacy_test fin! :^)\n";

     return 0;
}

*/

Technical Details
Message-ID: <11akn3i$aq12$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

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: getting back into Relacy...
Date: Tue, 13 Oct 2026 00:37:22 -0700
Organization: A noiseless patient Spider
Lines: 232
Message-ID: <11akn3i$aq12$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 13 Oct 2026 07:37:23 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="354338"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19eW7+Dty2u+UGiqYfWKxlIVeJ/X6EwtL8="; posting-host="094496f6b743c51c3f22a7000b0b8b16"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:17e+Vbv/mEMqHuQBduSiiFnRwOs= sha256:oI1pjSzi/7r1gKzoQEIh+CUk/VUBVpHhm9QyyTFoLb4=
	sha1:OABK2f7K/n5aE4QWHPDhU0su9zk= sha256:X5egzRm7eQcdFc17rx/FXvrPZ7x4NvixBP5H6iYs1Y8=
Content-Language: en-US