Remove C/C++11 header files that we don't really use
[satcheck.git] / benchmarks / satcheck-precompiled / linuxrwlock / linuxrwlocks_unannotated.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdlib.h>
4
5 #include "libinterface.h"
6
7 #define RW_LOCK_BIAS            0x00100000
8 #define WRITE_LOCK_CMP          RW_LOCK_BIAS
9
10 /** Example implementation of linux rw lock along with 2 thread test
11  *  driver... */
12
13 typedef union {
14         int lock;
15 } rwlock_t;
16
17 static inline void read_lock(rwlock_t *rw)
18 {
19         int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
20
21         while (true) {
22                 if (priorvalue <= 0) {
23                 } else {
24                         break;
25                 }
26
27                 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
28
29                 while (true) {
30                         int status = load_32(&rw->lock);
31                         if (status > 0) {
32                         } else {
33                                 break;
34                         }
35                         MC2_yield();
36                 }
37
38                 
39                 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
40         }
41 }
42
43 static inline void write_lock(rwlock_t *rw)
44 {
45         int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
46         while(true) {
47                 if (priorvalue != RW_LOCK_BIAS) {
48                 } else {
49                         break;
50                 }
51
52                 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
53                 
54                 while (true) {
55                         int status = load_32(&rw->lock);
56                         if (status != RW_LOCK_BIAS) {
57                         } else {
58                                 break;
59                         }
60                         MC2_yield();
61                 }
62
63                 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
64         }
65 }
66
67 static inline bool write_trylock(rwlock_t *rw)
68 {
69         int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
70
71         int flag = priorvalue != RW_LOCK_BIAS;
72         if (!flag) {
73                 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
74         }
75
76         return flag;
77 }
78
79 static inline void read_unlock(rwlock_t *rw)
80 {
81         rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
82 }
83
84 static inline void write_unlock(rwlock_t *rw)
85 {
86         rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
87 }
88
89 rwlock_t * mylock;
90 int shareddata;
91
92 static void foo() {
93         int res = write_trylock(mylock);
94         if (res) {
95                 write_unlock(mylock);
96         } else {
97         }
98 }
99
100 static void a(void *obj)
101 {
102         int i;
103         for(i=0;i<PROBLEMSIZE;i++)
104                 foo();
105 }
106
107 int user_main(int argc, char **argv)
108 {
109         mylock = (rwlock_t*)malloc(sizeof(rwlock_t));
110
111         thrd_t t1, t2;
112         //, t3, t4;
113         store_32(&mylock->lock, RW_LOCK_BIAS);
114
115         thrd_create(&t1, (thrd_start_t)&a, NULL);
116         thrd_create(&t2, (thrd_start_t)&a, NULL);
117         //      thrd_create(&t3, (thrd_start_t)&a, NULL);
118         //      thrd_create(&t4, (thrd_start_t)&a, NULL);
119
120         thrd_join(t1);
121         thrd_join(t2);
122         //      thrd_join(t3);
123         //      thrd_join(t4);
124         free(mylock);
125
126         return 0;
127 }