Add a ProcessManager class to store Process objects.
Trying out a new method for exposing global objects directly via a variable.
This commit is contained in:
parent
b58186265e
commit
7fe6c24aa5
|
@ -14,6 +14,7 @@ add_executable(zion
|
||||||
scheduler/context_switch.s
|
scheduler/context_switch.s
|
||||||
scheduler/jump_user_space.s
|
scheduler/jump_user_space.s
|
||||||
scheduler/process.cpp
|
scheduler/process.cpp
|
||||||
|
scheduler/process_manager.cpp
|
||||||
scheduler/scheduler.cpp
|
scheduler/scheduler.cpp
|
||||||
scheduler/thread.cpp
|
scheduler/thread.cpp
|
||||||
syscall/syscall.cpp
|
syscall/syscall.cpp
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
#include "loader/elf_loader.h"
|
#include "loader/elf_loader.h"
|
||||||
#include "scheduler/process.h"
|
#include "scheduler/process.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/process_manager.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
@ -25,6 +25,6 @@ const limine_file& GetInitProgram() {
|
||||||
void LoadInitProgram() {
|
void LoadInitProgram() {
|
||||||
const limine_file& init_prog = GetInitProgram();
|
const limine_file& init_prog = GetInitProgram();
|
||||||
|
|
||||||
sched::InsertProcess(
|
gProcMan->InsertProcess(
|
||||||
new Process(reinterpret_cast<uint64_t>(init_prog.address)));
|
new Process(reinterpret_cast<uint64_t>(init_prog.address)));
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include "scheduler/process_manager.h"
|
||||||
|
|
||||||
|
ProcessManager* gProcMan = nullptr;
|
||||||
|
|
||||||
|
void ProcessManager::Init() {
|
||||||
|
gProcMan = new ProcessManager();
|
||||||
|
gProcMan->InsertProcess(Process::RootProcess());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProcessManager::InsertProcess(const SharedPtr<Process>& proc) {
|
||||||
|
proc_list_.PushBack(proc);
|
||||||
|
}
|
||||||
|
|
||||||
|
Process& ProcessManager::FromId(uint64_t pid) {
|
||||||
|
auto iter = proc_list_.begin();
|
||||||
|
while (iter != proc_list_.end()) {
|
||||||
|
if (iter->id() == pid) {
|
||||||
|
return **iter;
|
||||||
|
}
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
|
||||||
|
panic("Searching for invalid process id");
|
||||||
|
return *((Process*)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ProcessManager::DumpProcessStates() {
|
||||||
|
dbgln("Process States: %u", proc_list_.size());
|
||||||
|
auto iter = proc_list_.begin();
|
||||||
|
while (iter != proc_list_.end()) {
|
||||||
|
dbgln("%u: %u", iter->id(), iter->GetState());
|
||||||
|
++iter;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "lib/linked_list.h"
|
||||||
|
#include "lib/shared_ptr.h"
|
||||||
|
#include "scheduler/process.h"
|
||||||
|
|
||||||
|
class ProcessManager {
|
||||||
|
public:
|
||||||
|
// Initializes the ProcessManager
|
||||||
|
// and stores it in the global variable.
|
||||||
|
static void Init();
|
||||||
|
|
||||||
|
void InsertProcess(const SharedPtr<Process>& proc);
|
||||||
|
Process& FromId(uint64_t id);
|
||||||
|
|
||||||
|
void DumpProcessStates();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// TODO: This should be a hashmap.
|
||||||
|
LinkedList<SharedPtr<Process>> proc_list_;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern ProcessManager* gProcMan;
|
|
@ -2,36 +2,26 @@
|
||||||
|
|
||||||
#include "debug/debug.h"
|
#include "debug/debug.h"
|
||||||
#include "lib/linked_list.h"
|
#include "lib/linked_list.h"
|
||||||
|
#include "scheduler/process_manager.h"
|
||||||
|
|
||||||
namespace sched {
|
namespace sched {
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
|
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
|
||||||
|
|
||||||
void DumpProcessStates(LinkedList<SharedPtr<Process>>& proc_list) {
|
|
||||||
dbgln("Process States: %u", proc_list.size());
|
|
||||||
auto iter = proc_list.begin();
|
|
||||||
while (iter != proc_list.end()) {
|
|
||||||
dbgln("%u: %u", iter->id(), iter->GetState());
|
|
||||||
++iter;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Scheduler {
|
class Scheduler {
|
||||||
public:
|
public:
|
||||||
Scheduler() {
|
Scheduler() {
|
||||||
SharedPtr<Process> root = Process::RootProcess();
|
Process& root = gProcMan->FromId(0);
|
||||||
sleep_thread_ = root->GetThread(0);
|
sleep_thread_ = root.GetThread(0);
|
||||||
// TODO: Implement a separate sleep thread?
|
// TODO: Implement a separate sleep thread?
|
||||||
current_thread_ = sleep_thread_;
|
current_thread_ = sleep_thread_;
|
||||||
proc_list_.PushBack(root);
|
|
||||||
}
|
}
|
||||||
void Enable() { enabled_ = true; }
|
void Enable() { enabled_ = true; }
|
||||||
|
|
||||||
Process& CurrentProcess() { return CurrentThread().process(); }
|
Process& CurrentProcess() { return CurrentThread().process(); }
|
||||||
Thread& CurrentThread() { return *current_thread_; }
|
Thread& CurrentThread() { return *current_thread_; }
|
||||||
|
|
||||||
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
|
|
||||||
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
||||||
|
|
||||||
void SwapToCurrent(Thread& prev) {
|
void SwapToCurrent(Thread& prev) {
|
||||||
|
@ -90,7 +80,7 @@ class Scheduler {
|
||||||
if (runnable_threads_.size() == 0) {
|
if (runnable_threads_.size() == 0) {
|
||||||
current_thread_ = sleep_thread_;
|
current_thread_ = sleep_thread_;
|
||||||
dbgln("Sleeping");
|
dbgln("Sleeping");
|
||||||
DumpProcessStates(proc_list_);
|
gProcMan->DumpProcessStates();
|
||||||
} else {
|
} else {
|
||||||
// FIXME: Memory operation.
|
// FIXME: Memory operation.
|
||||||
current_thread_ = runnable_threads_.PopFront();
|
current_thread_ = runnable_threads_.PopFront();
|
||||||
|
@ -102,8 +92,6 @@ class Scheduler {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool enabled_ = false;
|
bool enabled_ = false;
|
||||||
// TODO: move this to a separate process manager class.
|
|
||||||
LinkedList<SharedPtr<Process>> proc_list_;
|
|
||||||
|
|
||||||
SharedPtr<Thread> current_thread_;
|
SharedPtr<Thread> current_thread_;
|
||||||
LinkedList<SharedPtr<Thread>> runnable_threads_;
|
LinkedList<SharedPtr<Thread>> runnable_threads_;
|
||||||
|
@ -128,7 +116,6 @@ void EnableScheduler() { GetScheduler().Enable(); }
|
||||||
void Preempt() { GetScheduler().Preempt(); }
|
void Preempt() { GetScheduler().Preempt(); }
|
||||||
void Yield() { GetScheduler().Yield(); }
|
void Yield() { GetScheduler().Yield(); }
|
||||||
|
|
||||||
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
|
|
||||||
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
|
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
|
||||||
|
|
||||||
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }
|
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }
|
||||||
|
|
|
@ -21,10 +21,6 @@ void Preempt();
|
||||||
// Used when a thread blocks or exits.
|
// Used when a thread blocks or exits.
|
||||||
void Yield();
|
void Yield();
|
||||||
|
|
||||||
// Scheduler will take ownership
|
|
||||||
// of the created process.
|
|
||||||
void InsertProcess(Process* proc);
|
|
||||||
|
|
||||||
void EnqueueThread(Thread* thread);
|
void EnqueueThread(Thread* thread);
|
||||||
|
|
||||||
Process& CurrentProcess();
|
Process& CurrentProcess();
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include "memory/kernel_heap.h"
|
#include "memory/kernel_heap.h"
|
||||||
#include "memory/paging_util.h"
|
#include "memory/paging_util.h"
|
||||||
#include "memory/physical_memory.h"
|
#include "memory/physical_memory.h"
|
||||||
|
#include "scheduler/process_manager.h"
|
||||||
#include "scheduler/scheduler.h"
|
#include "scheduler/scheduler.h"
|
||||||
#include "syscall/syscall.h"
|
#include "syscall/syscall.h"
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@ extern "C" void zion() {
|
||||||
InitSyscall();
|
InitSyscall();
|
||||||
|
|
||||||
dbgln("[boot] Init scheduler.");
|
dbgln("[boot] Init scheduler.");
|
||||||
|
ProcessManager::Init();
|
||||||
// Schedule every 50ms.
|
// Schedule every 50ms.
|
||||||
SetFrequency(/* hertz= */ 20);
|
SetFrequency(/* hertz= */ 20);
|
||||||
sched::InitScheduler();
|
sched::InitScheduler();
|
||||||
|
|
Loading…
Reference in New Issue