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);
38 SET_THREAD_LOCAL(rand_seed_, tid_+1);
40 SET_THREAD_LOCAL(rand_seed_, nbd_rand_seed(tid_+1));
42 lwt_thread_init(ti->thread_id);
43 rcu_thread_init(ti->thread_id);
44 void *ret = ti->start_routine(ti->arg);
49 int nbd_thread_create (pthread_t *restrict thread, int thread_id, void *(*start_routine)(void *), void *restrict arg) {
50 thread_info_t *ti = (thread_info_t *)nbd_malloc(sizeof(thread_info_t));
51 ti->thread_id = thread_id;
52 ti->start_routine = start_routine;
54 return pthread_create(thread, NULL, worker, ti);
58 LOCALIZE_THREAD_LOCAL(rand_seed_, unsigned);
59 unsigned r = rand_r(&rand_seed_);
60 SET_THREAD_LOCAL(rand_seed_, r);
64 uint64_t nbd_rand_seed (int i) {
65 return rdtsc() + -715159705 + i * 129;
68 // Fairly fast random numbers
69 int nbd_next_rand (uint64_t *r) {
70 *r = (*r * 0x5DEECE66DLL + 0xBLL) & MASK(48);
71 return (*r >> 17) & 0x7FFFFFFF;