Refactor scheduler API.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-01-09 10:47:22 +01:00
parent 9ba7f26bf0
commit 193b76f8cb
17 changed files with 59 additions and 36 deletions

View File

@ -1,6 +1,6 @@
/******************************************************************************* /*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012. Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2013.
This file is part of Sortix. This file is part of Sortix.
@ -17,17 +17,27 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
Sortix. If not, see <http://www.gnu.org/licenses/>. Sortix. If not, see <http://www.gnu.org/licenses/>.
scheduler.h sortix/kernel/scheduler.h
Decides the order to execute threads in and switching between them. Decides the order to execute threads in and switching between them.
*******************************************************************************/ *******************************************************************************/
#ifndef SORTIX_SCHEDULER_H #ifndef INCLUDE_SORTIX_KERNEL_SCHEDULER_H
#define SORTIX_SCHEDULER_H #define INCLUDE_SORTIX_KERNEL_SCHEDULER_H
#include "thread.h" #include <sortix/kernel/decl.h>
namespace Sortix { namespace Sortix {
class Process;
class Thread;
namespace CPU {
struct InterruptRegisters;
} // namespace CPU
enum ThreadState { NONE, RUNNABLE, BLOCKING, DEAD };
namespace Scheduler { namespace Scheduler {
void Init(); void Init();
@ -39,14 +49,15 @@ inline static void ExitThread()
asm volatile ("int $132"); asm volatile ("int $132");
__builtin_unreachable(); __builtin_unreachable();
} }
void SetThreadState(Thread* thread, Thread::State state); void SetThreadState(Thread* thread, ThreadState state);
Thread::State GetThreadState(Thread* thread); ThreadState GetThreadState(Thread* thread);
void SetIdleThread(Thread* thread); void SetIdleThread(Thread* thread);
void SetDummyThreadOwner(Process* process); void SetDummyThreadOwner(Process* process);
void SetInitProcess(Process* init); void SetInitProcess(Process* init);
Process* GetInitProcess(); Process* GetInitProcess();
} // namespace Scheduler } // namespace Scheduler
} // namespace Sortix } // namespace Sortix
#endif #endif

View File

@ -25,13 +25,13 @@
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/interrupt.h> #include <sortix/kernel/interrupt.h>
#include <sortix/kernel/scheduler.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "x86-family/idt.h" #include "x86-family/idt.h"
#include "scheduler.h"
#include "signal.h" #include "signal.h"
#include "process.h" #include "process.h"

View File

@ -44,6 +44,7 @@
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/interrupt.h> #include <sortix/kernel/interrupt.h>
#include <sortix/kernel/time.h> #include <sortix/kernel/time.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/fcntl.h> #include <sortix/fcntl.h>
#include <sortix/stat.h> #include <sortix/stat.h>
@ -60,7 +61,6 @@
#include "multiboot.h" #include "multiboot.h"
#include "thread.h" #include "thread.h"
#include "process.h" #include "process.h"
#include "scheduler.h"
#include "signal.h" #include "signal.h"
#include "ata.h" #include "ata.h"
#include "com.h" #include "com.h"

View File

@ -25,10 +25,12 @@
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/kthread.h> #include <sortix/kernel/kthread.h>
#include <sortix/kernel/worker.h> #include <sortix/kernel/worker.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/signal.h> #include <sortix/signal.h>
#include "signal.h" #include "signal.h"
#include "thread.h" #include "thread.h"
#include "scheduler.h"
namespace Sortix { namespace Sortix {
@ -36,7 +38,7 @@ namespace Sortix {
static void kthread_do_kill_thread(void* user) static void kthread_do_kill_thread(void* user)
{ {
Thread* thread = (Thread*) user; Thread* thread = (Thread*) user;
while ( thread->state != Thread::State::DEAD ) while ( thread->state != ThreadState::DEAD )
kthread_yield(); kthread_yield();
delete thread; delete thread;
} }

View File

@ -26,8 +26,13 @@
#include <sortix/kernel/kthread.h> #include <sortix/kernel/kthread.h>
#include <sortix/kernel/refcount.h> #include <sortix/kernel/refcount.h>
#include <sortix/kernel/textbuffer.h> #include <sortix/kernel/textbuffer.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/vga.h> #include <sortix/vga.h>
#include <string.h> #include <string.h>
#include "../thread.h"
#include "vga.h" #include "vga.h"
#include "lfbtextbuffer.h" #include "lfbtextbuffer.h"

View File

@ -30,6 +30,7 @@
#include <sortix/kernel/inode.h> #include <sortix/kernel/inode.h>
#include <sortix/kernel/keyboard.h> #include <sortix/kernel/keyboard.h>
#include <sortix/kernel/poll.h> #include <sortix/kernel/poll.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/termmode.h> #include <sortix/termmode.h>
#include <sortix/termios.h> #include <sortix/termios.h>
@ -40,9 +41,9 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "utf8.h" #include "utf8.h"
#include "process.h" #include "process.h"
#include "scheduler.h"
#include "logterminal.h" #include "logterminal.h"
namespace Sortix { namespace Sortix {

View File

@ -35,6 +35,7 @@
#include <sortix/kernel/string.h> #include <sortix/kernel/string.h>
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/sortedlist.h> #include <sortix/kernel/sortedlist.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/signal.h> #include <sortix/signal.h>
#include <sortix/unistd.h> #include <sortix/unistd.h>
@ -50,7 +51,6 @@
#include "thread.h" #include "thread.h"
#include "process.h" #include "process.h"
#include "scheduler.h"
#include "initrd.h" #include "initrd.h"
#include "elf.h" #include "elf.h"

View File

@ -37,13 +37,13 @@
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/interrupt.h> #include <sortix/kernel/interrupt.h>
#include <sortix/kernel/time.h> #include <sortix/kernel/time.h>
#include <sortix/kernel/scheduler.h>
#include "x86-family/gdt.h" #include "x86-family/gdt.h"
#include "x86-family/float.h" #include "x86-family/float.h"
#include "thread.h" #include "thread.h"
#include "process.h" #include "process.h"
#include "signal.h" #include "signal.h"
#include "scheduler.h"
namespace Sortix { namespace Sortix {
namespace Scheduler { namespace Scheduler {
@ -196,7 +196,7 @@ static void ThreadExitCPU(CPU::InterruptRegisters* regs, void* /*user*/)
{ {
// Can't use floating point instructions from now. // Can't use floating point instructions from now.
Float::NofityTaskExit(currentthread); Float::NofityTaskExit(currentthread);
SetThreadState(currentthread, Thread::State::DEAD); SetThreadState(currentthread, ThreadState::DEAD);
InterruptYieldCPU(regs, NULL); InterruptYieldCPU(regs, NULL);
} }
@ -206,7 +206,7 @@ void SetIdleThread(Thread* thread)
{ {
assert(!idlethread); assert(!idlethread);
idlethread = thread; idlethread = thread;
SetThreadState(thread, Thread::State::NONE); SetThreadState(thread, ThreadState::NONE);
SetCurrentThread(thread); SetCurrentThread(thread);
} }
@ -225,13 +225,13 @@ Process* GetInitProcess()
return initprocess; return initprocess;
} }
void SetThreadState(Thread* thread, Thread::State state) void SetThreadState(Thread* thread, ThreadState state)
{ {
bool wasenabled = Interrupt::SetEnabled(false); bool wasenabled = Interrupt::SetEnabled(false);
// Remove the thread from the list of runnable threads. // Remove the thread from the list of runnable threads.
if ( thread->state == Thread::State::RUNNABLE && if ( thread->state == ThreadState::RUNNABLE &&
state != Thread::State::RUNNABLE ) state != ThreadState::RUNNABLE )
{ {
if ( thread == firstrunnablethread ) { firstrunnablethread = thread->schedulerlistnext; } if ( thread == firstrunnablethread ) { firstrunnablethread = thread->schedulerlistnext; }
if ( thread == firstrunnablethread ) { firstrunnablethread = NULL; } if ( thread == firstrunnablethread ) { firstrunnablethread = NULL; }
@ -244,8 +244,8 @@ void SetThreadState(Thread* thread, Thread::State state)
} }
// Insert the thread into the scheduler's carousel linked list. // Insert the thread into the scheduler's carousel linked list.
if ( thread->state != Thread::State::RUNNABLE && if ( thread->state != ThreadState::RUNNABLE &&
state == Thread::State::RUNNABLE ) state == ThreadState::RUNNABLE )
{ {
if ( firstrunnablethread == NULL ) { firstrunnablethread = thread; } if ( firstrunnablethread == NULL ) { firstrunnablethread = thread; }
thread->schedulerlistprev = firstrunnablethread->schedulerlistprev; thread->schedulerlistprev = firstrunnablethread->schedulerlistprev;
@ -256,13 +256,13 @@ void SetThreadState(Thread* thread, Thread::State state)
thread->state = state; thread->state = state;
assert(thread->state != Thread::State::RUNNABLE || thread->schedulerlistprev); assert(thread->state != ThreadState::RUNNABLE || thread->schedulerlistprev);
assert(thread->state != Thread::State::RUNNABLE || thread->schedulerlistnext); assert(thread->state != ThreadState::RUNNABLE || thread->schedulerlistnext);
Interrupt::SetEnabled(wasenabled); Interrupt::SetEnabled(wasenabled);
} }
Thread::State GetThreadState(Thread* thread) ThreadState GetThreadState(Thread* thread)
{ {
return thread->state; return thread->state;
} }

View File

@ -24,11 +24,13 @@
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/keyboard.h> #include <sortix/kernel/keyboard.h>
#include <sortix/kernel/scheduler.h>
#include <string.h> #include <string.h>
#include "vga.h" #include "vga.h"
#include "uart.h" #include "uart.h"
#include "serialterminal.h" #include "serialterminal.h"
#include "scheduler.h"
namespace Sortix namespace Sortix
{ {

View File

@ -25,6 +25,8 @@
#ifndef SORTIX_SIGNAL_H #ifndef SORTIX_SIGNAL_H
#define SORTIX_SIGNAL_H #define SORTIX_SIGNAL_H
#include <sortix/signal.h>
#include "cpu.h" #include "cpu.h"
namespace Sortix { namespace Sortix {

View File

@ -26,10 +26,10 @@
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/scheduler.h>
#include "process.h" #include "process.h"
#include "thread.h" #include "thread.h"
#include "scheduler.h"
namespace Sortix { namespace Sortix {
namespace Syscall { namespace Syscall {

View File

@ -28,6 +28,7 @@
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/interrupt.h> #include <sortix/kernel/interrupt.h>
#include <sortix/kernel/time.h> #include <sortix/kernel/time.h>
#include <sortix/kernel/scheduler.h>
#include <sortix/mman.h> #include <sortix/mman.h>
#include <sortix/signal.h> #include <sortix/signal.h>
@ -38,7 +39,6 @@
#include "process.h" #include "process.h"
#include "thread.h" #include "thread.h"
#include "scheduler.h"
namespace Sortix namespace Sortix
{ {
@ -152,7 +152,7 @@ namespace Sortix
void StartKernelThread(Thread* thread) void StartKernelThread(Thread* thread)
{ {
Scheduler::SetThreadState(thread, Thread::State::RUNNABLE); Scheduler::SetThreadState(thread, ThreadState::RUNNABLE);
} }
Thread* RunKernelThread(Process* process, CPU::InterruptRegisters* regs) Thread* RunKernelThread(Process* process, CPU::InterruptRegisters* regs)

View File

@ -26,6 +26,9 @@
#define SORTIX_THREAD_H #define SORTIX_THREAD_H
#include <sortix/signal.h> #include <sortix/signal.h>
#include <sortix/kernel/scheduler.h>
#include "signal.h" #include "signal.h"
typedef struct multiboot_info multiboot_info_t; typedef struct multiboot_info multiboot_info_t;
@ -75,9 +78,6 @@ namespace Sortix
friend void KernelInit(unsigned long magic, multiboot_info_t* bootinfo); friend void KernelInit(unsigned long magic, multiboot_info_t* bootinfo);
friend void Thread__OnSigKill(Thread* thread); friend void Thread__OnSigKill(Thread* thread);
public:
enum State { NONE, RUNNABLE, BLOCKING, DEAD };
public: public:
static void Init(); static void Init();
@ -99,7 +99,7 @@ namespace Sortix
public: public:
Thread* schedulerlistprev; Thread* schedulerlistprev;
Thread* schedulerlistnext; Thread* schedulerlistnext;
volatile State state; volatile ThreadState state;
uint8_t fpuenv[512UL + 16UL]; uint8_t fpuenv[512UL + 16UL];
uint8_t* fpuenvaligned; uint8_t* fpuenvaligned;
bool fpuinitialized; bool fpuinitialized;

View File

@ -37,9 +37,9 @@
#include <sortix/kernel/interrupt.h> #include <sortix/kernel/interrupt.h>
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/time.h> #include <sortix/kernel/time.h>
#include <sortix/kernel/scheduler.h>
#include "process.h" #include "process.h"
#include "scheduler.h"
#include "sound.h" #include "sound.h"
#ifdef PLATFORM_SERIAL #ifdef PLATFORM_SERIAL

View File

@ -30,13 +30,13 @@
#include <sortix/kernel/descriptor.h> #include <sortix/kernel/descriptor.h>
#include <sortix/kernel/interlock.h> #include <sortix/kernel/interlock.h>
#include <sortix/kernel/syscall.h> #include <sortix/kernel/syscall.h>
#include <sortix/kernel/scheduler.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "fs/util.h" #include "fs/util.h"
#include "vga.h" #include "vga.h"
#include "scheduler.h"
#include "process.h" #include "process.h"
#define TEST_VGAFONT 0 #define TEST_VGAFONT 0

View File

@ -24,7 +24,7 @@
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include <sortix/kernel/memorymanagement.h> #include <sortix/kernel/memorymanagement.h>
#include "scheduler.h" #include <sortix/kernel/scheduler.h>
namespace Sortix namespace Sortix
{ {

View File

@ -23,7 +23,7 @@
*******************************************************************************/ *******************************************************************************/
#include <sortix/kernel/platform.h> #include <sortix/kernel/platform.h>
#include "scheduler.h" #include <sortix/kernel/scheduler.h>
#include <sortix/kernel/memorymanagement.h> #include <sortix/kernel/memorymanagement.h>
namespace Sortix namespace Sortix