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