X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=frontend%2F3ds%2Fpthread.h;h=4c58fe5ea8434fccc27d843d830b23138ae14190;hb=7c455640bf54adc32e13e76a2f8a6f84b4e972a8;hp=56d9085212b0a9d4ad32eec0a33cfb9216cc296d;hpb=4e45781a20e189ed5efbe471ce6343c571385646;p=pcsx_rearmed.git diff --git a/frontend/3ds/pthread.h b/frontend/3ds/pthread.h index 56d90852..4c58fe5e 100644 --- a/frontend/3ds/pthread.h +++ b/frontend/3ds/pthread.h @@ -2,32 +2,47 @@ #ifndef _3DS_PTHREAD_WRAP__ #define _3DS_PTHREAD_WRAP__ -#include "3ds.h" -#include "stdlib.h" -#include "string.h" -#include "stdio.h" +#include +#include +#include +#include "3ds_utils.h" #define CTR_PTHREAD_STACK_SIZE 0x10000 +#define FALSE 0 -typedef struct -{ - Handle handle; - u32* stack; -}pthread_t; +typedef struct { + uint32_t semaphore; + LightLock lock; + uint32_t waiting; +} 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) { + int procnum = -2; // use default cpu + bool isNew3DS; + APT_CheckNew3DS(&isNew3DS); - thread->stack = linearMemAlign(CTR_PTHREAD_STACK_SIZE, 8); - - svcCreateThread(&thread->handle, (ThreadFunc)start_routine,arg, - (u32*)((u32)thread->stack + CTR_PTHREAD_STACK_SIZE), - 0x25, 1); + if (isNew3DS) + procnum = 2; - return 1; + *thread = threadCreate(start_routine, arg, CTR_PTHREAD_STACK_SIZE, 0x25, procnum, FALSE); + return 0; } @@ -35,10 +50,10 @@ static inline int pthread_join(pthread_t thread, void **retval) { (void)retval; - if(svcWaitSynchronization(thread.handle, INT64_MAX)) + if(threadJoin(thread, INT64_MAX)) return -1; - linearFree(thread.stack); + threadFree(thread); return 0; } @@ -48,7 +63,80 @@ static inline void pthread_exit(void *retval) { (void)retval; - svcExitThread(); + threadExit(0); +} + +static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { + LightLock_Init(mutex); + return 0; +} + +static inline int pthread_mutex_lock(pthread_mutex_t *mutex) { + LightLock_Lock(mutex); + return 0; +} + +static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) { + LightLock_Unlock(mutex); + return 0; +} + +static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) { + return 0; +} + +static inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr) { + cond_t *cond_data = calloc(1, sizeof(cond_t)); + if (!cond_data) + goto error; + + 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_data->semaphore); + if (cond_data) + free(cond_data); + return -1; +} + +static inline int pthread_cond_signal(pthread_cond_t *cond) { + int32_t count; + 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_data->lock); + return 0; +} + +static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *lock) { + cond_t *cond_data = (cond_t *)*cond; + LightLock_Lock(&cond_data->lock); + cond_data->waiting++; + LightLock_Unlock(lock); + 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) { + if (*cond) { + cond_t *cond_data = (cond_t *)*cond; + + svcCloseHandle(cond_data->semaphore); + free(*cond); + } + *cond = 0; + return 0; }