2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
12 DECLARE_THREAD_LOCAL(tid_, int);
13 DECLARE_THREAD_LOCAL(rand_seed_, unsigned);
15 typedef struct thread_info {
17 void *(*start_routine)(void *);
21 __attribute__ ((constructor)) void nbd_init (void) {
23 INIT_THREAD_LOCAL(rand_seed_);
24 INIT_THREAD_LOCAL(tid_);
25 SET_THREAD_LOCAL(tid_, 0);
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);
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;
48 return pthread_create(thread, NULL, worker, ti);
52 LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned);
53 unsigned r = rand_r(&rand_seed_);
54 SET_THREAD_LOCAL(rand_seed_, r);