SDL_thread.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  98. #undef SDL_CreateThread
  99. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  100. #else
  101. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  102. #endif
  103. #else
  104. /**
  105. * Create a thread.
  106. *
  107. * Thread naming is a little complicated: Most systems have very small
  108. * limits for the string length (Haiku has 32 bytes, Linux currently has 16,
  109. * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
  110. * have to see what happens with your system's debugger. The name should be
  111. * UTF-8 (but using the naming limits of C identifiers is a better bet).
  112. * There are no requirements for thread naming conventions, so long as the
  113. * string is null-terminated UTF-8, but these guidelines are helpful in
  114. * choosing a name:
  115. *
  116. * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
  117. *
  118. * If a system imposes requirements, SDL will try to munge the string for
  119. * it (truncate, etc), but the original string contents will be available
  120. * from SDL_GetThreadName().
  121. */
  122. extern DECLSPEC SDL_Thread *SDLCALL
  123. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  124. #endif
  125. /**
  126. * Get the thread name, as it was specified in SDL_CreateThread().
  127. * This function returns a pointer to a UTF-8 string that names the
  128. * specified thread, or NULL if it doesn't have a name. This is internal
  129. * memory, not to be free()'d by the caller, and remains valid until the
  130. * specified thread is cleaned up by SDL_WaitThread().
  131. */
  132. extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  133. /**
  134. * Get the thread identifier for the current thread.
  135. */
  136. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  137. /**
  138. * Get the thread identifier for the specified thread.
  139. *
  140. * Equivalent to SDL_ThreadID() if the specified thread is NULL.
  141. */
  142. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  143. /**
  144. * Set the priority for the current thread
  145. */
  146. extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  147. /**
  148. * Wait for a thread to finish. Threads that haven't been detached will
  149. * remain (as a "zombie") until this function cleans them up. Not doing so
  150. * is a resource leak.
  151. *
  152. * Once a thread has been cleaned up through this function, the SDL_Thread
  153. * that references it becomes invalid and should not be referenced again.
  154. * As such, only one thread may call SDL_WaitThread() on another.
  155. *
  156. * The return code for the thread function is placed in the area
  157. * pointed to by \c status, if \c status is not NULL.
  158. *
  159. * You may not wait on a thread that has been used in a call to
  160. * SDL_DetachThread(). Use either that function or this one, but not
  161. * both, or behavior is undefined.
  162. *
  163. * It is safe to pass NULL to this function; it is a no-op.
  164. */
  165. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  166. /**
  167. * A thread may be "detached" to signify that it should not remain until
  168. * another thread has called SDL_WaitThread() on it. Detaching a thread
  169. * is useful for long-running threads that nothing needs to synchronize
  170. * with or further manage. When a detached thread is done, it simply
  171. * goes away.
  172. *
  173. * There is no way to recover the return code of a detached thread. If you
  174. * need this, don't detach the thread and instead use SDL_WaitThread().
  175. *
  176. * Once a thread is detached, you should usually assume the SDL_Thread isn't
  177. * safe to reference again, as it will become invalid immediately upon
  178. * the detached thread's exit, instead of remaining until someone has called
  179. * SDL_WaitThread() to finally clean it up. As such, don't detach the same
  180. * thread more than once.
  181. *
  182. * If a thread has already exited when passed to SDL_DetachThread(), it will
  183. * stop waiting for a call to SDL_WaitThread() and clean up immediately.
  184. * It is not safe to detach a thread that might be used with SDL_WaitThread().
  185. *
  186. * You may not call SDL_WaitThread() on a thread that has been detached.
  187. * Use either that function or this one, but not both, or behavior is
  188. * undefined.
  189. *
  190. * It is safe to pass NULL to this function; it is a no-op.
  191. */
  192. extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
  193. /**
  194. * \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
  195. *
  196. * \return The newly created thread local storage identifier, or 0 on error
  197. *
  198. * \code
  199. * static SDL_SpinLock tls_lock;
  200. * static SDL_TLSID thread_local_storage;
  201. *
  202. * void SetMyThreadData(void *value)
  203. * {
  204. * if (!thread_local_storage) {
  205. * SDL_AtomicLock(&tls_lock);
  206. * if (!thread_local_storage) {
  207. * thread_local_storage = SDL_TLSCreate();
  208. * }
  209. * SDL_AtomicUnLock(&tls_lock);
  210. * }
  211. * SDL_TLSSet(thread_local_storage, value);
  212. * }
  213. *
  214. * void *GetMyThreadData(void)
  215. * {
  216. * return SDL_TLSGet(thread_local_storage);
  217. * }
  218. * \endcode
  219. *
  220. * \sa SDL_TLSGet()
  221. * \sa SDL_TLSSet()
  222. */
  223. extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
  224. /**
  225. * \brief Get the value associated with a thread local storage ID for the current thread.
  226. *
  227. * \param id The thread local storage ID
  228. *
  229. * \return The value associated with the ID for the current thread, or NULL if no value has been set.
  230. *
  231. * \sa SDL_TLSCreate()
  232. * \sa SDL_TLSSet()
  233. */
  234. extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
  235. /**
  236. * \brief Set the value associated with a thread local storage ID for the current thread.
  237. *
  238. * \param id The thread local storage ID
  239. * \param value The value to associate with the ID for the current thread
  240. * \param destructor A function called when the thread exits, to free the value.
  241. *
  242. * \return 0 on success, -1 on error
  243. *
  244. * \sa SDL_TLSCreate()
  245. * \sa SDL_TLSGet()
  246. */
  247. extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (*destructor)(void*));
  248. /* Ends C function definitions when using C++ */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #include "close_code.h"
  253. #endif /* _SDL_thread_h */
  254. /* vi: set ts=4 sw=4 expandtab: */