X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Facesskernel_src%2Fthreads.c;h=a2a58981eb2e44e1059917f6f959e0a733aa0802;hb=cfe6a9f2a126cc26ac74d5454e8378bd1193fcf8;hp=1e7d0c3fa61e89b072ad0d2b51ebf0b2e1d5cc3c;hpb=6bcb63ea0505129d32b249772b554b0848bb5612;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/threads.c b/AcessNative/acesskernel_src/threads.c index 1e7d0c3f..a2a58981 100644 --- a/AcessNative/acesskernel_src/threads.c +++ b/AcessNative/acesskernel_src/threads.c @@ -8,18 +8,27 @@ #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 + +#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); @@ -70,6 +79,9 @@ typedef struct sThread tProcess *Process; + Uint32 Events, WaitMask; + SDL_sem *EventSem; + } tThread; // === PROTOTYPES === @@ -147,6 +159,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 +177,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 +188,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 +204,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) { @@ -232,11 +245,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; @@ -290,7 +299,7 @@ void Threads_Exit(int TID, int Status) int Threads_Wake(tThread *Thread) { Thread->State = 0; - kill( Thread->KernelTID, SIGUSR1 ); + Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP); return 0; } @@ -359,3 +368,32 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd) return AmmountToAdd; } +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); + } +} +