source: tests/concurrent/semaphore.cfa

Last change on this file was 237df76, checked in by Peter A. Buhr <pabuhr@…>, 2 months ago

convert from test programs I/O acquire to mutex statement

  • Property mode set to 100644
File size: 1.2 KB
Line 
1#include <fstream.hfa>
2#include <locks.hfa>
3#include <thread.hfa>
4#include <mutex_stmt.hfa>
5
6enum { num_blockers = 17, num_unblockers = 13 };
7
8void thrash() {
9        unsigned t[100];
10        for(i; 100) {
11                t[i] = 0xDEADBEEF;
12        }
13}
14
15ThreadBenaphore ben;
16
17// const unsigned int num_blocks = 25000;
18const unsigned int num_blocks = 5;
19
20thread Blocker {
21        size_t sum;
22};
23
24void main(Blocker & this) {
25        thread$ * me = active_thread();
26        this.sum = 0;
27        for(num_blocks) {
28                this.sum += (unsigned)me;
29                thrash();
30                P(ben);
31                if(((thread&)this).seqable.next != 0p) mutex(sout) sout | "Link not invalidated";
32                thrash();
33        }
34}
35
36thread Unblocker {
37        size_t sum;
38};
39
40void main(Unblocker & this) {
41        this.sum = 0;
42        LOOP: for() {
43                waitfor( ^?{} : this) {
44                        break LOOP;
45                }
46                or else {}
47
48                thread$ * t = V(ben, false);
49                if(t) {
50                        this.sum += (unsigned)t;
51                        unpark(t);
52                }
53                yield(random(10));
54        }
55}
56
57int main() {
58        size_t usum = 0;
59        size_t bsum = 0;
60
61        sout | "Starting";
62        {
63                Blocker   blockers  [num_blockers  ];
64                Unblocker unblockers[num_unblockers];
65
66                for(i;num_blockers) {
67                        bsum += join(blockers[i]).sum;
68                }
69
70                sout | "Done!";
71
72                for(i;num_unblockers) {
73                        usum += join(unblockers[i]).sum;
74                }
75        }
76        if(bsum == usum) sout | "Match!";
77        else sout | "No Match!" | usum | "!=" | bsum;
78}
Note: See TracBrowser for help on using the repository browser.