SDL_thread.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. * # CategoryThread
  22. *
  23. * SDL thread management routines.
  24. */
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. /* Thread synchronization primitives */
  28. #include <SDL3/SDL_atomic.h>
  29. #include <SDL3/SDL_mutex.h>
  30. #if (defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)) && !defined(SDL_PLATFORM_WINRT)
  31. #include <process.h> /* _beginthreadex() and _endthreadex() */
  32. #endif
  33. #include <SDL3/SDL_begin_code.h>
  34. /* Set up for C function definitions, even when using C++ */
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * The SDL thread object.
  40. *
  41. * These are opaque data.
  42. *
  43. * \since This datatype is available since SDL 3.0.0.
  44. *
  45. * \sa SDL_CreateThread
  46. * \sa SDL_WaitThread
  47. */
  48. typedef struct SDL_Thread SDL_Thread;
  49. /**
  50. * A unique numeric ID that identifies a thread.
  51. *
  52. * These are different that SDL_Thread objects, which are generally what an
  53. * application will operate on, but having a way to uniquely identify a
  54. * thread can be useful at times.
  55. *
  56. * \since This datatype is available since SDL 3.0.0.
  57. *
  58. * \sa SDL_GetThreadID
  59. * \sa SDL_GetCurrentThreadID
  60. */
  61. typedef Uint64 SDL_ThreadID;
  62. /**
  63. * Thread local storage ID values.
  64. *
  65. * 0 is the invalid ID. An app can create these and then set data for
  66. * these IDs that is unique to each thread.
  67. *
  68. * \since This datatype is available since SDL 3.0.0.
  69. *
  70. * \sa SDL_CreateTLS
  71. * \sa SDL_GetTLS
  72. * \sa SDL_SetTLS
  73. */
  74. typedef Uint32 SDL_TLSID;
  75. /**
  76. * The SDL thread priority.
  77. *
  78. * SDL will make system changes as necessary in order to apply the thread
  79. * priority. Code which attempts to control thread state related to priority
  80. * should be aware that calling SDL_SetThreadPriority may alter such state.
  81. * SDL_HINT_THREAD_PRIORITY_POLICY can be used to control aspects of this
  82. * behavior.
  83. *
  84. * \since This enum is available since SDL 3.0.0.
  85. */
  86. typedef enum SDL_ThreadPriority {
  87. SDL_THREAD_PRIORITY_LOW,
  88. SDL_THREAD_PRIORITY_NORMAL,
  89. SDL_THREAD_PRIORITY_HIGH,
  90. SDL_THREAD_PRIORITY_TIME_CRITICAL
  91. } SDL_ThreadPriority;
  92. /**
  93. * The function passed to SDL_CreateThread() as the new thread's entry point.
  94. *
  95. * \param data what was passed as `data` to SDL_CreateThread()
  96. * \returns a value that can be reported through SDL_WaitThread().
  97. *
  98. * \since This datatype is available since SDL 3.0.0.
  99. */
  100. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  101. #if (defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)) && !defined(SDL_PLATFORM_WINRT)
  102. #ifndef SDL_BeginThreadFunction
  103. /**
  104. * Macro that manages the compiler's `_beginthreadex` implementation.
  105. *
  106. * On Windows (and maybe other platforms), a program might use a different
  107. * C runtime than its libraries. Or, in SDL's case, it might use a C runtime
  108. * while SDL uses none at all.
  109. *
  110. * C runtimes expect to initialize thread-specific details when a new thread
  111. * is created, but to do this in SDL_CreateThread would require SDL to know
  112. * intimate details about the caller's C runtime, which is not possible.
  113. *
  114. * So SDL_CreateThread has two extra parameters, which are
  115. * hidden at compile time by macros: the C runtime's `_beginthreadex` and
  116. * `_endthreadex` entry points. If these are not NULL, they are used to spin
  117. * and terminate the new thread; otherwise the standard Win32 `CreateThread`
  118. * function is used. When `SDL_CreateThread` is called from a compiler that
  119. * needs this C runtime thread init function, macros insert the appropriate
  120. * function pointers for SDL_CreateThread's caller (which might be a different
  121. * compiler with a different runtime in different calls to SDL_CreateThread!).
  122. *
  123. * This defaults to `_beginthreadex` on Windows (and NULL everywhere else),
  124. * but apps that have extremely specific special needs can define this to
  125. * something else and the SDL headers will use it, passing the app-defined
  126. * value to SDL_CreateThread calls. Redefine this with caution!
  127. *
  128. * Unless you are doing something extremely complicated, like perhaps a
  129. * language binding, **you should never reference this directly**. Let SDL's
  130. * macros handle this platform-specific detail transparently!
  131. *
  132. * \threadsafety It is safe to call this macro from any thread.
  133. *
  134. * \since This macro is available since SDL 3.0.0.
  135. *
  136. * \sa SDL_CreateThread
  137. */
  138. #define SDL_BeginThreadFunction _beginthreadex
  139. #endif
  140. #ifndef SDL_EndThreadFunction
  141. /**
  142. * Macro that manages the compiler's `_endthreadex` implementation.
  143. *
  144. * Please see the detailed explanation in SDL_BeginThreadFunction.
  145. *
  146. * This defaults to `_endthreadex` on Windows (and NULL everywhere else),
  147. * but apps that have extremely specific special needs can define this to
  148. * something else and the SDL headers will use it, passing the app-defined
  149. * value to SDL_CreateThread calls. Redefine this with caution!
  150. *
  151. * Unless you are doing something extremely complicated, like perhaps a
  152. * language binding, **you should never reference this directly**. Let SDL's
  153. * macros handle this platform-specific detail transparently!
  154. *
  155. * \threadsafety It is safe to call this macro from any thread.
  156. *
  157. * \since This macro is available since SDL 3.0.0.
  158. *
  159. * \sa SDL_CreateThread
  160. */
  161. #define SDL_EndThreadFunction _endthreadex
  162. #endif
  163. #endif
  164. /* currently no other platforms than Windows use _beginthreadex/_endthreadex things. */
  165. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  166. #ifndef SDL_BeginThreadFunction
  167. #define SDL_BeginThreadFunction NULL
  168. #endif
  169. #ifndef SDL_EndThreadFunction
  170. #define SDL_EndThreadFunction NULL
  171. #endif
  172. #endif
  173. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  174. /* Note that this isn't the correct function signature, but this is what the API reference manual should look like for all intents and purposes. */
  175. /**
  176. * Create a new thread with a default stack size.
  177. *
  178. * This is equivalent to calling:
  179. *
  180. * ```c
  181. * SDL_CreateThreadWithStackSize(fn, name, 0, data);
  182. * ```
  183. *
  184. * Note that this "function" is actually a macro that calls an internal
  185. * function with two extra parameters not listed here; they are
  186. * hidden through preprocessor macros and are needed to support various C
  187. * runtimes at the point of the function call. Language bindings that aren't
  188. * using the C headers will need to deal with this.
  189. *
  190. * Usually, apps should just call this function the same way on every platform and
  191. * let the macros hide the details. See SDL_BeginThreadFunction for the
  192. * technical details.
  193. *
  194. * \param fn the SDL_ThreadFunction function to call in the new thread
  195. * \param name the name of the thread
  196. * \param data a pointer that is passed to `fn`
  197. * \returns an opaque pointer to the new thread object on success, NULL if the
  198. * new thread could not be created; call SDL_GetError() for more
  199. * information.
  200. *
  201. * \since This function is available since SDL 3.0.0.
  202. *
  203. * \sa SDL_CreateThreadWithStackSize
  204. * \sa SDL_WaitThread
  205. */
  206. extern SDL_DECLSPEC SDL_Thread * SDLCALL SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  207. /**
  208. * Create a new thread with a specific stack size.
  209. *
  210. * SDL makes an attempt to report `name` to the system, so that debuggers can
  211. * display it. Not all platforms support this.
  212. *
  213. * Thread naming is a little complicated: Most systems have very small limits
  214. * for the string length (Haiku has 32 bytes, Linux currently has 16, Visual
  215. * C++ 6.0 has _nine_!), and possibly other arbitrary rules. You'll have to
  216. * see what happens with your system's debugger. The name should be UTF-8 (but
  217. * using the naming limits of C identifiers is a better bet). There are no
  218. * requirements for thread naming conventions, so long as the string is
  219. * null-terminated UTF-8, but these guidelines are helpful in choosing a name:
  220. *
  221. * https://stackoverflow.com/questions/149932/naming-conventions-for-threads
  222. *
  223. * If a system imposes requirements, SDL will try to munge the string for it
  224. * (truncate, etc), but the original string contents will be available from
  225. * SDL_GetThreadName().
  226. *
  227. * The size (in bytes) of the new stack can be specified. Zero means "use the
  228. * system default" which might be wildly different between platforms. x86
  229. * Linux generally defaults to eight megabytes, an embedded device might be a
  230. * few kilobytes instead. You generally need to specify a stack that is a
  231. * multiple of the system's page size (in many cases, this is 4 kilobytes, but
  232. * check your system documentation).
  233. *
  234. * Note that this "function" is actually a macro that calls an internal
  235. * function with two extra parameters not listed here; they are
  236. * hidden through preprocessor macros and are needed to support various C
  237. * runtimes at the point of the function call. Language bindings that aren't
  238. * using the C headers will need to deal with this.
  239. *
  240. * The actual symbol in SDL's library is `SDL_CreateThreadRuntime` (or
  241. * `SDL_CreateThreadWithStackSpaceRuntime`), so there is no symbol clash, but
  242. * trying to load an SDL shared library and look for "SDL_CreateThread"
  243. * will fail.
  244. *
  245. * Usually, apps should just call this function the same way on every platform and
  246. * let the macros hide the details. See SDL_BeginThreadFunction for the
  247. * technical details.
  248. *
  249. * \param fn the SDL_ThreadFunction function to call in the new thread
  250. * \param name the name of the thread
  251. * \param stacksize the size, in bytes, to allocate for the new thread stack.
  252. * \param data a pointer that is passed to `fn`
  253. * \returns an opaque pointer to the new thread object on success, NULL if the
  254. * new thread could not be created; call SDL_GetError() for more
  255. * information.
  256. *
  257. * \since This function is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_CreateThread
  260. * \sa SDL_WaitThread
  261. */
  262. extern SDL_DECLSPEC SDL_Thread * SDLCALL SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data);
  263. #endif
  264. #ifndef SDL_WIKI_DOCUMENTATION_SECTION
  265. /* These are the actual functions exported from SDL! Don't use them directly! Use the SDL_CreateThread and SDL_CreateThreadWithStackSize macros! */
  266. extern SDL_DECLSPEC SDL_Thread *SDLCALL SDL_CreateThreadRuntime(SDL_ThreadFunction fn, const char *name, void *data, SDL_FunctionPointer pfnBeginThread, SDL_FunctionPointer pfnEndThread);
  267. extern SDL_DECLSPEC SDL_Thread *SDLCALL SDL_CreateThreadWithStackSizeRuntime(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data,SDL_FunctionPointer pfnBeginThread, SDL_FunctionPointer pfnEndThread);
  268. #define SDL_CreateThread(fn, name, data) SDL_CreateThreadRuntime(fn, name, data, (SDL_FunctionPointer) (SDL_BeginThreadFunction), (SDL_FunctionPointer) (SDL_EndThreadFunction))
  269. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSizeRuntime(fn, name, stacksize, data, (SDL_FunctionPointer) (SDL_BeginThreadFunction), (SDL_FunctionPointer) (SDL_EndThreadFunction))
  270. #endif
  271. /**
  272. * Get the thread name as it was specified in SDL_CreateThread().
  273. *
  274. * This is internal memory, not to be freed by the caller, and remains valid
  275. * until the specified thread is cleaned up by SDL_WaitThread().
  276. *
  277. * \param thread the thread to query
  278. * \returns a pointer to a UTF-8 string that names the specified thread, or
  279. * NULL if it doesn't have a name.
  280. *
  281. * \since This function is available since SDL 3.0.0.
  282. */
  283. extern SDL_DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  284. /**
  285. * Get the thread identifier for the current thread.
  286. *
  287. * This thread identifier is as reported by the underlying operating system.
  288. * If SDL is running on a platform that does not support threads the return
  289. * value will always be zero.
  290. *
  291. * This function also returns a valid thread ID when called from the main
  292. * thread.
  293. *
  294. * \returns the ID of the current thread.
  295. *
  296. * \since This function is available since SDL 3.0.0.
  297. *
  298. * \sa SDL_GetThreadID
  299. */
  300. extern SDL_DECLSPEC SDL_ThreadID SDLCALL SDL_GetCurrentThreadID(void);
  301. /**
  302. * Get the thread identifier for the specified thread.
  303. *
  304. * This thread identifier is as reported by the underlying operating system.
  305. * If SDL is running on a platform that does not support threads the return
  306. * value will always be zero.
  307. *
  308. * \param thread the thread to query
  309. * \returns the ID of the specified thread, or the ID of the current thread if
  310. * `thread` is NULL.
  311. *
  312. * \since This function is available since SDL 3.0.0.
  313. *
  314. * \sa SDL_GetCurrentThreadID
  315. */
  316. extern SDL_DECLSPEC SDL_ThreadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  317. /**
  318. * Set the priority for the current thread.
  319. *
  320. * Note that some platforms will not let you alter the priority (or at least,
  321. * promote the thread to a higher priority) at all, and some require you to be
  322. * an administrator account. Be prepared for this to fail.
  323. *
  324. * \param priority the SDL_ThreadPriority to set
  325. * \returns 0 on success or a negative error code on failure; call
  326. * SDL_GetError() for more information.
  327. *
  328. * \since This function is available since SDL 3.0.0.
  329. */
  330. extern SDL_DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  331. /**
  332. * Wait for a thread to finish.
  333. *
  334. * Threads that haven't been detached will remain (as a "zombie") until this
  335. * function cleans them up. Not doing so is a resource leak.
  336. *
  337. * Once a thread has been cleaned up through this function, the SDL_Thread
  338. * that references it becomes invalid and should not be referenced again. As
  339. * such, only one thread may call SDL_WaitThread() on another.
  340. *
  341. * The return code for the thread function is placed in the area pointed to by
  342. * `status`, if `status` is not NULL.
  343. *
  344. * You may not wait on a thread that has been used in a call to
  345. * SDL_DetachThread(). Use either that function or this one, but not both, or
  346. * behavior is undefined.
  347. *
  348. * It is safe to pass a NULL thread to this function; it is a no-op.
  349. *
  350. * Note that the thread pointer is freed by this function and is not valid
  351. * afterward.
  352. *
  353. * \param thread the SDL_Thread pointer that was returned from the
  354. * SDL_CreateThread() call that started this thread
  355. * \param status pointer to an integer that will receive the value returned
  356. * from the thread function by its 'return', or NULL to not
  357. * receive such value back.
  358. *
  359. * \since This function is available since SDL 3.0.0.
  360. *
  361. * \sa SDL_CreateThread
  362. * \sa SDL_DetachThread
  363. */
  364. extern SDL_DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  365. /**
  366. * Let a thread clean up on exit without intervention.
  367. *
  368. * A thread may be "detached" to signify that it should not remain until
  369. * another thread has called SDL_WaitThread() on it. Detaching a thread is
  370. * useful for long-running threads that nothing needs to synchronize with or
  371. * further manage. When a detached thread is done, it simply goes away.
  372. *
  373. * There is no way to recover the return code of a detached thread. If you
  374. * need this, don't detach the thread and instead use SDL_WaitThread().
  375. *
  376. * Once a thread is detached, you should usually assume the SDL_Thread isn't
  377. * safe to reference again, as it will become invalid immediately upon the
  378. * detached thread's exit, instead of remaining until someone has called
  379. * SDL_WaitThread() to finally clean it up. As such, don't detach the same
  380. * thread more than once.
  381. *
  382. * If a thread has already exited when passed to SDL_DetachThread(), it will
  383. * stop waiting for a call to SDL_WaitThread() and clean up immediately. It is
  384. * not safe to detach a thread that might be used with SDL_WaitThread().
  385. *
  386. * You may not call SDL_WaitThread() on a thread that has been detached. Use
  387. * either that function or this one, but not both, or behavior is undefined.
  388. *
  389. * It is safe to pass NULL to this function; it is a no-op.
  390. *
  391. * \param thread the SDL_Thread pointer that was returned from the
  392. * SDL_CreateThread() call that started this thread
  393. *
  394. * \since This function is available since SDL 3.0.0.
  395. *
  396. * \sa SDL_CreateThread
  397. * \sa SDL_WaitThread
  398. */
  399. extern SDL_DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
  400. /**
  401. * Create a piece of thread-local storage.
  402. *
  403. * This creates an identifier that is globally visible to all threads but
  404. * refers to data that is thread-specific.
  405. *
  406. * \returns the newly created thread local storage identifier or 0 on error.
  407. *
  408. * \since This function is available since SDL 3.0.0.
  409. *
  410. * \sa SDL_GetTLS
  411. * \sa SDL_SetTLS
  412. */
  413. extern SDL_DECLSPEC SDL_TLSID SDLCALL SDL_CreateTLS(void);
  414. /**
  415. * Get the current thread's value associated with a thread local storage ID.
  416. *
  417. * \param id the thread local storage ID
  418. * \returns the value associated with the ID for the current thread or NULL if
  419. * no value has been set; call SDL_GetError() for more information.
  420. *
  421. * \since This function is available since SDL 3.0.0.
  422. *
  423. * \sa SDL_SetTLS
  424. */
  425. extern SDL_DECLSPEC void * SDLCALL SDL_GetTLS(SDL_TLSID id);
  426. /**
  427. * Set the current thread's value associated with a thread local storage ID.
  428. *
  429. * The function prototype for `destructor` is:
  430. *
  431. * ```c
  432. * void destructor(void *value)
  433. * ```
  434. *
  435. * where its parameter `value` is what was passed as `value` to SDL_SetTLS().
  436. *
  437. * \param id the thread local storage ID
  438. * \param value the value to associate with the ID for the current thread
  439. * \param destructor a function called when the thread exits, to free the
  440. * value
  441. * \returns 0 on success or a negative error code on failure; call
  442. * SDL_GetError() for more information.
  443. *
  444. * \since This function is available since SDL 3.0.0.
  445. *
  446. * \sa SDL_GetTLS
  447. */
  448. extern SDL_DECLSPEC int SDLCALL SDL_SetTLS(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void*));
  449. /**
  450. * Cleanup all TLS data for this thread.
  451. *
  452. * \since This function is available since SDL 3.0.0.
  453. */
  454. extern SDL_DECLSPEC void SDLCALL SDL_CleanupTLS(void);
  455. /* Ends C function definitions when using C++ */
  456. #ifdef __cplusplus
  457. }
  458. #endif
  459. #include <SDL3/SDL_close_code.h>
  460. #endif /* SDL_thread_h_ */