]> pd.if.org Git - nbds/blobdiff - runtime/runtime.c
Mac OS fix
[nbds] / runtime / runtime.c
index d4946cfc0b47dd8fb8e673915786a41e44eb4958..415a1617816f18133728538b7059bf1b89ee9980 100644 (file)
@@ -2,6 +2,8 @@
  * Written by Josh Dybnis and released to the public domain, as explained at
  * http://creativecommons.org/licenses/publicdomain
  */
+#define _POSIX_C_SOURCE 1 // for rand_r()
+#include <stdlib.h>
 #include <pthread.h>
 #include "common.h"
 #include "runtime.h"
@@ -18,8 +20,8 @@ typedef struct thread_info {
     void *restrict arg;
 } thread_info_t;
 
-void nbd_init (void) {
-    sranddev();
+__attribute__ ((constructor)) void nbd_init (void) {
+    //sranddev();
     INIT_THREAD_LOCAL(rand_seed_);
     INIT_THREAD_LOCAL(tid_);
     SET_THREAD_LOCAL(tid_, 0);
@@ -32,7 +34,11 @@ static void *worker (void *arg) {
     thread_info_t *ti = (thread_info_t *)arg;
     SET_THREAD_LOCAL(tid_, ti->thread_id);
     LOCALIZE_THREAD_LOCAL(tid_, int);
+#ifndef NDEBUG
     SET_THREAD_LOCAL(rand_seed_, tid_+1);
+#else
+    SET_THREAD_LOCAL(rand_seed_, nbd_rand_seed(tid_+1));
+#endif
     lwt_thread_init(ti->thread_id);
     rcu_thread_init(ti->thread_id);
     void *ret = ti->start_routine(ti->arg);
@@ -54,3 +60,13 @@ int nbd_rand (void) {
     SET_THREAD_LOCAL(rand_seed_, r);
     return r;
 }
+
+uint64_t nbd_rand_seed (int i) {
+    return rdtsc() + -715159705 + i * 129;
+}
+
+// Fairly fast random numbers
+int nbd_next_rand (uint64_t *r) {
+    *r = (*r * 0x5DEECE66DLL + 0xBLL) & MASK(48);
+    return (*r >> 17) & 0x7FFFFFFF;
+}