X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=96a38dacda3fc12160fb4657a9a73c615a54ce87;hb=0e9730abc6c9ba710a3f71356720a70d79e407ab;hp=dd7cfcd243f7e3f5ccfe87676d293e3069650988;hpb=d1f16adf5f2e94e836ea6658186a6ff6d94f54d8;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index dd7cfcd2..96a38dac 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -3,7 +3,7 @@ * threads.c * - Common Thread Control */ -#include +#include #include #include @@ -25,7 +25,7 @@ extern int Proc_Clone(Uint *Err, Uint Flags); // === PROTOTYPES === void Threads_Init(); -void Threads_SetName(char *NewName); + int Threads_SetName(char *NewName); char *Threads_GetName(int ID); void Threads_SetTickets(int Num); tThread *Threads_CloneTCB(Uint *Err, Uint Flags); @@ -41,8 +41,10 @@ void Threads_Wake(tThread *Thread); void Threads_AddActive(tThread *Thread); int Threads_GetPID(); int Threads_GetTID(); - int Threads_GetUID(); - int Threads_GetGID(); +tUID Threads_GetUID(); + int Threads_SetUID(Uint *Errno, tUID ID); +tGID Threads_GetGID(); + int Threads_SetGID(Uint *Errno, tUID ID); void Threads_Dump(); // === GLOBALS === @@ -116,13 +118,14 @@ void Threads_Init() * \fn void Threads_SetName(char *NewName) * \brief Sets the current thread's name */ -void Threads_SetName(char *NewName) +int Threads_SetName(char *NewName) { tThread *cur = Proc_GetCurThread(); if( IsHeap(cur->ThreadName) ) free( cur->ThreadName ); cur->ThreadName = malloc(strlen(NewName)+1); strcpy(cur->ThreadName, NewName); + return 0; } /** @@ -335,7 +338,7 @@ void Threads_AddToDelete(tThread *Thread) } /** - * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread) + * \fn tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) * \brief Gets the previous entry in a thead linked list */ tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) @@ -373,7 +376,8 @@ void Threads_Exit(int TID, int Status) /** * \fn void Threads_Kill(tThread *Thread, int Status) * \brief Kill a thread - * \param TID Thread ID (0 for current) + * \param Thread Thread to kill + * \param Status Status code to return to the parent */ void Threads_Kill(tThread *Thread, int Status) { @@ -464,7 +468,7 @@ void Threads_Sleep() tThread *cur = Proc_GetCurThread(); tThread *thread; - //Log("Proc_Sleep: %i going to sleep", cur->TID); + //Log_Log("Threads", "%i going to sleep", cur->TID); // Acquire Spinlock LOCK( &giThreadListLock ); @@ -472,7 +476,8 @@ void Threads_Sleep() // Get thread before current thread thread = Threads_int_GetPrev( &gActiveThreads, cur ); if(!thread) { - Warning("Proc_Sleep - Current thread is not on the active queue"); + Warning("Threads_Sleep - Current thread is not on the active queue"); + Threads_Dump(); return; } @@ -502,11 +507,11 @@ void Threads_Sleep() // Release Spinlock RELEASE( &giThreadListLock ); - HALT(); + while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } -/**c0108919: +/** * \fn void Threads_Wake( tThread *Thread ) * \brief Wakes a sleeping/waiting thread up */ @@ -517,11 +522,14 @@ void Threads_Wake(tThread *Thread) { case THREAD_STAT_ACTIVE: break; case THREAD_STAT_SLEEPING: + //Log_Log("Threads", "Waking %i (%p) from sleeping", Thread->TID, Thread); LOCK( &giThreadListLock ); prev = Threads_int_GetPrev(&gSleepingThreads, Thread); prev->Next = Thread->Next; // Remove from sleeping queue Thread->Next = gActiveThreads; // Add to active queue gActiveThreads = Thread; + giNumActiveThreads ++; + giTotalTickets += Thread->NumTickets; Thread->Status = THREAD_STAT_ACTIVE; RELEASE( &giThreadListLock ); break; @@ -537,6 +545,11 @@ void Threads_Wake(tThread *Thread) } } +void Threads_WakeTID(tTID Thread) +{ + Threads_Wake( Threads_GetThread(Thread) ); +} + /** * \fn void Threads_AddActive(tThread *Thread) * \brief Adds a thread to the active queue @@ -606,23 +619,47 @@ void Threads_SendSignal(int TID, int Num) #endif // --- Process Structure Access Functions --- -int Threads_GetPID() +tPID Threads_GetPID() { return Proc_GetCurThread()->TGID; } -int Threads_GetTID() +tTID Threads_GetTID() { return Proc_GetCurThread()->TID; } -int Threads_GetUID() +tUID Threads_GetUID() { return Proc_GetCurThread()->UID; } -int Threads_GetGID() +tGID Threads_GetGID() { return Proc_GetCurThread()->GID; } +int Threads_SetUID(Uint *Errno, tUID ID) +{ + tThread *t = Proc_GetCurThread(); + if( t->UID != 0 ) { + *Errno = -EACCES; + return -1; + } + Log("Threads_SetUID - Setting User ID to %i", ID); + t->UID = ID; + return 0; +} + +int Threads_SetGID(Uint *Errno, tGID ID) +{ + tThread *t = Proc_GetCurThread(); + if( t->UID != 0 ) { + *Errno = -EACCES; + return -1; + } + Log("Threads_SetGID - Setting Group ID to %i", ID); + t->GID = ID; + return 0; +} + /** * \fn void Threads_Dump() * \brief Dums a list of currently running threads @@ -630,18 +667,23 @@ int Threads_GetGID() void Threads_Dump() { tThread *thread; + tThread *cur = Proc_GetCurThread(); Log("Active Threads:"); for(thread=gActiveThreads;thread;thread=thread->Next) { - Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName); + Log("%c%i (%i) - %s", + (thread==cur?'*':' '), + thread->TID, thread->TGID, thread->ThreadName); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } Log("Sleeping Threads:"); for(thread=gSleepingThreads;thread;thread=thread->Next) { - Log(" %i (%i) - %s", thread->TID, thread->TGID, thread->ThreadName); + Log("%c%i (%i) - %s", + (thread==cur?'*':' '), + thread->TID, thread->TGID, thread->ThreadName); Log(" %i Tickets, Quantum %i", thread->NumTickets, thread->Quantum); Log(" KStack 0x%x", thread->KernelStack); } @@ -657,10 +699,15 @@ tThread *Threads_GetNextToRun(int CPU) int ticket; int number; - if(giNumActiveThreads == 0) return NULL; + if(giNumActiveThreads == 0) { + //Log_Debug("Threads", "CPU%i has no threads to run", CPU); + return NULL; + } // Special case: 1 thread if(giNumActiveThreads == 1) { + //Log_Debug("Threads", "CPU%i has only one thread %i %s", + // CPU, gActiveThreads->TID, gActiveThreads->ThreadName); return gActiveThreads; } @@ -688,6 +735,9 @@ tThread *Threads_GetNextToRun(int CPU) giTotalTickets, number); } + //Log_Debug("Threads", "Switching CPU%i to %p (%s)", + // CPU, thread, thread->ThreadName); + return thread; } @@ -701,3 +751,6 @@ void Threads_SegFault(tVAddr Addr) Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); Threads_Exit( 0, -1 ); } + +// === EXPORTS === +EXPORT(Threads_GetUID);