source: tests/unified_locking/mutex_test.hfa

Last change on this file was 35d73c0, checked in by Thierry Delisle <tdelisle@…>, 2 weeks ago

Mutex test now counts number of critical sections in attempt to help debug fast lock

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