use NULL instead of a sentinal node in skiplist and list
[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 DECLARE_THREAD_LOCAL(rand_seed_, unsigned);
14
15 typedef struct thread_info {
16     int thread_id;
17     void *(*start_routine)(void *);
18     void *restrict arg;
19 } thread_info_t;
20
21 void nbd_init (void) {
22     sranddev();
23     INIT_THREAD_LOCAL(rand_seed_);
24     INIT_THREAD_LOCAL(tid_);
25     SET_THREAD_LOCAL(tid_, 0);
26     mem_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     LOCALIZE_THREAD_LOCAL(tid_, int);
35     SET_THREAD_LOCAL(rand_seed_, tid_+1);
36     lwt_thread_init(ti->thread_id);
37     rcu_thread_init(ti->thread_id);
38     void *ret = ti->start_routine(ti->arg);
39     nbd_free(ti);
40     return ret;
41 }
42
43 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
44     thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
45     ti->thread_id = thread_id;
46     ti->start_routine = start_routine;
47     ti->arg = arg;
48     return pthread_create(thread, NULL, worker, ti);
49 }
50
51 int nbd_rand (void) {
52     LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned);
53     unsigned r = rand_r(&rand_seed_);
54     SET_THREAD_LOCAL(rand_seed_, r);
55     return r;
56 }