]> pd.if.org Git - nbds/blobdiff - test/rcu_test.c
work in progress
[nbds] / test / rcu_test.c
index d4696481a8b1369759c93fb6f5307b9cf00cfe37..b2258554ad037684494509a3768df61581f73fb3 100644 (file)
@@ -1,7 +1,7 @@
-#define _POSIX_C_SOURCE 1 // for rand_r
 #include <stdio.h>
 #include <errno.h>
 #include <pthread.h>
+#include <unistd.h>
 #include <sys/time.h>
 #include "common.h"
 #include "runtime.h"
@@ -30,18 +30,18 @@ static lifo_t *lifo_alloc (void) {
 static void lifo_aba_push (lifo_t *stk, node_t *x) {
     node_t *head;
     do {
-        head = ((volatile lifo_t *)stk)->head;
-        ((volatile node_t *)x)->next = head;
-    } while (__sync_val_compare_and_swap(&stk->head, head, x) != head);
+        head = VOLATILE_DEREF(stk).head;
+        VOLATILE_DEREF(x).next = head;
+    } while (SYNC_CAS(&stk->head, head, x) != head);
 }
 
 node_t *lifo_aba_pop (lifo_t *stk) {
     node_t *head;
     do {
-        head = ((volatile lifo_t *)stk)->head;
+        head = VOLATILE_DEREF(stk).head;
         if (head == NULL)
             return NULL;
-    } while (__sync_val_compare_and_swap(&stk->head, head, head->next) != head);
+    } while (SYNC_CAS(&stk->head, head, head->next) != head);
     head->next = NULL;
     return head;
 }
@@ -53,16 +53,15 @@ node_t *node_alloc (void) {
 }
 
 void *worker (void *arg) {
-    int id = (int)(size_t)arg;
-    unsigned int rand_seed = (unsigned int)id + 1;
+    nbd_thread_init();
 
     // Wait for all the worker threads to be ready.
-    __sync_fetch_and_add(&wait_, -1);
+    (void)__sync_fetch_and_add(&wait_, -1);
     do {} while (wait_); 
 
     int i;
     for (i = 0; i < NUM_ITERATIONS; ++ i) {
-        int n = rand_r(&rand_seed);
+        int n = nbd_rand();
         if (n & 0x1) {
             lifo_aba_push(stk_, node_alloc());
         } else {
@@ -78,9 +77,10 @@ void *worker (void *arg) {
 }
 
 int main (int argc, char **argv) {
-    //lwt_set_trace_level("m0r0");
+    nbd_thread_init();
+    lwt_set_trace_level("m3r3");
 
-    int num_threads = 2;
+    int num_threads = sysconf(_SC_NPROCESSORS_CONF);
     if (argc == 2)
     {
         errno = 0;
@@ -104,7 +104,7 @@ int main (int argc, char **argv) {
 
     pthread_t thread[num_threads];
     for (int i = 0; i < num_threads; ++i) {
-        int rc = nbd_thread_create(thread + i, i, worker, (void *)(size_t)i);
+        int rc = pthread_create(thread + i, NULL, worker, (void *)(size_t)i);
         if (rc != 0) { perror("pthread_create"); return rc; }
     }
     for (int i = 0; i < num_threads; ++i) {