2 * Written by Josh Dybnis and released to the public domain, as explained at
3 * http://creativecommons.org/licenses/publicdomain
5 #define _POSIX_C_SOURCE 1 // for rand_r()
14 DECLARE_THREAD_LOCAL(tid_, int);
15 DECLARE_THREAD_LOCAL(rand_seed_, unsigned);
17 typedef struct thread_info {
19 void *(*start_routine)(void *);
23 __attribute__ ((constructor(102))) void nbd_init (void) {
25 INIT_THREAD_LOCAL(rand_seed_);
26 INIT_THREAD_LOCAL(tid_);
27 SET_THREAD_LOCAL(tid_, 0);
32 static void *worker (void *arg) {
33 thread_info_t *ti = (thread_info_t *)arg;
34 SET_THREAD_LOCAL(tid_, ti->thread_id);
35 LOCALIZE_THREAD_LOCAL(tid_, int);
36 SET_THREAD_LOCAL(rand_seed_, tid_+1);
37 lwt_thread_init(ti->thread_id);
38 rcu_thread_init(ti->thread_id);
39 void *ret = ti->start_routine(ti->arg);
44 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
45 thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
46 ti->thread_id = thread_id;
47 ti->start_routine = start_routine;
49 return pthread_create(thread, NULL, worker, ti);
53 LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned);
54 unsigned r = rand_r(&rand_seed_);
55 SET_THREAD_LOCAL(rand_seed_, r);