Revise PID and TID generation to align with standards

This commit is contained in:
EnderIce2 2024-03-02 00:51:06 +02:00
parent 525a102f20
commit d3a16469ad
Signed by untrusted user who does not match committer: enderice2
GPG Key ID: EACC3AD603BAB4DD
9 changed files with 27 additions and 20 deletions

View File

@ -501,6 +501,13 @@ namespace Driver
return tcb->ID; 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) int KillProcess(dev_t MajorID, pid_t pId, int ExitCode)
{ {
dbg_api("%d, %d, %d", MajorID, pId, ExitCode); dbg_api("%d, %d, %d", MajorID, pId, ExitCode);
@ -512,11 +519,11 @@ namespace Driver
return 0; 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); 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) if (!tcb)
return -EINVAL; return -EINVAL;
TaskManager->KillThread(tcb, (Tasking::KillCode)ExitCode); TaskManager->KillThread(tcb, (Tasking::KillCode)ExitCode);
@ -913,6 +920,7 @@ namespace Driver
api->CreateKernelProcess = CreateKernelProcess; api->CreateKernelProcess = CreateKernelProcess;
api->CreateKernelThread = CreateKernelThread; api->CreateKernelThread = CreateKernelThread;
api->GetCurrentProcess = GetCurrentProcess;
api->KillProcess = KillProcess; api->KillProcess = KillProcess;
api->KillThread = KillThread; api->KillThread = KillThread;
api->Yield = Yield; api->Yield = Yield;

View File

@ -298,8 +298,9 @@ typedef struct
/* Scheduling */ /* Scheduling */
pid_t (*CreateKernelProcess)(dev_t MajorID, const char *Name); 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 (*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 (*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 (*Yield)(dev_t MajorID);
void (*Sleep)(dev_t MajorID, uint64_t Milliseconds); void (*Sleep)(dev_t MajorID, uint64_t Milliseconds);

View File

@ -58,7 +58,7 @@ namespace Tasking::Scheduler
assert(!"GetProcessByID not implemented"); assert(!"GetProcessByID not implemented");
} }
virtual TCB *GetThreadByID(TID ID) virtual TCB *GetThreadByID(TID ID, PCB* Parent)
{ {
assert(!"GetThreadByID not implemented"); assert(!"GetThreadByID not implemented");
} }
@ -119,7 +119,7 @@ namespace Tasking::Scheduler
bool RemoveThread(TCB *tcb) final; bool RemoveThread(TCB *tcb) final;
bool RemoveProcess(PCB *pcb) final; bool RemoveProcess(PCB *pcb) final;
PCB *GetProcessByID(TID ID) final; PCB *GetProcessByID(TID ID) final;
TCB *GetThreadByID(TID ID) final; TCB *GetThreadByID(TID ID, PCB* Parent) final;
std::list<PCB *> &GetProcessList() final; std::list<PCB *> &GetProcessList() final;
void StartIdleProcess() final; void StartIdleProcess() final;
void StartScheduler() final; void StartScheduler() final;

View File

@ -474,7 +474,6 @@ namespace Tasking
NewLock(TaskingLock); NewLock(TaskingLock);
PID NextPID = 0; PID NextPID = 0;
TID NextTID = 0;
PCB *KernelProcess = nullptr; PCB *KernelProcess = nullptr;
@ -546,7 +545,7 @@ namespace Tasking
PCB *GetProcessByID(PID ID); PCB *GetProcessByID(PID ID);
TCB *GetThreadByID(TID ID); TCB *GetThreadByID(TID ID, PCB* Parent);
/** Wait for process to terminate */ /** Wait for process to terminate */
void WaitForProcess(PCB *pcb); void WaitForProcess(PCB *pcb);

View File

@ -114,7 +114,8 @@ void KernelMainThread()
KPrint("Executing %s", Config.InitPath); KPrint("Executing %s", Config.InitPath);
int ExitCode = -1; int ExitCode = -1;
Tasking::TCB *initThread = nullptr; Tasking::TCB *initThread;
Tasking::PCB *initProc;
int tid = SpawnInit(); int tid = SpawnInit();
if (tid < 0) if (tid < 0)
{ {
@ -127,7 +128,8 @@ void KernelMainThread()
Config.InitPath); Config.InitPath);
thisThread->SetPriority(Tasking::Idle); thisThread->SetPriority(Tasking::Idle);
initThread = TaskManager->GetThreadByID(tid); initProc = TaskManager->GetProcessByID(tid);
initThread = TaskManager->GetThreadByID(tid, initProc);
TaskManager->WaitForThread(initThread); TaskManager->WaitForThread(initThread);
ExitCode = initThread->GetExitCode(); ExitCode = initThread->GetExitCode();
Exit: Exit:

View File

@ -1781,7 +1781,7 @@ static __noreturn void linux_exit_group(SysFrm *sf, int status)
/* https://man7.org/linux/man-pages/man2/tgkill.2.html */ /* https://man7.org/linux/man-pages/man2/tgkill.2.html */
static int linux_tgkill(SysFrm *sf, pid_t tgid, pid_t tid, int sig) 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) if (!target)
return -ESRCH; return -ESRCH;

View File

@ -155,15 +155,12 @@ namespace Tasking::Scheduler
return nullptr; 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; return nullptr;
} }

View File

@ -62,9 +62,9 @@ namespace Tasking
return ((Scheduler::Base *)Scheduler)->GetProcessByID(ID); 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<PCB *> Task::GetProcessList() std::list<PCB *> Task::GetProcessList()

View File

@ -426,7 +426,7 @@ namespace Tasking
this->Parent = Parent; this->Parent = Parent;
this->ctx = ctx; this->ctx = ctx;
this->ID = ctx->NextTID++; this->ID = (TID)this->Parent->ID + (TID)this->Parent->Threads.size();
if (this->Name) if (this->Name)
delete[] this->Name; delete[] this->Name;