X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Facesskernel_src%2Fthreads.c;h=c390a031e9e72d8350fa85ca46f96134714f746a;hb=6516331968f11dd9e5c495572f27cc69fa3d4c48;hp=1e7d0c3fa61e89b072ad0d2b51ebf0b2e1d5cc3c;hpb=6bcb63ea0505129d32b249772b554b0848bb5612;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/threads.c b/AcessNative/acesskernel_src/threads.c index 1e7d0c3f..c390a031 100644 --- a/AcessNative/acesskernel_src/threads.c +++ b/AcessNative/acesskernel_src/threads.c @@ -8,18 +8,28 @@ #define _SIGNAL_H_ // Stop the acess signal.h being used #define _HEAP_H_ // Stop heap.h being imported (collides with stdlib heap) #define _VFS_EXT_H // Stop vfs_ext.h being imported (collides with fd_set) -#undef CLONE_VM // Such a hack + +#define off_t _acess_off_t #include #undef NULL // Remove acess definition #include #include #include +#include +#include + +#undef CLONE_VM // Such a hack +#undef off_t + +// - Native headers #include #include #include #include "/usr/include/signal.h" #include +#define THREAD_EVENT_WAKEUP 0x80000000 + // === IMPORTS === void VFS_CloneHandleList(int PID); @@ -32,46 +42,6 @@ typedef struct sState } tState; #endif -typedef struct sProcess -{ - int nThreads; - int NativePID; - char *CWD; - char *Chroot; - int MaxFD; -} tProcess; - -typedef struct sThread -{ - struct sThread *GlobalNext; - struct sThread *Next; - - int KernelTID; - - tTID TID, PID; - tUID UID, GID; - - struct sThread *Parent; - - char *ThreadName; - - int State; // 0: Dead, 1: Active, 2: Paused, 3: Asleep - int ExitStatus; - int _errno; - - // Threads waiting for this thread to exit. - // Quit logic: - // - Wait for `WaitingThreads` to be non-null (maybe?) - // - Wake first in the queue, wait for it to be removed - // - Repeat - // - Free thread and quit kernel thread - struct sThread *WaitingThreads; - struct sThread *WaitingThreadsEnd; - - tProcess *Process; - -} tThread; - // === PROTOTYPES === int Threads_Wake(tThread *Thread); @@ -83,7 +53,7 @@ tProcess gProcessZero = { .MaxFD = 100 }; tThread gThreadZero = { - .State=1, + .Status=THREAD_STAT_ACTIVE, .ThreadName="ThreadZero", .Process = &gProcessZero }; @@ -124,13 +94,14 @@ void Threads_SetThread(int TID) Log_Error("Threads", "_SetThread - Thread %i is not on global list", TID); } -tThread *Threads_GetThread(int TID) +tThread *Threads_GetThread(Uint TID) { tThread *thread; for( thread = gpThreads; thread; thread = thread->GlobalNext ) { - if( thread->TID == TID ) + if( thread->TID == TID ) { return thread; + } } return NULL; } @@ -147,6 +118,7 @@ tThread *Threads_CloneTCB(tThread *TemplateThread) ret->TID = giThreads_NextThreadID ++; ret->ThreadName = strdup(TemplateThread->ThreadName); + ret->EventSem = SDL_CreateSemaphore(0); ret->WaitingThreads = NULL; ret->WaitingThreadsEnd = NULL; @@ -164,10 +136,10 @@ tGID Threads_GetGID() { return gpCurrentThread->GID; } tTID Threads_GetTID() { return gpCurrentThread->TID; } tPID Threads_GetPID() { return gpCurrentThread->PID; } -int Threads_SetUID(int *Errno, tUID NewUID) +int Threads_SetUID(tUID NewUID) { if(Threads_GetUID() != 0) { - if(Errno) *Errno = -EACCES; + errno = EACCES; return -1; } @@ -175,10 +147,10 @@ int Threads_SetUID(int *Errno, tUID NewUID) return 0; } -int Threads_SetGID(int *Errno, tGID NewGID) +int Threads_SetGID(tGID NewGID) { if(Threads_GetUID() != 0) { - if(Errno) *Errno = -EACCES; + errno = -EACCES; return -1; } @@ -191,7 +163,7 @@ char **Threads_GetCWD(void) { return &gpCurrentThread->Process->CWD; } char **Threads_GetChroot(void) { return &gpCurrentThread->Process->Chroot; } int *Threads_GetMaxFD(void) { return &gpCurrentThread->Process->MaxFD; }; -int Threads_WaitTID(int TID, int *Status) +tTID Threads_WaitTID(int TID, int *Status) { // Any Child if(TID == -1) { @@ -219,7 +191,7 @@ int Threads_WaitTID(int TID, int *Status) if(!thread) return -1; us->Next = NULL; - us->State = 3; + us->Status = THREAD_STAT_WAITING; // TODO: Locking if(thread->WaitingThreadsEnd) { @@ -232,11 +204,7 @@ int Threads_WaitTID(int TID, int *Status) thread->WaitingThreadsEnd = us; } - while(thread->State != 0) - { - pause(); - Log_Debug("Threads", "Huh?... state = %i", thread->State); - } + Threads_WaitEvents( THREAD_EVENT_WAKEUP ); if(Status) *Status = thread->ExitStatus; thread->WaitingThreads = thread->WaitingThreads->Next; @@ -289,8 +257,8 @@ void Threads_Exit(int TID, int Status) int Threads_Wake(tThread *Thread) { - Thread->State = 0; - kill( Thread->KernelTID, SIGUSR1 ); + Thread->Status = THREAD_STAT_ACTIVE; + Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP); return 0; } @@ -323,6 +291,9 @@ int Threads_Fork(void) return thread->PID; } +// -------------------------------------------------------------------- +// Mutexes +// -------------------------------------------------------------------- int Mutex_Acquire(tMutex *Mutex) { if(!Mutex->Protector.IsValid) { @@ -338,6 +309,9 @@ void Mutex_Release(tMutex *Mutex) pthread_mutex_unlock( &Mutex->Protector.Mutex ); } +// -------------------------------------------------------------------- +// Semaphores +// -------------------------------------------------------------------- void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name) { memset(Sem, 0, sizeof(tSemaphore)); @@ -359,3 +333,40 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd) return AmmountToAdd; } +// -------------------------------------------------------------------- +// Event handling +// -------------------------------------------------------------------- +Uint32 Threads_WaitEvents(Uint32 Mask) +{ + Uint32 rv; + + Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events); + + gpCurrentThread->WaitMask = Mask; + if( !(gpCurrentThread->Events & Mask) ) + { + SDL_SemWait( gpCurrentThread->EventSem ); + } + rv = gpCurrentThread->Events & Mask; + gpCurrentThread->Events &= ~Mask; + gpCurrentThread->WaitMask = -1; + + return rv; +} + +void Threads_PostEvent(tThread *Thread, Uint32 Events) +{ + Thread->Events |= Events; + Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events); + + if( Thread->WaitMask & Events ) { + SDL_SemPost( Thread->EventSem ); +// Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName); + } +} + +void Threads_ClearEvent(Uint32 EventMask) +{ + gpCurrentThread->Events &= ~EventMask; +} +