SDL_thread.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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. * SDL will make system changes as necessary in order to apply the thread priority.
  46. * Code which attempts to control thread state related to priority should be aware
  47. * that calling SDL_SetThreadPriority may alter such state.
  48. * SDL_HINT_THREAD_PRIORITY_POLICY can be used to control aspects of this behavior.
  49. *
  50. * \note On many systems you require special privileges to set high or time critical priority.
  51. */
  52. typedef enum {
  53. SDL_THREAD_PRIORITY_LOW,
  54. SDL_THREAD_PRIORITY_NORMAL,
  55. SDL_THREAD_PRIORITY_HIGH,
  56. SDL_THREAD_PRIORITY_TIME_CRITICAL
  57. } SDL_ThreadPriority;
  58. /**
  59. * The function passed to SDL_CreateThread().
  60. *
  61. * \param data what was passed as `data` to SDL_CreateThread()
  62. * \returns a value that can be reported through SDL_WaitThread().
  63. */
  64. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  65. #if defined(__WIN32__)
  66. /**
  67. * \file SDL_thread.h
  68. *
  69. * We compile SDL into a DLL. This means, that it's the DLL which
  70. * creates a new thread for the calling process with the SDL_CreateThread()
  71. * API. There is a problem with this, that only the RTL of the SDL2.DLL will
  72. * be initialized for those threads, and not the RTL of the calling
  73. * application!
  74. *
  75. * To solve this, we make a little hack here.
  76. *
  77. * We'll always use the caller's _beginthread() and _endthread() APIs to
  78. * start a new thread. This way, if it's the SDL2.DLL which uses this API,
  79. * then the RTL of SDL2.DLL will be used to create the new thread, and if it's
  80. * the application, then the RTL of the application will be used.
  81. *
  82. * So, in short:
  83. * Always use the _beginthread() and _endthread() of the calling runtime
  84. * library!
  85. */
  86. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  87. #include <process.h> /* _beginthreadex() and _endthreadex() */
  88. typedef uintptr_t (__cdecl * pfnSDL_CurrentBeginThread)
  89. (void *, unsigned, unsigned (__stdcall *func)(void *),
  90. void * /*arg*/, unsigned, unsigned * /* threadID */);
  91. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  92. #ifndef SDL_beginthread
  93. #define SDL_beginthread _beginthreadex
  94. #endif
  95. #ifndef SDL_endthread
  96. #define SDL_endthread _endthreadex
  97. #endif
  98. /**
  99. * Create a thread.
  100. */
  101. extern DECLSPEC SDL_Thread *SDLCALL
  102. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  103. pfnSDL_CurrentBeginThread pfnBeginThread,
  104. pfnSDL_CurrentEndThread pfnEndThread);
  105. extern DECLSPEC SDL_Thread *SDLCALL
  106. SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
  107. const char *name, const size_t stacksize, void *data,
  108. pfnSDL_CurrentBeginThread pfnBeginThread,
  109. pfnSDL_CurrentEndThread pfnEndThread);
  110. /**
  111. * Create a thread.
  112. */
  113. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  114. #undef SDL_CreateThread
  115. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  116. #undef SDL_CreateThreadWithStackSize
  117. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  118. #else
  119. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  120. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)SDL_endthread)
  121. #endif
  122. #elif defined(__OS2__)
  123. /*
  124. * just like the windows case above: We compile SDL2
  125. * into a dll with Watcom's runtime statically linked.
  126. */
  127. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  128. #ifndef __EMX__
  129. #include <process.h>
  130. #else
  131. #include <stdlib.h>
  132. #endif
  133. typedef int (*pfnSDL_CurrentBeginThread)(void (*func)(void *), void *, unsigned, void * /*arg*/);
  134. typedef void (*pfnSDL_CurrentEndThread)(void);
  135. #ifndef SDL_beginthread
  136. #define SDL_beginthread _beginthread
  137. #endif
  138. #ifndef SDL_endthread
  139. #define SDL_endthread _endthread
  140. #endif
  141. extern DECLSPEC SDL_Thread *SDLCALL
  142. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  143. pfnSDL_CurrentBeginThread pfnBeginThread,
  144. pfnSDL_CurrentEndThread pfnEndThread);
  145. extern DECLSPEC SDL_Thread *SDLCALL
  146. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data,
  147. pfnSDL_CurrentBeginThread pfnBeginThread,
  148. pfnSDL_CurrentEndThread pfnEndThread);
  149. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  150. #undef SDL_CreateThread
  151. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  152. #undef SDL_CreateThreadWithStackSize
  153. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  154. #else
  155. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  156. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)SDL_beginthread, (pfnSDL_CurrentEndThread)SDL_endthread)
  157. #endif
  158. #else
  159. /**
  160. * Create a new thread with a default stack size.
  161. *
  162. * This is equivalent to calling:
  163. *
  164. * ```c
  165. * SDL_CreateThreadWithStackSize(fn, name, 0, data);
  166. * ```
  167. *
  168. * \param fn the SDL_ThreadFunction function to call in the new thread
  169. * \param name the name of the thread
  170. * \param data a pointer that is passed to `fn`
  171. * \returns an opaque pointer to the new thread object on success, NULL if the
  172. * new thread could not be created; call SDL_GetError() for more
  173. * information.
  174. *
  175. * \sa SDL_CreateThreadWithStackSize
  176. * \sa SDL_WaitThread
  177. */
  178. extern DECLSPEC SDL_Thread *SDLCALL
  179. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  180. /**
  181. * Create a new thread with a specific stack size.
  182. *
  183. * SDL makes an attempt to report `name` to the system, so that debuggers
  184. * can display it. Not all platforms support this.
  185. *
  186. * Thread naming is a little complicated: Most systems have very small
  187. * limits for the string length (Haiku has 32 bytes, Linux currently has 16,
  188. * Visual C++ 6.0 has _nine_!), and possibly other arbitrary rules. You'll
  189. * have to see what happens with your system's debugger. The name should be
  190. * UTF-8 (but using the naming limits of C identifiers is a better bet).
  191. * There are no requirements for thread naming conventions, so long as the
  192. * string is null-terminated UTF-8, but these guidelines are helpful in
  193. * choosing a name:
  194. *
  195. * https://stackoverflow.com/questions/149932/naming-conventions-for-threads
  196. *
  197. * If a system imposes requirements, SDL will try to munge the string for
  198. * it (truncate, etc), but the original string contents will be available
  199. * from SDL_GetThreadName().
  200. *
  201. * The size (in bytes) of the new stack can be specified. Zero means "use
  202. * the system default" which might be wildly different between platforms.
  203. * x86 Linux generally defaults to eight megabytes, an embedded device
  204. * might be a few kilobytes instead. You generally need to specify a stack
  205. * that is a multiple of the system's page size (in many cases, this is 4
  206. * kilobytes, but check your system documentation).
  207. *
  208. * In SDL 2.1, stack size will be folded into the original SDL_CreateThread
  209. * function, but for backwards compatibility, this is currently a separate
  210. * function.
  211. *
  212. * \param fn the SDL_ThreadFunction function to call in the new thread
  213. * \param name the name of the thread
  214. * \param stacksize the size, in bytes, to allocate for the new thread stack.
  215. * \param data a pointer that is passed to `fn`
  216. * \returns an opaque pointer to the new thread object on success, NULL if the
  217. * new thread could not be created; call SDL_GetError() for more
  218. * information.
  219. *
  220. * \sa SDL_WaitThread
  221. */
  222. extern DECLSPEC SDL_Thread *SDLCALL
  223. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data);
  224. #endif
  225. /**
  226. * Get the thread name as it was specified in SDL_CreateThread().
  227. *
  228. * This is internal memory, not to be freed by the caller, and remains valid
  229. * until the specified thread is cleaned up by SDL_WaitThread().
  230. *
  231. * \param thread the thread to query
  232. * \returns a pointer to a UTF-8 string that names the specified thread, or
  233. * NULL if it doesn't have a name.
  234. *
  235. * \sa SDL_CreateThread
  236. */
  237. extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  238. /**
  239. * Get the thread identifier for the current thread.
  240. *
  241. * This thread identifier is as reported by the underlying operating system.
  242. * If SDL is running on a platform that does not support threads the return
  243. * value will always be zero.
  244. *
  245. * This function also returns a valid thread ID when called from the main
  246. * thread.
  247. *
  248. * \returns the ID of the current thread.
  249. *
  250. * \sa SDL_GetThreadID
  251. */
  252. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  253. /**
  254. * Get the thread identifier for the specified thread.
  255. *
  256. * This thread identifier is as reported by the underlying operating system.
  257. * If SDL is running on a platform that does not support threads the return
  258. * value will always be zero.
  259. *
  260. * \param thread the thread to query
  261. * \returns the ID of the specified thread, or the ID of the current thread if
  262. * `thread` is NULL.
  263. *
  264. * \sa SDL_ThreadID
  265. */
  266. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  267. /**
  268. * Set the priority for the current thread.
  269. *
  270. * Note that some platforms will not let you alter the priority (or at least,
  271. * promote the thread to a higher priority) at all, and some require you
  272. * to be an administrator account. Be prepared for this to fail.
  273. *
  274. * \param priority the SDL_ThreadPriority to set
  275. * \returns 0 on success or a negative error code on failure; call
  276. * SDL_GetError() for more information.
  277. */
  278. extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  279. /**
  280. * Wait for a thread to finish.
  281. *
  282. * Threads that haven't been detached will remain (as a "zombie") until this
  283. * function cleans them up. Not doing so is a resource leak.
  284. *
  285. * Once a thread has been cleaned up through this function, the SDL_Thread
  286. * that references it becomes invalid and should not be referenced again. As
  287. * such, only one thread may call SDL_WaitThread() on another.
  288. *
  289. * The return code for the thread function is placed in the area pointed to by
  290. * `status`, if `status` is not NULL.
  291. *
  292. * You may not wait on a thread that has been used in a call to
  293. * SDL_DetachThread(). Use either that function or this one, but not both, or
  294. * behavior is undefined.
  295. *
  296. * It is safe to pass a NULL thread to this function; it is a no-op.
  297. *
  298. * Note that the thread pointer is freed by this function and is not valid
  299. * afterward.
  300. *
  301. * \param thread the SDL_Thread pointer that was returned from the
  302. * SDL_CreateThread() call that started this thread
  303. * \param status pointer to an integer that will receive the value returned
  304. * from the thread function by its 'return', or NULL to not
  305. * receive such value back.
  306. *
  307. * \sa SDL_CreateThread
  308. * \sa SDL_DetachThread
  309. */
  310. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  311. /**
  312. * Let a thread clean up on exit without intervention.
  313. *
  314. * A thread may be "detached" to signify that it should not remain until
  315. * another thread has called SDL_WaitThread() on it. Detaching a thread is
  316. * useful for long-running threads that nothing needs to synchronize with or
  317. * further manage. When a detached thread is done, it simply goes away.
  318. *
  319. * There is no way to recover the return code of a detached thread. If you
  320. * need this, don't detach the thread and instead use SDL_WaitThread().
  321. *
  322. * Once a thread is detached, you should usually assume the SDL_Thread isn't
  323. * safe to reference again, as it will become invalid immediately upon the
  324. * detached thread's exit, instead of remaining until someone has called
  325. * SDL_WaitThread() to finally clean it up. As such, don't detach the same
  326. * thread more than once.
  327. *
  328. * If a thread has already exited when passed to SDL_DetachThread(), it will
  329. * stop waiting for a call to SDL_WaitThread() and clean up immediately. It is
  330. * not safe to detach a thread that might be used with SDL_WaitThread().
  331. *
  332. * You may not call SDL_WaitThread() on a thread that has been detached. Use
  333. * either that function or this one, but not both, or behavior is undefined.
  334. *
  335. * It is safe to pass NULL to this function; it is a no-op.
  336. *
  337. * \param thread the SDL_Thread pointer that was returned from the
  338. * SDL_CreateThread() call that started this thread
  339. *
  340. * \since This function is available since SDL 2.0.2.
  341. *
  342. * \sa SDL_CreateThread
  343. * \sa SDL_WaitThread
  344. */
  345. extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
  346. /**
  347. * Create a piece of thread-local storage.
  348. *
  349. * This creates an identifier that is globally visible to all
  350. * threads but refers to data that is thread-specific.
  351. *
  352. * \returns the newly created thread local storage identifier or 0 on error.
  353. *
  354. * \since This function is available since SDL 2.0.0.
  355. *
  356. * \sa SDL_TLSGet
  357. * \sa SDL_TLSSet
  358. */
  359. extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
  360. /**
  361. * Get the current thread's value associated with a thread local storage ID.
  362. *
  363. * \param id the thread local storage ID
  364. * \returns the value associated with the ID for the current thread or NULL if
  365. * no value has been set; call SDL_GetError() for more information.
  366. *
  367. * \since This function is available since SDL 2.0.0.
  368. *
  369. * \sa SDL_TLSCreate
  370. * \sa SDL_TLSSet
  371. */
  372. extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
  373. /**
  374. * Set the current thread's value associated with a thread local storage ID.
  375. *
  376. * The function prototype for `destructor` is:
  377. *
  378. * ```c
  379. * void destructor(void *value)
  380. * ```
  381. *
  382. * where its parameter `value` is what was passed as `value` to SDL_TLSSet().
  383. *
  384. * \param id the thread local storage ID
  385. * \param value the value to associate with the ID for the current thread
  386. * \param destructor a function called when the thread exits, to free the
  387. * value
  388. * \returns 0 on success or a negative error code on failure; call
  389. * SDL_GetError() for more information.
  390. *
  391. * \since This function is available since SDL 2.0.0.
  392. *
  393. * \sa SDL_TLSCreate
  394. * \sa SDL_TLSGet
  395. */
  396. extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void*));
  397. /**
  398. * \brief Cleanup all TLS data for this thread.
  399. */
  400. extern DECLSPEC void SDLCALL SDL_TLSCleanup(void);
  401. /* Ends C function definitions when using C++ */
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #include "close_code.h"
  406. #endif /* SDL_thread_h_ */
  407. /* vi: set ts=4 sw=4 expandtab: */