From 31f52a9f9ab5c7090f67cb6a80bac09bbb47dbf2 Mon Sep 17 00:00:00 2001 From: Justin Weiss Date: Mon, 17 Aug 2020 19:19:21 -0700 Subject: [3DS] Support latest libctru --- frontend/3ds/pthread.h | 63 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 21 deletions(-) (limited to 'frontend/3ds/pthread.h') diff --git a/frontend/3ds/pthread.h b/frontend/3ds/pthread.h index cc3c965..4c58fe5 100644 --- a/frontend/3ds/pthread.h +++ b/frontend/3ds/pthread.h @@ -11,20 +11,26 @@ #define CTR_PTHREAD_STACK_SIZE 0x10000 #define FALSE 0 -typedef int32_t pthread_t; -typedef int pthread_attr_t; - -typedef LightLock pthread_mutex_t; -typedef int pthread_mutexattr_t; - typedef struct { uint32_t semaphore; LightLock lock; uint32_t waiting; -} pthread_cond_t; +} cond_t; + +#if !defined(PTHREAD_SCOPE_PROCESS) +/* An earlier version of devkitARM does not define the pthread types. Can remove in r54+. */ + +typedef uint32_t pthread_t; +typedef int pthread_attr_t; +typedef LightLock pthread_mutex_t; +typedef int pthread_mutexattr_t; + +typedef uint32_t pthread_cond_t; typedef int pthread_condattr_t; +#endif + static inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) { @@ -80,41 +86,56 @@ static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) { } static inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { - if (svcCreateSemaphore(&cond->semaphore, 0, 1)) + cond_t *cond_data = calloc(1, sizeof(cond_t)); + if (!cond_data) goto error; - LightLock_Init(&cond->lock); - cond->waiting = 0; + if (svcCreateSemaphore(&cond_data->semaphore, 0, 1)) + goto error; + + LightLock_Init(&cond_data->lock); + cond_data->waiting = 0; + *cond = cond_data; return 0; error: - svcCloseHandle(cond->semaphore); + svcCloseHandle(cond_data->semaphore); + if (cond_data) + free(cond_data); return -1; } static inline int pthread_cond_signal(pthread_cond_t *cond) { int32_t count; - LightLock_Lock(&cond->lock); - if (cond->waiting) { - cond->waiting--; - svcReleaseSemaphore(&count, cond->semaphore, 1); + cond_t *cond_data = (cond_t *)*cond; + LightLock_Lock(&cond_data->lock); + if (cond_data->waiting) { + cond_data->waiting--; + svcReleaseSemaphore(&count, cond_data->semaphore, 1); } - LightLock_Unlock(&cond->lock); + LightLock_Unlock(&cond_data->lock); return 0; } static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *lock) { - LightLock_Lock(&cond->lock); - cond->waiting++; + cond_t *cond_data = (cond_t *)*cond; + LightLock_Lock(&cond_data->lock); + cond_data->waiting++; LightLock_Unlock(lock); - LightLock_Unlock(&cond->lock); - svcWaitSynchronization(cond->semaphore, INT64_MAX); + LightLock_Unlock(&cond_data->lock); + svcWaitSynchronization(cond_data->semaphore, INT64_MAX); LightLock_Lock(lock); return 0; } static inline int pthread_cond_destroy(pthread_cond_t *cond) { - svcCloseHandle(cond->semaphore); + if (*cond) { + cond_t *cond_data = (cond_t *)*cond; + + svcCloseHandle(cond_data->semaphore); + free(*cond); + } + *cond = 0; return 0; } -- cgit v1.2.3