source: tests/concurrent/futures/multi.cfa@ 84cd72d

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 84cd72d was 76c94bf, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added a little bit of debug information to the multi future test.

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