Commit cf58ff8b authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Merge branch 'rand_fix' into 'master'

Replace calls to rand() by calls to rand_r() in the scheduler

Implements #302 using @d74ksy's changes. 

See merge request !405
parents 06771ad2 4c6ca41f
...@@ -1749,7 +1749,8 @@ void *runner_main(void *data) { ...@@ -1749,7 +1749,8 @@ void *runner_main(void *data) {
struct runner *r = (struct runner *)data; struct runner *r = (struct runner *)data;
struct engine *e = r->e; struct engine *e = r->e;
struct scheduler *sched = &e->sched; struct scheduler *sched = &e->sched;
unsigned int seed = r->id;
pthread_setspecific(sched->local_seed_pointer, &seed);
/* Main loop. */ /* Main loop. */
while (1) { while (1) {
......
...@@ -1377,7 +1377,9 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) { ...@@ -1377,7 +1377,9 @@ void scheduler_enqueue(struct scheduler *s, struct task *t) {
if (qid >= s->nr_queues) error("Bad computed qid."); if (qid >= s->nr_queues) error("Bad computed qid.");
/* If no previous owner, pick a random queue. */ /* If no previous owner, pick a random queue. */
if (qid < 0) qid = rand() % s->nr_queues; if (qid < 0)
qid = rand_r((unsigned int *)pthread_getspecific(s->local_seed_pointer)) %
s->nr_queues;
/* Increase the waiting counter. */ /* Increase the waiting counter. */
atomic_inc(&s->waiting); atomic_inc(&s->waiting);
...@@ -1609,6 +1611,7 @@ void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks, ...@@ -1609,6 +1611,7 @@ void scheduler_init(struct scheduler *s, struct space *space, int nr_tasks,
s->size = 0; s->size = 0;
s->tasks = NULL; s->tasks = NULL;
s->tasks_ind = NULL; s->tasks_ind = NULL;
pthread_key_create(&s->local_seed_pointer, NULL);
scheduler_reset(s, nr_tasks); scheduler_reset(s, nr_tasks);
} }
......
...@@ -102,6 +102,9 @@ struct scheduler { ...@@ -102,6 +102,9 @@ struct scheduler {
/* The node we are working on. */ /* The node we are working on. */
int nodeID; int nodeID;
/* 'Pointer' to the seed for the random number generator */
pthread_key_t local_seed_pointer;
}; };
/* Inlined functions (for speed). */ /* Inlined functions (for speed). */
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment