void ScheduleNext()
{
if (m_exiting == true)
{
Print("Exiting...");
m_continuation.Restore(2);
}
if (m_currentThread != null && m_threadsScheduledAfterManagers > m_scheduledThreadCount)
{
Print("Enough threads have been run, calling RunManagers()");
RunManagers();
}
while (m_currentThread == null)
{
#if EXTRA_CHECKS
if (m_scheduledThreadCount != 0 || m_previousThread != null)
{
throw new Exception();
}
#endif
Print("No threads running");
if (m_waitingThreadCount == 0 && m_sleepingThreadCount == 0)
{
Print("No threads running, waiting or sleeping. Exiting...");
m_continuation.Restore(2);
}
// TODO run managers when we have threads running
RunManagers();
}
//Print("going to run {0}, state {1}", m_currentThread, m_currentThread.m_state);
m_threadsScheduledAfterManagers++;
if (m_currentThread.m_state == MicroThreadState.Starting)
{
m_continuation.Restore(1);
}
else if (m_currentThread.m_state == MicroThreadState.Scheduled)
{
Print("Resuming thread {0}", m_currentThread);
m_currentThread.m_state = MicroThreadState.Running;
#if MT_TIMING
m_stopWatch.Reset();
m_stopWatch.Start();
#endif
m_currentThread.m_continuation.Restore(1);
// Execution never reaches this point
throw new Exception();
}
else
{
throw new Exception(String.Format("Illegal thread state in scheduler: {0}", m_currentThread.m_state));
}
}