5 #include "libinterface.h"
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 void read_lock(rwlock_t *rw)
19 int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
22 if (priorvalue <= 0) {
27 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
30 int status = load_32(&rw->lock);
38 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
43 static inline void write_lock(rwlock_t *rw)
45 int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
48 if (priorvalue != RW_LOCK_BIAS) {
53 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
56 int status = load_32(&rw->lock);
57 if (status != RW_LOCK_BIAS) {
64 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
68 static inline bool write_trylock(rwlock_t *rw)
70 int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
72 int flag = priorvalue != RW_LOCK_BIAS;
75 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
81 static inline void read_unlock(rwlock_t *rw)
83 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
86 static inline void write_unlock(rwlock_t *rw)
88 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
95 int flag=write_trylock(&mylock);
97 write_unlock(&mylock);
101 static void a(void *obj)
108 int user_main(int argc, char **argv)
112 store_32(&mylock.lock, RW_LOCK_BIAS);
114 thrd_create(&t1, (thrd_start_t)&a, NULL);
115 thrd_create(&t2, (thrd_start_t)&a, NULL);
116 // thrd_create(&t3, (thrd_start_t)&a, NULL);
117 // thrd_create(&t4, (thrd_start_t)&a, NULL);