source: tests/concurrent/spinaphore.cfa @ 7039ab9

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

Removed debug comments

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include <fstream.hfa>
2#include <locks.hfa>
3#include <thread.hfa>
4
5enum { num_blockers = 17, num_unblockers = 13 };
6
7void thrash() {
8        unsigned t[100];
9        for(i; 100) {
10                t[i] = 0xDEADBEEF;
11        }
12}
13
14Semaphore0nary sem;
15
16const unsigned int num_blocks = 1625;
17
18thread Blocker {
19        size_t sum;
20};
21void main(Blocker & this);
22
23Blocker * from_thread($thread * t) {
24        Blocker & nullb = *(Blocker*)0p;
25        $thread & nullt = (thread&)nullb;
26        uintptr_t offset  = (uintptr_t)&nullt;
27        uintptr_t address = ((uintptr_t)t) - offset;
28        return (Blocker*)address;
29}
30
31void main(Blocker & this) {
32        $thread * me = active_thread();
33        Blocker * me1 = &this;
34        Blocker * me2 = from_thread(me);
35        if( me1 != me2 ) sout | "Bad casting!" | me1 | "vs" | me2;
36        this.sum = 0;
37        for(num_blocks) {
38                P(sem);
39                if(((thread&)this).seqable.next != 0p) sout | "Link not invalidated";
40        }
41}
42
43const unsigned int num_unblocks = 2125;
44
45thread Unblocker {
46        size_t sum;
47};
48
49void main(Unblocker & this) {
50        this.sum = 0;
51        unsigned me = (unsigned)&this;
52        for(num_unblocks) {
53                $thread * t = V(sem, false);
54                Blocker * b = from_thread(t);
55                b->sum += me;
56                this.sum += (unsigned)b;
57                unpark(t);
58                yield(random(10));
59        }
60}
61
62int main() {
63        size_t usum = 0;
64        size_t bsum = 0;
65
66        if((num_unblocks * num_unblockers) != (num_blocks * num_blockers)) sout | "Mismatched Operations: " | (num_unblocks * num_unblockers) | "vs" | (num_blocks * num_blockers);
67
68        sout | "Starting";
69        {
70                Blocker   blockers  [num_blockers  ];
71                Unblocker unblockers[num_unblockers];
72
73                for(i;num_blockers) {
74                        for(num_blocks)
75                                usum += (unsigned)&blockers[i];
76                }
77
78                for(i;num_unblockers) {
79                        for(num_unblocks)
80                                bsum += (unsigned)&unblockers[i];
81                }
82
83                for(i;num_blockers) {
84                        bsum -= join(blockers[i]).sum;
85                }
86
87                for(i;num_unblockers) {
88                        usum -= join(unblockers[i]).sum;
89                }
90        }
91        sout | "Done!";
92        if(bsum == 0 && usum == 0) sout | "Match!";
93        else sout | "No Match!" | usum | "/" | bsum;
94}
Note: See TracBrowser for help on using the repository browser.