Last change
on this file since 3397eed was
c26bea2a,
checked in by Peter A. Buhr <pabuhr@…>, 18 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.5 KB
|
Rev | Line | |
---|
[357ab79] | 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; |
---|
| 10 | thread$ * id; |
---|
| 11 | uint32_t sum; |
---|
| 12 | uint32_t cnt; |
---|
| 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(uint32_t & entries) { |
---|
| 25 | thread$ * me = active_thread(); |
---|
| 26 | uint32_t value; |
---|
| 27 | lock(mo.l); |
---|
| 28 | { |
---|
| 29 | entries++; |
---|
| 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); |
---|
| 40 | return value; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | thread LockCheck { |
---|
| 44 | uint32_t sum; |
---|
| 45 | uint32_t entries; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | void main(LockCheck & this) { |
---|
| 49 | this.sum = 0; |
---|
| 50 | this.entries = 0; |
---|
| 51 | for(num_times) { |
---|
| 52 | trash(); |
---|
| 53 | this.sum += cs( this.entries ); |
---|
| 54 | trash(); |
---|
| 55 | yield(random(10)); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void test() { |
---|
| 60 | uint32_t sum = -32; |
---|
| 61 | mo.sum = -32; |
---|
| 62 | mo.cnt = 0; |
---|
| 63 | uint32_t real_entries = 0; |
---|
| 64 | processor p[2]; |
---|
| 65 | sout | "Starting"; |
---|
| 66 | { |
---|
| 67 | LockCheck checkers[13]; |
---|
| 68 | for(i;13) { |
---|
| 69 | LockCheck & curr = join(checkers[i]); |
---|
| 70 | sum += curr.sum; |
---|
| 71 | real_entries += curr.entries; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | sout | "Done!"; |
---|
| 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 | ')'; |
---|
| 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.