source: tests/concurrency/futures/multi.cfa @ b59c21a

Last change on this file since b59c21a was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 18 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
4enum {NFUTURES = 10};
5
6thread Server {
7        int pending, done, iteration;
8        multi_future(int) * request;
9};
10
11void ?{}( 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
19void ^?{}( Server & mutex this ) {
20        assert(this.pending == 0);
21        this.request = 0p;
22}
23
24void init( Server & this , multi_future(int) * f ) {
25        this.request = f;
26}
27
28void call( Server & mutex this ) {
29        this.pending++;
30}
31
32void finish( Server & mutex this ) {
33        this.done++;
34}
35
36void 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
60Server * the_server;
61thread Worker {};
62void ?{}(Worker & this) {
63        ((thread&)this){"Worker Thread"};
64}
65
66multi_future(int) * shared_future;
67
68void thrash(void) {
69        volatile int locals[250];
70        for(i; 250) {
71                locals[i] = 0xdeadbeef;
72        }
73}
74
75void work(int num) {
76        call( *the_server );
77        int res = wait( *shared_future );
78        if( res != num ) abort();
79        finish( *the_server );
80}
81
82void main( Worker & ) {
83        for (i; 10) {
84                thrash();
85                work(i);
86                thrash();
87        }
88}
89
90int 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.