source:
tests/unified_locking/mutex_test.hfa
@
48a91e2
Last change on this file since 48a91e2 was 21a99cc, checked in by , 3 years ago | |
---|---|
|
|
File size: 1.2 KB |
Rev | Line | |
---|---|---|
[4ae968e] | 1 | |
2 | #include <fstream.hfa> | |
3 | #include <locks.hfa> | |
4 | #include <thread.hfa> | |
5 | ||
6 | const unsigned int num_times = 50; | |
7 | ||
8 | struct MutexObj { | |
9 | LOCK l; | |
[b16e15e] | 10 | thread$ * id; |
[4ae968e] | 11 | uint32_t sum; |
[35d73c0] | 12 | uint32_t cnt; |
[4ae968e] | 13 | }; |
14 | ||
15 | MutexObj mo; | |
16 | ||
17 | void trash() { | |
18 | unsigned t[100]; | |
19 | for(i; 100) { | |
20 | t[i] = 0xDEADBEEF; | |
21 | } | |
22 | } | |
23 | ||
24 | uint32_t cs() { | |
[b16e15e] | 25 | thread$ * me = active_thread(); |
[4ae968e] | 26 | uint32_t value; |
27 | lock(mo.l); | |
28 | { | |
29 | uint32_t tsum = mo.sum; | |
[35d73c0] | 30 | uint32_t cnt = mo.cnt; |
[4ae968e] | 31 | mo.id = me; |
32 | yield(random(5)); | |
33 | value = ((uint32_t)random()) ^ ((uint32_t)me); | |
34 | if(mo.id != me) sout | "Intruder!"; | |
[35d73c0] | 35 | mo.cnt = cnt + 1; |
[4ae968e] | 36 | mo.sum = tsum + value; |
37 | } | |
38 | unlock(mo.l); | |
39 | return value; | |
40 | } | |
41 | ||
42 | thread LockCheck { | |
43 | uint32_t sum; | |
44 | }; | |
45 | ||
46 | void 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 | ||
[21a99cc] | 56 | void test() { |
[4ae968e] | 57 | uint32_t sum = -32; |
58 | mo.sum = -32; | |
[35d73c0] | 59 | mo.cnt = 0; |
[4ae968e] | 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!"; | |
[35d73c0] | 69 | if(mo.cnt != (13 * num_times)) sout | "Invalid cs count!" | mo.cnt | "vs "| (13 * num_times) | "(13 *" | num_times | ')'; |
[4ae968e] | 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.