[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 { |
---|
| 29 | barrier & bar; |
---|
| 30 | int pipe[2]; |
---|
| 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) { |
---|
| 47 | bool do_read = has_user_level_blocking( (fptr_t)async_read ); |
---|
| 48 | |
---|
| 49 | for(TIMES) { |
---|
| 50 | io_future_t f; |
---|
| 51 | if ( do_read ) { |
---|
| 52 | char thrash[1]; |
---|
| 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 | } |
---|
| 57 | |
---|
| 58 | block( globals.bar ); |
---|
| 59 | |
---|
| 60 | yield( prng( this, 15 ) ); |
---|
| 61 | |
---|
| 62 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); |
---|
| 63 | if(0 == (i % 100)) sout | i; |
---|
| 64 | |
---|
| 65 | wait( f ); |
---|
| 66 | |
---|
| 67 | if(f.result < 0) |
---|
| 68 | abort | "Read error" | -f.result | ":" | strerror(-f.result); |
---|
| 69 | |
---|
| 70 | block( globals.bar ); |
---|
| 71 | } |
---|
| 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) { |
---|
| 79 | for(TIMES) { |
---|
| 80 | block( globals.bar ); |
---|
| 81 | |
---|
| 82 | sleep( 1`us ); |
---|
| 83 | |
---|
| 84 | char buf[1] = { '+' }; |
---|
| 85 | int ret = write( globals.pipe[1], buf, 1 ); |
---|
| 86 | if(ret < 0) |
---|
| 87 | abort | "Write error" | errno | ":" | strerror(errno); |
---|
| 88 | |
---|
| 89 | block( globals.bar ); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int main() { |
---|
| 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); |
---|
| 99 | |
---|
| 100 | sout | "starting"; |
---|
| 101 | { |
---|
| 102 | Reader ior; |
---|
| 103 | Writer iow; |
---|
| 104 | } |
---|
| 105 | sout | "done"; |
---|
| 106 | } |
---|