X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=3be978928545eb90c14263daaedb788b5d70ae21;hb=b6c3b3cf61caafbd91bbf3acc81995e472656a5b;hp=c89be3f4ab9333e9d707e0d8296545a8f272cdb9;hpb=5b5dc0127fe0d498037410e87d0940d65b1bed0d;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index c89be3f4..3be97892 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); @@ -30,6 +38,7 @@ 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(); @@ -93,10 +102,9 @@ void Threads_Init() cur->ThreadName = "Idle Thread"; Threads_SetTickets(0); // Never called randomly cur->Quantum = 1; // 1 slice quantum + HALT(); for(;;) { - Log("---- Idle"); - Threads_Dump(); - __asm__ __volatile__ ("hlt"); // Just yeilds + HALT(); // Just yeilds } } #endif @@ -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,14 +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) -{ - Threads_Dump(); - +{ // Any Child if(TID == -1) { @@ -165,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) { @@ -258,7 +363,11 @@ 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 } /** @@ -279,7 +388,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); } } @@ -330,6 +439,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(); } @@ -352,7 +464,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 ); @@ -360,7 +472,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; } @@ -390,7 +503,7 @@ void Threads_Sleep() // Release Spinlock RELEASE( &giThreadListLock ); - HALT(); + while(cur->Status != THREAD_STAT_ACTIVE) HALT(); } @@ -425,6 +538,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) @@ -439,6 +568,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) { @@ -501,18 +631,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); } @@ -528,15 +663,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) { @@ -556,3 +696,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 ); +}