SDL_thread.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef _SDL_thread_h
  19. #define _SDL_thread_h
  20. /**
  21. * \file SDL_thread.h
  22. *
  23. * Header for the SDL thread management routines.
  24. */
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. /* Thread synchronization primitives */
  28. #include "SDL_atomic.h"
  29. #include "SDL_mutex.h"
  30. #include "begin_code.h"
  31. /* Set up for C function definitions, even when using C++ */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* The SDL thread structure, defined in SDL_thread.c */
  36. struct SDL_Thread;
  37. typedef struct SDL_Thread SDL_Thread;
  38. /* The SDL thread ID */
  39. typedef unsigned long SDL_threadID;
  40. /* Thread local storage ID, 0 is the invalid ID */
  41. typedef unsigned int SDL_TLSID;
  42. /**
  43. * The SDL thread priority.
  44. *
  45. * \note On many systems you require special privileges to set high priority.
  46. */
  47. typedef enum {
  48. SDL_THREAD_PRIORITY_LOW,
  49. SDL_THREAD_PRIORITY_NORMAL,
  50. SDL_THREAD_PRIORITY_HIGH
  51. } SDL_ThreadPriority;
  52. /**
  53. * The function passed to SDL_CreateThread().
  54. * It is passed a void* user context parameter and returns an int.
  55. */
  56. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  57. #if defined(__WIN32__) && !defined(HAVE_LIBC)
  58. /**
  59. * \file SDL_thread.h
  60. *
  61. * We compile SDL into a DLL. This means, that it's the DLL which
  62. * creates a new thread for the calling process with the SDL_CreateThread()
  63. * API. There is a problem with this, that only the RTL of the SDL.DLL will
  64. * be initialized for those threads, and not the RTL of the calling
  65. * application!
  66. *
  67. * To solve this, we make a little hack here.
  68. *
  69. * We'll always use the caller's _beginthread() and _endthread() APIs to
  70. * start a new thread. This way, if it's the SDL.DLL which uses this API,
  71. * then the RTL of SDL.DLL will be used to create the new thread, and if it's
  72. * the application, then the RTL of the application will be used.
  73. *
  74. * So, in short:
  75. * Always use the _beginthread() and _endthread() of the calling runtime
  76. * library!
  77. */
  78. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  79. #include <process.h> /* This has _beginthread() and _endthread() defined! */
  80. typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
  81. unsigned (__stdcall *
  82. func) (void
  83. *),
  84. void *arg, unsigned,
  85. unsigned *threadID);
  86. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  87. /**
  88. * Create a thread.
  89. */
  90. extern DECLSPEC SDL_Thread *SDLCALL
  91. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  92. pfnSDL_CurrentBeginThread pfnBeginThread,
  93. pfnSDL_CurrentEndThread pfnEndThread);
  94. /**
  95. * Create a thread.
  96. */
  97. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  98. #else
  99. /**
  100. * Create a thread.
  101. *
  102. * Thread naming is a little complicated: Most systems have very small
  103. * limits for the string length (BeOS has 32 bytes, Linux currently has 16,
  104. * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
  105. * have to see what happens with your system's debugger. The name should be
  106. * UTF-8 (but using the naming limits of C identifiers is a better bet).
  107. * There are no requirements for thread naming conventions, so long as the
  108. * string is null-terminated UTF-8, but these guidelines are helpful in
  109. * choosing a name:
  110. *
  111. * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
  112. *
  113. * If a system imposes requirements, SDL will try to munge the string for
  114. * it (truncate, etc), but the original string contents will be available
  115. * from SDL_GetThreadName().
  116. */
  117. extern DECLSPEC SDL_Thread *SDLCALL
  118. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  119. #endif
  120. /**
  121. * Get the thread name, as it was specified in SDL_CreateThread().
  122. * This function returns a pointer to a UTF-8 string that names the
  123. * specified thread, or NULL if it doesn't have a name. This is internal
  124. * memory, not to be free()'d by the caller, and remains valid until the
  125. * specified thread is cleaned up by SDL_WaitThread().
  126. */
  127. extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  128. /**
  129. * Get the thread identifier for the current thread.
  130. */
  131. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  132. /**
  133. * Get the thread identifier for the specified thread.
  134. *
  135. * Equivalent to SDL_ThreadID() if the specified thread is NULL.
  136. */
  137. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  138. /**
  139. * Set the priority for the current thread
  140. */
  141. extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  142. /**
  143. * Wait for a thread to finish.
  144. *
  145. * The return code for the thread function is placed in the area
  146. * pointed to by \c status, if \c status is not NULL.
  147. */
  148. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  149. /**
  150. * \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
  151. *
  152. * \return The newly created thread local storage identifier, or 0 on error
  153. *
  154. * \code
  155. * static SDL_SpinLock tls_lock;
  156. * static SDL_TLSID thread_local_storage;
  157. *
  158. * void SetMyThreadData(void *value)
  159. * {
  160. * if (!thread_local_storage) {
  161. * SDL_AtomicLock(&tls_lock);
  162. * if (!thread_local_storage) {
  163. * thread_local_storage = SDL_TLSCreate();
  164. * }
  165. * SDL_AtomicUnLock(&tls_lock);
  166. * }
  167. * SDL_TLSSet(thread_local_storage, value);
  168. * }
  169. *
  170. * void *GetMyThreadData(void)
  171. * {
  172. * return SDL_TLSGet(thread_local_storage);
  173. * }
  174. * \endcode
  175. *
  176. * \sa SDL_TLSGet()
  177. * \sa SDL_TLSSet()
  178. */
  179. extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
  180. /**
  181. * \brief Get the value associated with a thread local storage ID for the current thread.
  182. *
  183. * \param id The thread local storage ID
  184. *
  185. * \return The value associated with the ID for the current thread, or NULL if no value has been set.
  186. *
  187. * \sa SDL_TLSCreate()
  188. * \sa SDL_TLSSet()
  189. */
  190. extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
  191. /**
  192. * \brief Set the value associated with a thread local storage ID for the current thread.
  193. *
  194. * \param id The thread local storage ID
  195. * \param value The value to associate with the ID for the current thread
  196. * \param destructor A function called when the thread exits, to free the value.
  197. *
  198. * \return 0 on success, -1 on error
  199. *
  200. * \sa SDL_TLSCreate()
  201. * \sa SDL_TLSGet()
  202. */
  203. extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void*));
  204. /* Ends C function definitions when using C++ */
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #include "close_code.h"
  209. #endif /* _SDL_thread_h */
  210. /* vi: set ts=4 sw=4 expandtab: */