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 next() { return {item_->next}; }
Iterator& operator++() {
item_ = item_->next;
return *this;
}
T& operator*() { return item_->item; }
T& operator->() { return item_->item; }

View File

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

View File

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

View File

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