X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=00fc0506429a9eef8c98673ab4b3e46eb0379052;hb=95a7eaaa4a1065334125b65130866f8d1048ddb7;hp=27356613be9b79bbf7b0a28034f2c63c0e11c3f5;hpb=35e419b3110ef56ffdc3f16c1fc819874b888ba5;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index 27356613..00fc0506 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -5,29 +5,40 @@ */ #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(); +extern void Proc_Start(); extern tThread *Proc_GetCurThread(); 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); tThread *Threads_int_GetPrev(tThread **List, tThread *Thread); void Threads_Exit(int TID, int Status); void Threads_Kill(tThread *Thread, int Status); void Threads_Yield(); void Threads_Sleep(); void Threads_Wake(tThread *Thread); +void Threads_AddActive(tThread *Thread); int Threads_GetPID(); int Threads_GetTID(); int Threads_GetUID(); @@ -91,9 +102,14 @@ void Threads_Init() cur->ThreadName = "Idle Thread"; Threads_SetTickets(0); // Never called randomly cur->Quantum = 1; // 1 slice quantum - for(;;) __asm__ __volatile__ ("hlt"); // Just yeilds + HALT(); + for(;;) { + HALT(); // Just yeilds + } } #endif + + Proc_Start(); } /** @@ -109,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 @@ -127,12 +155,94 @@ 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 */ int Threads_WaitTID(int TID, int *status) -{ +{ // Any Child if(TID == -1) { @@ -155,16 +265,22 @@ 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) { case THREAD_STAT_ZOMBIE: t->Status = THREAD_STAT_DEAD; - *status = 0; + if(status) *status = 0; + Threads_AddToDelete( t ); break; default: - *status = -1; + if(status) *status = -1; break; } return ret; @@ -203,7 +319,23 @@ tThread *Threads_GetThread(Uint TID) } /** - * \fn tThread *Threads_int_GetPrev(tThread *List, tThread *Thread) + * \fn void Threads_AddToDelete(tThread *Thread) + * \brief Adds a thread to the delete queue + */ +void Threads_AddToDelete(tThread *Thread) +{ + // Add to delete queue + if(gDeleteThreads) { + Thread->Next = gDeleteThreads; + gDeleteThreads = Thread; + } else { + Thread->Next = NULL; + gDeleteThreads = 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) @@ -231,13 +363,18 @@ tThread *Threads_int_GetPrev(tThread **List, tThread *Thread) */ void Threads_Exit(int TID, int Status) { - Threads_Kill( Proc_GetCurThread(), (Uint)Status & 0xFF ); + if( TID == 0 ) + 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) { @@ -252,7 +389,7 @@ void Threads_Kill(tThread *Thread, int Status) child; child = child->Next) { - if(child->PTID == gCurrentThread->TID) + if(child->PTID == Thread->TID) Threads_Kill(child, -1); } } @@ -295,14 +432,7 @@ void Threads_Kill(tThread *Thread, int Status) if(Status == -1) { Thread->Status = THREAD_STAT_DEAD; - // Add to delete queue - if(gDeleteThreads) { - Thread->Next = gDeleteThreads; - gDeleteThreads = Thread; - } else { - Thread->Next = NULL; - gDeleteThreads = Thread; - } + Threads_AddToDelete( Thread ); } else { Thread->Status = THREAD_STAT_ZOMBIE; } @@ -310,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(); } @@ -319,7 +452,7 @@ void Threads_Kill(tThread *Thread, int Status) */ void Threads_Yield() { - Proc_GetCurThread()->Quantum = 0; + Proc_GetCurThread()->Remaining = 0; HALT(); } @@ -332,7 +465,7 @@ void Threads_Sleep() tThread *cur = Proc_GetCurThread(); tThread *thread; - //Log("Proc_Sleep: %i going to sleep", gCurrentThread->TID); + Log("Proc_Sleep: %i going to sleep", cur->TID); // Acquire Spinlock LOCK( &giThreadListLock ); @@ -340,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; } @@ -370,7 +504,7 @@ void Threads_Sleep() // Release Spinlock RELEASE( &giThreadListLock ); - HALT(); + while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } @@ -405,6 +539,22 @@ void Threads_Wake(tThread *Thread) } } +/** + * \fn void Threads_AddActive(tThread *Thread) + * \brief Adds a thread to the active queue + */ +void Threads_AddActive(tThread *Thread) +{ + LOCK( &giThreadListLock ); + Thread->Next = gActiveThreads; + gActiveThreads = Thread; + giNumActiveThreads ++; + giTotalTickets += Thread->NumTickets; + //Log("Threads_AddActive: giNumActiveThreads = %i, giTotalTickets = %i", + // giNumActiveThreads, giTotalTickets); + RELEASE( &giThreadListLock ); +} + #if 0 /** * \fn void Threads_SetSignalHandler(int Num, void *Handler) @@ -419,6 +569,7 @@ void Threads_SetSignalHandler(int Num, void *Handler) /** * \fn void Threads_SendSignal(int TID, int Num) + * \brief Send a signal to a thread */ void Threads_SendSignal(int TID, int Num) { @@ -481,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); } @@ -508,15 +664,20 @@ 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; } + //Log(" Threads_GetNextToRun: giNumActiveThreads=%i,giTotalTickets=%i", + // giNumActiveThreads, giTotalTickets); // Get the ticket number ticket = number = rand() % giTotalTickets; + //Log(" Threads_GetNextToRun: ticket = %i", ticket); + // Find the next thread for(thread=gActiveThreads;thread;thread=thread->Next) { @@ -536,3 +697,14 @@ tThread *Threads_GetNextToRun(int CPU) return thread; } + +/** + * \fn void Threads_SegFault(tVAddr Addr) + * \brief Called when a Segment Fault occurs + */ +void Threads_SegFault(tVAddr Addr) +{ + //Threads_SendSignal( Proc_GetCurThread()->TID, SIGSEGV ); + Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + Threads_Exit( 0, -1 ); +}