[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 | //
|
---|
[d923fca] | 7 | // lockfree_stack.cfa --
|
---|
[2314aac] | 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Thu May 25 15:36:50 2023
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[37b2c2c] | 12 | // Last Modified On : Fri Jun 9 14:01:07 2023
|
---|
| 13 | // Update Count : 68
|
---|
[d923fca] | 14 | //
|
---|
[2314aac] | 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__
|
---|
[37b2c2c] | 31 | };
|
---|
[2314aac] | 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
|
---|
[d923fca] | 44 | Link temp{ { &n, n.next.count + 1 } };
|
---|
[37b2c2c] | 45 | if ( CASV( s.stack.atom, n.next.atom, temp.atom ) ) break; // attempt to update top node
|
---|
[2314aac] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | Node * pop( Stack & s ) with(s) {
|
---|
| 50 | Link t = stack; // atomic assignment unnecessary
|
---|
| 51 | for () { // busy wait
|
---|
| 52 | if ( t.top == NULL ) return NULL; // empty stack ?
|
---|
[d923fca] | 53 | Link temp{ { t.top->next.top, t.count } };
|
---|
[37b2c2c] | 54 | if ( CASV( stack.atom, t.atom, temp.atom ) ) return t.top; // attempt to update top node
|
---|
[2314aac] | 55 | }
|
---|
| 56 | }
|
---|
| 57 | void ?{}( Stack & s ) with(s) { stack.atom = 0; }
|
---|
| 58 |
|
---|
| 59 | Stack stack; // global stack
|
---|
| 60 |
|
---|
[37b2c2c] | 61 | enum { Times = 2_000_000 };
|
---|
[d8d9c115] | 62 |
|
---|
[2314aac] | 63 | thread Worker {};
|
---|
[d923fca] | 64 | void main( Worker & ) {
|
---|
[d8d9c115] | 65 | for ( i; Times ) {
|
---|
| 66 | Node & n = *pop( stack ); // pop any node
|
---|
[2314aac] | 67 | assert( &n != NULL );
|
---|
[d8d9c115] | 68 | n.next.top = 0p; // scrub fields
|
---|
[2314aac] | 69 | n.next.count = 0;
|
---|
| 70 | //yield( rand() % 3 );
|
---|
[d8d9c115] | 71 | push( stack, n ); // push it back
|
---|
[2314aac] | 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | int main() {
|
---|
[d8d9c115] | 76 | enum { N = 8 }; // kernel threads
|
---|
| 77 | processor p[N - 1]; // add kernel threads
|
---|
[2314aac] | 78 |
|
---|
| 79 | for ( i; N ) { // push N values on stack
|
---|
[37b2c2c] | 80 | push( stack, *(Node *)new() ); // must be 16-byte aligned
|
---|
[2314aac] | 81 | }
|
---|
| 82 | {
|
---|
| 83 | Worker workers[N]; // run test
|
---|
| 84 | }
|
---|
| 85 | for ( i; N ) { // pop N nodes from list
|
---|
| 86 | free( pop( stack ) );
|
---|
| 87 | }
|
---|
| 88 | sout | "done"; // non-empty .expect file
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | // Local Variables: //
|
---|
| 92 | // compile-command: "cfa -g -O3 lockfree_stack.cfa" //
|
---|
| 93 | // End: //
|
---|