diff --git a/zion/lib/linked_list.h b/zion/lib/linked_list.h index 9377913..0d57792 100644 --- a/zion/lib/linked_list.h +++ b/zion/lib/linked_list.h @@ -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; }; diff --git a/zion/scheduler/scheduler.cpp b/zion/scheduler/scheduler.cpp index 41e19b6..4cf6a27 100644 --- a/zion/scheduler/scheduler.cpp +++ b/zion/scheduler/scheduler.cpp @@ -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(LinkedList& 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(); } - - void DumpProcessStates() { - ProcEntry* p = front_; - dbgln("Process States:"); - while (p != nullptr) { - dbgln("%u: %u", p->proc->id(), p->proc->GetState()); - p = p->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 proc_list_; LinkedList runnable_threads_; };