X-Git-Url: https://pd.if.org/git/?p=nbds;a=blobdiff_plain;f=runtime%2Fruntime.c;h=0731bfdc70226d81ba2c87a95d9405a7dcfd500c;hp=ed93e8f8764f57e4e95176f94850a3ec85e267c8;hb=9ec5405d406696c6cbdb7a47ade7fccc736a8b53;hpb=efd90a1b8a9d3bbb1bdd8e6ae41b3462e7193fb2 diff --git a/runtime/runtime.c b/runtime/runtime.c index ed93e8f..0731bfd 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -2,23 +2,46 @@ * Written by Josh Dybnis and released to the public domain, as explained at * http://creativecommons.org/licenses/publicdomain */ +#include #include "common.h" -#include "rcu.h" -#include "lwt.h" +#include "runtime.h" +#include "runtime_local.h" #include "mem.h" -#include "nbd.h" #include "tls.h" +#undef malloc +#undef free + DECLARE_THREAD_LOCAL(tid_, int); +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); 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 (int id) { - SET_THREAD_LOCAL(tid_, id); - lwt_thread_init(id); - rcu_thread_init(id); +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); }