Tools/NetTest - Add TCP retransmit test
[tpg/acess2.git] / AcessNative / acesskernel_src / threads_glue.c
index d400b27..7c13780 100644 (file)
@@ -18,10 +18,11 @@ typedef void        **tShortSpinlock;
 //#include "/usr/include/signal.h"
 #include <SDL/SDL.h>
 #include <pthread.h>
-#include <assert.h>
 
 #define NORETURN       __attribute__((noreturn))
+#include <stdbool.h>
 #include <logdebug.h>  // Kernel land, but uses standards
+#include <errno.h>
 
 // === CODE ===
 void Threads_Glue_Yield(void)
@@ -66,35 +67,51 @@ int Threads_Glue_SemWait(void *Ptr, int Max)
 
 int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd )
 {
-        int    i;
-       for( i = 0; i < AmmountToAdd; i ++ )
+       for( int i = 0; i < AmmountToAdd; i ++ )
                SDL_SemPost( Ptr );
        return AmmountToAdd;
 }
 
-// --------------------------------------------------------------------
-// Event handling
-// --------------------------------------------------------------------
-int RWLock_AcquireRead(tRWLock *Lock)
+void Threads_Glue_SemDestroy( void *Ptr )
 {
-       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;
+       SDL_DestroySemaphore(Ptr);
 }
-int RWLock_AcquireWrite(tRWLock *Lock)
+
+// ---
+// Short Locks
+// ---
+void Threads_int_ShortLock(void **MutexPtr)
 {
-       if( !Lock->ReaderWaiting ) {
-               Lock->ReaderWaiting = malloc(sizeof(pthread_rwlock_t));
-               pthread_rwlock_init( (void*)Lock->ReaderWaiting, 0 );
+       if( !*MutexPtr ) {
+               *MutexPtr = malloc( sizeof(pthread_mutex_t) );
+               pthread_mutexattr_t     attr;
+               pthread_mutexattr_init(&attr);
+               pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+               pthread_mutex_init(*MutexPtr, NULL);
+       }
+       if( pthread_mutex_lock(*MutexPtr) ) {
+               fprintf(stderr, "ERROR: Mutex pointer %p double locked\n", MutexPtr);
+               AcessNative_Exit();
        }
-       pthread_rwlock_wrlock( (void*)Lock->ReaderWaiting );
-       return 0;
 }
-void RWLock_Release(tRWLock *Lock)
+
+void Threads_int_ShortRel(void **MutexPtr)
 {
-       pthread_rwlock_unlock( (void*)Lock->ReaderWaiting );
+       pthread_mutex_unlock(*MutexPtr);
+}
+
+int Threads_int_ShortHas(void **Ptr)
+{
+       if( !*Ptr )
+               return 0;
+       int rv = pthread_mutex_trylock(*Ptr);
+       if( rv == 0 ) {
+               pthread_mutex_unlock(*Ptr);
+               return 0;
+       }
+       if( rv == EBUSY ) {
+               return 0;
+       }
+       return 1;
 }
 

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