| [a552a8c] | 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 | // comp_fair.cfa -- Test that spinning doesn't cause completions to get stuck. | 
|---|
|  | 8 | //                  This test should work without io_uring but isn't very useful without | 
|---|
|  | 9 | // | 
|---|
|  | 10 | // Author           : Thierry Delisle | 
|---|
|  | 11 | // Created On       : Thu Mar 10 15:06:50 2022 | 
|---|
|  | 12 | // Last Modified By : | 
|---|
|  | 13 | // Last Modified On : | 
|---|
|  | 14 | // Update Count     : | 
|---|
|  | 15 | // | 
|---|
|  | 16 |  | 
|---|
|  | 17 |  | 
|---|
| [1417f6b] | 18 | #include <concurrency/barrier.hfa> | 
|---|
| [a552a8c] | 19 | #include <fstream.hfa> | 
|---|
|  | 20 | #include <iofwd.hfa> | 
|---|
|  | 21 | #include <kernel.hfa> | 
|---|
|  | 22 | #include <thread.hfa> | 
|---|
|  | 23 |  | 
|---|
|  | 24 | #include <errno.h> | 
|---|
|  | 25 | #include <string.h> | 
|---|
|  | 26 | #include <unistd.h> | 
|---|
|  | 27 |  | 
|---|
| [aca0d2f] | 28 | static struct { | 
|---|
| [680137a] | 29 | barrier & bar; | 
|---|
|  | 30 | int pipe[2]; | 
|---|
| [a552a8c] | 31 | } globals; | 
|---|
|  | 32 |  | 
|---|
|  | 33 | Duration default_preemption() { | 
|---|
|  | 34 | return 0; | 
|---|
|  | 35 | } | 
|---|
|  | 36 |  | 
|---|
|  | 37 | enum { TIMES = 1000 }; | 
|---|
|  | 38 |  | 
|---|
|  | 39 | volatile unsigned counter = 0; | 
|---|
|  | 40 |  | 
|---|
|  | 41 |  | 
|---|
|  | 42 | // ----- Spinner ----- | 
|---|
|  | 43 | // spins trying to prevent other threads from getting to this processor | 
|---|
|  | 44 | thread Spinner {}; | 
|---|
|  | 45 | void main(Spinner &) { | 
|---|
|  | 46 | unsigned last = 0; | 
|---|
|  | 47 | for() { | 
|---|
|  | 48 | unsigned curr = __atomic_load_n(&counter, __ATOMIC_SEQ_CST); | 
|---|
|  | 49 |  | 
|---|
| [72ba508] | 50 | if(curr >= TIMES) break; | 
|---|
| [a552a8c] | 51 |  | 
|---|
|  | 52 | if(last == curr) { | 
|---|
| [be1d00c] | 53 | sched_yield(); | 
|---|
| [a552a8c] | 54 | continue; | 
|---|
|  | 55 | } | 
|---|
|  | 56 |  | 
|---|
|  | 57 | last = curr; | 
|---|
|  | 58 | yield(); | 
|---|
|  | 59 | } | 
|---|
|  | 60 | } | 
|---|
|  | 61 |  | 
|---|
|  | 62 | // ----- Reader ----- | 
|---|
|  | 63 | // Reader from the pipe to test completion doesn't starve | 
|---|
|  | 64 | thread Reader {}; | 
|---|
|  | 65 | void main(Reader & this) { | 
|---|
| [69698d2] | 66 | char thrash[1]; | 
|---|
| [680137a] | 67 | bool do_read = has_user_level_blocking( (fptr_t)async_read ); | 
|---|
|  | 68 |  | 
|---|
|  | 69 | for(TIMES) { | 
|---|
|  | 70 | io_future_t f; | 
|---|
|  | 71 | if ( do_read ) { | 
|---|
|  | 72 | async_read(f, globals.pipe[0], thrash, 1, 0); | 
|---|
|  | 73 | } else { | 
|---|
|  | 74 | fulfil(f, 0); // If we don't have user-level blocking just play along | 
|---|
|  | 75 | } | 
|---|
| [a552a8c] | 76 |  | 
|---|
| [680137a] | 77 | block( globals.bar ); | 
|---|
| [1417f6b] | 78 |  | 
|---|
| [7bc84b8] | 79 | yield( prng( this, 15 ) ); | 
|---|
| [a552a8c] | 80 |  | 
|---|
| [680137a] | 81 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); | 
|---|
| [a552a8c] | 82 | if(0 == (i % 100)) sout | i; | 
|---|
|  | 83 |  | 
|---|
| [680137a] | 84 | wait( f ); | 
|---|
| [a552a8c] | 85 |  | 
|---|
| [680137a] | 86 | if(f.result < 0) | 
|---|
|  | 87 | abort | "Read error" | -f.result | ":" | strerror(-f.result); | 
|---|
| [a552a8c] | 88 |  | 
|---|
| [680137a] | 89 | block( globals.bar ); | 
|---|
|  | 90 | } | 
|---|
| [a552a8c] | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 | // ----- Writer ----- | 
|---|
|  | 94 | // Writes to the pipe so the Reader can unblock | 
|---|
|  | 95 | // takes its sweet time so the Reader has to block | 
|---|
|  | 96 | thread Writer {}; | 
|---|
|  | 97 | void main(Writer & this) { | 
|---|
| [680137a] | 98 | for(TIMES) { | 
|---|
|  | 99 | block( globals.bar ); | 
|---|
| [a552a8c] | 100 |  | 
|---|
| [680137a] | 101 | sleep( 1`us ); | 
|---|
| [a552a8c] | 102 |  | 
|---|
| [680137a] | 103 | char buf[1] = { '+' }; | 
|---|
|  | 104 | int ret = write( globals.pipe[1], buf, 1 ); | 
|---|
|  | 105 | if(ret < 0) | 
|---|
|  | 106 | abort | "Write error" | errno | ":" | strerror(errno); | 
|---|
| [a552a8c] | 107 |  | 
|---|
| [680137a] | 108 | block( globals.bar ); | 
|---|
|  | 109 | } | 
|---|
| [a552a8c] | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | // ----- Yielder ----- | 
|---|
|  | 113 | // Add some chaos into the mix | 
|---|
|  | 114 | thread Yielder {}; | 
|---|
|  | 115 | void ^?{}(Yielder &mutex ) {} | 
|---|
|  | 116 | void main(Yielder&) { | 
|---|
|  | 117 | while(TIMES > __atomic_load_n(&counter, __ATOMIC_SEQ_CST)) { | 
|---|
|  | 118 | yield(); | 
|---|
|  | 119 | } | 
|---|
|  | 120 | } | 
|---|
|  | 121 |  | 
|---|
|  | 122 | int main() { | 
|---|
| [680137a] | 123 | barrier bar = { 2 }; | 
|---|
|  | 124 | &globals.bar = &bar; | 
|---|
|  | 125 | int ret = pipe(globals.pipe); | 
|---|
|  | 126 | if(ret != 0) | 
|---|
|  | 127 | abort | "Pipe error" | errno | ":" | strerror(errno); | 
|---|
| [a552a8c] | 128 |  | 
|---|
|  | 129 | processor p; | 
|---|
|  | 130 | sout | "starting"; | 
|---|
|  | 131 | { | 
|---|
|  | 132 | Yielder y; | 
|---|
|  | 133 | Spinner s; | 
|---|
|  | 134 | Reader ior; | 
|---|
| [680137a] | 135 | Writer iow; | 
|---|
| [a552a8c] | 136 | } | 
|---|
|  | 137 | sout | "done"; | 
|---|
| [d2ad151] | 138 | } | 
|---|