source: tests/concurrency/futures/multi.cfa @ 89da3a9

Last change on this file since 89da3a9 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
RevLine 
[44e37ef]1#include <thread.hfa>
2#include <future.hfa>
3
4enum {NFUTURES = 10};
5
6thread Server {
[e2cc3c7]7        int pending, done, iteration;
[44e37ef]8        multi_future(int) * request;
9};
10
11void ?{}( Server & this ) {
[76c94bf]12        ((thread&)this){"Server Thread"};
[e2cc3c7]13        this.pending = 0;
14        this.done = 0;
[44e37ef]15        this.iteration = 0;
16        this.request = 0p;
17}
18
19void ^?{}( Server & mutex this ) {
[e2cc3c7]20        assert(this.pending == 0);
21        this.request = 0p;
[44e37ef]22}
23
24void init( Server & this , multi_future(int) * f ) {
25        this.request = f;
26}
27
28void call( Server & mutex this ) {
[e2cc3c7]29        this.pending++;
[44e37ef]30}
31
[e2cc3c7]32void finish( Server & mutex this ) {
33        this.done++;
34}
[44e37ef]35
36void main( Server & this ) {
[e2cc3c7]37        MAIN_LOOP:
[44e37ef]38        for() {
39                waitfor( ^?{} : this ) {
40                        break;
41                }
[e2cc3c7]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 );
[44e37ef]51                        }
[e2cc3c7]52
53                        reset( *this.request );
54                        this.done = 0;
[44e37ef]55                }
56        }
57
58}
59
60Server * the_server;
61thread Worker {};
[76c94bf]62void ?{}(Worker & this) {
63        ((thread&)this){"Worker Thread"};
64}
65
[44e37ef]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.