2011-08-05 08:25:00 -04:00
|
|
|
/******************************************************************************
|
|
|
|
|
|
|
|
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011.
|
|
|
|
|
|
|
|
This file is part of Sortix.
|
|
|
|
|
|
|
|
Sortix is free software: you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
version.
|
|
|
|
|
|
|
|
Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with Sortix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
scheduler.cpp
|
|
|
|
Handles context switching between tasks and deciding when to execute what.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "platform.h"
|
|
|
|
#include "panic.h"
|
2011-09-21 14:52:29 -04:00
|
|
|
#include "thread.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "time.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
#include "scheduler.h"
|
|
|
|
#include "memorymanagement.h"
|
2011-10-26 18:05:20 -04:00
|
|
|
#include "syscall.h"
|
2011-08-05 08:25:00 -04:00
|
|
|
|
|
|
|
namespace Sortix
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
// Internal forward-declarations.
|
2011-08-05 08:25:00 -04:00
|
|
|
namespace Scheduler
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
void InitCPU();
|
|
|
|
Thread* PopNextThread();
|
|
|
|
void WakeSleeping();
|
|
|
|
void LogBeginContextSwitch(Thread* current, const CPU::InterruptRegisters* state);
|
|
|
|
void LogContextSwitch(Thread* current, Thread* next);
|
|
|
|
void LogEndContextSwitch(Thread* current, const CPU::InterruptRegisters* state);
|
2011-10-26 18:05:20 -04:00
|
|
|
void SysSleep(size_t secs);
|
|
|
|
void SysUSleep(size_t usecs);
|
2011-09-21 14:52:29 -04:00
|
|
|
void HandleSigIntHack(CPU::InterruptRegisters* regs);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
namespace Scheduler
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
byte dummythreaddata[sizeof(Thread)];
|
|
|
|
Thread* dummythread = (Thread*) &dummythreaddata;
|
|
|
|
Thread* currentthread;
|
|
|
|
Thread* idlethread;
|
|
|
|
Thread* firstrunnablethread;
|
|
|
|
Thread* firstsleepingthread;
|
2011-11-05 13:49:30 -04:00
|
|
|
Process* initprocess;
|
2011-09-21 14:52:29 -04:00
|
|
|
bool hacksigintpending = false;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
// We use a dummy so that the first context switch won't crash when
|
|
|
|
// currentthread is accessed. This lets us avoid checking whether
|
|
|
|
// currentthread is NULL (which it only will be once) which gives
|
|
|
|
// simpler code.
|
|
|
|
currentthread = dummythread;
|
|
|
|
firstrunnablethread = NULL;
|
|
|
|
idlethread = NULL;
|
|
|
|
hacksigintpending = false;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Syscall::Register(SYSCALL_SLEEP, (void*) SysSleep);
|
|
|
|
Syscall::Register(SYSCALL_USLEEP, (void*) SysUSleep);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
InitCPU();
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
// The no operating thread is a thread stuck in an infinite loop that
|
|
|
|
// executes absolutely nothing, which is only run when the system has
|
|
|
|
// nothing to do.
|
|
|
|
void SetIdleThread(Thread* thread)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
ASSERT(idlethread == NULL);
|
|
|
|
idlethread = thread;
|
|
|
|
SetThreadState(thread, Thread::State::NONE);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void SetDummyThreadOwner(Process* process)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
dummythread->process = process;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-11-05 13:49:30 -04:00
|
|
|
void SetInitProcess(Process* init)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-11-05 13:49:30 -04:00
|
|
|
initprocess = init;
|
|
|
|
}
|
|
|
|
|
|
|
|
Process* GetInitProcess()
|
|
|
|
{
|
|
|
|
return initprocess;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
|
|
|
|
void MainLoop()
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
// Wait for the first hardware interrupt to trigger a context switch
|
|
|
|
// into the first task! Then the init process should gracefully
|
|
|
|
// start executing.
|
|
|
|
while(true);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void Switch(CPU::InterruptRegisters* regs)
|
|
|
|
{
|
|
|
|
LogBeginContextSwitch(currentthread, regs);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( hacksigintpending ) { HandleSigIntHack(regs); }
|
2011-08-27 17:03:39 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
WakeSleeping();
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* nextthread = PopNextThread();
|
|
|
|
if ( !nextthread ) { Panic("had no thread to switch to"); }
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
LogContextSwitch(currentthread, nextthread);
|
|
|
|
if ( nextthread == currentthread ) { return; }
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
currentthread->SaveRegisters(regs);
|
|
|
|
nextthread->LoadRegisters(regs);
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
addr_t newaddrspace = nextthread->process->addrspace;
|
|
|
|
Memory::SwitchAddressSpace(newaddrspace);
|
|
|
|
currentthread = nextthread;
|
2011-10-26 18:05:20 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
LogEndContextSwitch(currentthread, regs);
|
2011-11-06 17:06:32 -05:00
|
|
|
|
|
|
|
if ( currentthread->scfunc ) { Syscall::Resume(regs); }
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-11-05 13:52:11 -04:00
|
|
|
void ProcessTerminated(CPU::InterruptRegisters* regs)
|
|
|
|
{
|
|
|
|
currentthread = dummythread;
|
|
|
|
Switch(regs);
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
const bool DEBUG_BEGINCTXSWITCH = false;
|
|
|
|
const bool DEBUG_CTXSWITCH = false;
|
|
|
|
const bool DEBUG_ENDCTXSWITCH = false;
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void LogBeginContextSwitch(Thread* current, const CPU::InterruptRegisters* state)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( DEBUG_BEGINCTXSWITCH && current->process->pid != 0 )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Log::PrintF("Switching from 0x%p", current);
|
|
|
|
state->LogRegisters();
|
|
|
|
Log::Print("\n");
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void LogContextSwitch(Thread* current, Thread* next)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( DEBUG_CTXSWITCH && current != next )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Log::PrintF("switching from %u:%u (0x%p) to %u:%u (0x%p) \n",
|
|
|
|
current->process->pid, 0, current,
|
|
|
|
next->process->pid, 0, next);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogEndContextSwitch(Thread* current, const CPU::InterruptRegisters* state)
|
|
|
|
{
|
|
|
|
if ( DEBUG_ENDCTXSWITCH && current->process->pid != 0 )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Log::PrintF("Switched to 0x%p", current);
|
|
|
|
state->LogRegisters();
|
|
|
|
Log::Print("\n");
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
2011-09-21 14:52:29 -04:00
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* PopNextThread()
|
|
|
|
{
|
|
|
|
if ( !firstrunnablethread ) { return idlethread; }
|
|
|
|
Thread* result = firstrunnablethread;
|
|
|
|
firstrunnablethread = firstrunnablethread->schedulerlistnext;
|
|
|
|
return result;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void SetThreadState(Thread* thread, Thread::State state)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( thread->state == state ) { return; }
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( thread->state == Thread::State::RUNNABLE )
|
2011-08-27 17:03:39 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( thread == firstrunnablethread ) { firstrunnablethread = thread->schedulerlistnext; }
|
|
|
|
if ( thread == firstrunnablethread ) { firstrunnablethread = NULL; }
|
|
|
|
thread->schedulerlistprev->schedulerlistnext = thread->schedulerlistnext;
|
|
|
|
thread->schedulerlistnext->schedulerlistprev = thread->schedulerlistprev;
|
|
|
|
thread->schedulerlistprev = NULL;
|
|
|
|
thread->schedulerlistnext = NULL;
|
2011-08-27 17:03:39 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
// Insert the thread into the scheduler's carousel linked list.
|
|
|
|
if ( state == Thread::State::RUNNABLE )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( firstrunnablethread == NULL ) { firstrunnablethread = thread; }
|
|
|
|
thread->schedulerlistprev = firstrunnablethread->schedulerlistprev;
|
|
|
|
thread->schedulerlistnext = firstrunnablethread;
|
|
|
|
firstrunnablethread->schedulerlistprev = thread;
|
|
|
|
thread->schedulerlistprev->schedulerlistnext = thread;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
thread->state = state;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread::State GetThreadState(Thread* thread)
|
|
|
|
{
|
|
|
|
return thread->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PutThreadToSleep(Thread* thread, uintmax_t usecs)
|
|
|
|
{
|
|
|
|
SetThreadState(thread, Thread::State::BLOCKING);
|
|
|
|
thread->sleepuntil = Time::MicrosecondsSinceBoot() + usecs;
|
2011-08-06 19:15:26 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
// We use a simple linked linked list sorted after wake-up time to
|
|
|
|
// keep track of the threads that are sleeping.
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( firstsleepingthread == NULL )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
thread->nextsleepingthread = NULL;
|
|
|
|
firstsleepingthread = thread;
|
|
|
|
return;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( thread->sleepuntil < firstsleepingthread->sleepuntil )
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
thread->nextsleepingthread = firstsleepingthread;
|
|
|
|
firstsleepingthread = thread;
|
|
|
|
return;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
for ( Thread* tmp = firstsleepingthread; tmp != NULL; tmp = tmp->nextsleepingthread )
|
|
|
|
{
|
|
|
|
if ( tmp->nextsleepingthread == NULL ||
|
|
|
|
thread->sleepuntil < tmp->nextsleepingthread->sleepuntil )
|
|
|
|
{
|
|
|
|
thread->nextsleepingthread = tmp->nextsleepingthread;
|
|
|
|
tmp->nextsleepingthread = thread;
|
|
|
|
return;
|
|
|
|
}
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-05 13:52:11 -04:00
|
|
|
void EarlyWakeUp(Thread* thread)
|
|
|
|
{
|
|
|
|
uintmax_t now = Time::MicrosecondsSinceBoot();
|
|
|
|
if ( thread->sleepuntil < now ) { return; }
|
|
|
|
thread->sleepuntil = now;
|
|
|
|
|
|
|
|
SetThreadState(thread, Thread::State::RUNNABLE);
|
|
|
|
|
|
|
|
if ( firstsleepingthread == thread )
|
|
|
|
{
|
|
|
|
firstsleepingthread = thread->nextsleepingthread;
|
|
|
|
thread->nextsleepingthread = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( Thread* tmp = firstsleepingthread; tmp->nextsleepingthread != NULL; tmp = tmp->nextsleepingthread )
|
|
|
|
{
|
|
|
|
if ( tmp->nextsleepingthread == thread )
|
|
|
|
{
|
|
|
|
tmp->nextsleepingthread = thread->nextsleepingthread;
|
|
|
|
thread->nextsleepingthread = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void WakeSleeping()
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
uintmax_t now = Time::MicrosecondsSinceBoot();
|
|
|
|
|
|
|
|
while ( firstsleepingthread && firstsleepingthread->sleepuntil < now )
|
2011-08-28 10:59:07 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
SetThreadState(firstsleepingthread, Thread::State::RUNNABLE);
|
2011-11-05 13:52:11 -04:00
|
|
|
Thread* next = firstsleepingthread->nextsleepingthread;
|
|
|
|
firstsleepingthread->nextsleepingthread = NULL;
|
|
|
|
firstsleepingthread = next;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void HandleSigIntHack(CPU::InterruptRegisters* regs)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
if ( currentthread == idlethread ) { return; }
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
hacksigintpending = false;
|
|
|
|
Log::PrintF("^C\n");
|
2011-08-05 08:25:00 -04:00
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
Process::Execute("sh", regs);
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-09-21 14:52:29 -04:00
|
|
|
void SigIntHack()
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
hacksigintpending = true;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
void SysSleep(size_t secs)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* thread = currentthread;
|
|
|
|
uintmax_t timetosleep = ((uintmax_t) secs) * 1000ULL * 1000ULL;
|
|
|
|
if ( timetosleep == 0 ) { return; }
|
|
|
|
PutThreadToSleep(thread, timetosleep);
|
2011-10-26 18:05:20 -04:00
|
|
|
Syscall::Incomplete();
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:05:20 -04:00
|
|
|
void SysUSleep(size_t usecs)
|
2011-08-05 08:25:00 -04:00
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
Thread* thread = currentthread;
|
|
|
|
uintmax_t timetosleep = usecs;
|
|
|
|
if ( timetosleep == 0 ) { return; }
|
|
|
|
PutThreadToSleep(thread, timetosleep);
|
2011-10-26 18:05:20 -04:00
|
|
|
Syscall::Incomplete();
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread* CurrentThread()
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
return Scheduler::currentthread;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Process* CurrentProcess()
|
|
|
|
{
|
2011-09-21 14:52:29 -04:00
|
|
|
return Scheduler::currentthread->process;
|
2011-08-05 08:25:00 -04:00
|
|
|
}
|
|
|
|
}
|