🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

464 lines
17 KiB

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