# include "pantasks.h"

/* the RunScheduler thread should continuously loop over:
  
  - tasks
  - jobs

  occasionally, it should:
  - check controller state
  - check for new input files to be loaded

  alternatively, we could have separate threads for each of these
  tasks and simply have them each grab a mutex before they go:

  CheckTasks:
  - grab mutex
  - loop over all tasks
  - release mutex
  - nanosleep? 
  
  CheckJobs:
  - grab mutex
  - loop over all jobs
  - release mutex
  - nanosleep?

  CheckController:
  - grab mutex
  - check status
  - check output
  - release mutex
  - usleep 10000

  CheckInputs
  - grab mutex
  - check for inputs on list
  - load inputs
  - release mutex
  - usleep 200000

  this strategy seems fairly clean, with the somewhat odd design feature
  that all of these 'body' threads are force to run in serial, not parallel, 
  by use of the mutexes.  It is somewhat cleaner programming.  The only
  restriction on the commands allowed to the clients is that they must not
  be allowed to change any of the pantasks internal state variables (variables,
  macros, lists, etc).  This is solved by restricting the clients to output
  message functions only (reporting functions).

*/

# define MAX_DELAY 0.1
struct timeval start, last;
static int Ncheck = 0;

int CheckSystem () {

  gettimeofday (&start, (void *) NULL);

  if (Ncheck < 100) {
    CheckTasks ();
    CheckJobs ();
    Ncheck ++;
  } else {
    CheckController ();
    CheckControllerOutput ();
    Ncheck = 0;
  }
  /* ohana_memcheck (FALSE); */
  return (TRUE);
}

int TestElapsedCheck () {

  struct timeval stop;
  float dtime;

  gettimeofday (&stop, (void *) NULL);
  dtime = DTIME (stop, start);
  if (dtime > MAX_DELAY) return (TRUE);
  return (FALSE);
}

  /* do we really need to check the controller on every call?
     or perhaps alternate?
  */
