SDL_thread.h 16 KB

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