2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/process.h"
|
|
|
|
|
|
|
|
#include "debug/debug.h"
|
2023-05-30 23:55:42 -07:00
|
|
|
#include "include/cap_types.h"
|
2023-05-18 13:56:54 -07:00
|
|
|
#include "memory/paging_util.h"
|
|
|
|
#include "memory/physical_memory.h"
|
2023-05-18 13:28:22 -07:00
|
|
|
#include "scheduler/scheduler.h"
|
2023-05-18 12:43:53 -07:00
|
|
|
#include "scheduler/thread.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static uint64_t gNextId = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:50:38 -07:00
|
|
|
SharedPtr<Process> Process::RootProcess() {
|
2023-05-30 21:39:19 -07:00
|
|
|
SharedPtr<Process> proc(new Process(0));
|
2023-05-30 21:27:20 -07:00
|
|
|
proc->threads_.PushBack(Thread::RootThread(*proc));
|
2023-05-18 12:43:53 -07:00
|
|
|
proc->next_thread_id_ = 1;
|
|
|
|
|
|
|
|
return proc;
|
|
|
|
}
|
|
|
|
|
2023-05-30 21:39:19 -07:00
|
|
|
Process::Process() : id_(gNextId++), state_(RUNNING) {}
|
2023-05-18 13:56:54 -07:00
|
|
|
|
2023-05-30 01:27:47 -07:00
|
|
|
void Process::CreateThread(uint64_t entry) {
|
2023-05-30 21:27:20 -07:00
|
|
|
Thread* thread = new Thread(*this, next_thread_id_++, entry);
|
2023-05-29 15:08:02 -07:00
|
|
|
threads_.PushBack(thread);
|
2023-05-30 23:55:42 -07:00
|
|
|
caps_.PushBack(new Capability(this, Capability::PROCESS, next_cap_id_++,
|
|
|
|
ZC_PROC_SPAWN_CHILD));
|
2023-05-29 23:48:32 -07:00
|
|
|
gScheduler->Enqueue(thread);
|
2023-05-18 13:24:02 -07:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:50:38 -07:00
|
|
|
SharedPtr<Thread> Process::GetThread(uint64_t tid) {
|
2023-05-29 15:08:02 -07:00
|
|
|
auto iter = threads_.begin();
|
|
|
|
while (iter != threads_.end()) {
|
|
|
|
if (iter->tid() == tid) {
|
|
|
|
return *iter;
|
2023-05-18 12:43:53 -07:00
|
|
|
}
|
2023-05-29 15:08:02 -07:00
|
|
|
++iter;
|
2023-05-18 12:43:53 -07:00
|
|
|
}
|
|
|
|
panic("Bad thread access.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2023-05-29 13:51:00 -07:00
|
|
|
|
|
|
|
void Process::CheckState() {
|
2023-05-29 15:08:02 -07:00
|
|
|
auto iter = threads_.begin();
|
|
|
|
while (iter != threads_.end()) {
|
|
|
|
if (iter->GetState() != Thread::FINISHED) {
|
2023-05-29 13:51:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2023-05-29 15:08:02 -07:00
|
|
|
++iter;
|
2023-05-29 13:51:00 -07:00
|
|
|
}
|
|
|
|
state_ = FINISHED;
|
|
|
|
}
|
2023-05-30 23:55:42 -07:00
|
|
|
|
|
|
|
SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
|
|
|
|
auto iter = caps_.begin();
|
|
|
|
while (iter != caps_.end()) {
|
|
|
|
if (iter->id() == cid) {
|
|
|
|
return *iter;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
dbgln("Bad cap access");
|
|
|
|
return {};
|
|
|
|
}
|