From efbf7167d3668adcc761af8c8d5419bf8bc52f32 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 8 Feb 2013 18:50:54 +0800 Subject: [PATCH] Tools/nativelib - Threading fixes --- Tools/nativelib/include/pthread_weak.h | 1 + Tools/nativelib/threads.c | 1 + Tools/nativelib/threads_int.c | 15 +++++++++++---- Tools/nativelib/time.c | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Tools/nativelib/include/pthread_weak.h b/Tools/nativelib/include/pthread_weak.h index 30ac2503..ccc4002d 100644 --- a/Tools/nativelib/include/pthread_weak.h +++ b/Tools/nativelib/include/pthread_weak.h @@ -18,6 +18,7 @@ extern int pthread_mutex_destroy (pthread_mutex_t*) __attribute__ ((weak)); extern int sem_init(sem_t *sem, int pshared, unsigned int value) __attribute__ ((weak)); extern int sem_wait(sem_t *sem) __attribute__ ((weak)); +extern int sem_trywait(sem_t *sem) __attribute__ ((weak)); extern int sem_post(sem_t *sem) __attribute__ ((weak)); extern int sem_getvalue(sem_t *sem, int *sval) __attribute__ ((weak)); diff --git a/Tools/nativelib/threads.c b/Tools/nativelib/threads.c index b698770a..aec7076e 100644 --- a/Tools/nativelib/threads.c +++ b/Tools/nativelib/threads.c @@ -23,6 +23,7 @@ tShortSpinlock glThreadListLock; void Threads_int_Init(void) { lpThreads_This = Threads_int_CreateTCB(NULL); + Threads_SetName("ThreadZero"); } tThread *Proc_GetCurThread(void) diff --git a/Tools/nativelib/threads_int.c b/Tools/nativelib/threads_int.c index 324b952d..b2ad9f66 100644 --- a/Tools/nativelib/threads_int.c +++ b/Tools/nativelib/threads_int.c @@ -8,6 +8,7 @@ #include #include #include +#include // printf debugging #include #include #include @@ -90,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 { @@ -102,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 { @@ -151,7 +155,9 @@ void SHORTLOCK(tShortSpinlock *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)); } } @@ -165,6 +171,7 @@ void SHORTREL(tShortSpinlock *Lock) else { pthread_mutex_unlock(*Lock); +// printf("%p: SHORTLOCK rel\n", lpThreads_This); } } diff --git a/Tools/nativelib/time.c b/Tools/nativelib/time.c index 4eb74631..f2f18d82 100644 --- a/Tools/nativelib/time.c +++ b/Tools/nativelib/time.c @@ -19,6 +19,7 @@ void Time_ScheduleTimer(tTimer *Timer, int Delta) if( !Timer ) { // SIGALRM + Log_Warning("Time", "TODO: Alarm event in %i ms", Delta); } else { -- 2.20.1