Move Process to storing Threads in a linked list.

This should really be a vector or hashmap of some sort but this is fine
for now.
This commit is contained in:
Drew Galbraith 2023-05-29 15:08:02 -07:00
parent 5cab9f843e
commit 9f3ffbf5b4
4 changed files with 19 additions and 34 deletions

View File

@ -76,6 +76,10 @@ class LinkedList {
Iterator(ListItem* item) : item_(item) {} Iterator(ListItem* item) : item_(item) {}
Iterator next() { return {item_->next}; } Iterator next() { return {item_->next}; }
Iterator& operator++() {
item_ = item_->next;
return *this;
}
T& operator*() { return item_->item; } T& operator*() { return item_->item; }
T& operator->() { return item_->item; } T& operator->() { return item_->item; }

View File

@ -16,10 +16,7 @@ Process* Process::RootProcess() {
uint64_t pml4_addr = 0; uint64_t pml4_addr = 0;
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr)); asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
Process* proc = new Process(0, pml4_addr); Process* proc = new Process(0, pml4_addr);
proc->thread_list_front_ = new ThreadEntry{ proc->threads_.PushBack(Thread::RootThread(proc));
.thread = Thread::RootThread(proc),
.next = nullptr,
};
proc->next_thread_id_ = 1; proc->next_thread_id_ = 1;
return proc; return proc;
@ -33,42 +30,29 @@ Process::Process(uint64_t elf_ptr) : id_(gNextId++), state_(RUNNING) {
void Process::CreateThread(uint64_t elf_ptr) { void Process::CreateThread(uint64_t elf_ptr) {
Thread* thread = new Thread(this, next_thread_id_++, elf_ptr); Thread* thread = new Thread(this, next_thread_id_++, elf_ptr);
ThreadEntry* tentry = new ThreadEntry{ threads_.PushBack(thread);
.thread = thread,
.next = nullptr,
};
if (thread_list_front_ == nullptr) {
thread_list_front_ = tentry;
} else {
ThreadEntry* entry = thread_list_front_;
while (entry->next != nullptr) {
entry = entry->next;
}
entry->next = tentry;
}
sched::EnqueueThread(thread); sched::EnqueueThread(thread);
} }
Thread* Process::GetThread(uint64_t tid) { Thread* Process::GetThread(uint64_t tid) {
ThreadEntry* entry = thread_list_front_; auto iter = threads_.begin();
while (entry != nullptr) { while (iter != threads_.end()) {
if (entry->thread->tid() == tid) { if (iter->tid() == tid) {
return entry->thread; return *iter;
} }
++iter;
} }
panic("Bad thread access."); panic("Bad thread access.");
return nullptr; return nullptr;
} }
void Process::CheckState() { void Process::CheckState() {
ThreadEntry* entry = thread_list_front_; auto iter = threads_.begin();
while (iter != threads_.end()) {
while (entry != nullptr) { if (iter->GetState() != Thread::FINISHED) {
if (entry->thread->GetState() != Thread::FINISHED) {
return; return;
} }
entry = entry->next; ++iter;
} }
state_ = FINISHED; state_ = FINISHED;
} }

View File

@ -2,6 +2,8 @@
#include <stdint.h> #include <stdint.h>
#include "lib/linked_list.h"
// Forward decl due to cyclic dependency. // Forward decl due to cyclic dependency.
class Thread; class Thread;
@ -37,10 +39,5 @@ class Process {
uint64_t next_thread_id_ = 0; uint64_t next_thread_id_ = 0;
// FIXME: Make a better data structure for this. LinkedList<Thread*> threads_;
struct ThreadEntry {
Thread* thread;
ThreadEntry* next;
};
ThreadEntry* thread_list_front_;
}; };

View File

@ -13,7 +13,7 @@ void DumpProcessStates(LinkedList<Process*>& proc_list) {
auto iter = proc_list.begin(); auto iter = proc_list.begin();
while (iter != proc_list.end()) { while (iter != proc_list.end()) {
dbgln("%u: %u", iter->id(), iter->GetState()); dbgln("%u: %u", iter->id(), iter->GetState());
iter = iter.next(); ++iter;
} }
} }