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