| [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 |  | 
|---|
|  | 32 | } globals; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | Duration default_preemption() { | 
|---|
|  | 35 | return 0; | 
|---|
|  | 36 | } | 
|---|
|  | 37 |  | 
|---|
|  | 38 | enum { TIMES = 1000 }; | 
|---|
|  | 39 |  | 
|---|
|  | 40 | volatile unsigned counter = 0; | 
|---|
|  | 41 |  | 
|---|
|  | 42 |  | 
|---|
|  | 43 | // ----- Spinner ----- | 
|---|
|  | 44 | // spins trying to prevent other threads from getting to this processor | 
|---|
|  | 45 | thread Spinner {}; | 
|---|
|  | 46 | void main(Spinner &) { | 
|---|
|  | 47 | unsigned last = 0; | 
|---|
|  | 48 | for() { | 
|---|
|  | 49 | unsigned curr = __atomic_load_n(&counter, __ATOMIC_SEQ_CST); | 
|---|
|  | 50 |  | 
|---|
| [72ba508] | 51 | if(curr >= TIMES) break; | 
|---|
| [a552a8c] | 52 |  | 
|---|
|  | 53 | if(last == curr) { | 
|---|
| [be1d00c] | 54 | sched_yield(); | 
|---|
| [a552a8c] | 55 | continue; | 
|---|
|  | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | last = curr; | 
|---|
|  | 59 | yield(); | 
|---|
|  | 60 | } | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | // ----- Reader ----- | 
|---|
|  | 64 | // Reader from the pipe to test completion doesn't starve | 
|---|
|  | 65 | thread Reader {}; | 
|---|
|  | 66 | void main(Reader & this) { | 
|---|
| [69698d2] | 67 | char thrash[1]; | 
|---|
| [680137a] | 68 | bool do_read = has_user_level_blocking( (fptr_t)async_read ); | 
|---|
|  | 69 |  | 
|---|
|  | 70 | for(TIMES) { | 
|---|
|  | 71 | io_future_t f; | 
|---|
|  | 72 | if ( do_read ) { | 
|---|
|  | 73 | async_read(f, globals.pipe[0], thrash, 1, 0); | 
|---|
|  | 74 | } else { | 
|---|
|  | 75 | fulfil(f, 0); // If we don't have user-level blocking just play along | 
|---|
|  | 76 | } | 
|---|
| [a552a8c] | 77 |  | 
|---|
| [680137a] | 78 | block( globals.bar ); | 
|---|
| [1417f6b] | 79 |  | 
|---|
| [7bc84b8] | 80 | yield( prng( this, 15 ) ); | 
|---|
| [a552a8c] | 81 |  | 
|---|
| [680137a] | 82 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); | 
|---|
| [a552a8c] | 83 | if(0 == (i % 100)) sout | i; | 
|---|
|  | 84 |  | 
|---|
| [680137a] | 85 | wait( f ); | 
|---|
| [a552a8c] | 86 |  | 
|---|
| [680137a] | 87 | if(f.result < 0) | 
|---|
|  | 88 | abort | "Read error" | -f.result | ":" | strerror(-f.result); | 
|---|
| [a552a8c] | 89 |  | 
|---|
| [680137a] | 90 | block( globals.bar ); | 
|---|
|  | 91 | } | 
|---|
| [a552a8c] | 92 | } | 
|---|
|  | 93 |  | 
|---|
|  | 94 | // ----- Writer ----- | 
|---|
|  | 95 | // Writes to the pipe so the Reader can unblock | 
|---|
|  | 96 | // takes its sweet time so the Reader has to block | 
|---|
|  | 97 | thread Writer {}; | 
|---|
|  | 98 | void main(Writer & this) { | 
|---|
| [680137a] | 99 | for(TIMES) { | 
|---|
|  | 100 | block( globals.bar ); | 
|---|
| [a552a8c] | 101 |  | 
|---|
| [680137a] | 102 | sleep( 1`us ); | 
|---|
| [a552a8c] | 103 |  | 
|---|
| [680137a] | 104 | char buf[1] = { '+' }; | 
|---|
|  | 105 | int ret = write( globals.pipe[1], buf, 1 ); | 
|---|
|  | 106 | if(ret < 0) | 
|---|
|  | 107 | abort | "Write error" | errno | ":" | strerror(errno); | 
|---|
| [a552a8c] | 108 |  | 
|---|
| [680137a] | 109 | block( globals.bar ); | 
|---|
|  | 110 | } | 
|---|
| [a552a8c] | 111 | } | 
|---|
|  | 112 |  | 
|---|
|  | 113 | // ----- Yielder ----- | 
|---|
|  | 114 | // Add some chaos into the mix | 
|---|
|  | 115 | thread Yielder {}; | 
|---|
|  | 116 | void ^?{}(Yielder &mutex ) {} | 
|---|
|  | 117 | void main(Yielder&) { | 
|---|
|  | 118 | while(TIMES > __atomic_load_n(&counter, __ATOMIC_SEQ_CST)) { | 
|---|
|  | 119 | yield(); | 
|---|
|  | 120 | } | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
|  | 123 | int main() { | 
|---|
| [680137a] | 124 | barrier bar = { 2 }; | 
|---|
|  | 125 | &globals.bar = &bar; | 
|---|
|  | 126 | int ret = pipe(globals.pipe); | 
|---|
|  | 127 | if(ret != 0) | 
|---|
|  | 128 | abort | "Pipe error" | errno | ":" | strerror(errno); | 
|---|
| [a552a8c] | 129 |  | 
|---|
|  | 130 | processor p; | 
|---|
|  | 131 | sout | "starting"; | 
|---|
|  | 132 | { | 
|---|
|  | 133 | Yielder y; | 
|---|
|  | 134 | Spinner s; | 
|---|
|  | 135 | Reader ior; | 
|---|
| [680137a] | 136 | Writer iow; | 
|---|
| [a552a8c] | 137 | } | 
|---|
|  | 138 | sout | "done"; | 
|---|
| [d2ad151] | 139 | } | 
|---|