# include "pantasks.h"

/* this mutex is used by the serialized threads */

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void SerialThreadLock () {
    pthread_mutex_lock (&mutex);
}

void SerialThreadUnlock () {
    pthread_mutex_unlock (&mutex);
}

/* the threaded version of pantasks does not worry about limiting the 
   time spent in one of the test loops -- comms with the client are
   in a separate thread anyway...
*/

int TestElapsedCheck () {
  return (FALSE);
}

/** things related to CheckTasks **/

static int CheckTasksRun = FALSE;

void CheckTasksSetState (int state) {
  CheckTasksRun = state;
}
int CheckTasksGetState () {
  return (CheckTasksRun);
}

void *CheckTasksThread (void *data) {

  gprintInit ();  // each thread needs to init the printing system
  while (1) {

    // check for thread suspend
    if (!CheckTasksRun) {
      usleep (100000); // idle if thread action is suspended
      continue;
    }

    // one run of the task checker
    SerialThreadLock ();
    CheckTasks ();
    CheckJobs ();
    SerialThreadUnlock ();
    fprintf (stderr, "T");
    usleep (10000); // allow other threads a chance to run
  }
}

# if 0
// XXX need to harvest jobs on the same thread as they were
// spawned.  for the moment, put CheckTasks and CheckJobs in
// a single thread.
/** things related to CheckJobs **/

static int CheckJobsRun = FALSE;

void CheckJobsSetState (int state) {
  CheckJobsRun = state;
}
int CheckJobsGetState () {
  return (CheckJobsRun);
}

void *CheckJobsThread (void *data) {

  gprintInit ();  // each thread needs to init the printing system
  while (1) {

    // check for thread suspend
    if (!CheckJobsRun) {
      usleep (100000); // idle if thread action is suspended
      continue;
    }

    // one run of the task checker
    SerialThreadLock ();
    CheckJobs ();
    SerialThreadUnlock ();
    fprintf (stderr, "J");
    usleep (10000); // allow other threads a chance to run
  }
}
# endif

/** things related to CheckController **/

static int CheckControllerRun = FALSE;

void CheckControllerSetState (int state) {
  CheckControllerRun = state;
}
int CheckControllerGetState () {
  return (CheckControllerRun);
}

void *CheckControllerThread (void *data) {

  gprintInit ();  // each thread needs to init the printing system
  while (1) {

    // check for thread suspend
    if (!CheckControllerRun) {
      usleep (100000); // idle if thread action is suspended
      continue;
    }

    // one run of the task checker
    SerialThreadLock ();
    CheckController ();
    CheckControllerOutput ();
    SerialThreadUnlock ();
    fprintf (stderr, "C");
    usleep (10000); // allow other threads a chance to run
  }
}

/** things related to CheckInputs **/

static int CheckInputsRun = TRUE;

void CheckInputsSetState (int state) {
  CheckInputsRun = state;
}
int CheckInputsGetState () {
  return (CheckInputsRun);
}

void *CheckInputsThread (void *data) {

  gprintInit ();  // each thread needs to init the printing system
  while (1) {

    // check for thread suspend
    if (!CheckInputsRun) {
      usleep (100000); // idle if thread action is suspended
      continue;
    }

    // one run of the task checker
    SerialThreadLock ();
    CheckInputs ();
    SerialThreadUnlock ();
    fprintf (stderr, "I");
    usleep (10000); // allow other threads a chance to run
  }
}
