SDL_thread.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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 or time critical priority.
  46. */
  47. typedef enum {
  48. SDL_THREAD_PRIORITY_LOW,
  49. SDL_THREAD_PRIORITY_NORMAL,
  50. SDL_THREAD_PRIORITY_HIGH,
  51. SDL_THREAD_PRIORITY_TIME_CRITICAL
  52. } SDL_ThreadPriority;
  53. /**
  54. * The function passed to SDL_CreateThread().
  55. * It is passed a void* user context parameter and returns an int.
  56. */
  57. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  58. #if defined(__WIN32__)
  59. /**
  60. * \file SDL_thread.h
  61. *
  62. * We compile SDL into a DLL. This means, that it's the DLL which
  63. * creates a new thread for the calling process with the SDL_CreateThread()
  64. * API. There is a problem with this, that only the RTL of the SDL2.DLL will
  65. * be initialized for those threads, and not the RTL of the calling
  66. * application!
  67. *
  68. * To solve this, we make a little hack here.
  69. *
  70. * We'll always use the caller's _beginthread() and _endthread() APIs to
  71. * start a new thread. This way, if it's the SDL2.DLL which uses this API,
  72. * then the RTL of SDL2.DLL will be used to create the new thread, and if it's
  73. * the application, then the RTL of the application will be used.
  74. *
  75. * So, in short:
  76. * Always use the _beginthread() and _endthread() of the calling runtime
  77. * library!
  78. */
  79. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  80. #include <process.h> /* _beginthreadex() and _endthreadex() */
  81. typedef uintptr_t (__cdecl * pfnSDL_CurrentBeginThread)
  82. (void *, unsigned, unsigned (__stdcall *func)(void *),
  83. void * /*arg*/, unsigned, unsigned * /* threadID */);
  84. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  85. #ifndef SDL_beginthread
  86. #define SDL_beginthread _beginthreadex
  87. #endif
  88. #ifndef SDL_endthread
  89. #define SDL_endthread _endthreadex
  90. #endif
  91. /**
  92. * Create a thread.
  93. */
  94. extern DECLSPEC SDL_Thread *SDLCALL
  95. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  96. pfnSDL_CurrentBeginThread pfnBeginThread,
  97. pfnSDL_CurrentEndThread pfnEndThread);
  98. extern DECLSPEC SDL_Thread *SDLCALL
  99. SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
  100. const char *name, const size_t stacksize, void *data,
  101. pfnSDL_CurrentBeginThread pfnBeginThread,
  102. pfnSDL_CurrentEndThread pfnEndThread);
  103. /**
  104. * Create a thread.
  105. */
  106. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  107. #undef SDL_CreateThread
  108. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  109. #undef SDL_CreateThreadWithStackSize
  110. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  111. #else
  112. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  113. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)SDL_endthread)
  114. #endif
  115. #elif defined(__OS2__)
  116. /*
  117. * just like the windows case above: We compile SDL2
  118. * into a dll with Watcom's runtime statically linked.
  119. */
  120. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  121. #ifndef __EMX__
  122. #include <process.h>
  123. #else
  124. #include <stdlib.h>
  125. #endif
  126. typedef int (*pfnSDL_CurrentBeginThread)(void (*func)(void *), void *, unsigned, void * /*arg*/);
  127. typedef void (*pfnSDL_CurrentEndThread)(void);
  128. #ifndef SDL_beginthread
  129. #define SDL_beginthread _beginthread
  130. #endif
  131. #ifndef SDL_endthread
  132. #define SDL_endthread _endthread
  133. #endif
  134. extern DECLSPEC SDL_Thread *SDLCALL
  135. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  136. pfnSDL_CurrentBeginThread pfnBeginThread,
  137. pfnSDL_CurrentEndThread pfnEndThread);
  138. extern DECLSPEC SDL_Thread *SDLCALL
  139. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data,
  140. pfnSDL_CurrentBeginThread pfnBeginThread,
  141. pfnSDL_CurrentEndThread pfnEndThread);
  142. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  143. #undef SDL_CreateThread
  144. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  145. #undef SDL_CreateThreadWithStackSize
  146. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  147. #else
  148. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  149. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  150. #endif
  151. #else
  152. /**
  153. * Create a thread with a default stack size.
  154. *
  155. * This is equivalent to calling:
  156. * SDL_CreateThreadWithStackSize(fn, name, 0, data);
  157. */
  158. extern DECLSPEC SDL_Thread *SDLCALL
  159. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  160. /**
  161. * Create a thread.
  162. *
  163. * Thread naming is a little complicated: Most systems have very small
  164. * limits for the string length (Haiku has 32 bytes, Linux currently has 16,
  165. * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
  166. * have to see what happens with your system's debugger. The name should be
  167. * UTF-8 (but using the naming limits of C identifiers is a better bet).
  168. * There are no requirements for thread naming conventions, so long as the
  169. * string is null-terminated UTF-8, but these guidelines are helpful in
  170. * choosing a name:
  171. *
  172. * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
  173. *
  174. * If a system imposes requirements, SDL will try to munge the string for
  175. * it (truncate, etc), but the original string contents will be available
  176. * from SDL_GetThreadName().
  177. *
  178. * The size (in bytes) of the new stack can be specified. Zero means "use
  179. * the system default" which might be wildly different between platforms
  180. * (x86 Linux generally defaults to eight megabytes, an embedded device
  181. * might be a few kilobytes instead).
  182. *
  183. * In SDL 2.1, stacksize will be folded into the original SDL_CreateThread
  184. * function.
  185. */
  186. extern DECLSPEC SDL_Thread *SDLCALL
  187. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data);
  188. #endif
  189. /**
  190. * Get the thread name, as it was specified in SDL_CreateThread().
  191. * This function returns a pointer to a UTF-8 string that names the
  192. * specified thread, or NULL if it doesn't have a name. This is internal
  193. * memory, not to be free()'d by the caller, and remains valid until the
  194. * specified thread is cleaned up by SDL_WaitThread().
  195. */
  196. extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  197. /**
  198. * Get the thread identifier for the current thread.
  199. */
  200. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  201. /**
  202. * Get the thread identifier for the specified thread.
  203. *
  204. * Equivalent to SDL_ThreadID() if the specified thread is NULL.
  205. */
  206. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  207. /**
  208. * Set the priority for the current thread
  209. */
  210. extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  211. /**
  212. * Wait for a thread to finish. Threads that haven't been detached will
  213. * remain (as a "zombie") until this function cleans them up. Not doing so
  214. * is a resource leak.
  215. *
  216. * Once a thread has been cleaned up through this function, the SDL_Thread
  217. * that references it becomes invalid and should not be referenced again.
  218. * As such, only one thread may call SDL_WaitThread() on another.
  219. *
  220. * The return code for the thread function is placed in the area
  221. * pointed to by \c status, if \c status is not NULL.
  222. *
  223. * You may not wait on a thread that has been used in a call to
  224. * SDL_DetachThread(). Use either that function or this one, but not
  225. * both, or behavior is undefined.
  226. *
  227. * It is safe to pass NULL to this function; it is a no-op.
  228. */
  229. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  230. /**
  231. * A thread may be "detached" to signify that it should not remain until
  232. * another thread has called SDL_WaitThread() on it. Detaching a thread
  233. * is useful for long-running threads that nothing needs to synchronize
  234. * with or further manage. When a detached thread is done, it simply
  235. * goes away.
  236. *
  237. * There is no way to recover the return code of a detached thread. If you
  238. * need this, don't detach the thread and instead use SDL_WaitThread().
  239. *
  240. * Once a thread is detached, you should usually assume the SDL_Thread isn't
  241. * safe to reference again, as it will become invalid immediately upon
  242. * the detached thread's exit, instead of remaining until someone has called
  243. * SDL_WaitThread() to finally clean it up. As such, don't detach the same
  244. * thread more than once.
  245. *
  246. * If a thread has already exited when passed to SDL_DetachThread(), it will
  247. * stop waiting for a call to SDL_WaitThread() and clean up immediately.
  248. * It is not safe to detach a thread that might be used with SDL_WaitThread().
  249. *
  250. * You may not call SDL_WaitThread() on a thread that has been detached.
  251. * Use either that function or this one, but not both, or behavior is
  252. * undefined.
  253. *
  254. * It is safe to pass NULL to this function; it is a no-op.
  255. */
  256. extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
  257. /**
  258. * \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
  259. *
  260. * \return The newly created thread local storage identifier, or 0 on error
  261. *
  262. * \code
  263. * static SDL_SpinLock tls_lock;
  264. * static SDL_TLSID thread_local_storage;
  265. *
  266. * void SetMyThreadData(void *value)
  267. * {
  268. * if (!thread_local_storage) {
  269. * SDL_AtomicLock(&tls_lock);
  270. * if (!thread_local_storage) {
  271. * thread_local_storage = SDL_TLSCreate();
  272. * }
  273. * SDL_AtomicUnlock(&tls_lock);
  274. * }
  275. * SDL_TLSSet(thread_local_storage, value, 0);
  276. * }
  277. *
  278. * void *GetMyThreadData(void)
  279. * {
  280. * return SDL_TLSGet(thread_local_storage);
  281. * }
  282. * \endcode
  283. *
  284. * \sa SDL_TLSGet()
  285. * \sa SDL_TLSSet()
  286. */
  287. extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
  288. /**
  289. * \brief Get the value associated with a thread local storage ID for the current thread.
  290. *
  291. * \param id The thread local storage ID
  292. *
  293. * \return The value associated with the ID for the current thread, or NULL if no value has been set.
  294. *
  295. * \sa SDL_TLSCreate()
  296. * \sa SDL_TLSSet()
  297. */
  298. extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
  299. /**
  300. * \brief Set the value associated with a thread local storage ID for the current thread.
  301. *
  302. * \param id The thread local storage ID
  303. * \param value The value to associate with the ID for the current thread
  304. * \param destructor A function called when the thread exits, to free the value.
  305. *
  306. * \return 0 on success, -1 on error
  307. *
  308. * \sa SDL_TLSCreate()
  309. * \sa SDL_TLSGet()
  310. */
  311. extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void*));
  312. /* Ends C function definitions when using C++ */
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #include "close_code.h"
  317. #endif /* SDL_thread_h_ */
  318. /* vi: set ts=4 sw=4 expandtab: */