X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Tools%2Fnativelib%2Fthreads_int.c;h=b2ad9f661667835a6359bb9157772f35d054baaa;hb=d1e3e105c7605d0c9f02dd7e2f3b1377ea61aea4;hp=2274eeaab982748c0383a93f22850e6886be6538;hpb=c6bba3a75fd1a3773750ef0cdc1e7a5b811336b5;p=tpg%2Facess2.git diff --git a/Tools/nativelib/threads_int.c b/Tools/nativelib/threads_int.c index 2274eeaa..b2ad9f66 100644 --- a/Tools/nativelib/threads_int.c +++ b/Tools/nativelib/threads_int.c @@ -8,16 +8,38 @@ #include #include #include +#include // printf debugging #include #include #include +#include // === TYPES === -typedef struct sThread tThread; struct sThreadIntMutex { int lock; }; struct sThreadIntSem { int val; }; +// === PROTOTYPES === + // === CODE === +int Threads_int_ThreadingEnabled(void) +{ + return !!pthread_create; +} + +tThreadIntMutex *Threads_int_MutexCreate(void) +{ + if( pthread_mutex_init ) + { + tThreadIntMutex *ret = malloc(sizeof(pthread_mutex_t)); + pthread_mutex_init( (void*)ret, NULL ); + return ret; + } + else + { + return calloc(sizeof(tThreadIntMutex), 1); + } +} + void Threads_int_MutexLock(tThreadIntMutex *Mutex) { if( !Mutex ) { @@ -53,11 +75,25 @@ void Threads_int_MutexRelease(tThreadIntMutex *Mutex) } } +tThreadIntSem *Threads_int_SemCreate(void) +{ + if( sem_init ) + { + tThreadIntSem *ret = malloc(sizeof(sem_t)); + sem_init( (void*)ret, 0, 0 ); + return ret; + } + else + { + return calloc(sizeof(tThreadIntSem), 1); + } +} + void Threads_int_SemSignal(tThreadIntSem *Sem) { - if( sem_wait ) + if( sem_post ) { - sem_wait( (void*)Sem ); + sem_post( (void*)Sem ); } else { @@ -67,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 { @@ -82,6 +121,8 @@ void Threads_int_SemWaitAll(tThreadIntSem *Sem) void *Threads_int_ThreadRoot(void *ThreadPtr) { tThread *thread = ThreadPtr; + lpThreads_This = thread; + Log_Debug("Threads", "SpawnFcn: %p, SpawnData: %p", thread->SpawnFcn, thread->SpawnData); thread->SpawnFcn(thread->SpawnData); return NULL; } @@ -92,7 +133,7 @@ int Threads_int_CreateThread(tThread *Thread) { pthread_t *pthread = malloc(sizeof(pthread_t)); Thread->ThreadHandle = pthread; - return pthread_create(pthread, NULL, Threads_int_ThreadRoot, Thread); + return pthread_create(pthread, NULL, &Threads_int_ThreadRoot, Thread); } else { @@ -101,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; +} +