linuxrwlocks: add linux locks test
authorBrian Norris <banorris@uci.edu>
Sat, 3 Nov 2012 01:41:30 +0000 (18:41 -0700)
committerBrian Norris <banorris@uci.edu>
Sat, 3 Nov 2012 01:41:30 +0000 (18:41 -0700)
Makefile
linuxrwlocks/.gitignore [new file with mode: 0644]
linuxrwlocks/Makefile [new file with mode: 0644]
linuxrwlocks/linuxrwlocks.c [new file with mode: 0644]

index e5ff6cc..9e102ec 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-DIRS := barrier mcs-lock mpmc-queue spsc-queue
+DIRS := barrier mcs-lock mpmc-queue spsc-queue linuxrwlocks
 
 .PHONY: $(DIRS)
 
diff --git a/linuxrwlocks/.gitignore b/linuxrwlocks/.gitignore
new file mode 100644 (file)
index 0000000..2fdb632
--- /dev/null
@@ -0,0 +1 @@
+/linuxrwlocks
diff --git a/linuxrwlocks/Makefile b/linuxrwlocks/Makefile
new file mode 100644 (file)
index 0000000..d66ada3
--- /dev/null
@@ -0,0 +1,11 @@
+include ../benchmarks.mk
+
+TESTNAME = linuxrwlocks
+
+all: $(TESTNAME)
+
+$(TESTNAME): $(TESTNAME).c
+       $(CC) -o $@ $< $(CPPFLAGS) $(LDFLAGS)
+
+clean:
+       rm -f $(TESTNAME) *.o
diff --git a/linuxrwlocks/linuxrwlocks.c b/linuxrwlocks/linuxrwlocks.c
new file mode 100644 (file)
index 0000000..7e317aa
--- /dev/null
@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include <threads.h>
+#include <stdatomic.h>
+
+#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 read_can_lock(rwlock_t *lock)
+{
+       return atomic_load_explicit(&lock->lock, memory_order_relaxed) > 0;
+}
+
+static inline int write_can_lock(rwlock_t *lock)
+{
+       return atomic_load_explicit(&lock->lock, memory_order_relaxed) == RW_LOCK_BIAS;
+}
+
+static inline void read_lock(rwlock_t *rw)
+{
+       int priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
+       while (priorvalue <= 0) {
+               atomic_fetch_add_explicit(&rw->lock, 1, memory_order_relaxed);
+               do {
+                       priorvalue = atomic_load_explicit(&rw->lock, memory_order_relaxed);
+               } while (priorvalue <= 0);
+               priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
+       }
+}
+
+static inline void write_lock(rwlock_t *rw)
+{
+       int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
+       while (priorvalue != RW_LOCK_BIAS) {
+               atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_relaxed);
+               do {
+                       priorvalue = atomic_load_explicit(&rw->lock, memory_order_relaxed);
+               } while (priorvalue != RW_LOCK_BIAS);
+               priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
+       }
+}
+
+static inline int read_trylock(rwlock_t *rw)
+{
+       int priorvalue = atomic_fetch_sub_explicit(&rw->lock, 1, memory_order_acquire);
+       if (priorvalue > 0)
+               return 1;
+
+       atomic_fetch_add_explicit(&rw->lock, 1, memory_order_relaxed);
+       return 0;
+}
+
+static inline int write_trylock(rwlock_t *rw)
+{
+       int priorvalue = atomic_fetch_sub_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_acquire);
+       if (priorvalue == RW_LOCK_BIAS)
+               return 1;
+
+       atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_relaxed);
+       return 0;
+}
+
+static inline void read_unlock(rwlock_t *rw)
+{
+       atomic_fetch_add_explicit(&rw->lock, 1, memory_order_release);
+}
+
+static inline void write_unlock(rwlock_t *rw)
+{
+       atomic_fetch_add_explicit(&rw->lock, RW_LOCK_BIAS, memory_order_release);
+}
+
+rwlock_t mylock;
+int shareddata;
+
+static void a(void *obj)
+{
+       int i;
+       for(i = 0; i < 2; i++) {
+               if ((i % 2) == 0) {
+                       read_lock(&mylock);
+                       load_32(&shareddata);
+                       read_unlock(&mylock);
+               } else {
+                       write_lock(&mylock);
+                       store_32(&shareddata,(unsigned int)i);
+                       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;
+}