source: tests/concurrent/futures/multi.cfa @ 474d610

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 474d610 was 44e37ef, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added simple test for multi-future

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