source: tests/unified_locking/mutex_test.hfa @ bc4a433

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since bc4a433 was b16e15e, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

fix mutex_test to not user $thread

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