source:
tests/unified_locking/mutex_test.hfa
@
8bee858
Last change on this file since 8bee858 was 1dbbef6, checked in by , 2 years ago | |
---|---|
|
|
File size: 1.5 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 | ||
[1dbbef6] | 24 | uint32_t cs(uint32_t & entries) { |
[b16e15e] | 25 | thread$ * me = active_thread(); |
[4ae968e] | 26 | uint32_t value; |
27 | lock(mo.l); | |
28 | { | |
[1dbbef6] | 29 | entries++; |
[4ae968e] | 30 | uint32_t tsum = mo.sum; |
[35d73c0] | 31 | uint32_t cnt = mo.cnt; |
[4ae968e] | 32 | mo.id = me; |
33 | yield(random(5)); | |
34 | value = ((uint32_t)random()) ^ ((uint32_t)me); | |
35 | if(mo.id != me) sout | "Intruder!"; | |
[35d73c0] | 36 | mo.cnt = cnt + 1; |
[4ae968e] | 37 | mo.sum = tsum + value; |
38 | } | |
39 | unlock(mo.l); | |
40 | return value; | |
41 | } | |
42 | ||
43 | thread LockCheck { | |
44 | uint32_t sum; | |
[1dbbef6] | 45 | uint32_t entries; |
[4ae968e] | 46 | }; |
47 | ||
48 | void main(LockCheck & this) { | |
49 | this.sum = 0; | |
[1dbbef6] | 50 | this.entries = 0; |
[4ae968e] | 51 | for(num_times) { |
52 | trash(); | |
[1dbbef6] | 53 | this.sum += cs( this.entries ); |
[4ae968e] | 54 | trash(); |
55 | yield(random(10)); | |
56 | } | |
57 | } | |
58 | ||
[21a99cc] | 59 | void test() { |
[4ae968e] | 60 | uint32_t sum = -32; |
61 | mo.sum = -32; | |
[35d73c0] | 62 | mo.cnt = 0; |
[1dbbef6] | 63 | uint32_t real_entries = 0; |
[4ae968e] | 64 | processor p[2]; |
65 | sout | "Starting"; | |
66 | { | |
67 | LockCheck checkers[13]; | |
68 | for(i;13) { | |
[1dbbef6] | 69 | LockCheck & curr = join(checkers[i]); |
70 | sum += curr.sum; | |
71 | real_entries += curr.entries; | |
[4ae968e] | 72 | } |
73 | } | |
74 | sout | "Done!"; | |
[1dbbef6] | 75 | if(real_entries != (13 * num_times)) sout | "Invalid real cs count!" | mo.cnt | "vs "| (13 * num_times) | "(13 *" | num_times | ')'; |
76 | if(mo.cnt != (13 * num_times)) sout | "Invalid concurrent cs count!" | mo.cnt | "vs "| (13 * num_times) | "(13 *" | num_times | ')'; | |
[4ae968e] | 77 | if(sum == mo.sum) sout | "Match!"; |
78 | else sout | "No Match!" | sum | "vs" | mo.sum; | |
79 | } |
Note: See TracBrowser
for help on using the repository browser.