diff --git a/zion/lib/linked_list.h b/zion/lib/linked_list.h index 0d57792..40f3b64 100644 --- a/zion/lib/linked_list.h +++ b/zion/lib/linked_list.h @@ -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; } diff --git a/zion/scheduler/process.cpp b/zion/scheduler/process.cpp index 43bd613..ac80b61 100644 --- a/zion/scheduler/process.cpp +++ b/zion/scheduler/process.cpp @@ -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; } diff --git a/zion/scheduler/process.h b/zion/scheduler/process.h index 1e7d38f..b5f19d5 100644 --- a/zion/scheduler/process.h +++ b/zion/scheduler/process.h @@ -2,6 +2,8 @@ #include +#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 threads_; }; diff --git a/zion/scheduler/scheduler.cpp b/zion/scheduler/scheduler.cpp index 4cf6a27..121e378 100644 --- a/zion/scheduler/scheduler.cpp +++ b/zion/scheduler/scheduler.cpp @@ -13,7 +13,7 @@ void DumpProcessStates(LinkedList& proc_list) { auto iter = proc_list.begin(); while (iter != proc_list.end()) { dbgln("%u: %u", iter->id(), iter->GetState()); - iter = iter.next(); + ++iter; } }