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);
39 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)));
47 if (priorvalue != RW_LOCK_BIAS) {
52 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
55 int status = load_32(&rw->lock);
56 if (status != RW_LOCK_BIAS) {
63 priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
67 static inline bool write_trylock(rwlock_t *rw)
69 int priorvalue=rmw_32(ADD, &rw->lock, /* dummy */ 0, ((unsigned int)-(RW_LOCK_BIAS)));
71 int flag = priorvalue != RW_LOCK_BIAS;
73 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
79 static inline void read_unlock(rwlock_t *rw)
81 rmw_32(ADD, &rw->lock, /* dummy */ 0, 1);
84 static inline void write_unlock(rwlock_t *rw)
86 rmw_32(ADD, &rw->lock, /* dummy */ 0, RW_LOCK_BIAS);
93 int res = write_trylock(mylock);
100 static void a(void *obj)
103 for(i=0;i<PROBLEMSIZE;i++)
107 int user_main(int argc, char **argv)
109 mylock = (rwlock_t*)malloc(sizeof(rwlock_t));
113 store_32(&mylock->lock, RW_LOCK_BIAS);
115 thrd_create(&t1, (thrd_start_t)&a, NULL);
116 thrd_create(&t2, (thrd_start_t)&a, NULL);
117 // thrd_create(&t3, (thrd_start_t)&a, NULL);
118 // thrd_create(&t4, (thrd_start_t)&a, NULL);