Tools/nativelib - Threading fixes
[tpg/acess2.git] / Tools / nativelib / threads_int.c
index f25ab95..b2ad9f6 100644 (file)
@@ -8,12 +8,13 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdio.h>     // printf debugging
 #include <acess_logging.h>
 #include <threads_int.h>
 #include <pthread_weak.h>
+#include <shortlock.h>
 
 // === TYPES ===
-typedef struct sThread tThread;
 struct sThreadIntMutex { int lock; };
 struct sThreadIntSem { int val; };
 
@@ -22,7 +23,6 @@ struct sThreadIntSem { int val; };
 // === CODE ===
 int Threads_int_ThreadingEnabled(void)
 {
-       Log_Debug("Threads", "pthread_create = %p", pthread_create);
        return !!pthread_create;
 }
 
@@ -91,9 +91,9 @@ tThreadIntSem *Threads_int_SemCreate(void)
 
 void Threads_int_SemSignal(tThreadIntSem *Sem)
 {
-       if( sem_wait )
+       if( sem_post )
        {
-               sem_wait( (void*)Sem );
+               sem_post( (void*)Sem );
        }
        else
        {
@@ -103,9 +103,12 @@ void Threads_int_SemSignal(tThreadIntSem *Sem)
 
 void Threads_int_SemWaitAll(tThreadIntSem *Sem)
 {
-       if( sem_post )
+       if( sem_wait )
        {
-               sem_post( (void*)Sem );
+               // TODO: Handle multiples
+               sem_wait( (void*)Sem );
+               while( sem_trywait((void*)Sem) )
+                       ;
        }
        else
        {
@@ -139,3 +142,41 @@ int Threads_int_CreateThread(tThread *Thread)
        }
 }
 
+void SHORTLOCK(tShortSpinlock *Lock)
+{
+       if( !pthread_mutex_init )
+       {
+               if(*Lock)       Log_KernelPanic("---", "Double short lock");
+               *Lock = (void*)1;
+       }
+       else
+       {
+               if( !*Lock ) {
+                       *Lock = malloc(sizeof(pthread_mutex_t));
+                       pthread_mutex_init(*Lock, NULL);
+               }
+//             printf("%p: SHORTLOCK wait\n", lpThreads_This);
+               pthread_mutex_lock(*Lock);
+//             printf("%p: SHORTLOCK held %p\n", lpThreads_This, __builtin_return_address(0));
+       }
+}
+
+void SHORTREL(tShortSpinlock *Lock)
+{
+       if( !pthread_mutex_init )
+       {
+               if(!*Lock)      Log_Notice("---", "Short release when not held");
+               *Lock = NULL;
+       }
+       else
+       {
+               pthread_mutex_unlock(*Lock);
+//             printf("%p: SHORTLOCK rel\n", lpThreads_This);
+       }
+}
+
+int CPU_HAS_LOCK(tShortSpinlock *Lock)
+{
+       return 0;
+}
+

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