X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=AcessNative%2Facesskernel_src%2Fthreads_glue.c;fp=AcessNative%2Facesskernel_src%2Fthreads_glue.c;h=ba407b81262cc270b8edf8582425769a51d7928f;hb=510431249326d5a44fc502ed5cf798b79a442ce8;hp=0000000000000000000000000000000000000000;hpb=57e4db716b3e7db0be336abd9f256962e3b19aa3;p=tpg%2Facess2.git diff --git a/AcessNative/acesskernel_src/threads_glue.c b/AcessNative/acesskernel_src/threads_glue.c new file mode 100644 index 00000000..ba407b81 --- /dev/null +++ b/AcessNative/acesskernel_src/threads_glue.c @@ -0,0 +1,99 @@ +/* + * Acess2 Native Kernel + * - Acess kernel emulation on another OS using SDL and UDP + * + * threads.c + * - Thread and process handling + */ +#include "include/threads_glue.h" + +#define _ACESS_H +typedef void **tShortSpinlock; +#include + +// - Native headers +#include +#include +#include +//#include "/usr/include/signal.h" +#include +#include +#include + +#include // Kernel land, but uses standards + +// === CODE === +void Threads_Glue_Yield(void) +{ + SDL_Delay(10); +} + +void Threads_Glue_AcquireMutex(void **Lock) +{ + if(!*Lock) { + *Lock = malloc( sizeof(pthread_mutex_t) ); + pthread_mutex_init( *Lock, NULL ); + } + pthread_mutex_lock( *Lock ); +} + +void Threads_Glue_ReleaseMutex(void **Lock) +{ + pthread_mutex_unlock( *Lock ); +} + +void Threads_Glue_SemInit(void **Ptr, int Val) +{ + *Ptr = SDL_CreateSemaphore(Val); + if( !*Ptr ) { + Log_Warning("Threads", "Semaphore creation failed - %s", SDL_GetError()); + } +} + +int Threads_Glue_SemWait(void *Ptr, int Max) +{ + int have = 0; + assert(Ptr); + do { + if( SDL_SemWait( Ptr ) == -1 ) { + return -1; + } + have ++; + } while( SDL_SemValue(Ptr) && have < Max ); + return have; +} + +int Threads_Glue_SemSignal( void *Ptr, int AmmountToAdd ) +{ + int i; + for( i = 0; i < AmmountToAdd; i ++ ) + SDL_SemPost( Ptr ); + 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 ); +} +