Index: trunk/Ohana/src/opihi/pantasks/CheckTasks.c
===================================================================
--- trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 12332)
+++ trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 12404)
@@ -1,36 +1,49 @@
 # include "pantasks.h"
 
-int CheckTasks () {
+float CheckTasks () {
 
   Job *job;
   Task *task;
   int status;
+  float time_running, next_timeout;
   // struct timeval now;
+
+  // actual maximum delay is controlled in job_threads.c
+  next_timeout = 1.0;
 
   /** test all tasks: ready to test? ready to run? **/
   while ((task = NextTask ()) != NULL) {
 
-    if (!task[0].active) continue;
+    /*** test for all reasons we should skip this task ***/
 
-    /* ready to test? : check exec period */
-    if (GetTaskTimer(task[0].last, FALSE) < task[0].exec_period) continue;
-
-    /* need to check if the current time is within valid/invalid periods */
+    /* task has been de-activated by the user */
+    if (!task[0].active) {
+      continue;
+    }
+    /* current time is not within valid/invalid periods */
     if (!CheckTimeRanges (task[0].ranges, task[0].Nranges)) {
-	gettimeofday (&task[0].last, (void *) NULL);
-	continue;
+      continue;
     }
+    /* all allowed tasks have been run in this period */
     if (task[0].Nmax && (task[0].Njobs >= task[0].Nmax)) {
-	gettimeofday (&task[0].last, (void *) NULL);
-	continue;
+      continue;
     }
+    /* too many outstanding jobs */
     if (task[0].NpendingMax && (task[0].Npending >= task[0].NpendingMax)) {
-	// fprintf (stderr, "npending: %d, max npending: %d\n", task[0].Npending, task[0].NpendingMax);
-	gettimeofday (&task[0].last, (void *) NULL);
-	continue;
+      continue;
     }
 
-    // gettimeofday (&now, (void *) NULL);
-    // fprintf (stderr, "t0: %d %6d  - \n", now.tv_sec, now.tv_usec);
+    /* ready to test? : check time since last exec */
+    time_running = GetTaskTimer(task[0].last, FALSE);
+    if (time_running < task[0].exec_period) {
+      // is we are to ready to run, set time to timeout, if shortest of all tasks
+      next_timeout = MIN (next_timeout, task[0].exec_period - time_running);
+      continue;
+    }
+
+    /* ready to try running the task : reset the timer */
+    next_timeout = 0.0;
+    gettimeofday (&task[0].last, (void *) NULL);
+    // XXX here is where I should add a fuzz factor (fraction of exec_period) 
 
     /* ready to run? : run task.exec macro */
@@ -38,6 +51,5 @@
       status = exec_loop (task[0].exec);
       if (!status) {
-	  gettimeofday (&task[0].last, (void *) NULL);
-	  continue;
+	continue;
       }
     }
@@ -48,6 +60,5 @@
     /* check if there are errors with this task */
     if (!ValidateTask (task, TRUE)) { 
-	gettimeofday (&task[0].last, (void *) NULL);
-	continue;
+      continue;
     }
     
@@ -67,6 +78,5 @@
     // task[0].last.tv_sec, task[0].last.tv_usec);
 
-    /* reset timer on task (don't do this if Create/Submit fails) (why not??) */
-    gettimeofday (&task[0].last, (void *) NULL);
+    /* increment job counters */
     task[0].Njobs ++;
     task[0].Npending ++;
@@ -77,7 +87,5 @@
     /* increment Nrun for inclusive ranges with Nmax */
     BumpTimeRanges (task[0].ranges, task[0].Nranges);
-
-    if (TestElapsedCheck()) return (TRUE);
   }
-  return (TRUE);
+  return (next_timeout);
 }
