From 471ff4a9998aed7b5b9a25217d35c458b1f86d2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Mon, 17 Oct 2016 20:49:37 -0300 Subject: [PATCH] (libretro-common) Add thread local storage --- libretro-common/include/rthreads/rthreads.h | 41 +++++++++++++++++++++ libretro-common/rthreads/rthreads.c | 36 ++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/libretro-common/include/rthreads/rthreads.h b/libretro-common/include/rthreads/rthreads.h index a960d7f8ec..86d1fde8b0 100644 --- a/libretro-common/include/rthreads/rthreads.h +++ b/libretro-common/include/rthreads/rthreads.h @@ -35,6 +35,7 @@ RETRO_BEGIN_DECLS typedef struct sthread sthread_t; typedef struct slock slock_t; typedef struct scond scond_t; +typedef unsigned sthread_tls_t; /** * sthread_create: @@ -178,6 +179,46 @@ int scond_broadcast(scond_t *cond); **/ void scond_signal(scond_t *cond); +/** + * @brief Creates a thread local storage key + * + * This function shall create thread-specific data key visible to all threads in + * the process. The same key can be used by multiple threads to store + * thread-local data. + * + * When the key is created NULL shall be associated with it in all active + * threads. Whenever a new thread is spawned the all defined keys will be + * associated with NULL on that thread. + * + * @param tls + * @return whether the operation suceeded or not + */ +bool sthread_tls_create(sthread_tls_t *tls); + +/** + * @brief Deletes a thread local storage + * @param tls + * @return whether the operation suceeded or not + */ +bool sthread_tls_delete(sthread_tls_t *tls); + +/** + * @brief Retrieves thread specific data associated with a key + * + * There is no way to tell whether this function failed. + * + * @param tls + * @return + */ +void *sthread_tls_get(sthread_tls_t *tls); + +/** + * @brief Binds thread specific data to a key + * @param tls + * @return whether the operation suceeded or not + */ +bool sthread_tls_set(sthread_tls_t *tls, const void *data); + RETRO_END_DECLS #endif diff --git a/libretro-common/rthreads/rthreads.c b/libretro-common/rthreads/rthreads.c index 1c17d650b7..aca3bd1e88 100644 --- a/libretro-common/rthreads/rthreads.c +++ b/libretro-common/rthreads/rthreads.c @@ -474,3 +474,39 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) return (ret == 0); #endif } + +bool sthread_tls_create(sthread_tls_t *tls) +{ +#ifdef USE_WIN32_THREADS + return (*tls = TlsAlloc()) != TLS_OUT_OF_INDEXES; +#else + return pthread_key_create(tls, NULL) == 0; +#endif +} + +bool sthread_tls_delete(sthread_tls_t *tls) +{ +#ifdef USE_WIN32_THREADS + return TlsFree(*tls) != 0; +#else + return pthread_key_delete(*tls) == 0; +#endif +} + +void *sthread_tls_get(sthread_tls_t *tls) +{ +#ifdef USE_WIN32_THREADS + return TlsGetValue(*tls); +#else + return pthread_getspecific(*tls); +#endif +} + +bool sthread_tls_set(sthread_tls_t *tls, const void *data) +{ +#ifdef USE_WIN32_THREADS + return TlsSetValue(*tls, (void*)data) != 0; +#else + return pthread_setspecific(*tls, data) == 0; +#endif +}