
#include "RobboLito_def.h"
#include "RobboTripleBase.h"
#include <string.h>
#include "RobboTotalBase.h"

static boolean volatile LOAD_ON_WEAK_PROBE;
static boolean volatile ALLOW_WEAK_PROBE;

void OfftoneTripleSMP ();
void SubsumeTripleSMP ();

#include <time.h>
void NANO_SLEEP (int x)
{
#ifdef WINDOWS
  Sleep (x / 1000000);
#else
  struct timespec TS[1];
  TS->tv_sec = 0;
  TS->tv_nsec = x;
  nanosleep (TS, NULL);
#endif
}

#ifdef WINDOWS
#define WEAK_LOAD_THREAD(A) DWORD WINAPI weak_load_thread (LPVOID A)
HANDLE PTHREAD_WEAK_LOAD;
#else
#define WEAK_LOAD_THREAD(A) void* weak_load_thread (void* A)
pthread_t PTHREAD_WEAK_LOAD;
#endif

WEAK_LOAD_THREAD (A)
{
  while (TRUE)
    {
      OfftoneTripleSMP (); /* clear */
      NANO_SLEEP (1000000); /* indicate */
      if (LOAD_ON_WEAK_PROBE && ALLOW_WEAK_PROBE)
	SubsumeTripleSMP ();
    }
  return NULL;
}

void InitBackGroundLoader ()
{
  LOAD_ON_WEAK_PROBE = FALSE;
  ALLOW_WEAK_PROBE = FALSE;
  PTHREAD_CREATE (&PTHREAD_WEAK_LOAD, NULL, weak_load_thread, NULL);
}

boolean weak_probe_modify (int type, int allow)
{
  if (type != -1)
    LOAD_ON_WEAK_PROBE = type;
  if (allow != -1)
    ALLOW_WEAK_PROBE = allow;
  return TRUE;
}
