source:
tests/concurrent/futures/basic.cfa@
c4072d8e
Last change on this file since c4072d8e was 2fc9664b, checked in by , 5 years ago | |
---|---|
|
|
File size: 1.4 KB |
Rev | Line | |
---|---|---|
[a77496cb] | 1 | #include <thread.hfa> |
[2fc9664b] | 2 | |
[a77496cb] | 3 | enum {NFUTURES = 10}; |
4 | ||
5 | thread Server { | |
6 | int cnt; | |
7 | future_t * requests[NFUTURES]; | |
8 | }; | |
9 | ||
10 | void ?{}( Server & this ) { | |
11 | this.cnt = 0; | |
12 | for(i; NFUTURES) { | |
13 | this.requests[i] = 0p; | |
14 | } | |
15 | } | |
16 | ||
17 | void ^?{}( Server & mutex this ) { | |
18 | assert(this.cnt == 0); | |
19 | for(i; NFUTURES) { | |
20 | this.requests[i] = 0p; | |
21 | } | |
22 | } | |
23 | ||
24 | void process( Server & this, int i ) { | |
25 | if( this.requests[i] == 0p ) return; | |
26 | future_t * f = this.requests[i]; | |
27 | this.requests[i] = 0p; | |
28 | this.cnt--; | |
29 | fulfil( *f ); | |
30 | } | |
31 | ||
32 | void call( Server & mutex this, future_t & f ) { | |
33 | for(i; NFUTURES) { | |
34 | if( this.requests[i] == 0p ) { | |
35 | this.requests[i] = &f; | |
36 | this.cnt++; | |
37 | return; | |
38 | } | |
39 | } | |
40 | abort("Monitor Error"); | |
41 | } | |
42 | ||
43 | void main( Server & this ) { | |
44 | unsigned i = 0; | |
45 | for() { | |
46 | waitfor( ^?{} : this ) { | |
47 | break; | |
48 | } | |
49 | or when( this.cnt < NFUTURES ) waitfor( call: this ) {} | |
50 | or else { | |
51 | process( this, i % NFUTURES ); | |
52 | i++; | |
53 | } | |
54 | } | |
55 | ||
56 | for(i; NFUTURES) { | |
57 | process( this, i ); | |
58 | } | |
59 | } | |
60 | ||
61 | Server * the_server; | |
62 | thread Worker {}; | |
63 | ||
64 | void thrash(void) { | |
65 | volatile int locals[250]; | |
66 | for(i; 250) { | |
67 | locals[i] = 0xdeadbeef; | |
68 | } | |
69 | } | |
70 | ||
71 | void work(void) { | |
72 | future_t mine; | |
73 | call( *the_server, mine ); | |
74 | wait( mine ); | |
75 | } | |
76 | ||
77 | void main( Worker & ) { | |
78 | for(150) { | |
79 | thrash(); | |
80 | work(); | |
81 | thrash(); | |
82 | } | |
83 | } | |
84 | ||
85 | int main() { | |
[2fc9664b] | 86 | printf( "start\n" ); // non-empty .expect file |
[a77496cb] | 87 | processor procs[2]; |
88 | { | |
89 | Server server; | |
90 | the_server = &server; | |
91 | { | |
92 | Worker workers[17]; | |
93 | } | |
94 | } | |
[66812dd] | 95 | printf( "done\n" ); // non-empty .expect file |
96 | ||
97 | } |
Note:
See TracBrowser
for help on using the repository browser.