AcessNative - Changes to allow _compiling_ on windows.
[tpg/acess2.git] / AcessNative / acesskernel_src / threads.c
index 9de6140..9400d72 100644 (file)
@@ -5,29 +5,16 @@
  * 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 <arch.h>
-#undef NULL    // Remove acess definition
 #include <acess.h>
 #include <mutex.h>
-#include <semaphore.h>
+#include "../../KernelLand/Kernel/include/semaphore.h"
 #include <rwlock.h>
 #include <events.h>
 #include <threads_int.h>
-
-#undef CLONE_VM        // Such a hack
-#undef off_t   
-
-// - Native headers
-#include <unistd.h>
-#include <sys/types.h>
-#include <stdint.h>
-#include "/usr/include/signal.h"
-#include <SDL/SDL.h>
+#include <limits.h>
+#include "include/threads_glue.h"
 
 #define THREAD_EVENT_WAKEUP    0x80000000
 
 void   VFS_CloneHandleList(int PID);
 
 // === STRUCTURES ===
-#if 0
-typedef struct sState
-{
-       void    *CurState;
-       Uint    SP, BP, IP;
-}      tState;
-#endif
-
 // === PROTOTYPES ===
  int   Threads_Wake(tThread *Thread);
 
@@ -119,10 +98,7 @@ tThread *Threads_CloneTCB(tThread *TemplateThread)
        ret->TID = giThreads_NextThreadID ++;
        
        ret->ThreadName = strdup(TemplateThread->ThreadName);
-       ret->EventSem = SDL_CreateSemaphore(0);
-       if( !ret->EventSem ) {
-               Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError());
-       }
+       Threads_Glue_SemInit( &ret->EventSem, 0 );
        
        ret->WaitingThreads = NULL;
        ret->WaitingThreadsEnd = NULL;
@@ -223,7 +199,7 @@ tTID Threads_WaitTID(int TID, int *Status)
 void Threads_Sleep(void)
 {
        // TODO: Add to a sleeping queue
-       pause();
+       //pause();
 }
 
 void Threads_Yield(void)
@@ -244,7 +220,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
        
@@ -255,7 +231,7 @@ void Threads_Exit(int TID, int Status)
                Threads_Wake(toWake);
                
                while(gpCurrentThread->WaitingThreads == toWake)
-                       SDL_Delay(10);
+                       Threads_Glue_Yield();
        }
 }
 
@@ -300,17 +276,13 @@ int Threads_Fork(void)
 // --------------------------------------------------------------------
 int Mutex_Acquire(tMutex *Mutex)
 {
-       if(!Mutex->Protector.IsValid) {
-               pthread_mutex_init( &Mutex->Protector.Mutex, NULL );
-               Mutex->Protector.IsValid = 1;
-       }
-       pthread_mutex_lock( &Mutex->Protector.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);
 }
 
 // --------------------------------------------------------------------
@@ -320,50 +292,19 @@ void Semaphore_Init(tSemaphore *Sem, int InitValue, int MaxValue, const char *Mo
 {
        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;
-}
-
-// --------------------------------------------------------------------
-// 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 );
+       return Threads_Glue_SemSignal( Sem->Protector.Mutex, AmmountToAdd );
 }
 
-
 // --------------------------------------------------------------------
 // Event handling
 // --------------------------------------------------------------------
@@ -376,14 +317,10 @@ Uint32 Threads_WaitEvents(Uint32 Mask)
        gpCurrentThread->WaitMask = Mask;
        if( !(gpCurrentThread->Events & Mask) )
        {
-               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
+               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;
@@ -401,7 +338,7 @@ void Threads_PostEvent(tThread *Thread, Uint32 Events)
        Log_Debug("Threads", "Trigger event %x (->Events = %p)", Events, Thread->Events);
        
        if( Events == 0 || Thread->WaitMask & Events ) {
-               SDL_SemPost( Thread->EventSem );
+               Threads_Glue_SemSignal( Thread->EventSem, 1 );
 //             Log_Debug("Threads", "Waking %p(%i %s)", Thread, Thread->TID, Thread->ThreadName);
        }
 }

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