Re: How to smartly deal with SIGSEGV (hazard pointers)

Liste des GroupesRevenir à cl c++ 
Sujet : Re: How to smartly deal with SIGSEGV (hazard pointers)
De : chris.m.thomasson.1 (at) *nospam* gmail.com (Chris M. Thomasson)
Groupes : comp.lang.c++
Date : 11. Mar 2026, 00:59:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <10oqba1$mbce$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 3/10/2026 1:56 AM, Bonita Montero wrote:
Am 09.03.2026 um 23:36 schrieb Chris M. Thomasson:
 
I don't think it mentions it there, but SEH is used. I prefer proxy collectors over hazard pointers, but that's just me. Well, Joe knows. Hazard pointers basically need async membar behavior or else they eat your lunch with that damn #StoreLoad.
 I've developed a lockfree stack with SEH. With one thread it is
lower than a SLIST, with two threads it is faster and with 32
threads it is magnitudes faster:
I need some more time to really examine it. Seems to look okay for now. Well, I cannot notice anything obviously wrong so far.
Notice where you align?
 > alignas(2 * sizeof(size_t)) head m_head = { 0, 0 };
Well, I don't think a SLIST anchor is automatically aligned. You should align it on a l2 cache line boundary and pad it up to a l2 cache line.
https://learn.microsoft.com/en-us/windows/win32/sync/using-singly-linked-lists
notice:
typedef struct _PROGRAM_ITEM {
     SLIST_ENTRY ItemEntry;
     ULONG Signature;
} PROGRAM_ITEM, *PPROGRAM_ITEM;
? Notice _aligned_malloc?
Also, have you checked that your compare_exchange_strong is using CMPXCHG8B on 32 bit systems and CMPXCHG16B on 64 bit systems? That used to piss me off when it said not always lock-free.
Little shits.
PROGRAM_ITEM needs to be aligned and padded, and PSLIST_HEADER needs to be aligned and padded.
Also, how are you testing it? If you never delete any nodes, your SEH is useless. Also, so, well, what memory allocator are you using?

 #pragma once
#if defined(_WIN32)
     #include <Windows.h>
#endif
#include <atomic>
 template<typename T>
struct lockfree_stack
{
     struct node : T
     {
         using T::T;
     private:
         template<typename T>
         friend struct lockfree_stack;
         node *m_next;
     };
     void push( node *nd ) noexcept;
     node *pop() noexcept;
private:
     struct head { node *ptr; size_t ctr; };
     static_assert(atomic_ref<head>::is_always_lock_free);
     alignas(2 * sizeof(size_t)) head m_head = { 0, 0 };
};
 template<typename T>
void lockfree_stack<T>::push( node *nd ) noexcept
{
     using namespace std;
     atomic_ref ptr( m_head.ptr );
     atomic_ref ctr( m_head.ctr );
     head hdRef( ptr.load( memory_order_relaxed ), ctr.load( memory_order_relaxed ) );
     atomic_ref<head> aHead( m_head );
     do
         nd->m_next = (node *)hdRef.ptr;
     while( aHead.compare_exchange_strong( hdRef, head( nd, hdRef.ctr + 1 ), memory_order_release, memory_order_relaxed ) );
}
 template<typename T>
auto lockfree_stack<T>::pop() noexcept -> node *
{
     using namespace std;
     atomic_ref ptr( m_head.ptr );
     atomic_ref ctr( m_head.ctr );
     head hdRef( ptr.load( memory_order_relaxed ), ctr.load( memory_order_relaxed ) );
     atomic_ref<head> aHead( m_head );
     for( ; ; )
     {
         node *next;
         if( !hdRef.ptr )
             return nullptr;
         __try
         {
             next = hdRef.ptr->m_next;
         }
         __except( EXCEPTION_EXECUTE_HANDLER )
         {
             hdRef = head( ptr.load( memory_order_relaxed ), ctr.load( memory_order_relaxed ) );
             continue;
         }
         if( aHead.compare_exchange_strong( hdRef, head( next, hdRef.ctr + 1 ), memory_order_acquire, memory_order_relaxed ) )
             return hdRef.ptr;
     }
}
 In the next step I make this Posix'd with my scoped_signal.
 

Date Sujet#  Auteur
9 Mar 26 * How to smartly deal with SIGSEGV (hazard pointers)33Bonita Montero
9 Mar 26 `* Re: How to smartly deal with SIGSEGV (hazard pointers)32Chris M. Thomasson
10 Mar 26  +* Re: How to smartly deal with SIGSEGV (hazard pointers)30Bonita Montero
10 Mar 26  i+- Re: How to smartly deal with SIGSEGV (hazard pointers)1Bonita Montero
10 Mar 26  i+* Re: How to smartly deal with SIGSEGV (hazard pointers)5Tristan Wibberley
10 Mar 26  ii`* Re: How to smartly deal with SIGSEGV (hazard pointers)4Bonita Montero
10 Mar 26  ii `* Re: How to smartly deal with SIGSEGV (hazard pointers)3Tristan Wibberley
11 Mar 26  ii  `* Re: How to smartly deal with SIGSEGV (hazard pointers)2Bonita Montero
16 Mar 26  ii   `- Re: How to smartly deal with SIGSEGV (hazard pointers)1Tristan Wibberley
11 Mar 26  i+* Re: How to smartly deal with SIGSEGV (hazard pointers)22Chris M. Thomasson
11 Mar 26  ii`* Re: How to smartly deal with SIGSEGV (hazard pointers)21Bonita Montero
11 Mar 26  ii `* Re: How to smartly deal with SIGSEGV (hazard pointers)20Chris M. Thomasson
12 Mar 26  ii  `* Re: How to smartly deal with SIGSEGV (hazard pointers)19Bonita Montero
12 Mar 26  ii   `* Re: How to smartly deal with SIGSEGV (hazard pointers)18Chris M. Thomasson
12 Mar 26  ii    `* Re: How to smartly deal with SIGSEGV (hazard pointers)17Bonita Montero
12 Mar 26  ii     `* Re: How to smartly deal with SIGSEGV (hazard pointers)16Bonita Montero
13 Mar 26  ii      `* Re: How to smartly deal with SIGSEGV (hazard pointers)15Chris M. Thomasson
13 Mar 26  ii       `* Re: How to smartly deal with SIGSEGV (hazard pointers)14Bonita Montero
13 Mar 26  ii        `* Re: How to smartly deal with SIGSEGV (hazard pointers)13Chris M. Thomasson
13 Mar 26  ii         `* Re: How to smartly deal with SIGSEGV (hazard pointers)12Bonita Montero
13 Mar 26  ii          `* Re: How to smartly deal with SIGSEGV (hazard pointers)11Chris M. Thomasson
13 Mar 26  ii           `* Re: How to smartly deal with SIGSEGV (hazard pointers)10Bonita Montero
13 Mar 26  ii            `* Re: How to smartly deal with SIGSEGV (hazard pointers)9Chris M. Thomasson
13 Mar 26  ii             +* Re: How to smartly deal with SIGSEGV (hazard pointers)2Chris M. Thomasson
13 Mar 26  ii             i`- Re: How to smartly deal with SIGSEGV (hazard pointers)1Chris M. Thomasson
15 Mar 26  ii             `* Re: How to smartly deal with SIGSEGV (hazard pointers)6Bonita Montero
15 Mar 26  ii              `* Re: How to smartly deal with SIGSEGV (hazard pointers)5Chris M. Thomasson
16 Mar 26  ii               `* Re: How to smartly deal with SIGSEGV (hazard pointers)4Bonita Montero
16 Mar 26  ii                `* Re: How to smartly deal with SIGSEGV (hazard pointers)3Chris M. Thomasson
17 Mar 26  ii                 `* Re: How to smartly deal with SIGSEGV (hazard pointers)2Tristan Wibberley
17 Mar 26  ii                  `- Re: How to smartly deal with SIGSEGV (hazard pointers)1Chris M. Thomasson
11 Mar 26  i`- Re: How to smartly deal with SIGSEGV (hazard pointers)1Chris M. Thomasson
11 Mar 26  `- Re: How to smartly deal with SIGSEGV (hazard pointers)1Chris M. Thomasson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal