X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=00fc0506429a9eef8c98673ab4b3e46eb0379052;hb=5fc81fa5e050f48374a6aff5636f3e60313dfc78;hp=bcea3fa9bbfdf327958995487f099883f523027a;hpb=1a96e0dd77d6922078edd703fc7c2e809b9499b8;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index bcea3fa9..00fc0506 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -5,11 +5,17 @@ */ #include #include +#include // === CONSTANTS === #define DEFAULT_QUANTUM 10 #define DEFAULT_TICKETS 5 #define MAX_TICKETS 10 +const enum eConfigTypes cCONFIG_TYPES[] = { + CFGT_HEAPSTR, // CFG_VFS_CWD + CFGT_INT, // CFG_VFS_MAXFILES + CFGT_NULL +}; // === IMPORTS === extern void ArchThreads_Init(); @@ -20,7 +26,9 @@ extern int Proc_Clone(Uint *Err, Uint Flags); // === PROTOTYPES === void Threads_Init(); void Threads_SetName(char *NewName); +char *Threads_GetName(int ID); void Threads_SetTickets(int Num); +tThread *Threads_CloneTCB(Uint *Err, Uint Flags); int Threads_WaitTID(int TID, int *status); tThread *Threads_GetThread(Uint TID); void Threads_AddToDelete(tThread *Thread); @@ -117,6 +125,18 @@ void Threads_SetName(char *NewName) strcpy(cur->ThreadName, NewName); } +/** + * \fn char *Threads_GetName(int ID) + * \brief Gets a thread's name + */ +char *Threads_GetName(int ID) +{ + if(ID == -1) { + return Proc_GetCurThread()->ThreadName; + } + return NULL; +} + /** * \fn void Threads_SetTickets(int Num) * \brief Sets the 'priority' of a task @@ -135,6 +155,88 @@ void Threads_SetTickets(int Num) RELEASE( &giThreadListLock ); } +/** + * \fn tThread *Threads_CloneTCB(Uint *Err, Uint Flags) + */ +tThread *Threads_CloneTCB(Uint *Err, Uint Flags) +{ + tThread *cur, *new; + int i; + cur = Proc_GetCurThread(); + + new = malloc(sizeof(tThread)); + if(new == NULL) { + *Err = -ENOMEM; + return NULL; + } + + new->Next = NULL; + new->IsLocked = 0; + new->Status = THREAD_STAT_ACTIVE; + new->RetStatus = 0; + + // Get Thread ID + new->TID = giNextTID++; + new->PTID = cur->TID; + + // Clone Name + new->ThreadName = malloc(strlen(cur->ThreadName)+1); + strcpy(new->ThreadName, cur->ThreadName); + + // Set Thread Group ID (PID) + if(Flags & CLONE_VM) + new->TGID = new->TID; + else + new->TGID = cur->TGID; + + // Messages are not inherited + new->Messages = NULL; + new->LastMessage = NULL; + + // Set State + new->Remaining = new->Quantum = cur->Quantum; + new->NumTickets = cur->NumTickets; + + // Set Signal Handlers + new->CurSignal = 0; + if(Flags & CLONE_VM) + memset(new->SignalHandlers, 0, sizeof(new->SignalHandlers)); + else + memcpy(new->SignalHandlers, cur->SignalHandlers, sizeof(new->SignalHandlers)); + memset(&new->SignalState, 0, sizeof(tTaskState)); + + for( i = 0; i < NUM_CFG_ENTRIES; i ++ ) + { + switch(cCONFIG_TYPES[i]) + { + default: + new->Config[i] = cur->Config[i]; + break; + case CFGT_HEAPSTR: + if(cur->Config[i]) + new->Config[i] = (Uint) strdup( (void*)cur->Config[i] ); + else + new->Config[i] = 0; + break; + } + } + + return new; +} + +/** + * \fn Uint *Threads_GetCfgPtr(int Id) + */ +Uint *Threads_GetCfgPtr(int Id) +{ + if(Id < 0 || Id >= NUM_CFG_ENTRIES) { + Warning("Threads_GetCfgPtr: Index %i is out of bounds", Id); + return NULL; + } + + return &Proc_GetCurThread()->Config[Id]; +} + /** * \fn void Threads_WaitTID(int TID, int *status) * \brief Wait for a task to change state @@ -163,7 +265,12 @@ int Threads_WaitTID(int TID, int *status) tThread *t = Threads_GetThread(TID); int initStatus = t->Status; int ret; - while(t->Status == initStatus) Threads_Yield(); + + if(initStatus != THREAD_STAT_ZOMBIE) + while(t->Status == initStatus) { + Threads_Yield(); + } + ret = t->RetStatus; switch(t->Status) { @@ -228,7 +335,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) @@ -260,12 +367,14 @@ void Threads_Exit(int TID, int Status) Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF ); else Threads_Kill( Threads_GetThread(TID), (Uint)Status & 0xFF ); + for(;;) HALT(); // Just in case } /** * \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) { @@ -273,7 +382,7 @@ void Threads_Kill(tThread *Thread, int Status) tMsg *msg; // Kill all children - #if 1 + #if 0 { tThread *child; for(child = gActiveThreads; @@ -331,6 +440,9 @@ void Threads_Kill(tThread *Thread, int Status) // Release spinlocks RELEASE( &Thread->IsLocked ); // Released first so that it IS released RELEASE( &giThreadListLock ); + + //Log("Thread %i went *hurk*", Thread->TID); + if(Status != -1) HALT(); } @@ -361,7 +473,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; } @@ -391,7 +504,7 @@ void Threads_Sleep() // Release Spinlock RELEASE( &giThreadListLock ); - HALT(); + while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } @@ -519,18 +632,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); } @@ -546,9 +664,10 @@ tThread *Threads_GetNextToRun(int CPU) int ticket; int number; + if(giNumActiveThreads == 0) return NULL; + // Special case: 1 thread - if(giNumActiveThreads == 1) - { + if(giNumActiveThreads == 1) { return gActiveThreads; } @@ -586,5 +705,6 @@ tThread *Threads_GetNextToRun(int CPU) void Threads_SegFault(tVAddr Addr) { //Threads_SendSignal( Proc_GetCurThread()->TID, SIGSEGV ); - Threads_Kill( Proc_GetCurThread(), 0 ); + Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + Threads_Exit( 0, -1 ); }