X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=runtime%2Fruntime.c;h=b145faa872e5826ed70a979d724a561cdc24738a;hp=0731bfdc70226d81ba2c87a95d9405a7dcfd500c;hb=0f6e9afb06b03647c4c5f2499ddab12f42b4340e;hpb=9ec5405d406696c6cbdb7a47ade7fccc736a8b53 diff --git a/runtime/runtime.c b/runtime/runtime.c index 0731bfd..b145faa 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -2,17 +2,17 @@ * 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 "runtime.h" -#include "runtime_local.h" +#include "rlocal.h" #include "mem.h" #include "tls.h" -#undef malloc -#undef free - DECLARE_THREAD_LOCAL(tid_, int); +DECLARE_THREAD_LOCAL(rand_seed_, unsigned); typedef struct thread_info { int thread_id; @@ -20,10 +20,11 @@ typedef struct thread_info { void *restrict arg; } thread_info_t; -void nbd_init (void) { - INIT_THREAD_LOCAL(tid_, NULL); - mem_init(); - lwt_init(); +__attribute__ ((constructor(102))) void nbd_init (void) { + //sranddev(); + INIT_THREAD_LOCAL(rand_seed_); + INIT_THREAD_LOCAL(tid_); + SET_THREAD_LOCAL(tid_, 0); lwt_thread_init(0); rcu_thread_init(0); } @@ -31,17 +32,26 @@ void nbd_init (void) { 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); + SET_THREAD_LOCAL(rand_seed_, tid_+1); lwt_thread_init(ti->thread_id); rcu_thread_init(ti->thread_id); void *ret = ti->start_routine(ti->arg); - free(ti); + 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 *)malloc(sizeof(thread_info_t)); + 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; +}