Implemented an API for thread-local storage: SDL_TLSCreate(), SDL_TLSSet(), SDL_TLSGet()

This commit is contained in:
Sam Lantinga 2013-07-10 02:32:04 -07:00
parent ab91b4ce14
commit bfcb08d569
14 changed files with 618 additions and 156 deletions

View file

@ -32,6 +32,7 @@
#include "SDL_error.h"
/* Thread synchronization primitives */
#include "SDL_atomic.h"
#include "SDL_mutex.h"
#include "begin_code.h"
@ -47,6 +48,9 @@ typedef struct SDL_Thread SDL_Thread;
/* The SDL thread ID */
typedef unsigned long SDL_threadID;
/* Thread local storage ID */
typedef int SDL_TLSID;
/* The SDL thread priority
*
* Note: On many systems you require special privileges to set high priority.
@ -166,6 +170,63 @@ extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
*/
extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
/**
* \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
*
* \return The newly created thread local storage identifier, or 0 on error
*
* \code
* static SDL_SpinLock tls_lock;
* static SDL_TLSID thread_local_storage;
*
* void SetMyThreadData(void *value)
* {
* if (!thread_local_storage) {
* SDL_AtomicLock(&tls_lock);
* if (!thread_local_storage) {
* thread_local_storage = SDL_TLSCreate();
* }
* SDL_AtomicUnLock(&tls_lock);
* }
* SDL_TLSSet(thread_local_storage, value);
* }
*
* void *GetMyThreadData(void)
* {
* return SDL_TLSGet(thread_local_storage);
* }
* \endcode
*
* \sa SDL_TLSGet()
* \sa SDL_TLSSet()
*/
extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate();
/**
* \brief Get the value associated with a thread local storage ID for the current thread.
*
* \param id The thread local storage ID
*
* \return The value associated with the ID for the current thread, or NULL if no value has been set.
*
* \sa SDL_TLSCreate()
* \sa SDL_TLSSet()
*/
extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
/**
* \brief Set the value associated with a thread local storage ID for the current thread.
*
* \param id The thread local storage ID
* \param value The value to associate with the ID for the current thread
*
* \return 0 on success, -1 on error
*
* \sa SDL_TLSCreate()
* \sa SDL_TLSGet()
*/
extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value);
/* Ends C function definitions when using C++ */
#ifdef __cplusplus