| 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2022 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| 7 | // lockfree_stack.cfa --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Peter A. Buhr
|
|---|
| 10 | // Created On : Thu May 25 15:36:50 2023
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Thu May 25 16:20:23 2023
|
|---|
| 13 | // Update Count : 2
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <thread.hfa>
|
|---|
| 17 | #include <atomic.hfa> // CASV
|
|---|
| 18 | #include <fstream.hfa>
|
|---|
| 19 |
|
|---|
| 20 | struct Node; // forward declaration
|
|---|
| 21 | union Link {
|
|---|
| 22 | struct { // 64-bit x 2
|
|---|
| 23 | Node * volatile top; // pointer to stack top
|
|---|
| 24 | uintptr_t count; // count each push
|
|---|
| 25 | };
|
|---|
| 26 | __int128 atom; // gcc, 128-bit integer
|
|---|
| 27 | } __attribute__(( aligned( 16 ) ));
|
|---|
| 28 |
|
|---|
| 29 | struct Node {
|
|---|
| 30 | // resource data
|
|---|
| 31 | Link next; // pointer to next node/count (resource)
|
|---|
| 32 | };
|
|---|
| 33 | struct Stack {
|
|---|
| 34 | Link stack;
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | void push( Stack & s, Node & n ) with(s) {
|
|---|
| 38 | n.next = stack; // atomic assignment unnecessary
|
|---|
| 39 | for () { // busy wait
|
|---|
| 40 | if ( CASV( stack.atom, n.next.atom, ((Link){ &n, n.next.count + 1 }.atom) ) ) break; // attempt to update top node
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | Node * pop( Stack & s ) with(s) {
|
|---|
| 45 | Link t = stack; // atomic assignment unnecessary
|
|---|
| 46 | for () { // busy wait
|
|---|
| 47 | if ( t.top == NULL ) return NULL; // empty stack ?
|
|---|
| 48 | if ( CASV( stack.atom, t.atom, ((Link){ t.top->next.top, t.count }.atom) ) ) return t.top; // attempt to update top node
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | void ?{}( Stack & s ) with(s) { stack.atom = 0; }
|
|---|
| 52 |
|
|---|
| 53 | Stack stack; // global stack
|
|---|
| 54 |
|
|---|
| 55 | thread Worker {};
|
|---|
| 56 | void main( Worker & w ) {
|
|---|
| 57 | for ( i; 100000 ) {
|
|---|
| 58 | Node & n = *pop( stack );
|
|---|
| 59 | assert( &n != NULL );
|
|---|
| 60 | n.next.top = 0p; // shrub fields
|
|---|
| 61 | n.next.count = 0;
|
|---|
| 62 | //yield( rand() % 3 );
|
|---|
| 63 | push( stack, n );
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | int main() {
|
|---|
| 69 | enum { N = 10 };
|
|---|
| 70 | processor p[N - 1]; // kernel threads
|
|---|
| 71 |
|
|---|
| 72 | for ( i; N ) { // push N values on stack
|
|---|
| 73 | // storage must be 16-bytes aligned for cmpxchg16b
|
|---|
| 74 | push( stack, *(Node *)memalign( 16, sizeof( Node ) ) );
|
|---|
| 75 | }
|
|---|
| 76 | {
|
|---|
| 77 | Worker workers[N]; // run test
|
|---|
| 78 | }
|
|---|
| 79 | for ( i; N ) { // pop N nodes from list
|
|---|
| 80 | free( pop( stack ) );
|
|---|
| 81 | }
|
|---|
| 82 | sout | "done"; // non-empty .expect file
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | // Local Variables: //
|
|---|
| 86 | // compile-command: "cfa -g -O3 lockfree_stack.cfa" //
|
|---|
| 87 | // End: //
|
|---|