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)) void nbd_init (void) {
25 INIT_THREAD_LOCAL(rand_seed_);
26 INIT_THREAD_LOCAL(tid_);
27 SET_THREAD_LOCAL(tid_, 0);
33 static void *worker (void *arg) {
34 thread_info_t *ti = (thread_info_t *)arg;
35 SET_THREAD_LOCAL(tid_, ti->thread_id);
36 LOCALIZE_THREAD_LOCAL(tid_, int);
37 SET_THREAD_LOCAL(rand_seed_, tid_+1);
38 lwt_thread_init(ti->thread_id);
39 rcu_thread_init(ti->thread_id);
40 void *ret = ti->start_routine(ti->arg);
45 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
46 thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
47 ti->thread_id = thread_id;
48 ti->start_routine = start_routine;
50 return pthread_create(thread, NULL, worker, ti);
54 LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned);
55 unsigned r = rand_r(&rand_seed_);
56 SET_THREAD_LOCAL(rand_seed_, r);