source: tests/concurrency/unified_locking/mcs_spin.cfa @ 55b060d

Last change on this file since 55b060d was c26bea2a, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

first attempt at renaming directory tests/concurrent to tests/concurrency to harmonize with other concurrency directory names

  • 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        mcs_spin_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    mcs_spin_node node;
28        lock(mo.l, node);
29        {
30                uint32_t tsum = mo.sum;
31                uint32_t cnt = mo.cnt;
32                mo.id = me;
33                yield(random(5));
34                value = ((uint32_t)random()) ^ ((uint32_t)me);
35                if(mo.id != me) sout | "Intruder!";
36                mo.cnt = cnt + 1;
37                mo.sum = tsum + value;
38        }
39        unlock(mo.l, node);
40        return value;
41}
42
43thread LockCheck {
44        uint32_t sum;
45};
46
47void main(LockCheck & this) {
48        this.sum = 0;
49        for(num_times) {
50                trash();
51                this.sum += cs();
52                trash();
53                yield(random(10));
54        }
55}
56
57void test() {
58        uint32_t sum = -32;
59        mo.sum = -32;
60        mo.cnt = 0;
61        processor p[2];
62        sout | "Starting";
63        {
64                LockCheck checkers[13];
65                for(i;13) {
66                        sum += join(checkers[i]).sum;
67                }
68        }
69        sout | "Done!";
70        if(mo.cnt != (13 * num_times)) sout | "Invalid cs count!" | mo.cnt | "vs "| (13 * num_times) | "(13 *" | num_times | ')';
71        if(sum == mo.sum) sout | "Match!";
72        else sout | "No Match!" | sum | "vs" | mo.sum;
73}
74
75int main() {
76    test();
77        return 0;
78}
Note: See TracBrowser for help on using the repository browser.