[2314aac] | 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 |
---|
[d8d9c115] | 12 | // Last Modified On : Tue May 30 19:02:32 2023 |
---|
| 13 | // Update Count : 18 |
---|
[2314aac] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <thread.hfa> |
---|
| 17 | #include <atomic.hfa> // CASV |
---|
| 18 | #include <fstream.hfa> |
---|
| 19 | |
---|
| 20 | struct Node; // forward declaration |
---|
| 21 | union Link { |
---|
[d8d9c115] | 22 | struct { // 32/64-bit x 2 |
---|
[2314aac] | 23 | Node * volatile top; // pointer to stack top |
---|
| 24 | uintptr_t count; // count each push |
---|
| 25 | }; |
---|
[687b663] | 26 | #if defined( __SIZEOF_INT128__ ) |
---|
[2314aac] | 27 | __int128 atom; // gcc, 128-bit integer |
---|
[687b663] | 28 | #else |
---|
| 29 | int64_t atom; |
---|
| 30 | #endif // __SIZEOF_INT128__ |
---|
[2314aac] | 31 | } __attribute__(( aligned( 16 ) )); |
---|
| 32 | |
---|
| 33 | struct Node { |
---|
| 34 | // resource data |
---|
| 35 | Link next; // pointer to next node/count (resource) |
---|
| 36 | }; |
---|
| 37 | struct Stack { |
---|
| 38 | Link stack; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | void push( Stack & s, Node & n ) with(s) { |
---|
| 42 | n.next = stack; // atomic assignment unnecessary |
---|
| 43 | for () { // busy wait |
---|
| 44 | if ( CASV( stack.atom, n.next.atom, ((Link){ &n, n.next.count + 1 }.atom) ) ) break; // attempt to update top node |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | Node * pop( Stack & s ) with(s) { |
---|
| 49 | Link t = stack; // atomic assignment unnecessary |
---|
| 50 | for () { // busy wait |
---|
| 51 | 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 | } |
---|
| 54 | } |
---|
| 55 | void ?{}( Stack & s ) with(s) { stack.atom = 0; } |
---|
| 56 | |
---|
| 57 | Stack stack; // global stack |
---|
| 58 | |
---|
[d8d9c115] | 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 | }; |
---|
| 66 | |
---|
[2314aac] | 67 | thread Worker {}; |
---|
| 68 | void main( Worker & w ) { |
---|
[d8d9c115] | 69 | for ( i; Times ) { |
---|
| 70 | Node & n = *pop( stack ); // pop any node |
---|
[2314aac] | 71 | assert( &n != NULL ); |
---|
[d8d9c115] | 72 | n.next.top = 0p; // scrub fields |
---|
[2314aac] | 73 | n.next.count = 0; |
---|
| 74 | //yield( rand() % 3 ); |
---|
[d8d9c115] | 75 | push( stack, n ); // push it back |
---|
[2314aac] | 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | int main() { |
---|
[d8d9c115] | 80 | enum { N = 8 }; // kernel threads |
---|
| 81 | processor p[N - 1]; // add kernel threads |
---|
[2314aac] | 82 | |
---|
| 83 | 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 ) ) ); |
---|
| 86 | } |
---|
| 87 | { |
---|
| 88 | Worker workers[N]; // run test |
---|
| 89 | } |
---|
| 90 | for ( i; N ) { // pop N nodes from list |
---|
| 91 | free( pop( stack ) ); |
---|
| 92 | } |
---|
| 93 | sout | "done"; // non-empty .expect file |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | // Local Variables: // |
---|
| 97 | // compile-command: "cfa -g -O3 lockfree_stack.cfa" // |
---|
| 98 | // End: // |
---|