Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / benchmarks / cdschecker / linuxlock / linuxlocks.c.in
1 #include <stdio.h>
2 #include <threads.h>
3 #include <stdatomic.h>
4
5
6 /** Example implementation of linux rw lock along with 2 thread test
7  *  driver... */
8
9 typedef union {
10         atomic_int lock;
11 } lock_t;
12
13 static inline bool write_trylock(lock_t *rw) {
14         int oldvalue=0;
15         return atomic_compare_exchange_strong(&rw->lock, &oldvalue, 1);
16 }
17
18
19 static inline void write_unlock(lock_t *rw)
20 {
21         atomic_store(&rw->lock, 0);
22 }
23
24 lock_t mylock;
25 int shareddata;
26
27 static void foo() {
28         bool flag=write_trylock(&mylock);
29         if (flag) {
30                 write_unlock(&mylock);
31         }
32 }
33
34 static void a(void *obj)
35 {
36         int i;
37         for(i=0;i<PROBLEMSIZE;i++)
38                 foo();
39 }
40
41 int user_main(int argc, char **argv)
42 {
43         thrd_t t1, t2;
44         atomic_init(&mylock.lock, 0);
45         
46
47         thrd_create(&t1, (thrd_start_t)&a, NULL);
48         thrd_create(&t2, (thrd_start_t)&a, NULL);
49
50         thrd_join(t1);
51         thrd_join(t2);
52
53         return 0;
54 }