#include #include #include #include "librace.h" #define RW_LOCK_BIAS 0x00100000 #define WRITE_LOCK_CMP RW_LOCK_BIAS /** Example implementation of linux rw lock along with 2 thread test * driver... */ typedef union { atomic_int lock; } rwlock_t; static inline int write_trylock(rwlock_t *rw) { int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst); if (priorvalue == RW_LOCK_BIAS) return 1; atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst); return 0; } static inline void write_unlock(rwlock_t *rw) { atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_seq_cst); } rwlock_t mylock; int shareddata; static void a(void *obj) { int i; for(i = 0; i < PROBLEMSIZE; i++) { if (write_trylock(&mylock)) { write_unlock(&mylock); } } } int user_main(int argc, char **argv) { thrd_t t1, t2; atomic_init(&mylock.lock, RW_LOCK_BIAS); thrd_create(&t1, (thrd_start_t)&a, NULL); thrd_create(&t2, (thrd_start_t)&a, NULL); thrd_join(t1); thrd_join(t2); return 0; }