Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / clang / test / linuxrwlocks_unannotated.c
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.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                 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
39                 MC2_loopIterate();
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
48                 if (priorvalue != RW_LOCK_BIAS) {
49                 } else {
50                         break;
51                 }
52
53                 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
54                 
55                 while (true) {
56                         int status = load_32(&rw->lock);
57                         if (status != RW_LOCK_BIAS) {
58                         } else {
59                                 break;
60                         }
61                         MC2_yield();
62                 }
63
64                 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
65         }
66 }
67
68 static inline bool write_trylock(rwlock_t *rw)
69 {
70         int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
71
72         int flag = priorvalue != RW_LOCK_BIAS;
73         if (flag) {
74         } else {
75                 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
76         }
77
78         return flag;
79 }
80
81 static inline void read_unlock(rwlock_t *rw)
82 {
83         rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
84 }
85
86 static inline void write_unlock(rwlock_t *rw)
87 {
88         rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
89 }
90
91 rwlock_t mylock;
92 int shareddata;
93
94 static void foo() {
95         int flag=write_trylock(&mylock);
96         if (flag) {
97                 write_unlock(&mylock);
98         }
99 }
100
101 static void a(void *obj)
102 {
103         int i;
104         for(i=0;i<13;i++)
105                 foo();
106 }
107
108 int user_main(int argc, char **argv)
109 {
110         thrd_t t1, t2;
111         //, t3, t4;
112         store_32(&mylock.lock, RW_LOCK_BIAS);
113
114         thrd_create(&t1, (thrd_start_t)&a, NULL);
115         thrd_create(&t2, (thrd_start_t)&a, NULL);
116         //      thrd_create(&t3, (thrd_start_t)&a, NULL);
117         //      thrd_create(&t4, (thrd_start_t)&a, NULL);
118
119         thrd_join(t1);
120         thrd_join(t2);
121         //      thrd_join(t3);
122         //      thrd_join(t4);
123
124         return 0;
125 }