| [8b95bab] | 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_basic.cfa -- Test that simple completions work correctly | 
|---|
|  | 8 | //                   even when mixing io_uring and regular calls | 
|---|
|  | 9 | // | 
|---|
|  | 10 | // Author           : Thierry Delisle | 
|---|
|  | 11 | // Created On       : Thu Sep 01 11:27:08 2022 | 
|---|
|  | 12 | // Last Modified By : | 
|---|
|  | 13 | // Last Modified On : | 
|---|
|  | 14 | // Update Count     : | 
|---|
|  | 15 | // | 
|---|
|  | 16 |  | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include <concurrency/barrier.hfa> | 
|---|
|  | 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 |  | 
|---|
|  | 28 | struct { | 
|---|
| [680137a] | 29 | barrier & bar; | 
|---|
|  | 30 | int pipe[2]; | 
|---|
| [8b95bab] | 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 | // ----- Reader ----- | 
|---|
|  | 44 | // Reader from the pipe to test completion doesn't starve | 
|---|
|  | 45 | thread Reader {}; | 
|---|
|  | 46 | void main(Reader & this) { | 
|---|
| [69698d2] | 47 | char thrash[1]; | 
|---|
| [680137a] | 48 | bool do_read = has_user_level_blocking( (fptr_t)async_read ); | 
|---|
| [8b95bab] | 49 |  | 
|---|
| [680137a] | 50 | for(TIMES) { | 
|---|
|  | 51 | io_future_t f; | 
|---|
|  | 52 | if ( do_read ) { | 
|---|
|  | 53 | async_read(f, globals.pipe[0], thrash, 1, 0); | 
|---|
|  | 54 | } else { | 
|---|
|  | 55 | fulfil(f, 0); // If we don't have user-level blocking just play along | 
|---|
|  | 56 | } | 
|---|
| [8b95bab] | 57 |  | 
|---|
| [680137a] | 58 | block( globals.bar ); | 
|---|
| [8b95bab] | 59 |  | 
|---|
| [7bc84b8] | 60 | yield( prng( this, 15 ) ); | 
|---|
| [8b95bab] | 61 |  | 
|---|
| [680137a] | 62 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); | 
|---|
| [8b95bab] | 63 | if(0 == (i % 100)) sout | i; | 
|---|
|  | 64 |  | 
|---|
| [680137a] | 65 | wait( f ); | 
|---|
| [8b95bab] | 66 |  | 
|---|
| [680137a] | 67 | if(f.result < 0) | 
|---|
|  | 68 | abort | "Read error" | -f.result | ":" | strerror(-f.result); | 
|---|
| [8b95bab] | 69 |  | 
|---|
| [680137a] | 70 | block( globals.bar ); | 
|---|
|  | 71 | } | 
|---|
| [8b95bab] | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | // ----- Writer ----- | 
|---|
|  | 75 | // Writes to the pipe so the Reader can unblock | 
|---|
|  | 76 | // takes its sweet time so the Reader has to block | 
|---|
|  | 77 | thread Writer {}; | 
|---|
|  | 78 | void main(Writer & this) { | 
|---|
| [680137a] | 79 | for(TIMES) { | 
|---|
|  | 80 | block( globals.bar ); | 
|---|
| [8b95bab] | 81 |  | 
|---|
| [680137a] | 82 | sleep( 1`us ); | 
|---|
| [8b95bab] | 83 |  | 
|---|
| [680137a] | 84 | char buf[1] = { '+' }; | 
|---|
|  | 85 | int ret = write( globals.pipe[1], buf, 1 ); | 
|---|
|  | 86 | if(ret < 0) | 
|---|
|  | 87 | abort | "Write error" | errno | ":" | strerror(errno); | 
|---|
| [8b95bab] | 88 |  | 
|---|
| [680137a] | 89 | block( globals.bar ); | 
|---|
|  | 90 | } | 
|---|
| [8b95bab] | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 | int main() { | 
|---|
| [680137a] | 94 | barrier bar = { 2 }; | 
|---|
|  | 95 | &globals.bar = &bar; | 
|---|
|  | 96 | int ret = pipe(globals.pipe); | 
|---|
|  | 97 | if(ret != 0) | 
|---|
|  | 98 | abort | "Pipe error" | errno | ":" | strerror(errno); | 
|---|
| [8b95bab] | 99 |  | 
|---|
|  | 100 | sout | "starting"; | 
|---|
|  | 101 | { | 
|---|
|  | 102 | Reader ior; | 
|---|
| [680137a] | 103 | Writer iow; | 
|---|
| [8b95bab] | 104 | } | 
|---|
|  | 105 | sout | "done"; | 
|---|
| [d2ad151] | 106 | } | 
|---|