X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=runtime%2Fruntime.c;h=19024e2f389f237ecbd6f0e2edf906b0b198a1d2;hp=0731bfdc70226d81ba2c87a95d9405a7dcfd500c;hb=HEAD;hpb=9ec5405d406696c6cbdb7a47ade7fccc736a8b53 diff --git a/runtime/runtime.c b/runtime/runtime.c index 0731bfd..19024e2 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -1,47 +1,35 @@ -/* +/* * Written by Josh Dybnis and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ +#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(ThreadId, int); +static int ThreadIndex -DECLARE_THREAD_LOCAL(tid_, int); +static int MaxThreadId = 0; -typedef struct thread_info { - int thread_id; - void *(*start_routine)(void *); - void *restrict arg; -} thread_info_t; - -void nbd_init (void) { - INIT_THREAD_LOCAL(tid_, NULL); +__attribute__ ((constructor)) void nbd_init (void) { + rnd_init(); 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); - lwt_thread_init(ti->thread_id); - rcu_thread_init(ti->thread_id); - void *ret = ti->start_routine(ti->arg); - free(ti); - return ret; -} +void nbd_thread_init (void) { + LOCALIZE_THREAD_LOCAL(ThreadId, int); + + if (ThreadId == 0) { + ++MaxThreadId; // TODO: reuse thread id's of threads that have been destroyed + ASSERT(MaxThreadId <= MAX_NUM_THREADS); + SET_THREAD_LOCAL(ThreadId, MaxThreadId); + rnd_thread_init(); + } -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)); - ti->thread_id = thread_id; - ti->start_routine = start_routine; - ti->arg = arg; - return pthread_create(thread, NULL, worker, ti); + lwt_thread_init(); + rcu_thread_init(); }