Changeset 37b2c2c


Ignore:
Timestamp:
Jun 9, 2023, 2:31:04 PM (11 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ast-experimental, master
Children:
758c9ef
Parents:
0442f93f
Message:

update lockfree stack with double-wide CAS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/concurrency/lockfree_stack.cfa

    r0442f93f r37b2c2c  
    1010// Created On       : Thu May 25 15:36:50 2023
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 30 19:02:32 2023
    13 // Update Count     : 18
     12// Last Modified On : Fri Jun  9 14:01:07 2023
     13// Update Count     : 68
    1414//
    1515
     
    2929        int64_t atom;
    3030        #endif // __SIZEOF_INT128__
    31 } __attribute__(( aligned( 16 ) ));
     31};
    3232
    3333struct Node {
     
    4242        n.next = stack;                                                                         // atomic assignment unnecessary
    4343        for () {                                                                                        // busy wait
    44                 if ( CASV( stack.atom, n.next.atom, ((Link){ &n, n.next.count + 1 }.atom) ) ) break; // attempt to update top node
     44                Link temp{ &n, n.next.count + 1 };
     45                if ( CASV( s.stack.atom, n.next.atom, temp.atom ) ) break; // attempt to update top node
    4546        }
    4647}
     
    5051        for () {                                                                                        // busy wait
    5152                if ( t.top == NULL ) return NULL;                               // empty stack ?
    52                 if ( CASV( stack.atom, t.atom, ((Link){ t.top->next.top, t.count }.atom) ) ) return t.top; // attempt to update top node
     53                Link temp{ t.top->next.top, t.count };
     54                if ( CASV( stack.atom, t.atom, temp.atom ) ) return t.top; // attempt to update top node
    5355        }
    5456}
     
    5759Stack stack;                                                                                    // global stack
    5860
    59 enum { Times =
    60         #if defined( __ARM_ARCH )                                                       // ARM CASV is very slow
    61         10_000
    62         #else
    63         1_000_000
    64         #endif // __arm_64__
    65 };
     61enum { Times = 2_000_000 };
    6662
    6763thread Worker {};
     
    8278
    8379        for ( i; N ) {                                                                          // push N values on stack
    84                 // storage must be 16-bytes aligned for cmpxchg16b
    85                 push( stack, *(Node *)memalign( 16, sizeof( Node ) ) );
     80                push( stack, *(Node *)new() );                                  // must be 16-byte aligned
    8681        }
    8782        {
Note: See TracChangeset for help on using the changeset viewer.