source: tests/concurrent/futures/basic.cfa @ a77496cb

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

First draft at core objects of futures and basic tests

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