From ed94ec26a8dae63b6c7712aea88e80577f7dbde7 Mon Sep 17 00:00:00 2001 From: Pedro Gonnet <pedro.gonnet@durham.ac.uk> Date: Sat, 22 Jun 2013 12:18:55 +0000 Subject: [PATCH] try more than once to get a task before sleeping. Former-commit-id: 458a6f26e877376b6bb0f74c835c04954c691959 --- src/scheduler.c | 45 +++++++++++++++++++++------------------------ src/scheduler.h | 1 + 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/scheduler.c b/src/scheduler.c index 98b2ffe790..dfa68b973f 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -662,32 +662,29 @@ struct task *scheduler_gettask ( struct scheduler *s , int qid ) { /* Loop as long as there are tasks... */ while ( s->waiting > 0 ) { - /* Try to get a task from the suggested queue. */ - if ( ( res = queue_gettask( &s->queues[qid] , qid , 0 ) ) != NULL ) - return res; - - /* If unsucessful, try stealing from the largest queue. */ - if ( s->flags & scheduler_flag_steal ) { - int qids[ nr_queues ]; - for ( k = 0 ; k < nr_queues ; k++ ) - qids[k] = k; - /* int max_count = 0, max_ind = 0; - for ( k = 0 ; k < nr_queues ; k++ ) - if ( k != qid && s->queues[k].count > max_count ) { - max_ind = k; - max_count = s->queues[k].count; + /* Try more than once before sleeping. */ + for ( int tries = 0 ; tries < scheduler_flag_maxsteal ; tries++ ) { + + /* Try to get a task from the suggested queue. */ + if ( ( res = queue_gettask( &s->queues[qid] , qid , 0 ) ) != NULL ) + return res; + + /* If unsucessful, try stealing from the largest queue. */ + if ( s->flags & scheduler_flag_steal ){ + int qids[ nr_queues ]; + for ( k = 0 ; k < nr_queues ; k++ ) + qids[k] = k; + for ( k = 0 ; k < nr_queues ; k++ ) { + int j = k + ( rand() % (nr_queues - k) ); + int temp = qids[j]; + qids[j] = qids[k]; + if ( temp == qid ) + continue; + if ( s->queues[temp].count > 0 && ( res = queue_gettask( &s->queues[temp] , qid , 0 ) ) != NULL ) + return res; } - if ( max_count > 0 && ( res = queue_gettask( &s->queues[ max_ind ] , qid , 0 ) ) != NULL ) - return res; */ - for ( k = 0 ; k < nr_queues ; k++ ) { - if ( k == qid ) - continue; - int j = k + ( rand() % (nr_queues - k) ); - int temp = qids[j]; - qids[j] = qids[k]; - if ( s->queues[temp].count > 0 && ( res = queue_gettask( &s->queues[temp] , qid , 0 ) ) != NULL ) - return res; } + } /* If we failed, take a short nap. */ diff --git a/src/scheduler.h b/src/scheduler.h index f38929d72e..b4c7b871e0 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -27,6 +27,7 @@ /* Flags . */ #define scheduler_flag_none 0 #define scheduler_flag_steal 1 +#define scheduler_flag_maxsteal 2 /* Data of a scheduler. */ -- GitLab