source: tests/unified_locking/fast.cfa @ 474d610

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

Added two more tests for locks

  • Property mode set to 100644
File size: 947 bytes
Line 
1#include <fstream.hfa>
2#include <locks.hfa>
3#include <thread.hfa>
4
5const unsigned int num_times = 50;
6
7struct MutexObj {
8        fast_lock l;
9        $thread * id;
10        size_t sum;
11};
12
13MutexObj mo;
14
15void trash() {
16        unsigned t[100];
17        for(i; 100) {
18                t[i] = 0xDEADBEEF;
19        }
20}
21
22unsigned cs() {
23        $thread * me = active_thread();
24        unsigned value = (unsigned)me;
25        lock(mo.l);
26        {
27                size_t tsum = mo.sum;
28                mo.id = me;
29                yield(random(5));
30                if(mo.id != me) sout | "Intruder!";
31                mo.sum = tsum + value;
32        }
33        unlock(mo.l);
34        return value;
35}
36
37thread LockCheck {
38        size_t sum;
39};
40
41void main(LockCheck & this) {
42        this.sum = 0;
43        for(num_times) {
44                trash();
45                this.sum += cs();
46                trash();
47                yield(random(10));
48        }
49}
50
51int main() {
52        size_t sum = -32;
53        mo.sum = -32;
54        processor p[2];
55        sout | "Starting";
56        {
57                LockCheck checkers[13];
58                for(i;13) {
59                        sum += join(checkers[i]).sum;
60                }
61        }
62        sout | "Done!";
63        if(sum == mo.sum) sout | "Match!";
64        else sout | "No Match!" | sum | "vs" | mo.sum;
65}
Note: See TracBrowser for help on using the repository browser.