X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Tools%2Fnativelib%2Fthreads.c;h=f80447334fcc89e47d5d7332c2d1e3c526600c40;hb=13078002b01ee4f63eb2001d2ef479a2a006ea32;hp=b698770acc6df8b7ca82dd1e6378085ca95bccbc;hpb=652e2ab0285b66a388a7d73fe5d3551249a4df0b;p=tpg%2Facess2.git diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index b698770a..f8044733 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -5,9 +5,11 @@ * threads.c * - Threads handling */ +#define DEBUG 0 #include #include #include +#include // === PROTOTYPES === void Threads_int_Init(void) __attribute__((constructor(101))); @@ -22,7 +24,10 @@ tShortSpinlock glThreadListLock; // === CODE === void Threads_int_Init(void) { - lpThreads_This = Threads_int_CreateTCB(NULL); + if( !lpThreads_This ) { + lpThreads_This = Threads_int_CreateTCB(NULL); + Threads_SetName("ThreadZero"); + } } tThread *Proc_GetCurThread(void) @@ -30,51 +35,17 @@ tThread *Proc_GetCurThread(void) return lpThreads_This; } -void Threads_PostEvent(tThread *Thread, Uint32 Events) -{ - if( !Thread ) { - // nope.avi - return ; - } - SHORTLOCK( &Thread->IsLocked ); - Thread->PendingEvents |= Events; - if( Thread->WaitingEvents & Events ) - Threads_int_SemSignal(Thread->WaitSemaphore); - SHORTREL( &Thread->IsLocked ); -} - -Uint32 Threads_WaitEvents(Uint32 Events) -{ - if( !Threads_int_ThreadingEnabled() ) { - Log_Notice("Threads", "_WaitEvents: Threading disabled"); - return 0; - } - lpThreads_This->WaitingEvents = Events; - Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore); - lpThreads_This->WaitingEvents = 0; - Uint32 rv = lpThreads_This->PendingEvents; - return rv; -} - -void Threads_ClearEvent(Uint32 Mask) -{ - if( !Threads_int_ThreadingEnabled() ) { - Log_Notice("Threads", "_ClearEvent: Threading disabled"); - return ; - } - SHORTLOCK(&lpThreads_This->IsLocked); - lpThreads_This->PendingEvents &= ~Mask; - SHORTREL(&lpThreads_This->IsLocked); -} - tUID Threads_GetUID(void) { return 0; } tGID Threads_GetGID(void) { return 0; } tTID Threads_GetTID(void) { return lpThreads_This ? lpThreads_This->TID : 0; } -int *Threads_GetMaxFD(void) { return &lpThreads_This->Process->MaxFDs; } -char **Threads_GetCWD(void) { return &lpThreads_This->Process->CWD; } -char **Threads_GetChroot(void) { return &lpThreads_This->Process->Chroot; } +static inline tProcess* getproc(tProcess *Process) { + return (Process ? Process : lpThreads_This->Process); +} +int *Threads_GetMaxFD(tProcess *Process) { return &getproc(Process)->MaxFDs; } +char **Threads_GetCWD(tProcess *Process) { return &getproc(Process)->CWD; } +char **Threads_GetChroot(tProcess *Process) { return &getproc(Process)->Chroot; } void **Threads_GetHandlesPtr(void) { return &lpThreads_This->Process->Handles; } void Threads_Yield(void) @@ -88,12 +59,6 @@ void Threads_Sleep(void) Log_Warning("Threads", "Threads_Sleep shouldn't be used"); } -void Threads_int_WaitForStatusEnd(enum eThreadStatus Status) -{ - while( lpThreads_This->Status != Status ) - Threads_int_SemWaitAll(lpThreads_This->WaitSemaphore); -} - int Threads_SetName(const char *Name) { if( !lpThreads_This ) @@ -123,6 +88,7 @@ void Threads_AddActive(tThread *Thread) { Thread->Status = THREAD_STAT_ACTIVE; // Increment state-change semaphore + LOG("Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName); Threads_int_SemSignal(Thread->WaitSemaphore); } @@ -175,3 +141,54 @@ struct sThread *Proc_SpawnWorker(void (*Fcn)(void*), void *Data) } } +void Threads_int_WaitForStatusEnd(enum eThreadStatus Status) +{ + tThread *us = Proc_GetCurThread(); + assert(Status != THREAD_STAT_ACTIVE); + assert(Status != THREAD_STAT_DEAD); + LOG("%i(%s) - %i", us->TID, us->ThreadName, Status); + while( us->Status == Status ) + { + Threads_int_SemWaitAll(us->WaitSemaphore); + if( us->Status == Status ) + Log_Warning("Threads", "Thread %p(%i %s) rescheduled while in %s state", + us, us->TID, us->ThreadName, casTHREAD_STAT[Status]); + } + LOG("%p(%i %s) Awake", us, us->TID, us->ThreadName); +} + +int Threads_int_Sleep(enum eThreadStatus Status, void *Ptr, int Num, tThread **ListHead, tThread **ListTail, tShortSpinlock *Lock) +{ + tThread *us = Proc_GetCurThread(); + us->Next = NULL; + // - Mark as sleeping + us->Status = Status; + us->WaitPointer = Ptr; + us->RetStatus = Num; // Use RetStatus as a temp variable + + // - Add to waiting + if( ListTail ) { + if(*ListTail) { + (*ListTail)->Next = us; + } + else { + *ListHead = us; + } + *ListTail = us; + } + else if(ListHead) { + us->Next = *ListHead; + *ListHead = us; + } + else { + // nothing + } + + if( Lock ) { + SHORTREL( Lock ); + } + Threads_int_WaitForStatusEnd(Status); + us->WaitPointer = NULL; + return us->RetStatus; +} +