From 5e42401fdb1c83dd6b4d3870e8e4e242a58c640c Mon Sep 17 00:00:00 2001 From: meepingsnesroms Date: Wed, 17 Jan 2018 14:00:57 -0800 Subject: [PATCH] Add 3DS threads --- Makefile.ctr | 3 +- libretro-common/rthreads/ctr_pthread.h | 193 +++++++++++++++++++++++++ libretro-common/rthreads/rthreads.c | 4 +- translation/ocr_driver.c | 35 +++++ translation/ocr_driver.h | 6 +- 5 files changed, 238 insertions(+), 3 deletions(-) create mode 100644 libretro-common/rthreads/ctr_pthread.h diff --git a/Makefile.ctr b/Makefile.ctr index 9a2ee1cfc9..d47cc66c25 100644 --- a/Makefile.ctr +++ b/Makefile.ctr @@ -46,7 +46,7 @@ ifeq ($(GRIFFIN_BUILD), 1) OBJ += griffin/griffin.o DEFINES += -DHAVE_GRIFFIN=1 -DHAVE_MENU -DHAVE_RGUI -DHAVE_XMB -DHAVE_MATERIALUI -DHAVE_LIBRETRODB -DHAVE_CC_RESAMPLER DEFINES += -DHAVE_ZLIB -DHAVE_RPNG -DHAVE_RJPEG -DHAVE_RBMP -DHAVE_RTGA -DWANT_ZLIB - DEFINES += -DHAVE_NETWORKING -DHAVE_CHEEVOS -DHAVE_SOCKET_LEGACY + DEFINES += -DHAVE_NETWORKING -DHAVE_CHEEVOS -DHAVE_SOCKET_LEGACY -DHAVE_THREADS #-DHAVE_SSL -DMBEDTLS_SSL_DEBUG_ALL #ssl is currently incompatible with griffin due to use of the "static" flag on repeating functions that will conflict when included in one file else @@ -69,6 +69,7 @@ else HAVE_NETWORKING = 1 HAVE_CHEEVOS = 1 HAVE_SOCKET_LEGACY = 1 + HAVE_THREADS = 1 HAVE_SSL = 1 include Makefile.common diff --git a/libretro-common/rthreads/ctr_pthread.h b/libretro-common/rthreads/ctr_pthread.h new file mode 100644 index 0000000000..14e321344f --- /dev/null +++ b/libretro-common/rthreads/ctr_pthread.h @@ -0,0 +1,193 @@ +/* Copyright (C) 2010-2017 The RetroArch team + * + * --------------------------------------------------------------------------------------- + * The following license statement only applies to this file (gx_pthread.h). + * --------------------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, + * to any person obtaining a copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, + * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef _CTR_PTHREAD_WRAP_CTR_ +#define _CTR_PTHREAD_WRAP_CTR_ + +#include <3ds/thread.h> +#include <3ds/synchronization.h> +#include <3ds/svc.h> +#include +#include +#include + +#define STACKSIZE (4 * 1024) + +typedef Thread pthread_t; +typedef LightLock pthread_mutex_t; +typedef void* pthread_mutexattr_t; +typedef int pthread_attr_t; +typedef LightEvent pthread_cond_t; +typedef int pthread_condattr_t; + +/* libctru threads return void but pthreads return void pointer */ +bool mutex_inited = false; +LightLock safe_double_thread_launch; +void *(*start_routine_jump)(void*); + +static void ctr_thread_launcher(void* data) +{ + void *(*start_routine_jump_safe)(void*) = start_routine_jump; + LightLock_Unlock(&safe_double_thread_launch); + start_routine_jump_safe(data); +} + +static INLINE int pthread_create(pthread_t *thread, + const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) +{ + if (!mutex_inited) + { + LightLock_Init(&safe_double_thread_launch); + mutex_inited = true; + } + + /*Must wait if attempting to launch 2 threads at once to prevent corruption of function pointer*/ + while (LightLock_TryLock(&safe_double_thread_launch) != 0); + + s32 prio = 0; + svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); + start_routine_jump = start_routine; + thread = threadCreate(ctr_thread_launcher, arg, STACKSIZE, prio - 1, -1/*No affinity, use any CPU*/, false); + if (thread == NULL) + { + LightLock_Unlock(&safe_double_thread_launch); + return EAGAIN; + } + return 0; +} + +static INLINE pthread_t pthread_self(void) +{ + return threadGetCurrent(); +} + +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_destroy(pthread_mutex_t *mutex) +{ + /*Nothing to destroy*/ + return 0; +} + +static INLINE int pthread_mutex_lock(pthread_mutex_t *mutex) +{ + return LightLock_TryLock(mutex); +} + +static INLINE int pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + LightLock_Unlock(mutex); + return 0; +} + +static INLINE void pthread_exit(void *retval) +{ + /*Yes the pointer to int cast is not ideal*/ + /*threadExit((int)retval);*/ + (void)retval; +} + +static INLINE int pthread_detach(pthread_t thread) +{ + /* FIXME: pthread_detach equivalent missing? */ + (void)thread; + return 0; +} + +static INLINE int pthread_join(pthread_t thread, void **retval) +{ + /*retval is ignored*/ + return threadJoin(thread, U64_MAX); +} + +static INLINE int pthread_mutex_trylock(pthread_mutex_t *mutex) +{ + return LightLock_TryLock(mutex); +} + +static INLINE int pthread_cond_wait(pthread_cond_t *cond, + pthread_mutex_t *mutex) +{ + LightEvent_Wait(cond); + return 0; +} + +static INLINE int pthread_cond_timedwait(pthread_cond_t *cond, + pthread_mutex_t *mutex, const struct timespec *abstime) +{ + while (true) + { + struct timespec now = {0}; + /* Missing clock_gettime*/ + struct timeval tm; + gettimeofday(&tm, NULL); + now.tv_sec = tm.tv_sec; + now.tv_nsec = tm.tv_usec * 1000; + if (LightEvent_TryWait(cond) != 0 || now.tv_sec > abstime->tv_sec || (now.tv_sec == abstime->tv_sec && now.tv_nsec > abstime->tv_nsec)) + { + break; + } + } + + return 0; +} + +static INLINE int pthread_cond_init(pthread_cond_t *cond, + const pthread_condattr_t *attr) +{ + LightEvent_Init(cond, RESET_ONESHOT); + return 0; +} + +static INLINE int pthread_cond_signal(pthread_cond_t *cond) +{ + LightEvent_Signal(cond); + return 0; +} + +static INLINE int pthread_cond_broadcast(pthread_cond_t *cond) +{ + LightEvent_Signal(cond); + return 0; +} + +static INLINE int pthread_cond_destroy(pthread_cond_t *cond) +{ + /*nothing to destroy*/ + return 0; +} + +static INLINE int pthread_equal(pthread_t t1, pthread_t t2) +{ + if (threadGetHandle(t1) == threadGetHandle(t2)) + { + return 1; + } + return 0; +} + +#endif diff --git a/libretro-common/rthreads/rthreads.c b/libretro-common/rthreads/rthreads.c index a49c485242..bddf2f4763 100644 --- a/libretro-common/rthreads/rthreads.c +++ b/libretro-common/rthreads/rthreads.c @@ -47,6 +47,8 @@ #endif #elif defined(GEKKO) #include "gx_pthread.h" +#elif defined(_3DS) +#include "ctr_pthread.h" #elif defined(__CELLOS_LV2__) #include #include @@ -801,7 +803,7 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) sys_time_get_current_time(&s, &n); now.tv_sec = s; now.tv_nsec = n; -#elif defined(__mips__) || defined(VITA) +#elif defined(__mips__) || defined(VITA) || defined(_3DS) struct timeval tm; gettimeofday(&tm, NULL); diff --git a/translation/ocr_driver.c b/translation/ocr_driver.c index e69de29bb2..fd64d70671 100644 --- a/translation/ocr_driver.c +++ b/translation/ocr_driver.c @@ -0,0 +1,35 @@ +#include "ocr_driver.h" + +static const ocr_driver_t *ocr_backends[] = { +#ifdef HAVE_TESSERACT + &ocr_tesseract, +#endif + &ocr_null, + NULL +}; + +static const ocr_driver_t *current_ocr_backend = NULL; + +bool ocr_driver_init(void) +{ + /*TODO: find name of active driver*/ + + bool success = false; + if (current_ocr_backend) + success = (*current_ocr_backend->init)(); + return success; +} + +void ocr_driver_free(void) +{ + if (current_ocr_backend) + (*current_ocr_backend->free)(); +} + +char* ocr_driver_get_text(struct ocr_image_info image) +{ + char* image_string = NULL; + if (current_ocr_backend) + image_string = (*current_ocr_backend->get_text)(image); + return image_string; +} \ No newline at end of file diff --git a/translation/ocr_driver.h b/translation/ocr_driver.h index 2bbfbf4805..6c932914ee 100644 --- a/translation/ocr_driver.h +++ b/translation/ocr_driver.h @@ -20,9 +20,13 @@ typedef struct ocr_driver const char *ident; } ocr_driver_t; +#ifdef HAVE_TESSERACT extern const ocr_driver_t ocr_tesseract; +#endif extern const ocr_driver_t ocr_null; -char* ocr_get_text(struct ocr_image_info image); +bool ocr_driver_init(void); +void ocr_driver_free(void); +char* ocr_driver_get_text(struct ocr_image_info image); #endif \ No newline at end of file