X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Facesskernel_src%2Fthreads.c;h=de61ef47d584d8c43d9d647b6295376641073424;hb=5a66c3a4a2019a7ddea8931c8476bb64f4d29079;hp=71fa2941c5fa4852ad05140d457b12cfdd2fdba1;hpb=43c4f4db78b98e0427a04dc4b281359580591915;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/threads.c b/AcessNative/acesskernel_src/threads.c index 71fa2941..de61ef47 100644 --- a/AcessNative/acesskernel_src/threads.c +++ b/AcessNative/acesskernel_src/threads.c @@ -5,85 +5,26 @@ * threads.c * - Thread and process handling */ -#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) -#define off_t _acess_off_t #include -#undef NULL // Remove acess definition #include #include -#include +#include "../../KernelLand/Kernel/include/semaphore.h" +typedef signed long long int time_t; +#include "../../Usermode/Libraries/ld-acess.so_src/include_exp/acess/syscall_types.h" +#include #include - -#undef CLONE_VM // Such a hack -#undef off_t - -// - Native headers -#include -#include -#include -#include "/usr/include/signal.h" -#include +#include +#include +#include "include/threads_glue.h" #define THREAD_EVENT_WAKEUP 0x80000000 // === IMPORTS === -void VFS_CloneHandleList(int PID); +extern void VFS_CloneHandleList(int PID); +extern void VFS_CloneHandlesFromList(int PID, int nFD, int FDs[]); // === STRUCTURES === -#if 0 -typedef struct sState -{ - void *CurState; - Uint SP, BP, IP; -} tState; -#endif - -typedef struct sProcess -{ - int nThreads; - int NativePID; - char *CWD; - char *Chroot; - int MaxFD; -} tProcess; - -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; - - Uint32 Events, WaitMask; - SDL_sem *EventSem; - -}; - // === PROTOTYPES === int Threads_Wake(tThread *Thread); @@ -95,7 +36,7 @@ tProcess gProcessZero = { .MaxFD = 100 }; tThread gThreadZero = { - .State=1, + .Status=THREAD_STAT_ACTIVE, .ThreadName="ThreadZero", .Process = &gProcessZero }; @@ -136,13 +77,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; } @@ -159,7 +101,7 @@ tThread *Threads_CloneTCB(tThread *TemplateThread) ret->TID = giThreads_NextThreadID ++; ret->ThreadName = strdup(TemplateThread->ThreadName); - ret->EventSem = SDL_CreateSemaphore(0); + Threads_Glue_SemInit( &ret->EventSem, 0 ); ret->WaitingThreads = NULL; ret->WaitingThreadsEnd = NULL; @@ -232,7 +174,7 @@ tTID 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) { @@ -260,7 +202,7 @@ tTID Threads_WaitTID(int TID, int *Status) void Threads_Sleep(void) { // TODO: Add to a sleeping queue - pause(); + //pause(); } void Threads_Yield(void) @@ -281,7 +223,7 @@ void Threads_Exit(int TID, int Status) { // Wait for the thread to be waited upon while( gpCurrentThread->WaitingThreads == NULL ) - SDL_Delay(10); + Threads_Glue_Yield(); } #endif @@ -292,13 +234,13 @@ void Threads_Exit(int TID, int Status) Threads_Wake(toWake); while(gpCurrentThread->WaitingThreads == toWake) - SDL_Delay(10); + Threads_Glue_Yield(); } } int Threads_Wake(tThread *Thread) { - Thread->State = 0; + Thread->Status = THREAD_STAT_ACTIVE; Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP); return 0; } @@ -325,75 +267,120 @@ int Threads_Fork(void) { tThread *thread = Threads_CloneTCB(gpCurrentThread); thread->PID = thread->TID; + // Duplicate the VFS handles (and nodes) from vfs_handle.c - VFS_CloneHandleList(thread->PID); return thread->PID; } -int Mutex_Acquire(tMutex *Mutex) +int Threads_Spawn(int nFD, int FDs[], struct s_sys_spawninfo *info) { - if(!Mutex->Protector.IsValid) { - pthread_mutex_init( &Mutex->Protector.Mutex, NULL ); - Mutex->Protector.IsValid = 1; + tThread *thread = Threads_CloneTCB(gpCurrentThread); + thread->PID = thread->TID; + if( info ) + { + // TODO: PGID? + //if( info->flags & SPAWNFLAG_NEWPGID ) + // thread->PGID = thread->PID; + if( info->gid && thread->UID == 0 ) + thread->GID = info->gid; + if( info->uid && thread->UID == 0 ) // last because ->UID is used above + thread->UID = info->uid; } - pthread_mutex_lock( &Mutex->Protector.Mutex ); + + VFS_CloneHandlesFromList(thread->PID, nFD, FDs); + + Log_Debug("Threads", "_spawn: %i", thread->PID); + return thread->PID; +} + +// -------------------------------------------------------------------- +// Mutexes +// -------------------------------------------------------------------- +int Mutex_Acquire(tMutex *Mutex) +{ + Threads_Glue_AcquireMutex(&Mutex->Protector.Mutex); return 0; } void Mutex_Release(tMutex *Mutex) { - pthread_mutex_unlock( &Mutex->Protector.Mutex ); + Threads_Glue_ReleaseMutex(&Mutex->Protector.Mutex); } +// -------------------------------------------------------------------- +// Semaphores +// -------------------------------------------------------------------- void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Module, const char *Name) { memset(Sem, 0, sizeof(tSemaphore)); // HACK: Use `Sem->Protector` as space for the semaphore pointer - *(void**)(&Sem->Protector) = SDL_CreateSemaphore(InitValue); + Threads_Glue_SemInit( &Sem->Protector.Mutex, InitValue ); } int Semaphore_Wait(tSemaphore *Sem, int MaxToTake) { - SDL_SemWait( *(void**)(&Sem->Protector) ); - return 1; + return Threads_Glue_SemWait( Sem->Protector.Mutex, MaxToTake ); } int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd) { - int i; - for( i = 0; i < AmmountToAdd; i ++ ) - SDL_SemPost( *(void**)(&Sem->Protector) ); - return AmmountToAdd; + return Threads_Glue_SemSignal( Sem->Protector.Mutex, AmmountToAdd ); } +// -------------------------------------------------------------------- +// Event handling +// -------------------------------------------------------------------- Uint32 Threads_WaitEvents(Uint32 Mask) { Uint32 rv; - Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events); + //Log_Debug("Threads", "Mask = %x, ->Events = %x", Mask, gpCurrentThread->Events); gpCurrentThread->WaitMask = Mask; - if( !(gpCurrentThread->Events & Mask) ) + if( !(gpCurrentThread->EventState & Mask) ) { - SDL_SemWait( gpCurrentThread->EventSem ); + if( Threads_Glue_SemWait(gpCurrentThread->EventSem, INT_MAX) == -1 ) { + Log_Warning("Threads", "Wait on eventsem of %p, %p failed", + gpCurrentThread, gpCurrentThread->EventSem); + } + //Log_Debug("Threads", "Woken from nap (%i here)", SDL_SemValue(gpCurrentThread->EventSem)); } - rv = gpCurrentThread->Events & Mask; - gpCurrentThread->Events &= ~Mask; + rv = gpCurrentThread->EventState & Mask; + gpCurrentThread->EventState &= ~Mask; gpCurrentThread->WaitMask = -1; - + + //Log_Debug("Threads", "- rv = %x", rv); + 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 ); + Thread->EventState |= Events; +// Log_Debug("Threads", "Trigger event %x (->Events = %p) on %p", Events, Thread->Events, Thread); + + if( Events == 0 || Thread->WaitMask & Events ) { + Threads_Glue_SemSignal( Thread->EventSem, 1 ); // Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName); } } +void Threads_ClearEvent(Uint32 EventMask) +{ + gpCurrentThread->EventState &= ~EventMask; +} + +// -------------------------------------------------------------------- +// Signals +// -------------------------------------------------------------------- +void Threads_PostSignal(int SigNum) +{ + Log_Error("Threads", "TODO: %s", __func__); +} +void Threads_SignalGroup(tPGID PGID, int SignalNum) +{ + Log_Error("Threads", "TODO: %s", __func__); +} +