]> pd.if.org Git - nbds/blob - runtime/runtime.c
lock-free skiplist
[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 DECLARE_THREAD_LOCAL(tid_, int);
13
14 typedef struct thread_info {
15     int thread_id;
16     void *(*start_routine)(void *);
17     void *restrict arg;
18 } thread_info_t;
19
20 void nbd_init (void) {
21     INIT_THREAD_LOCAL(tid_);
22     SET_THREAD_LOCAL(tid_, 0);
23     mem_init();
24     lwt_thread_init(0);
25     rcu_thread_init(0);
26 }
27
28 static void *worker (void *arg) {
29     thread_info_t *ti = (thread_info_t *)arg;
30     SET_THREAD_LOCAL(tid_, ti->thread_id);
31     lwt_thread_init(ti->thread_id);
32     rcu_thread_init(ti->thread_id);
33     void *ret = ti->start_routine(ti->arg);
34     nbd_free(ti);
35     return ret;
36 }
37
38 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
39     thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
40     ti->thread_id = thread_id;
41     ti->start_routine = start_routine;
42     ti->arg = arg;
43     return pthread_create(thread, NULL, worker, ti);
44 }