Last change
on this file since cbbfba9 was
c26bea2a,
checked in by Peter A. Buhr <pabuhr@…>, 19 months ago
|
first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names
|
-
Property mode set to
100644
|
File size:
1.6 KB
|
Line | |
---|
1 | #include <thread.hfa> |
---|
2 | #include <future.hfa> |
---|
3 | |
---|
4 | enum {NFUTURES = 10}; |
---|
5 | |
---|
6 | thread Server { |
---|
7 | int pending, done, iteration; |
---|
8 | multi_future(int) * request; |
---|
9 | }; |
---|
10 | |
---|
11 | void ?{}( Server & this ) { |
---|
12 | ((thread&)this){"Server Thread"}; |
---|
13 | this.pending = 0; |
---|
14 | this.done = 0; |
---|
15 | this.iteration = 0; |
---|
16 | this.request = 0p; |
---|
17 | } |
---|
18 | |
---|
19 | void ^?{}( Server & mutex this ) { |
---|
20 | assert(this.pending == 0); |
---|
21 | this.request = 0p; |
---|
22 | } |
---|
23 | |
---|
24 | void init( Server & this , multi_future(int) * f ) { |
---|
25 | this.request = f; |
---|
26 | } |
---|
27 | |
---|
28 | void call( Server & mutex this ) { |
---|
29 | this.pending++; |
---|
30 | } |
---|
31 | |
---|
32 | void finish( Server & mutex this ) { |
---|
33 | this.done++; |
---|
34 | } |
---|
35 | |
---|
36 | void main( Server & this ) { |
---|
37 | MAIN_LOOP: |
---|
38 | for() { |
---|
39 | waitfor( ^?{} : this ) { |
---|
40 | break; |
---|
41 | } |
---|
42 | or waitfor( call: this ) { |
---|
43 | if (this.pending != NFUTURES) { continue MAIN_LOOP; } |
---|
44 | |
---|
45 | this.pending = 0; |
---|
46 | fulfil( *this.request, this.iteration ); |
---|
47 | this.iteration++; |
---|
48 | |
---|
49 | for(NFUTURES) { |
---|
50 | waitfor( finish: this ); |
---|
51 | } |
---|
52 | |
---|
53 | reset( *this.request ); |
---|
54 | this.done = 0; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | Server * the_server; |
---|
61 | thread Worker {}; |
---|
62 | void ?{}(Worker & this) { |
---|
63 | ((thread&)this){"Worker Thread"}; |
---|
64 | } |
---|
65 | |
---|
66 | multi_future(int) * shared_future; |
---|
67 | |
---|
68 | void thrash(void) { |
---|
69 | volatile int locals[250]; |
---|
70 | for(i; 250) { |
---|
71 | locals[i] = 0xdeadbeef; |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | void work(int num) { |
---|
76 | call( *the_server ); |
---|
77 | int res = wait( *shared_future ); |
---|
78 | if( res != num ) abort(); |
---|
79 | finish( *the_server ); |
---|
80 | } |
---|
81 | |
---|
82 | void main( Worker & ) { |
---|
83 | for (i; 10) { |
---|
84 | thrash(); |
---|
85 | work(i); |
---|
86 | thrash(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | int main() { |
---|
91 | printf( "start\n" ); // non-empty .expect file |
---|
92 | processor procs[2]; |
---|
93 | shared_future = new(); |
---|
94 | { |
---|
95 | Server server; |
---|
96 | the_server = &server; |
---|
97 | init(server, shared_future); |
---|
98 | { |
---|
99 | Worker workers[NFUTURES]; |
---|
100 | } |
---|
101 | } |
---|
102 | delete( shared_future ); |
---|
103 | printf( "done\n" ); // non-empty .expect file |
---|
104 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.