]> pd.if.org Git - nbds/blob - runtime/runtime.c
missing file from last commit
[nbds] / runtime / runtime.c
1 /* 
2  * Written by Josh Dybnis and released to the public domain, as explained at
3  * http://creativecommons.org/licenses/publicdomain
4  */
5 #include <pthread.h>
6 #include "common.h"
7 #include "runtime.h"
8 #include "runtime_local.h"
9 #include "mem.h"
10 #include "tls.h"
11
12 #undef malloc
13 #undef free
14
15 DECLARE_THREAD_LOCAL(tid_, int);
16
17 typedef struct thread_info {
18     int thread_id;
19     void *(*start_routine)(void *);
20     void *restrict arg;
21 } thread_info_t;
22
23 void nbd_init (void) {
24     INIT_THREAD_LOCAL(tid_, NULL);
25     mem_init();
26     lwt_init();
27     lwt_thread_init(0);
28     rcu_thread_init(0);
29 }
30
31 static void *worker (void *arg) {
32     thread_info_t *ti = (thread_info_t *)arg;
33     SET_THREAD_LOCAL(tid_, ti->thread_id);
34     lwt_thread_init(ti->thread_id);
35     rcu_thread_init(ti->thread_id);
36     void *ret = ti->start_routine(ti->arg);
37     free(ti);
38     return ret;
39 }
40
41 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
42     thread_info_t *ti = (thread_info_t *)malloc(sizeof(thread_info_t));
43     ti->thread_id = thread_id;
44     ti->start_routine = start_routine;
45     ti->arg = arg;
46     return pthread_create(thread, NULL, worker, ti);
47 }