X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Fthreads.c;h=c0c613297a309f88fa123273f65e9bfc188f4f7d;hb=e6795eb552a6be88b7870dae14a958ab391bfae8;hp=40e1729b9fa04369f147bf9c8774cd4f9b2a2952;hpb=6e1cf1d50bca85f01b5f802b1a76f090e362bf05;p=tpg%2Facess2.git diff --git a/Kernel/threads.c b/Kernel/threads.c index 40e1729b..c0c61329 100644 --- a/Kernel/threads.c +++ b/Kernel/threads.c @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include // Configuration #define DEBUG_TRACE_TICKETS 0 // Trace ticket counts @@ -20,10 +22,10 @@ #define SCHED_RR_SIM 2 // Single Queue Round Robin #define SCHED_RR_PRI 3 // Multi Queue Round Robin // Set scheduler type -#define SCHEDULER_TYPE SCHED_LOTTERY +#define SCHEDULER_TYPE SCHED_RR_PRI // === CONSTANTS === -#define DEFAULT_QUANTUM 10 +#define DEFAULT_QUANTUM 5 #define DEFAULT_PRIORITY 5 #define MIN_PRIORITY 10 const enum eConfigTypes cCONFIG_TYPES[] = { @@ -33,10 +35,6 @@ const enum eConfigTypes cCONFIG_TYPES[] = { }; // === IMPORTS === -extern void ArchThreads_Init(void); -extern void Proc_CallFaultHandler(tThread *Thread); -extern void Proc_DumpThreadCPUState(tThread *Thread); -extern int GetCPUNum(void); // === PROTOTYPES === void Threads_Init(void); @@ -755,6 +753,56 @@ int Threads_Wake(tThread *Thread) SHORTREL( &glThreadListLock ); return -EOK; + case THREAD_STAT_SEMAPHORESLEEP: { + tSemaphore *sem; + tThread *th, *prev=NULL; + + sem = Thread->WaitPointer; + + SHORTLOCK( &sem->Protector ); + + // Remove from sleeping queue + for( th = sem->Waiting; th; prev = th, th = th->Next ) + if( th == Thread ) break; + if( th ) + { + if(prev) + prev->Next = Thread->Next; + else + sem->Waiting = Thread->Next; + if(sem->LastWaiting == Thread) + sem->LastWaiting = prev; + } + else + { + prev = NULL; + for( th = sem->Signaling; th; prev = th, th = th->Next ) + if( th == Thread ) break; + if( !th ) { + Log_Warning("Threads", "Thread %p(%i %s) is not on semaphore %p(%s:%s)", + Thread, Thread->TID, Thread->ThreadName, + sem, sem->ModName, sem->Name); + return -EINTERNAL; + } + + if(prev) + prev->Next = Thread->Next; + else + sem->Signaling = Thread->Next; + if(sem->LastSignaling == Thread) + sem->LastSignaling = prev; + } + + SHORTLOCK( &glThreadListLock ); + Threads_AddActive( Thread ); + SHORTREL( &glThreadListLock ); + + #if DEBUG_TRACE_STATE + Log("Threads_Sleep: %p(%i %s) woken from semaphore", Thread, Thread->TID, Thread->ThreadName); + #endif + SHORTREL( &sem->Protector ); + } return -EOK; + case THREAD_STAT_WAITING: Warning("Threads_Wake - Waiting threads are not currently supported"); return -ENOTIMPL; @@ -801,7 +849,8 @@ void Threads_AddActive(tThread *Thread) if( Thread->Status == THREAD_STAT_ACTIVE ) { tThread *cur = Proc_GetCurThread(); - Warning("WTF, CPU%i %p (%i %s) is adding %p (%i %s) when it is active", + Log_Warning("Threads", "WTF, %p CPU%i %p (%i %s) is adding %p (%i %s) when it is active", + __builtin_return_address(0), GetCPUNum(), cur, cur->TID, cur->ThreadName, Thread, Thread->TID, Thread->ThreadName); SHORTREL( &glThreadListLock ); return ; @@ -811,13 +860,23 @@ void Threads_AddActive(tThread *Thread) Thread->Status = THREAD_STAT_ACTIVE; // Thread->CurCPU = -1; // Add to active list - #if SCHEDULER_TYPE == SCHED_RR_PRI - Thread->Next = gaActiveThreads[Thread->Priority]; - gaActiveThreads[Thread->Priority] = Thread; - #else - Thread->Next = gActiveThreads; - gActiveThreads = Thread; - #endif + { + tThread *tmp, *prev = NULL; + #if SCHEDULER_TYPE == SCHED_RR_PRI + for( tmp = gaActiveThreads[Thread->Priority]; tmp; prev = tmp, tmp = tmp->Next ); + if(prev) + prev->Next = Thread; + else + gaActiveThreads[Thread->Priority] = Thread; + #else + for( tmp = gActiveThreads; tmp; prev = tmp, tmp = tmp->Next ); + if(prev) + prev->Next = Thread; + else + gActiveThreads = Thread; + #endif + Thread->Next = NULL; + } // Update bookkeeping giNumActiveThreads ++; @@ -863,6 +922,9 @@ tThread *Threads_RemActive(void) #endif { SHORTREL( &glThreadListLock ); + Log_Warning("Threads", "Current thread %p(%i %s) is not on active queue", + ret, ret->TID, ret->ThreadName + ); return NULL; } @@ -900,8 +962,6 @@ void Threads_Fault(int Num) { tThread *thread = Proc_GetCurThread(); - Log_Log("Threads", "Threads_Fault: thread = %p", thread); - if(!thread) return ; Log_Log("Threads", "Threads_Fault: thread->FaultHandler = %p", thread->FaultHandler); @@ -920,6 +980,7 @@ void Threads_Fault(int Num) // Double Fault? Oh, F**k if(thread->CurFaultNum != 0) { + Log_Warning("Threads", "Threads_Fault: Double fault on %i", thread->TID); Threads_Kill(thread, -1); // For now, just kill HALT(); } @@ -935,7 +996,10 @@ void Threads_Fault(int Num) */ void Threads_SegFault(tVAddr Addr) { - Warning("Thread #%i committed a segfault at address %p", Proc_GetCurThread()->TID, Addr); + tThread *cur = Proc_GetCurThread(); + cur->bInstrTrace = 0; + Log_Warning("Threads", "Thread #%i committed a segfault at address %p", cur->TID, Addr); + MM_DumpTables(0, KERNEL_BASE); Threads_Fault( 1 ); //Threads_Exit( 0, -1 ); } @@ -1229,7 +1293,7 @@ tThread *Threads_GetNextToRun(int CPU, tThread *Last) } // If we fall onto the same queue again, special handling is // needed - if( i == Last->Priority ) { + if( Last && i == Last->Priority ) { tThread *savedThread = thread; // Find the next unscheduled thread in the list @@ -1423,7 +1487,6 @@ int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) else taken = Sem->Value; Sem->Value -= taken; - SHORTREL( &Sem->Protector ); } else { @@ -1456,7 +1519,7 @@ int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) SHORTREL( &Sem->Protector ); // Release first to make sure it is released SHORTREL( &glThreadListLock ); while(us->Status == THREAD_STAT_SEMAPHORESLEEP) Threads_Yield(); - // We're only woken when there's something avaliable + // We're only woken when there's something avaliable (or a signal arrives) us->WaitPointer = NULL; taken = us->RetStatus;