[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 | |
---|
[aca0d2f] | 28 | static struct { |
---|
[680137a] | 29 | barrier & bar; |
---|
| 30 | int pipe[2]; |
---|
[8b95bab] | 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 | // ----- Reader ----- |
---|
| 43 | // Reader from the pipe to test completion doesn't starve |
---|
| 44 | thread Reader {}; |
---|
| 45 | void main(Reader & this) { |
---|
[69698d2] | 46 | char thrash[1]; |
---|
[680137a] | 47 | bool do_read = has_user_level_blocking( (fptr_t)async_read ); |
---|
[8b95bab] | 48 | |
---|
[680137a] | 49 | for(TIMES) { |
---|
| 50 | io_future_t f; |
---|
| 51 | if ( do_read ) { |
---|
| 52 | async_read(f, globals.pipe[0], thrash, 1, 0); |
---|
| 53 | } else { |
---|
| 54 | fulfil(f, 0); // If we don't have user-level blocking just play along |
---|
| 55 | } |
---|
[8b95bab] | 56 | |
---|
[680137a] | 57 | block( globals.bar ); |
---|
[8b95bab] | 58 | |
---|
[7bc84b8] | 59 | yield( prng( this, 15 ) ); |
---|
[8b95bab] | 60 | |
---|
[680137a] | 61 | unsigned i = __atomic_add_fetch( &counter, 1, __ATOMIC_SEQ_CST ); |
---|
[8b95bab] | 62 | if(0 == (i % 100)) sout | i; |
---|
| 63 | |
---|
[680137a] | 64 | wait( f ); |
---|
[8b95bab] | 65 | |
---|
[680137a] | 66 | if(f.result < 0) |
---|
| 67 | abort | "Read error" | -f.result | ":" | strerror(-f.result); |
---|
[8b95bab] | 68 | |
---|
[680137a] | 69 | block( globals.bar ); |
---|
| 70 | } |
---|
[8b95bab] | 71 | } |
---|
| 72 | |
---|
| 73 | // ----- Writer ----- |
---|
| 74 | // Writes to the pipe so the Reader can unblock |
---|
| 75 | // takes its sweet time so the Reader has to block |
---|
| 76 | thread Writer {}; |
---|
| 77 | void main(Writer & this) { |
---|
[680137a] | 78 | for(TIMES) { |
---|
| 79 | block( globals.bar ); |
---|
[8b95bab] | 80 | |
---|
[680137a] | 81 | sleep( 1`us ); |
---|
[8b95bab] | 82 | |
---|
[680137a] | 83 | char buf[1] = { '+' }; |
---|
| 84 | int ret = write( globals.pipe[1], buf, 1 ); |
---|
| 85 | if(ret < 0) |
---|
| 86 | abort | "Write error" | errno | ":" | strerror(errno); |
---|
[8b95bab] | 87 | |
---|
[680137a] | 88 | block( globals.bar ); |
---|
| 89 | } |
---|
[8b95bab] | 90 | } |
---|
| 91 | |
---|
| 92 | int main() { |
---|
[680137a] | 93 | barrier bar = { 2 }; |
---|
| 94 | &globals.bar = &bar; |
---|
| 95 | int ret = pipe(globals.pipe); |
---|
| 96 | if(ret != 0) |
---|
| 97 | abort | "Pipe error" | errno | ":" | strerror(errno); |
---|
[8b95bab] | 98 | |
---|
| 99 | sout | "starting"; |
---|
| 100 | { |
---|
| 101 | Reader ior; |
---|
[680137a] | 102 | Writer iow; |
---|
[8b95bab] | 103 | } |
---|
| 104 | sout | "done"; |
---|
[d2ad151] | 105 | } |
---|