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