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:
Drew Galbraith 2023-05-29 23:35:44 -07:00
parent b58186265e
commit 7fe6c24aa5
7 changed files with 66 additions and 23 deletions

View file

@ -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;
}
}

View file

@ -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;

View file

@ -2,36 +2,26 @@
#include "debug/debug.h"
#include "lib/linked_list.h"
#include "scheduler/process_manager.h"
namespace sched {
namespace {
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 {
public:
Scheduler() {
SharedPtr<Process> root = Process::RootProcess();
sleep_thread_ = root->GetThread(0);
Process& root = gProcMan->FromId(0);
sleep_thread_ = root.GetThread(0);
// TODO: Implement a separate sleep thread?
current_thread_ = sleep_thread_;
proc_list_.PushBack(root);
}
void Enable() { enabled_ = true; }
Process& CurrentProcess() { return CurrentThread().process(); }
Thread& CurrentThread() { return *current_thread_; }
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
void SwapToCurrent(Thread& prev) {
@ -90,7 +80,7 @@ class Scheduler {
if (runnable_threads_.size() == 0) {
current_thread_ = sleep_thread_;
dbgln("Sleeping");
DumpProcessStates(proc_list_);
gProcMan->DumpProcessStates();
} else {
// FIXME: Memory operation.
current_thread_ = runnable_threads_.PopFront();
@ -102,8 +92,6 @@ class Scheduler {
private:
bool enabled_ = false;
// TODO: move this to a separate process manager class.
LinkedList<SharedPtr<Process>> proc_list_;
SharedPtr<Thread> current_thread_;
LinkedList<SharedPtr<Thread>> runnable_threads_;
@ -128,7 +116,6 @@ void EnableScheduler() { GetScheduler().Enable(); }
void Preempt() { GetScheduler().Preempt(); }
void Yield() { GetScheduler().Yield(); }
void InsertProcess(Process* process) { GetScheduler().InsertProcess(process); }
void EnqueueThread(Thread* thread) { GetScheduler().Enqueue(thread); }
Process& CurrentProcess() { return GetScheduler().CurrentProcess(); }

View file

@ -21,10 +21,6 @@ void Preempt();
// Used when a thread blocks or exits.
void Yield();
// Scheduler will take ownership
// of the created process.
void InsertProcess(Process* proc);
void EnqueueThread(Thread* thread);
Process& CurrentProcess();