From: John Hodge Date: Mon, 28 Jan 2013 06:09:55 +0000 (+0800) Subject: nativelib - Implimenting missing functions X-Git-Tag: rel0.15~597^2~7 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=c6bba3a75fd1a3773750ef0cdc1e7a5b811336b5;p=tpg%2Facess2.git nativelib - Implimenting missing functions --- diff --git a/Tools/nativelib/include/acess.h b/Tools/nativelib/include/acess.h index c7f77751..da1693fc 100644 --- a/Tools/nativelib/include/acess.h +++ b/Tools/nativelib/include/acess.h @@ -157,6 +157,7 @@ static inline int CPU_HAS_LOCK(tShortSpinlock *m) { return *m; } #endif static inline intptr_t MM_GetPhysAddr(void *Ptr) { return 1; } +static inline int MM_IsUser(const void *Ptr) { return 1; } #endif diff --git a/Tools/nativelib/include/threads_int.h b/Tools/nativelib/include/threads_int.h index cc0c3e05..240f5253 100644 --- a/Tools/nativelib/include/threads_int.h +++ b/Tools/nativelib/include/threads_int.h @@ -26,6 +26,7 @@ struct sProcess struct sThread { struct sThread *Next; + void *ThreadHandle; int TID; tThreadIntMutex *Protector; diff --git a/Tools/nativelib/threads_int.c b/Tools/nativelib/threads_int.c index 35125a17..2274eeaa 100644 --- a/Tools/nativelib/threads_int.c +++ b/Tools/nativelib/threads_int.c @@ -6,20 +6,98 @@ * - Internal threading functions */ #include +#include #include #include #include #include +// === TYPES === +typedef struct sThread tThread; +struct sThreadIntMutex { int lock; }; +struct sThreadIntSem { int val; }; + // === CODE === -void Threads_int_LockMutex(void *Mutex) +void Threads_int_MutexLock(tThreadIntMutex *Mutex) { + if( !Mutex ) { + return ; + } if( pthread_mutex_lock ) { + pthread_mutex_lock( (void*)Mutex ); + } + else + { + if( Mutex->lock ) + Log_KernelPanic("Threads", "Double mutex lock"); + Mutex->lock = 1; + } +} + +void Threads_int_MutexRelease(tThreadIntMutex *Mutex) +{ + if( !Mutex ) { + return ; + } + + if( pthread_mutex_unlock ) + { + pthread_mutex_unlock( (void*)Mutex ); + } + else + { + if( !Mutex->lock ) + Log_Notice("Threads", "Release of non-locked mutex %p", Mutex); + Mutex->lock = 0; + } +} + +void Threads_int_SemSignal(tThreadIntSem *Sem) +{ + if( sem_wait ) + { + sem_wait( (void*)Sem ); + } + else + { + Sem->val ++; + } +} + +void Threads_int_SemWaitAll(tThreadIntSem *Sem) +{ + if( sem_post ) + { + sem_post( (void*)Sem ); + } + else + { + if( !Sem->val ) + Log_KernelPanic("Threads", "Waiting on empty semaphre %p", Sem); + Sem->val = 0; + } +} + +void *Threads_int_ThreadRoot(void *ThreadPtr) +{ + tThread *thread = ThreadPtr; + thread->SpawnFcn(thread->SpawnData); + return NULL; +} + +int Threads_int_CreateThread(tThread *Thread) +{ + if( pthread_create ) + { + pthread_t *pthread = malloc(sizeof(pthread_t)); + Thread->ThreadHandle = pthread; + return pthread_create(pthread, NULL, Threads_int_ThreadRoot, Thread); } else { - + Log_KernelPanic("Threads", "Link with pthreads to use threading"); + return -1; } }