Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Revise PID and TID generation to align with standards
Browse files Browse the repository at this point in the history
  • Loading branch information
EnderIce2 committed Mar 1, 2024
1 parent 525a102 commit d3a1646
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 20 deletions.
12 changes: 10 additions & 2 deletions core/driver/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -913,6 +920,7 @@ namespace Driver

api->CreateKernelProcess = CreateKernelProcess;
api->CreateKernelThread = CreateKernelThread;
api->GetCurrentProcess = GetCurrentProcess;
api->KillProcess = KillProcess;
api->KillThread = KillThread;
api->Yield = Yield;
Expand Down
3 changes: 2 additions & 1 deletion driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down Expand Up @@ -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<PCB *> &GetProcessList() final;
void StartIdleProcess() final;
void StartScheduler() final;
Expand Down
3 changes: 1 addition & 2 deletions include/task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,6 @@ namespace Tasking
NewLock(TaskingLock);

PID NextPID = 0;
TID NextTID = 0;

PCB *KernelProcess = nullptr;

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions kernel_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion syscalls/linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 4 additions & 7 deletions tasking/scheduler/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions tasking/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PCB *> Task::GetProcessList()
Expand Down
2 changes: 1 addition & 1 deletion tasking/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d3a1646

Please sign in to comment.