From 193b76f8cb6c77d11d1c9c779b8952de53770cfe Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Wed, 9 Jan 2013 10:47:22 +0100 Subject: [PATCH] Refactor scheduler API. --- .../{ => include/sortix/kernel}/scheduler.h | 25 +++++++++++++------ sortix/interrupt.cpp | 2 +- sortix/kernel.cpp | 2 +- sortix/kthread.cpp | 6 +++-- sortix/lfbtextbuffer.cpp | 5 ++++ sortix/logterminal.cpp | 3 ++- sortix/process.cpp | 2 +- sortix/scheduler.cpp | 22 ++++++++-------- sortix/serialterminal.cpp | 4 ++- sortix/signal.h | 2 ++ sortix/syscall.cpp | 2 +- sortix/thread.cpp | 4 +-- sortix/thread.h | 8 +++--- sortix/time.cpp | 2 +- sortix/vga.cpp | 2 +- sortix/x64/scheduler.cpp | 2 +- sortix/x86/scheduler.cpp | 2 +- 17 files changed, 59 insertions(+), 36 deletions(-) rename sortix/{ => include/sortix/kernel}/scheduler.h (75%) diff --git a/sortix/scheduler.h b/sortix/include/sortix/kernel/scheduler.h similarity index 75% rename from sortix/scheduler.h rename to sortix/include/sortix/kernel/scheduler.h index b7c949f7..8fa3642e 100644 --- a/sortix/scheduler.h +++ b/sortix/include/sortix/kernel/scheduler.h @@ -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. @@ -17,17 +17,27 @@ You should have received a copy of the GNU General Public License along with Sortix. If not, see . - scheduler.h + sortix/kernel/scheduler.h Decides the order to execute threads in and switching between them. *******************************************************************************/ -#ifndef SORTIX_SCHEDULER_H -#define SORTIX_SCHEDULER_H +#ifndef INCLUDE_SORTIX_KERNEL_SCHEDULER_H +#define INCLUDE_SORTIX_KERNEL_SCHEDULER_H -#include "thread.h" +#include namespace Sortix { + +class Process; +class Thread; + +namespace CPU { +struct InterruptRegisters; +} // namespace CPU + +enum ThreadState { NONE, RUNNABLE, BLOCKING, DEAD }; + namespace Scheduler { void Init(); @@ -39,14 +49,15 @@ inline static void ExitThread() asm volatile ("int $132"); __builtin_unreachable(); } -void SetThreadState(Thread* thread, Thread::State state); -Thread::State GetThreadState(Thread* thread); +void SetThreadState(Thread* thread, ThreadState state); +ThreadState GetThreadState(Thread* thread); void SetIdleThread(Thread* thread); void SetDummyThreadOwner(Process* process); void SetInitProcess(Process* init); Process* GetInitProcess(); } // namespace Scheduler + } // namespace Sortix #endif diff --git a/sortix/interrupt.cpp b/sortix/interrupt.cpp index 853df984..e0ce38bb 100644 --- a/sortix/interrupt.cpp +++ b/sortix/interrupt.cpp @@ -25,13 +25,13 @@ #include #include #include +#include #include #include #include #include "x86-family/idt.h" -#include "scheduler.h" #include "signal.h" #include "process.h" diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp index 913ee543..1d03d3a0 100644 --- a/sortix/kernel.cpp +++ b/sortix/kernel.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include #include @@ -60,7 +61,6 @@ #include "multiboot.h" #include "thread.h" #include "process.h" -#include "scheduler.h" #include "signal.h" #include "ata.h" #include "com.h" diff --git a/sortix/kthread.cpp b/sortix/kthread.cpp index eeb0c7b6..0036cf42 100644 --- a/sortix/kthread.cpp +++ b/sortix/kthread.cpp @@ -25,10 +25,12 @@ #include #include #include +#include + #include + #include "signal.h" #include "thread.h" -#include "scheduler.h" namespace Sortix { @@ -36,7 +38,7 @@ namespace Sortix { static void kthread_do_kill_thread(void* user) { Thread* thread = (Thread*) user; - while ( thread->state != Thread::State::DEAD ) + while ( thread->state != ThreadState::DEAD ) kthread_yield(); delete thread; } diff --git a/sortix/lfbtextbuffer.cpp b/sortix/lfbtextbuffer.cpp index bea1f80f..b520e18e 100644 --- a/sortix/lfbtextbuffer.cpp +++ b/sortix/lfbtextbuffer.cpp @@ -26,8 +26,13 @@ #include #include #include +#include + #include + #include + +#include "../thread.h" #include "vga.h" #include "lfbtextbuffer.h" diff --git a/sortix/logterminal.cpp b/sortix/logterminal.cpp index 0eb1e3fc..8eb12b14 100644 --- a/sortix/logterminal.cpp +++ b/sortix/logterminal.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -40,9 +41,9 @@ #include #include + #include "utf8.h" #include "process.h" -#include "scheduler.h" #include "logterminal.h" namespace Sortix { diff --git a/sortix/process.cpp b/sortix/process.cpp index f1e9497b..2545ba35 100644 --- a/sortix/process.cpp +++ b/sortix/process.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -50,7 +51,6 @@ #include "thread.h" #include "process.h" -#include "scheduler.h" #include "initrd.h" #include "elf.h" diff --git a/sortix/scheduler.cpp b/sortix/scheduler.cpp index b1e10b1d..4373955d 100644 --- a/sortix/scheduler.cpp +++ b/sortix/scheduler.cpp @@ -37,13 +37,13 @@ #include #include #include +#include #include "x86-family/gdt.h" #include "x86-family/float.h" #include "thread.h" #include "process.h" #include "signal.h" -#include "scheduler.h" namespace Sortix { namespace Scheduler { @@ -196,7 +196,7 @@ static void ThreadExitCPU(CPU::InterruptRegisters* regs, void* /*user*/) { // Can't use floating point instructions from now. Float::NofityTaskExit(currentthread); - SetThreadState(currentthread, Thread::State::DEAD); + SetThreadState(currentthread, ThreadState::DEAD); InterruptYieldCPU(regs, NULL); } @@ -206,7 +206,7 @@ void SetIdleThread(Thread* thread) { assert(!idlethread); idlethread = thread; - SetThreadState(thread, Thread::State::NONE); + SetThreadState(thread, ThreadState::NONE); SetCurrentThread(thread); } @@ -225,13 +225,13 @@ Process* GetInitProcess() return initprocess; } -void SetThreadState(Thread* thread, Thread::State state) +void SetThreadState(Thread* thread, ThreadState state) { bool wasenabled = Interrupt::SetEnabled(false); // Remove the thread from the list of runnable threads. - if ( thread->state == Thread::State::RUNNABLE && - state != Thread::State::RUNNABLE ) + if ( thread->state == ThreadState::RUNNABLE && + state != ThreadState::RUNNABLE ) { if ( thread == firstrunnablethread ) { firstrunnablethread = thread->schedulerlistnext; } 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. - if ( thread->state != Thread::State::RUNNABLE && - state == Thread::State::RUNNABLE ) + if ( thread->state != ThreadState::RUNNABLE && + state == ThreadState::RUNNABLE ) { if ( firstrunnablethread == NULL ) { firstrunnablethread = thread; } thread->schedulerlistprev = firstrunnablethread->schedulerlistprev; @@ -256,13 +256,13 @@ void SetThreadState(Thread* thread, Thread::State state) thread->state = state; - assert(thread->state != Thread::State::RUNNABLE || thread->schedulerlistprev); - assert(thread->state != Thread::State::RUNNABLE || thread->schedulerlistnext); + assert(thread->state != ThreadState::RUNNABLE || thread->schedulerlistprev); + assert(thread->state != ThreadState::RUNNABLE || thread->schedulerlistnext); Interrupt::SetEnabled(wasenabled); } -Thread::State GetThreadState(Thread* thread) +ThreadState GetThreadState(Thread* thread) { return thread->state; } diff --git a/sortix/serialterminal.cpp b/sortix/serialterminal.cpp index 12b3f74e..9e403978 100644 --- a/sortix/serialterminal.cpp +++ b/sortix/serialterminal.cpp @@ -24,11 +24,13 @@ #include #include +#include + #include + #include "vga.h" #include "uart.h" #include "serialterminal.h" -#include "scheduler.h" namespace Sortix { diff --git a/sortix/signal.h b/sortix/signal.h index f841522b..63b36f71 100644 --- a/sortix/signal.h +++ b/sortix/signal.h @@ -25,6 +25,8 @@ #ifndef SORTIX_SIGNAL_H #define SORTIX_SIGNAL_H +#include + #include "cpu.h" namespace Sortix { diff --git a/sortix/syscall.cpp b/sortix/syscall.cpp index 7878227a..c5fbf7b4 100644 --- a/sortix/syscall.cpp +++ b/sortix/syscall.cpp @@ -26,10 +26,10 @@ #include #include +#include #include "process.h" #include "thread.h" -#include "scheduler.h" namespace Sortix { namespace Syscall { diff --git a/sortix/thread.cpp b/sortix/thread.cpp index 99b4733b..e4bd513e 100644 --- a/sortix/thread.cpp +++ b/sortix/thread.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -38,7 +39,6 @@ #include "process.h" #include "thread.h" -#include "scheduler.h" namespace Sortix { @@ -152,7 +152,7 @@ namespace Sortix void StartKernelThread(Thread* thread) { - Scheduler::SetThreadState(thread, Thread::State::RUNNABLE); + Scheduler::SetThreadState(thread, ThreadState::RUNNABLE); } Thread* RunKernelThread(Process* process, CPU::InterruptRegisters* regs) diff --git a/sortix/thread.h b/sortix/thread.h index e3f25f67..32d09b1e 100644 --- a/sortix/thread.h +++ b/sortix/thread.h @@ -26,6 +26,9 @@ #define SORTIX_THREAD_H #include + +#include + #include "signal.h" 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 Thread__OnSigKill(Thread* thread); - public: - enum State { NONE, RUNNABLE, BLOCKING, DEAD }; - public: static void Init(); @@ -99,7 +99,7 @@ namespace Sortix public: Thread* schedulerlistprev; Thread* schedulerlistnext; - volatile State state; + volatile ThreadState state; uint8_t fpuenv[512UL + 16UL]; uint8_t* fpuenvaligned; bool fpuinitialized; diff --git a/sortix/time.cpp b/sortix/time.cpp index 529f5ec8..04761e36 100644 --- a/sortix/time.cpp +++ b/sortix/time.cpp @@ -37,9 +37,9 @@ #include #include #include +#include #include "process.h" -#include "scheduler.h" #include "sound.h" #ifdef PLATFORM_SERIAL diff --git a/sortix/vga.cpp b/sortix/vga.cpp index bf03d84e..ad7a0966 100644 --- a/sortix/vga.cpp +++ b/sortix/vga.cpp @@ -30,13 +30,13 @@ #include #include #include +#include #include #include #include "fs/util.h" #include "vga.h" -#include "scheduler.h" #include "process.h" #define TEST_VGAFONT 0 diff --git a/sortix/x64/scheduler.cpp b/sortix/x64/scheduler.cpp index 7c050871..817e5dba 100644 --- a/sortix/x64/scheduler.cpp +++ b/sortix/x64/scheduler.cpp @@ -24,7 +24,7 @@ #include #include -#include "scheduler.h" +#include namespace Sortix { diff --git a/sortix/x86/scheduler.cpp b/sortix/x86/scheduler.cpp index 9e01f323..f7d39e4e 100644 --- a/sortix/x86/scheduler.cpp +++ b/sortix/x86/scheduler.cpp @@ -23,7 +23,7 @@ *******************************************************************************/ #include -#include "scheduler.h" +#include #include namespace Sortix