7 #define RW_LOCK_BIAS 0x00100000
8 #define WRITE_LOCK_CMP RW_LOCK_BIAS
10 /** Example implementation of linux rw lock along with 2 thread test
17 static inline int write_trylock(rwlock_t *rw)
19 int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst);
20 if (priorvalue == RW_LOCK_BIAS)
23 atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst);
27 static inline void write_unlock(rwlock_t *rw)
29 atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst);
35 static void a(void *obj) {
37 for(i = 0; i < PROBLEMSIZE; i++) {
38 if (write_trylock(&mylock)) {
39 write_unlock(&mylock);
44 int user_main(int argc, char **argv)
47 atomic_init(&mylock.lock, RW_LOCK_BIAS);
49 thrd_create(&t1, (thrd_start_t)&a, NULL);
50 thrd_create(&t2, (thrd_start_t)&a, NULL);