Cleanup and comments in process and thread code and bonus asserts.

This commit is contained in:
Ali Mashtizadeh 2023-09-06 13:51:47 -04:00
parent a9acaf0489
commit a58001cd9e
5 changed files with 97 additions and 2 deletions

View File

@ -103,6 +103,8 @@ void Process_Retain(Process *proc);
void Process_Release(Process *proc);
uint64_t Process_Wait(Process *proc, uint64_t pid);
#define TID_ANY 0xFFFFFFFF
// Thread functions
Thread *Thread_Create(Process *proc);
Thread *Thread_KThreadCreate(void (*f)(void*), void *arg);

View File

@ -41,6 +41,9 @@ CV_Destroy(CV *cv)
void
CV_Wait(CV *cv, Mutex *mtx)
{
/* Do not go to sleep holding a spinlock! */
ASSERT(Critical_Level() == 0);
WaitChannel_Lock(&cv->chan);
Mutex_Unlock(mtx);
WaitChannel_Sleep(&cv->chan);

View File

@ -109,6 +109,11 @@ Process_Destroy(Process *proc)
Slab_Free(&processSlab, proc);
}
/*
* Process_Lookup --
*
* Lookup a process by PID and increment its reference count.
*/
Process *
Process_Lookup(uint64_t pid)
{
@ -128,6 +133,11 @@ Process_Lookup(uint64_t pid)
return proc;
}
/*
* Process_Retain --
*
* Increment the reference count for a given process.
*/
void
Process_Retain(Process *proc)
{
@ -135,6 +145,11 @@ Process_Retain(Process *proc)
__sync_fetch_and_add(&proc->refCount, 1);
}
/*
* Process_Release --
*
* Decrement the reference count for a given process.
*/
void
Process_Release(Process *proc)
{
@ -144,7 +159,13 @@ Process_Release(Process *proc)
}
}
// XXX: Cleanup return value
/*
* Process_Wait --
*
* Wait for a process to exit and then cleanup it's references. If the
* pid == 0, we wait for any process, otherwise we wait for a specific
* process.
*/
uint64_t
Process_Wait(Process *proc, uint64_t pid)
{
@ -234,6 +255,10 @@ Debug_Processes(int argc, const char *argv[])
{
Process *proc;
/*
* We don't hold locks in case you the kernel debugger is entered while
* holding this lock.
*/
//Spinlock_Lock(&threadLock);
TAILQ_FOREACH(proc, &processList, processList)

View File

@ -28,6 +28,12 @@ Thread *curProc[MAX_CPUS];
* Scheduler Functions
*/
/*
* Sched_Current() --
*
* Get the currently executing thread. This function retains a reference
* count and you must release the reference by calling Thread_Release.
*/
Thread *
Sched_Current()
{
@ -41,6 +47,12 @@ Sched_Current()
return thr;
}
/*
* Sched_SetRunnable --
*
* Set the thread to the runnable state and move it from the wait queue if
* necessary to the runnable queue.
*/
void
Sched_SetRunnable(Thread *thr)
{
@ -60,6 +72,13 @@ Sched_SetRunnable(Thread *thr)
Spinlock_Unlock(&schedLock);
}
/*
* Sched_SetWaiting --
*
* Set the thread to the waiting state and move it to the wait queue. The
* thread should be currently running.
*
*/
void
Sched_SetWaiting(Thread *thr)
{
@ -74,11 +93,24 @@ Sched_SetWaiting(Thread *thr)
Spinlock_Unlock(&schedLock);
}
/*
* Sched_SetZombie --
*
* Set the thread to the zombie state and move it to the parent
* processes's zombie process queue. The thread should be currently
* running.
*
*/
void
Sched_SetZombie(Thread *thr)
{
Process *proc = thr->proc;
/*
* Do not go to sleep holding spinlocks.
*/
ASSERT(Critical_Level() == 0);
if (proc->threads == 1) {
// All processes have parents except 'init' and 'kernel'
ASSERT(proc->parent != NULL);
@ -108,6 +140,14 @@ Sched_SetZombie(Thread *thr)
Mutex_Unlock(&proc->parent->zombieProcLock);
}
/*
* Sched_Switch --
*
* Switch between threads. During the creation of kernel threads (and by
* proxy user threads) we may not return through this code path and thus
* the kernel thread initialization function must release the scheduler
* lock.
*/
static void
Sched_Switch(Thread *oldthr, Thread *newthr)
{
@ -117,6 +157,11 @@ Sched_Switch(Thread *oldthr, Thread *newthr)
Thread_SwitchArch(oldthr, newthr);
}
/*
* Sched_Scheduler --
*
* Run our round robin scheduler to find the process and switch to it.
*/
void
Sched_Scheduler()
{

View File

@ -211,6 +211,11 @@ Thread_Destroy(Thread *thr)
Slab_Free(&threadSlab, thr);
}
/*
* Thread_Lookup --
*
* Lookup a thread by TID and increment its reference count.
*/
Thread *
Thread_Lookup(Process *proc, uint64_t tid)
{
@ -230,6 +235,11 @@ Thread_Lookup(Process *proc, uint64_t tid)
return thr;
}
/*
* Thread_Retain --
*
* Increment the reference count for a given thread.
*/
void
Thread_Retain(Thread *thr)
{
@ -237,6 +247,11 @@ Thread_Retain(Thread *thr)
__sync_fetch_and_add(&thr->refCount, 1);
}
/*
* Thread_Release --
*
* Decrement the reference count for a given thread.
*/
void
Thread_Release(Thread *thr)
{
@ -246,6 +261,11 @@ Thread_Release(Thread *thr)
}
}
/*
* Thread_Wait --
*
* Wait for any thread (tid == TID_ANY) or a specific thread.
*/
uint64_t
Thread_Wait(Thread *thr, uint64_t tid)
{
@ -254,7 +274,7 @@ Thread_Wait(Thread *thr, uint64_t tid)
ASSERT(thr->proc != NULL);
if (tid == 0) {
if (tid == TID_ANY) {
t = TAILQ_FIRST(&thr->proc->zombieQueue);
if (!t) {
return SYSCALL_PACK(EAGAIN, 0);