include/SDL_thread.h
changeset 7391 a29895dc5e9a
parent 7191 75360622e65f
child 7393 358696c354a8
     1.1 --- a/include/SDL_thread.h	Tue Jul 09 12:58:54 2013 -0700
     1.2 +++ b/include/SDL_thread.h	Wed Jul 10 02:32:04 2013 -0700
     1.3 @@ -32,6 +32,7 @@
     1.4  #include "SDL_error.h"
     1.5  
     1.6  /* Thread synchronization primitives */
     1.7 +#include "SDL_atomic.h"
     1.8  #include "SDL_mutex.h"
     1.9  
    1.10  #include "begin_code.h"
    1.11 @@ -47,6 +48,9 @@
    1.12  /* The SDL thread ID */
    1.13  typedef unsigned long SDL_threadID;
    1.14  
    1.15 +/* Thread local storage ID */
    1.16 +typedef int SDL_TLSID;
    1.17 +
    1.18  /* The SDL thread priority
    1.19   *
    1.20   * Note: On many systems you require special privileges to set high priority.
    1.21 @@ -166,6 +170,63 @@
    1.22   */
    1.23  extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
    1.24  
    1.25 +/**
    1.26 + *  \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
    1.27 + *
    1.28 + *  \return The newly created thread local storage identifier, or 0 on error
    1.29 + *
    1.30 + *  \code
    1.31 + *  static SDL_SpinLock tls_lock;
    1.32 + *  static SDL_TLSID thread_local_storage;
    1.33 + * 
    1.34 + *  void SetMyThreadData(void *value)
    1.35 + *  {
    1.36 + *      if (!thread_local_storage) {
    1.37 + *          SDL_AtomicLock(&tls_lock);
    1.38 + *          if (!thread_local_storage) {
    1.39 + *              thread_local_storage = SDL_TLSCreate();
    1.40 + *          }
    1.41 + *          SDL_AtomicUnLock(&tls_lock);
    1.42 + *      }
    1.43 + *      SDL_TLSSet(thread_local_storage, value);
    1.44 + *  }
    1.45 + *  
    1.46 + *  void *GetMyThreadData(void)
    1.47 + *  {
    1.48 + *      return SDL_TLSGet(thread_local_storage);
    1.49 + *  }
    1.50 + *  \endcode
    1.51 + *
    1.52 + *  \sa SDL_TLSGet()
    1.53 + *  \sa SDL_TLSSet()
    1.54 + */
    1.55 +extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate();
    1.56 +
    1.57 +/**
    1.58 + *  \brief Get the value associated with a thread local storage ID for the current thread.
    1.59 + *
    1.60 + *  \param id The thread local storage ID
    1.61 + *
    1.62 + *  \return The value associated with the ID for the current thread, or NULL if no value has been set.
    1.63 + *
    1.64 + *  \sa SDL_TLSCreate()
    1.65 + *  \sa SDL_TLSSet()
    1.66 + */
    1.67 +extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
    1.68 +
    1.69 +/**
    1.70 + *  \brief Set the value associated with a thread local storage ID for the current thread.
    1.71 + *
    1.72 + *  \param id The thread local storage ID
    1.73 + *  \param value The value to associate with the ID for the current thread
    1.74 + *
    1.75 + *  \return 0 on success, -1 on error
    1.76 + *
    1.77 + *  \sa SDL_TLSCreate()
    1.78 + *  \sa SDL_TLSGet()
    1.79 + */
    1.80 +extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value);
    1.81 +
    1.82  
    1.83  /* Ends C function definitions when using C++ */
    1.84  #ifdef __cplusplus