source: tests/concurrent/futures/typed.cfa @ 2fc9664b

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

Added simple future tests.

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