1 | #include <thread.hfa> |
---|
2 | #include <future.hfa> |
---|
3 | |
---|
4 | enum {NFUTURES = 10}; |
---|
5 | |
---|
6 | thread Server { |
---|
7 | int pending, done, iteration; |
---|
8 | 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 , 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 | 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 = get( *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 | thread Worker2 {}; |
---|
91 | |
---|
92 | semaphore before{0}; |
---|
93 | semaphore after_server{0}; |
---|
94 | semaphore after_worker{0}; |
---|
95 | |
---|
96 | void work2( int num ) { |
---|
97 | P( before ); |
---|
98 | int res = get( *shared_future ); |
---|
99 | if( res != num ) abort(); |
---|
100 | V( after_server ); |
---|
101 | P( after_worker ); |
---|
102 | } |
---|
103 | |
---|
104 | void main( Worker2 & ) { |
---|
105 | for (i; 10) { |
---|
106 | thrash(); |
---|
107 | work2(i); |
---|
108 | thrash(); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | thread Server2 {}; |
---|
113 | |
---|
114 | void main( Server2 & ) { |
---|
115 | for (i; 10) { |
---|
116 | fulfil( *shared_future , i ); |
---|
117 | V( before, NFUTURES ); |
---|
118 | for ( i; NFUTURES ) P( after_server ); |
---|
119 | reset( *shared_future ); |
---|
120 | V( after_worker, NFUTURES ); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | thread Worker3 {}; |
---|
125 | |
---|
126 | void work3( int num ) { |
---|
127 | [int, bool] tt; |
---|
128 | do { |
---|
129 | tt = try_get( *shared_future ); |
---|
130 | } while ( ! tt.1 ); |
---|
131 | if( tt.0 != num ) abort(); |
---|
132 | V( after_server ); |
---|
133 | P( after_worker ); |
---|
134 | } |
---|
135 | |
---|
136 | void main( Worker3 & ) { |
---|
137 | for (i; 10) { |
---|
138 | thrash(); |
---|
139 | work3(i); |
---|
140 | thrash(); |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | thread Server3 {}; |
---|
145 | |
---|
146 | void main( Server3 & ) { |
---|
147 | for (i; 10) { |
---|
148 | fulfil( *shared_future , i ); |
---|
149 | for ( i; NFUTURES ) P( after_server ); |
---|
150 | reset( *shared_future ); |
---|
151 | V( after_worker, NFUTURES ); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | int main() { |
---|
156 | printf( "start 1: blocking path future test\n" ); |
---|
157 | processor procs[2]; |
---|
158 | shared_future = new(); |
---|
159 | { |
---|
160 | Server server; |
---|
161 | the_server = &server; |
---|
162 | init(server, shared_future); |
---|
163 | { |
---|
164 | Worker workers[NFUTURES]; |
---|
165 | } |
---|
166 | } |
---|
167 | delete( shared_future ); |
---|
168 | printf( "done 1\n" ); |
---|
169 | |
---|
170 | printf( "start 2: nonblocking path future test\n" ); |
---|
171 | shared_future = new(); |
---|
172 | |
---|
173 | { |
---|
174 | Server2 server; |
---|
175 | { |
---|
176 | Worker2 workers[NFUTURES]; |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | delete( shared_future ); |
---|
181 | printf( "done 2\n" ); |
---|
182 | |
---|
183 | printf( "start 3: try_get future test\n" ); |
---|
184 | shared_future = new(); |
---|
185 | |
---|
186 | { |
---|
187 | Worker3 workers[NFUTURES]; |
---|
188 | { |
---|
189 | Server3 server; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | delete( shared_future ); |
---|
194 | printf( "done 3\n" ); |
---|
195 | |
---|
196 | // C_TODO: add test for select statement once it is implemented |
---|
197 | } |
---|