From d6b95ea6e51da394ca1f499053dfce8f63cfe562 Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Thu, 17 Mar 2016 23:47:19 +0100
Subject: [PATCH] nailed it. once we've got the sleep mutex, make sure there's
 nothing in 'our' queue before taking a nap.

---
 src/scheduler.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/scheduler.c b/src/scheduler.c
index f1fccc7f74..3a7cc27641 100644
--- a/src/scheduler.c
+++ b/src/scheduler.c
@@ -28,7 +28,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 
 /* MPI headers. */
 #ifdef WITH_MPI
@@ -1279,7 +1278,7 @@ struct task *scheduler_gettask(struct scheduler *s, int qid,
         if (res != NULL) break;
       }
     }
-
+    
 /* If we failed, take a short nap. */
 #ifdef WITH_MPI
     if (res == NULL && qid > 1) {
@@ -1287,7 +1286,10 @@ struct task *scheduler_gettask(struct scheduler *s, int qid,
     if (res == NULL) {
 #endif
       pthread_mutex_lock(&s->sleep_mutex);
-      if (s->waiting > 0) pthread_cond_wait(&s->sleep_cond, &s->sleep_mutex);
+      res = queue_gettask(&s->queues[qid], prev, 0);
+      if (res == NULL && s->waiting > 0) {
+        pthread_cond_wait(&s->sleep_cond, &s->sleep_mutex);
+      }
       pthread_mutex_unlock(&s->sleep_mutex);
     }
   }
-- 
GitLab