source: tests/unified_locking/mutex_test.hfa@ 6ff08d8

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 6ff08d8 was 4ae968e, checked in by caparsons <caparson@…>, 4 years ago

refactored mutex testing and added lin backoff lock

  • Property mode set to 100644
File size: 985 bytes
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 LOCK l;
10 $thread * id;
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() {
24 $thread * me = active_thread();
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.