From 60d5004d14dd64a86a36af4320dd1321441f0cb2 Mon Sep 17 00:00:00 2001 From: zeromus Date: Mon, 21 Mar 2016 01:33:12 +0000 Subject: [PATCH] add sthread_isself --- .../include/rthreads/rthreads.h | 13 ++++ .../src/libretro-common/rthreads/gx_pthread.h | 6 ++ .../libretro-common/rthreads/psp_pthread.h | 7 +++ .../src/libretro-common/rthreads/rthreads.c | 61 +++++++++++++------ 4 files changed, 69 insertions(+), 18 deletions(-) diff --git a/desmume/src/libretro-common/include/rthreads/rthreads.h b/desmume/src/libretro-common/include/rthreads/rthreads.h index 90ae90fde..196af8c27 100644 --- a/desmume/src/libretro-common/include/rthreads/rthreads.h +++ b/desmume/src/libretro-common/include/rthreads/rthreads.h @@ -74,6 +74,19 @@ int sthread_detach(sthread_t *thread); */ void sthread_join(sthread_t *thread); +/** + * sthread_isself: + * @thread : pointer to thread object + * + * Join with a terminated thread. Waits for the thread specified by + * @thread to terminate. If that thread has already terminated, then + * it will return immediately. The thread specified by @thread must + * be joinable. + * + * Returns: true (1) if calling thread is the specified thread + */ +bool sthread_isself(sthread_t *thread); + /** * slock_new: * diff --git a/desmume/src/libretro-common/rthreads/gx_pthread.h b/desmume/src/libretro-common/rthreads/gx_pthread.h index 7a9573c24..6c2a3ed3a 100644 --- a/desmume/src/libretro-common/rthreads/gx_pthread.h +++ b/desmume/src/libretro-common/rthreads/gx_pthread.h @@ -107,6 +107,12 @@ static INLINE int pthread_create(pthread_t *thread, 0, STACKSIZE, 64, 0 /* unused */); } +static INLINE pthread_t pthread_self(void) +{ + /* zero 20-mar-2016: untested */ + return LWP_GetSelf(); +} + static INLINE int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { diff --git a/desmume/src/libretro-common/rthreads/psp_pthread.h b/desmume/src/libretro-common/rthreads/psp_pthread.h index 9d3032f80..5e262b7bf 100644 --- a/desmume/src/libretro-common/rthreads/psp_pthread.h +++ b/desmume/src/libretro-common/rthreads/psp_pthread.h @@ -190,4 +190,11 @@ static INLINE void pthread_exit(void *retval) (void)retval; } +static INLINE pthread_t pthread_self(void) +{ + /* zero 20-mar-2016: untested */ + return sceKernelGetThreadId(); +} + + #endif //_PSP_PTHREAD_WRAP__ diff --git a/desmume/src/libretro-common/rthreads/rthreads.c b/desmume/src/libretro-common/rthreads/rthreads.c index 9f3625c99..4fc312183 100644 --- a/desmume/src/libretro-common/rthreads/rthreads.c +++ b/desmume/src/libretro-common/rthreads/rthreads.c @@ -25,7 +25,10 @@ #include #include -#if defined(_WIN32) +/* with RETRO_WIN32_USE_PTHREADS, pthreads can be used even on win32. Maybe only supported in MSVC>=2005 */ + +#if defined(_WIN32) && !defined(RETRO_WIN32_USE_PTHREADS) +#define USE_WIN32_THREADS #ifdef _XBOX #include #else @@ -54,7 +57,7 @@ struct thread_data struct sthread { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS HANDLE thread; #else pthread_t id; @@ -63,7 +66,7 @@ struct sthread struct slock { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS HANDLE lock; #else pthread_mutex_t lock; @@ -72,14 +75,14 @@ struct slock struct scond { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS HANDLE event; #else pthread_cond_t cond; #endif }; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS static DWORD CALLBACK thread_wrap(void *data_) #else static void *thread_wrap(void *data_) @@ -119,7 +122,7 @@ sthread_t *sthread_create(void (*thread_func)(void*), void *userdata) data->func = thread_func; data->userdata = userdata; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS thread->thread = CreateThread(NULL, 0, thread_wrap, data, 0, NULL); thread_created = thread->thread; #else @@ -151,7 +154,7 @@ error: */ int sthread_detach(sthread_t *thread) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS CloseHandle(thread->thread); free(thread); return 0; @@ -173,7 +176,7 @@ int sthread_detach(sthread_t *thread) */ void sthread_join(sthread_t *thread) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS WaitForSingleObject(thread->thread, INFINITE); CloseHandle(thread->thread); #else @@ -182,6 +185,26 @@ void sthread_join(sthread_t *thread) free(thread); } +/** + * sthread_isself: + * @thread : pointer to thread object + * + * Join with a terminated thread. Waits for the thread specified by + * @thread to terminate. If that thread has already terminated, then + * it will return immediately. The thread specified by @thread must + * be joinable. + * + * Returns: true (1) if calling thread is the specified thread + */ +bool sthread_isself(sthread_t *thread) +{ +#ifdef USE_WIN32_THREADS + return GetCurrentThread() == thread->thread; +#else + return pthread_equal(pthread_self(),thread->id); +#endif +} + /** * slock_new: * @@ -197,7 +220,7 @@ slock_t *slock_new(void) if (!lock) return NULL; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS lock->lock = CreateMutex(NULL, FALSE, NULL); mutex_created = lock->lock; #else @@ -225,7 +248,7 @@ void slock_free(slock_t *lock) if (!lock) return; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS CloseHandle(lock->lock); #else pthread_mutex_destroy(&lock->lock); @@ -243,7 +266,7 @@ void slock_free(slock_t *lock) **/ void slock_lock(slock_t *lock) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS WaitForSingleObject(lock->lock, INFINITE); #else pthread_mutex_lock(&lock->lock); @@ -258,7 +281,7 @@ void slock_lock(slock_t *lock) **/ void slock_unlock(slock_t *lock) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS ReleaseMutex(lock->lock); #else pthread_mutex_unlock(&lock->lock); @@ -282,7 +305,7 @@ scond_t *scond_new(void) if (!cond) return NULL; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS cond->event = CreateEvent(NULL, FALSE, FALSE, NULL); event_created = cond->event; #else @@ -310,7 +333,7 @@ void scond_free(scond_t *cond) if (!cond) return; -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS CloseHandle(cond->event); #else pthread_cond_destroy(&cond->cond); @@ -327,7 +350,7 @@ void scond_free(scond_t *cond) **/ void scond_wait(scond_t *cond, slock_t *lock) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS WaitForSingleObject(cond->event, 0); SignalObjectAndWait(lock->lock, cond->event, INFINITE, FALSE); @@ -346,7 +369,7 @@ void scond_wait(scond_t *cond, slock_t *lock) **/ int scond_broadcast(scond_t *cond) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS /* FIXME _- check how this function should differ * from scond_signal implementation. */ SetEvent(cond->event); @@ -365,7 +388,7 @@ int scond_broadcast(scond_t *cond) **/ void scond_signal(scond_t *cond) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS SetEvent(cond->event); #else pthread_cond_signal(&cond->cond); @@ -386,7 +409,7 @@ void scond_signal(scond_t *cond) **/ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) { -#ifdef _WIN32 +#ifdef USE_WIN32_THREADS DWORD ret; WaitForSingleObject(cond->event, 0); @@ -423,6 +446,8 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) gettimeofday(&tm, NULL); now.tv_sec = tm.tv_sec; now.tv_nsec = tm.tv_usec * 1000; +#elif defined(RETRO_WIN32_USE_PTHREADS) + _ftime64_s(&now); #elif !defined(GEKKO) /* timeout on libogc is duration, not end time. */ clock_gettime(CLOCK_REALTIME, &now);