#include "stdafx.h"
#include "Scheduler.h"
#include "MP/MPScheduler.h"

volatile id_type s_currentId = 1; // 0 is an invalid ID, so valid IDs start at 1

void Scheduler::onFrame(Timer::value_type endTime)
{
#ifdef USE_MULTITHREADED_SCHEDULER
  // Run the full available time to let the other threads as much computation time as possible
  // (and to let them schedule tasks on the main thread if necessary).
  do
  {
    m_tasks.do_while(ExecuteTask(*this, endTime));
    if (!(Spar->getTimer()->currentTime() < endTime && Spar->getMultiThreadScheduler()->hasWorkerThreadsRunning()))
      break;
    Sleep(0);
  }
  while (Spar->getTimer()->currentTime() < endTime && Spar->getMultiThreadScheduler()->hasWorkerThreadsRunning());
#else
  m_tasks.do_while(ExecuteTask(*this, endTime));
#endif
}