From d3a16469adcba806e448f8a806bfecee15281b94 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sat, 2 Mar 2024 00:51:06 +0200 Subject: [PATCH] Revise PID and TID generation to align with standards --- core/driver/api.cpp | 12 ++++++++++-- driver.h | 3 ++- include/scheduler.hpp | 4 ++-- include/task.hpp | 3 +-- kernel_thread.cpp | 6 ++++-- syscalls/linux.cpp | 2 +- tasking/scheduler/custom.cpp | 11 ++++------- tasking/task.cpp | 4 ++-- tasking/thread.cpp | 2 +- 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/core/driver/api.cpp b/core/driver/api.cpp index 13fb760..71b475e 100644 --- a/core/driver/api.cpp +++ b/core/driver/api.cpp @@ -501,6 +501,13 @@ namespace Driver return tcb->ID; } + pid_t GetCurrentProcess(dev_t MajorID) + { + dbg_api("%d", MajorID); + + return TaskManager->GetCurrentProcess()->ID; + } + int KillProcess(dev_t MajorID, pid_t pId, int ExitCode) { dbg_api("%d, %d, %d", MajorID, pId, ExitCode); @@ -512,11 +519,11 @@ namespace Driver return 0; } - int KillThread(dev_t MajorID, pid_t tId, int ExitCode) + int KillThread(dev_t MajorID, pid_t tId, pid_t pId, int ExitCode) { dbg_api("%d, %d, %d", MajorID, tId, ExitCode); - Tasking::TCB *tcb = TaskManager->GetThreadByID(tId); + Tasking::TCB *tcb = TaskManager->GetThreadByID(tId, TaskManager->GetProcessByID(pId)); if (!tcb) return -EINVAL; TaskManager->KillThread(tcb, (Tasking::KillCode)ExitCode); @@ -913,6 +920,7 @@ namespace Driver api->CreateKernelProcess = CreateKernelProcess; api->CreateKernelThread = CreateKernelThread; + api->GetCurrentProcess = GetCurrentProcess; api->KillProcess = KillProcess; api->KillThread = KillThread; api->Yield = Yield; diff --git a/driver.h b/driver.h index b072c0c..ad5c26d 100644 --- a/driver.h +++ b/driver.h @@ -298,8 +298,9 @@ typedef struct /* Scheduling */ pid_t (*CreateKernelProcess)(dev_t MajorID, const char *Name); pid_t (*CreateKernelThread)(dev_t MajorID, pid_t pId, const char *Name, void *EntryPoint, void *Argument); + pid_t (*GetCurrentProcess)(dev_t MajorID); int (*KillProcess)(dev_t MajorID, pid_t pId, int ExitCode); - int (*KillThread)(dev_t MajorID, pid_t tId, int ExitCode); + int (*KillThread)(dev_t MajorID, pid_t tId, pid_t pId, int ExitCode); void (*Yield)(dev_t MajorID); void (*Sleep)(dev_t MajorID, uint64_t Milliseconds); diff --git a/include/scheduler.hpp b/include/scheduler.hpp index 9025589..4c83c83 100644 --- a/include/scheduler.hpp +++ b/include/scheduler.hpp @@ -58,7 +58,7 @@ namespace Tasking::Scheduler assert(!"GetProcessByID not implemented"); } - virtual TCB *GetThreadByID(TID ID) + virtual TCB *GetThreadByID(TID ID, PCB* Parent) { assert(!"GetThreadByID not implemented"); } @@ -119,7 +119,7 @@ namespace Tasking::Scheduler bool RemoveThread(TCB *tcb) final; bool RemoveProcess(PCB *pcb) final; PCB *GetProcessByID(TID ID) final; - TCB *GetThreadByID(TID ID) final; + TCB *GetThreadByID(TID ID, PCB* Parent) final; std::list &GetProcessList() final; void StartIdleProcess() final; void StartScheduler() final; diff --git a/include/task.hpp b/include/task.hpp index d6835d6..cb46030 100644 --- a/include/task.hpp +++ b/include/task.hpp @@ -474,7 +474,6 @@ namespace Tasking NewLock(TaskingLock); PID NextPID = 0; - TID NextTID = 0; PCB *KernelProcess = nullptr; @@ -546,7 +545,7 @@ namespace Tasking PCB *GetProcessByID(PID ID); - TCB *GetThreadByID(TID ID); + TCB *GetThreadByID(TID ID, PCB* Parent); /** Wait for process to terminate */ void WaitForProcess(PCB *pcb); diff --git a/kernel_thread.cpp b/kernel_thread.cpp index 047c7b0..61514c8 100644 --- a/kernel_thread.cpp +++ b/kernel_thread.cpp @@ -114,7 +114,8 @@ void KernelMainThread() KPrint("Executing %s", Config.InitPath); int ExitCode = -1; - Tasking::TCB *initThread = nullptr; + Tasking::TCB *initThread; + Tasking::PCB *initProc; int tid = SpawnInit(); if (tid < 0) { @@ -127,7 +128,8 @@ void KernelMainThread() Config.InitPath); thisThread->SetPriority(Tasking::Idle); - initThread = TaskManager->GetThreadByID(tid); + initProc = TaskManager->GetProcessByID(tid); + initThread = TaskManager->GetThreadByID(tid, initProc); TaskManager->WaitForThread(initThread); ExitCode = initThread->GetExitCode(); Exit: diff --git a/syscalls/linux.cpp b/syscalls/linux.cpp index 14311c0..3959a92 100644 --- a/syscalls/linux.cpp +++ b/syscalls/linux.cpp @@ -1781,7 +1781,7 @@ static __noreturn void linux_exit_group(SysFrm *sf, int status) /* https://man7.org/linux/man-pages/man2/tgkill.2.html */ static int linux_tgkill(SysFrm *sf, pid_t tgid, pid_t tid, int sig) { - Tasking::TCB *target = thisProcess->GetContext()->GetThreadByID(tid); + Tasking::TCB *target = thisProcess->GetContext()->GetThreadByID(tid, thisProcess); if (!target) return -ESRCH; diff --git a/tasking/scheduler/custom.cpp b/tasking/scheduler/custom.cpp index 286d570..17360c6 100644 --- a/tasking/scheduler/custom.cpp +++ b/tasking/scheduler/custom.cpp @@ -155,15 +155,12 @@ namespace Tasking::Scheduler return nullptr; } - TCB *Custom::GetThreadByID(TID ID) + TCB *Custom::GetThreadByID(TID ID, PCB *Parent) { - foreach (auto p in ProcessList) + foreach (auto t in Parent->Threads) { - foreach (auto t in p->Threads) - { - if (t->ID == ID) - return t; - } + if (t->ID == ID) + return t; } return nullptr; } diff --git a/tasking/task.cpp b/tasking/task.cpp index 808d242..b6ca133 100644 --- a/tasking/task.cpp +++ b/tasking/task.cpp @@ -62,9 +62,9 @@ namespace Tasking return ((Scheduler::Base *)Scheduler)->GetProcessByID(ID); } - TCB *Task::GetThreadByID(TID ID) + TCB *Task::GetThreadByID(TID ID, PCB* Parent) { - return ((Scheduler::Base *)Scheduler)->GetThreadByID(ID); + return ((Scheduler::Base *)Scheduler)->GetThreadByID(ID, Parent); } std::list Task::GetProcessList() diff --git a/tasking/thread.cpp b/tasking/thread.cpp index 10e0ea7..aa46f7f 100644 --- a/tasking/thread.cpp +++ b/tasking/thread.cpp @@ -426,7 +426,7 @@ namespace Tasking this->Parent = Parent; this->ctx = ctx; - this->ID = ctx->NextTID++; + this->ID = (TID)this->Parent->ID + (TID)this->Parent->Threads.size(); if (this->Name) delete[] this->Name;