Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / benchmarks / nidhugg / linuxrwlock / linuxrwlocks.c.in
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <atomic>
4
5
6 #define RW_LOCK_BIAS            0x00100000
7 #define WRITE_LOCK_CMP          RW_LOCK_BIAS
8
9 /** Example implementation of linux rw lock along with 2 thread test
10  *  driver... */
11
12 typedef union {
13         std::atomic<int> lock;
14 } rwlock_t;
15
16 static inline int write_trylock(rwlock_t *rw)
17 {
18         int priorvalue = atomic_fetch_sub(&rw->lock, RW_LOCK_BIAS);
19         if (priorvalue == RW_LOCK_BIAS)
20                 return 1;
21
22         atomic_fetch_add(&rw->lock, RW_LOCK_BIAS);
23         return 0;
24 }
25
26 static inline void write_unlock(rwlock_t *rw)
27 {
28         atomic_fetch_add(&rw->lock, RW_LOCK_BIAS);
29 }
30
31 rwlock_t mylock;
32 int shareddata;
33
34 void * a(void *obj) {
35 problemsize if (write_trylock(&mylock)) {write_unlock(&mylock);}
36  return NULL;
37 }
38
39 int main(int argc, char **argv)
40 {
41         pthread_t t1, t2;
42         mylock.lock = RW_LOCK_BIAS;
43
44         pthread_create(&t1, 0, &a, NULL);
45         pthread_create(&t2, 0, &a, NULL);
46
47         pthread_join(t1, 0);
48         pthread_join(t2, 0);
49
50         return 0;
51 }