Move process list to a linked list.

Add basic iteration ability to the linked list.
This commit is contained in:
Drew Galbraith 2023-05-29 14:59:23 -07:00
parent 71196dc90f
commit 5cab9f843e
2 changed files with 34 additions and 53 deletions

View File

@ -11,7 +11,7 @@ class LinkedList {
LinkedList(const LinkedList&) = delete;
uint64_t size() { return size_; }
uint64_t size() const { return size_; }
void PushBack(const T& item) {
size_++;
@ -65,15 +65,32 @@ class LinkedList {
return ret;
}
T PeekFront() { return front_->item; }
private:
uint64_t size_ = 0;
T PeekFront() const { return front_->item; }
struct ListItem {
T item;
ListItem* next;
};
class Iterator {
public:
Iterator(ListItem* item) : item_(item) {}
Iterator next() { return {item_->next}; }
T& operator*() { return item_->item; }
T& operator->() { return item_->item; }
bool operator==(const Iterator& other) { return item_ == other.item_; }
bool operator!=(const Iterator& other) { return item_ != other.item_; }
private:
ListItem* item_;
};
Iterator begin() { return {front_}; }
Iterator end() { return {nullptr}; }
private:
uint64_t size_ = 0;
ListItem* front_ = nullptr;
};

View File

@ -8,64 +8,28 @@ namespace {
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
// Simple linked list class with the intent of eventually replacing this with a
// map.
class ProcList {
public:
ProcList() {}
// Takes ownership.
void InsertProcess(Process* proc) {
if (front_ == nullptr) {
front_ = new ProcEntry{
.proc = proc,
.next = nullptr,
};
return;
}
ProcEntry* back = front_;
while (back->next != nullptr) {
back = back->next;
}
back->next = new ProcEntry{
.proc = proc,
.next = nullptr,
};
}
void DumpProcessStates() {
ProcEntry* p = front_;
dbgln("Process States:");
while (p != nullptr) {
dbgln("%u: %u", p->proc->id(), p->proc->GetState());
p = p->next;
void DumpProcessStates(LinkedList<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 = iter.next();
}
}
private:
struct ProcEntry {
Process* proc;
ProcEntry* next;
};
ProcEntry* front_ = nullptr;
};
class Scheduler {
public:
Scheduler() {
Process* root = Process::RootProcess();
runnable_threads_.PushBack(root->GetThread(0));
proc_list_.InsertProcess(Process::RootProcess());
proc_list_.PushBack(Process::RootProcess());
}
void Enable() { enabled_ = true; }
Process& CurrentProcess() { return CurrentThread().process(); }
Thread& CurrentThread() { return *runnable_threads_.PeekFront(); }
void InsertProcess(Process* process) { proc_list_.InsertProcess(process); }
void InsertProcess(Process* process) { proc_list_.PushBack(process); }
void Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
void Yield() {
@ -85,7 +49,7 @@ class Scheduler {
}
if (runnable_threads_.size() == 0) {
proc_list_.DumpProcessStates();
DumpProcessStates(proc_list_);
panic("FIXME: Implement Sleep");
}
@ -109,8 +73,8 @@ class Scheduler {
private:
bool enabled_ = false;
ProcList proc_list_;
// TODO: move this to a separate process manager class.
LinkedList<Process*> proc_list_;
LinkedList<Thread*> runnable_threads_;
};