6 #include "libinterface.h"
8 #define RW_LOCK_BIAS 0x00100000
9 #define WRITE_LOCK_CMP RW_LOCK_BIAS
11 /** Example implementation of linux rw lock along with 2 thread test
18 static inline void read_lock(rwlock_t *rw)
20 int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
23 if (priorvalue <= 0) {
28 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
31 int status = load_32(&rw->lock);
40 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-1));
44 static inline void write_lock(rwlock_t *rw)
46 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;
74 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
80 static inline void read_unlock(rwlock_t *rw)
82 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
85 static inline void write_unlock(rwlock_t *rw)
87 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
94 int res = write_trylock(mylock);
101 static void a(void *obj)
104 for(i=0;i<PROBLEMSIZE;i++)
108 int user_main(int argc, char **argv)
110 mylock = (rwlock_t*)malloc(sizeof(rwlock_t));
114 store_32(&mylock->lock, RW_LOCK_BIAS);
116 thrd_create(&t1, (thrd_start_t)&a, NULL);
117 thrd_create(&t2, (thrd_start_t)&a, NULL);
118 // thrd_create(&t3, (thrd_start_t)&a, NULL);
119 // thrd_create(&t4, (thrd_start_t)&a, NULL);