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