AcessNative - Loads login process and runs again (shell doesn't spawn)
[tpg/acess2.git] / AcessNative / acesskernel_src / threads.c
index a2a5898..c31c5d0 100644 (file)
@@ -15,7 +15,9 @@
 #include <acess.h>
 #include <mutex.h>
 #include <semaphore.h>
+#include <rwlock.h>
 #include <events.h>
+#include <threads_int.h>
 
 #undef CLONE_VM        // Such a hack
 #undef off_t   
@@ -41,49 +43,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;       
-
-       Uint32  Events, WaitMask;
-       SDL_sem *EventSem;
-
-}      tThread;
-
 // === PROTOTYPES ===
  int   Threads_Wake(tThread *Thread);
 
@@ -95,7 +54,7 @@ tProcess gProcessZero = {
        .MaxFD = 100
 };
 tThread        gThreadZero = {
-       .State=1,
+       .Status=THREAD_STAT_ACTIVE,
        .ThreadName="ThreadZero",
        .Process = &gProcessZero
 };
@@ -136,13 +95,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;
 }
@@ -160,6 +120,9 @@ tThread *Threads_CloneTCB(tThread *TemplateThread)
        
        ret->ThreadName = strdup(TemplateThread->ThreadName);
        ret->EventSem = SDL_CreateSemaphore(0);
+       if( !ret->EventSem ) {
+               Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError());
+       }
        
        ret->WaitingThreads = NULL;
        ret->WaitingThreadsEnd = NULL;
@@ -232,7 +195,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)
                {
@@ -298,7 +261,7 @@ void Threads_Exit(int TID, int Status)
 
 int Threads_Wake(tThread *Thread)
 {
-       Thread->State = 0;
+       Thread->Status = THREAD_STAT_ACTIVE;
        Threads_PostEvent(Thread, THREAD_EVENT_WAKEUP);
        return 0;
 }
@@ -332,6 +295,9 @@ int Threads_Fork(void)
        return thread->PID;
 }
 
+// --------------------------------------------------------------------
+// Mutexes 
+// --------------------------------------------------------------------
 int Mutex_Acquire(tMutex *Mutex)
 {
        if(!Mutex->Protector.IsValid) {
@@ -347,6 +313,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));
@@ -368,6 +337,36 @@ int Semaphore_Signal(tSemaphore *Sem, int AmmountToAdd)
        return AmmountToAdd;
 }
 
+// --------------------------------------------------------------------
+// Event handling
+// --------------------------------------------------------------------
+int RWLock_AcquireRead(tRWLock *Lock)
+{
+       if( !Lock->ReaderWaiting ) {
+               Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t));
+               pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 );
+       }
+       pthread_rwlock_rdlock( (void*)Lock->ReaderWaiting );
+       return 0;
+}
+int RWLock_AcquireWrite(tRWLock *Lock)
+{
+       if( !Lock->ReaderWaiting ) {
+               Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t));
+               pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 );
+       }
+       pthread_rwlock_wrlock( (void*)Lock->ReaderWaiting );
+       return 0;
+}
+void RWLock_Release(tRWLock *Lock)
+{
+       pthread_rwlock_unlock( (void*)Lock->ReaderWaiting );
+}
+
+
+// --------------------------------------------------------------------
+// Event handling
+// --------------------------------------------------------------------
 Uint32 Threads_WaitEvents(Uint32 Mask)
 {
        Uint32  rv;
@@ -377,12 +376,22 @@ Uint32 Threads_WaitEvents(Uint32 Mask)
        gpCurrentThread->WaitMask = Mask;
        if( !(gpCurrentThread->Events & Mask) )
        {
-               SDL_SemWait( gpCurrentThread->EventSem );
+               do {
+                       if( SDL_SemWait( gpCurrentThread->EventSem ) == -1 ) {
+                               Log_Warning("Threads", "Wait on eventsem of %p, %p failed",
+                                       gpCurrentThread, gpCurrentThread->EventSem);
+                               break;
+                       }
+               } while(SDL_SemValue(gpCurrentThread->EventSem));
+               // NOTE: While loop catches multiple event occurances
+               Log_Debug("Threads", "Woken from nap (%i here)", SDL_SemValue(gpCurrentThread->EventSem));
        }
        rv = gpCurrentThread->Events & Mask;
        gpCurrentThread->Events &= ~Mask;
        gpCurrentThread->WaitMask = -1;
-       
+
+       Log_Debug("Threads", "- rv = %x", rv);
+
        return rv;
 }
 
@@ -397,3 +406,8 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events)
        }
 }
 
+void Threads_ClearEvent(Uint32 EventMask)
+{
+       gpCurrentThread->Events &= ~EventMask;
+}
+

UCC git Repository :: git.ucc.asn.au