Move process list to a linked list.
Add basic iteration ability to the linked list.
This commit is contained in:
parent
71196dc90f
commit
5cab9f843e
|
@ -11,7 +11,7 @@ class LinkedList {
|
||||||
|
|
||||||
LinkedList(const LinkedList&) = delete;
|
LinkedList(const LinkedList&) = delete;
|
||||||
|
|
||||||
uint64_t size() { return size_; }
|
uint64_t size() const { return size_; }
|
||||||
|
|
||||||
void PushBack(const T& item) {
|
void PushBack(const T& item) {
|
||||||
size_++;
|
size_++;
|
||||||
|
@ -65,15 +65,32 @@ class LinkedList {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
T PeekFront() { return front_->item; }
|
T PeekFront() const { return front_->item; }
|
||||||
|
|
||||||
private:
|
|
||||||
uint64_t size_ = 0;
|
|
||||||
|
|
||||||
struct ListItem {
|
struct ListItem {
|
||||||
T item;
|
T item;
|
||||||
ListItem* next;
|
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;
|
ListItem* front_ = nullptr;
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,64 +8,28 @@ namespace {
|
||||||
|
|
||||||
extern "C" void context_switch(uint64_t* current_esp, uint64_t* next_esp);
|
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
|
void DumpProcessStates(LinkedList<Process*>& proc_list) {
|
||||||
// map.
|
dbgln("Process States: %u", proc_list.size());
|
||||||
class ProcList {
|
auto iter = proc_list.begin();
|
||||||
public:
|
while (iter != proc_list.end()) {
|
||||||
ProcList() {}
|
dbgln("%u: %u", iter->id(), iter->GetState());
|
||||||
|
iter = iter.next();
|
||||||
// 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
struct ProcEntry {
|
|
||||||
Process* proc;
|
|
||||||
ProcEntry* next;
|
|
||||||
};
|
|
||||||
|
|
||||||
ProcEntry* front_ = nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Scheduler {
|
class Scheduler {
|
||||||
public:
|
public:
|
||||||
Scheduler() {
|
Scheduler() {
|
||||||
Process* root = Process::RootProcess();
|
Process* root = Process::RootProcess();
|
||||||
runnable_threads_.PushBack(root->GetThread(0));
|
runnable_threads_.PushBack(root->GetThread(0));
|
||||||
proc_list_.InsertProcess(Process::RootProcess());
|
proc_list_.PushBack(Process::RootProcess());
|
||||||
}
|
}
|
||||||
void Enable() { enabled_ = true; }
|
void Enable() { enabled_ = true; }
|
||||||
|
|
||||||
Process& CurrentProcess() { return CurrentThread().process(); }
|
Process& CurrentProcess() { return CurrentThread().process(); }
|
||||||
Thread& CurrentThread() { return *runnable_threads_.PeekFront(); }
|
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 Enqueue(Thread* thread) { runnable_threads_.PushBack(thread); }
|
||||||
|
|
||||||
void Yield() {
|
void Yield() {
|
||||||
|
@ -85,7 +49,7 @@ class Scheduler {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (runnable_threads_.size() == 0) {
|
if (runnable_threads_.size() == 0) {
|
||||||
proc_list_.DumpProcessStates();
|
DumpProcessStates(proc_list_);
|
||||||
panic("FIXME: Implement Sleep");
|
panic("FIXME: Implement Sleep");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,8 +73,8 @@ class Scheduler {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool enabled_ = false;
|
bool enabled_ = false;
|
||||||
ProcList proc_list_;
|
// TODO: move this to a separate process manager class.
|
||||||
|
LinkedList<Process*> proc_list_;
|
||||||
LinkedList<Thread*> runnable_threads_;
|
LinkedList<Thread*> runnable_threads_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue