Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / benchmarks / satcheck / linuxlock / linuxlocks_unannotated.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <threads.h>
4 #include <stdbool.h>
5 #include "libinterface.h"
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)
15 {
16         int priorvalue=rmw_32(CAS, &rw->lock, 0, 1);
17         return priorvalue;
18 }
19
20 static inline void write_unlock(rwlock_t *rw)
21 {
22         store_32(&rw->lock, 0);
23 }
24
25 rwlock_t * mylock;
26
27 static void foo() {
28         int flag=write_trylock(mylock);
29         if (flag) {
30         } else {
31                 write_unlock(mylock);
32         }
33 }
34
35 static void a(void *obj)
36 {
37         int i;
38         for(i=0;i<PROBLEMSIZE;i++)
39                 foo();
40 }
41
42 int user_main(int argc, char **argv)
43 {
44         thrd_t t1, t2;//, t3, t4;
45         mylock = (rwlock_t*)malloc(sizeof(rwlock_t));
46         store_32(&mylock->lock, 0);
47
48         thrd_create(&t1, (thrd_start_t)&a, NULL);
49         thrd_create(&t2, (thrd_start_t)&a, NULL);
50         //thrd_create(&t3, (thrd_start_t)&a, NULL);
51         //thrd_create(&t4, (thrd_start_t)&a, NULL);
52
53         thrd_join(t1);
54         thrd_join(t2);
55         //thrd_join(t3);
56         //thrd_join(t4);
57
58
59
60         return 0;
61 }