(libretro-common) Add thread local storage

This commit is contained in:
Higor Eurípedes 2016-10-17 20:49:37 -03:00
parent d66bfc114e
commit 471ff4a999
2 changed files with 77 additions and 0 deletions

View File

@ -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

View File

@ -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
}