X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=runtime%2Fruntime.c;h=415a1617816f18133728538b7059bf1b89ee9980;hb=973c2b13fe82d6ebd030cd58869fa237b1fe5370;hp=ed93e8f8764f57e4e95176f94850a3ec85e267c8;hpb=efd90a1b8a9d3bbb1bdd8e6ae41b3462e7193fb2;p=nbds diff --git a/runtime/runtime.c b/runtime/runtime.c index ed93e8f..415a161 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -2,23 +2,71 @@ * 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 +#include #include "common.h" -#include "rcu.h" -#include "lwt.h" +#include "runtime.h" +#include "rlocal.h" #include "mem.h" -#include "nbd.h" #include "tls.h" DECLARE_THREAD_LOCAL(tid_, int); +DECLARE_THREAD_LOCAL(rand_seed_, unsigned); -void nbd_init (void) { - INIT_THREAD_LOCAL(tid_, NULL); +typedef struct thread_info { + int thread_id; + void *(*start_routine)(void *); + void *restrict arg; +} thread_info_t; + +__attribute__ ((constructor)) void nbd_init (void) { + //sranddev(); + INIT_THREAD_LOCAL(rand_seed_); + INIT_THREAD_LOCAL(tid_); + SET_THREAD_LOCAL(tid_, 0); mem_init(); - lwt_init(); + lwt_thread_init(0); + rcu_thread_init(0); +} + +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); + nbd_free(ti); + return ret; +} + +int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) { + thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t)); + ti->thread_id = thread_id; + ti->start_routine = start_routine; + ti->arg = arg; + return pthread_create(thread, NULL, worker, ti); +} + +int nbd_rand (void) { + LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned); + unsigned r = rand_r(&rand_seed_); + SET_THREAD_LOCAL(rand_seed_, r); + return r; +} + +uint64_t nbd_rand_seed (int i) { + return rdtsc() + -715159705 + i * 129; } -void nbd_thread_init (int id) { - SET_THREAD_LOCAL(tid_, id); - lwt_thread_init(id); - rcu_thread_init(id); +// Fairly fast random numbers +int nbd_next_rand (uint64_t *r) { + *r = (*r * 0x5DEECE66DLL + 0xBLL) & MASK(48); + return (*r >> 17) & 0x7FFFFFFF; }