SDL_thread.h 22 KB

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