SDL_thread.h 11 KB

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