SDL_thread.h 20 KB

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