Remove C/C++11 header files that we don't really use
[satcheck.git] / test / linuxlock.c
1 #include <stdio.h>
2 #include <threads.h>
3
4 #include "libinterface.h"
5
6
7 /** Example implementation of linux rw lock along with 2 thread test
8  *  driver... */
9
10 typedef union {
11         int lock;
12 } rwlock_t;
13
14 static inline bool write_trylock(rwlock_t *rw, MCID *retval)
15 {
16         MCID m_priorval=MC2_nextRMW(MCID_NODEP, MCID_NODEP, MCID_NODEP);
17         int priorvalue=rmw_32(CAS, &rw->lock, 0, 1);
18         *retval=m_priorval;
19         return priorvalue;
20 }
21
22 static inline void write_unlock(rwlock_t *rw)
23 {
24         MC2_nextOpStore(MCID_NODEP, MCID_NODEP);
25         store_32(&rw->lock, 0);
26 }
27
28 rwlock_t mylock;
29
30 static void foo() {
31         MCID val;
32         bool flag=write_trylock(&mylock,&val);
33         MCID br;
34         if (flag) {
35                 br=MC2_branchUsesID(val, 1, 2, true);
36                 MC2_merge(br);
37         } else {
38                 br=MC2_branchUsesID(val, 0, 2, true);
39                 write_unlock(&mylock);
40                 MC2_merge(br);
41         }
42 }
43
44 static void a(void *obj)
45 {
46         int i;
47         for(i=0;i<2;i++)
48                 foo();
49 }
50
51 int user_main(int argc, char **argv)
52 {
53         thrd_t t1, t2;//, t3, t4;
54         MC2_nextOpStore(MCID_NODEP, MCID_NODEP);
55         store_32(&mylock.lock, 0);
56
57         thrd_create(&t1, (thrd_start_t)&a, NULL);
58         thrd_create(&t2, (thrd_start_t)&a, NULL);
59         //thrd_create(&t3, (thrd_start_t)&a, NULL);
60         //thrd_create(&t4, (thrd_start_t)&a, NULL);
61
62         thrd_join(t1);
63         thrd_join(t2);
64         //thrd_join(t3);
65         //thrd_join(t4);
66
67         return 0;
68 }