🛠️🐜 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.

343 lines
13 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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. #include "begin_code.h"
  31. /* Set up for C function definitions, even when using C++ */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /* The SDL thread structure, defined in SDL_thread.c */
  36. struct SDL_Thread;
  37. typedef struct SDL_Thread SDL_Thread;
  38. /* The SDL thread ID */
  39. typedef unsigned long SDL_threadID;
  40. /* Thread local storage ID, 0 is the invalid ID */
  41. typedef unsigned int SDL_TLSID;
  42. /**
  43. * The SDL thread priority.
  44. *
  45. * \note On many systems you require special privileges to set high or time critical priority.
  46. */
  47. typedef enum {
  48. SDL_THREAD_PRIORITY_LOW,
  49. SDL_THREAD_PRIORITY_NORMAL,
  50. SDL_THREAD_PRIORITY_HIGH,
  51. SDL_THREAD_PRIORITY_TIME_CRITICAL
  52. } SDL_ThreadPriority;
  53. /**
  54. * The function passed to SDL_CreateThread().
  55. * It is passed a void* user context parameter and returns an int.
  56. */
  57. typedef int (SDLCALL * SDL_ThreadFunction) (void *data);
  58. #if defined(__WIN32__) && !defined(HAVE_LIBC)
  59. /**
  60. * \file SDL_thread.h
  61. *
  62. * We compile SDL into a DLL. This means, that it's the DLL which
  63. * creates a new thread for the calling process with the SDL_CreateThread()
  64. * API. There is a problem with this, that only the RTL of the SDL2.DLL will
  65. * be initialized for those threads, and not the RTL of the calling
  66. * application!
  67. *
  68. * To solve this, we make a little hack here.
  69. *
  70. * We'll always use the caller's _beginthread() and _endthread() APIs to
  71. * start a new thread. This way, if it's the SDL2.DLL which uses this API,
  72. * then the RTL of SDL2.DLL will be used to create the new thread, and if it's
  73. * the application, then the RTL of the application will be used.
  74. *
  75. * So, in short:
  76. * Always use the _beginthread() and _endthread() of the calling runtime
  77. * library!
  78. */
  79. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  80. #include <process.h> /* _beginthreadex() and _endthreadex() */
  81. typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread)
  82. (void *, unsigned, unsigned (__stdcall *func)(void *),
  83. void * /*arg*/, unsigned, unsigned * /* threadID */);
  84. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  85. /**
  86. * Create a thread.
  87. */
  88. extern DECLSPEC SDL_Thread *SDLCALL
  89. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  90. pfnSDL_CurrentBeginThread pfnBeginThread,
  91. pfnSDL_CurrentEndThread pfnEndThread);
  92. extern DECLSPEC SDL_Thread *SDLCALL
  93. SDL_CreateThreadWithStackSize(int (SDLCALL * fn) (void *),
  94. const char *name, const size_t stacksize, void *data,
  95. pfnSDL_CurrentBeginThread pfnBeginThread,
  96. pfnSDL_CurrentEndThread pfnEndThread);
  97. /**
  98. * Create a thread.
  99. */
  100. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  101. #undef SDL_CreateThread
  102. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  103. #undef SDL_CreateThreadWithStackSize
  104. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  105. #else
  106. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  107. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthreadex, (pfnSDL_CurrentEndThread)_endthreadex)
  108. #endif
  109. #elif defined(__OS2__)
  110. /*
  111. * just like the windows case above: We compile SDL2
  112. * into a dll with Watcom's runtime statically linked.
  113. */
  114. #define SDL_PASSED_BEGINTHREAD_ENDTHREAD
  115. #ifndef __EMX__
  116. #include <process.h>
  117. #else
  118. #include <stdlib.h>
  119. #endif
  120. typedef int (*pfnSDL_CurrentBeginThread)(void (*func)(void *), void *, unsigned, void * /*arg*/);
  121. typedef void (*pfnSDL_CurrentEndThread)(void);
  122. extern DECLSPEC SDL_Thread *SDLCALL
  123. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,
  124. pfnSDL_CurrentBeginThread pfnBeginThread,
  125. pfnSDL_CurrentEndThread pfnEndThread);
  126. extern DECLSPEC SDL_Thread *SDLCALL
  127. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data,
  128. pfnSDL_CurrentBeginThread pfnBeginThread,
  129. pfnSDL_CurrentEndThread pfnEndThread);
  130. #if defined(SDL_CreateThread) && SDL_DYNAMIC_API
  131. #undef SDL_CreateThread
  132. #define SDL_CreateThread(fn, name, data) SDL_CreateThread_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthread, (pfnSDL_CurrentEndThread)_endthread)
  133. #undef SDL_CreateThreadWithStackSize
  134. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize_REAL(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthread, (pfnSDL_CurrentEndThread)_endthread)
  135. #else
  136. #define SDL_CreateThread(fn, name, data) SDL_CreateThread(fn, name, data, (pfnSDL_CurrentBeginThread)_beginthread, (pfnSDL_CurrentEndThread)_endthread)
  137. #define SDL_CreateThreadWithStackSize(fn, name, stacksize, data) SDL_CreateThreadWithStackSize(fn, name, stacksize, data, (pfnSDL_CurrentBeginThread)_beginthread, (pfnSDL_CurrentEndThread)_endthread)
  138. #endif
  139. #else
  140. /**
  141. * Create a thread with a default stack size.
  142. *
  143. * This is equivalent to calling:
  144. * SDL_CreateThreadWithStackSize(fn, name, 0, data);
  145. */
  146. extern DECLSPEC SDL_Thread *SDLCALL
  147. SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);
  148. /**
  149. * Create a thread.
  150. *
  151. * Thread naming is a little complicated: Most systems have very small
  152. * limits for the string length (Haiku has 32 bytes, Linux currently has 16,
  153. * Visual C++ 6.0 has nine!), and possibly other arbitrary rules. You'll
  154. * have to see what happens with your system's debugger. The name should be
  155. * UTF-8 (but using the naming limits of C identifiers is a better bet).
  156. * There are no requirements for thread naming conventions, so long as the
  157. * string is null-terminated UTF-8, but these guidelines are helpful in
  158. * choosing a name:
  159. *
  160. * http://stackoverflow.com/questions/149932/naming-conventions-for-threads
  161. *
  162. * If a system imposes requirements, SDL will try to munge the string for
  163. * it (truncate, etc), but the original string contents will be available
  164. * from SDL_GetThreadName().
  165. *
  166. * The size (in bytes) of the new stack can be specified. Zero means "use
  167. * the system default" which might be wildly different between platforms
  168. * (x86 Linux generally defaults to eight megabytes, an embedded device
  169. * might be a few kilobytes instead).
  170. *
  171. * In SDL 2.1, stacksize will be folded into the original SDL_CreateThread
  172. * function.
  173. */
  174. extern DECLSPEC SDL_Thread *SDLCALL
  175. SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, const size_t stacksize, void *data);
  176. #endif
  177. /**
  178. * Get the thread name, as it was specified in SDL_CreateThread().
  179. * This function returns a pointer to a UTF-8 string that names the
  180. * specified thread, or NULL if it doesn't have a name. This is internal
  181. * memory, not to be free()'d by the caller, and remains valid until the
  182. * specified thread is cleaned up by SDL_WaitThread().
  183. */
  184. extern DECLSPEC const char *SDLCALL SDL_GetThreadName(SDL_Thread *thread);
  185. /**
  186. * Get the thread identifier for the current thread.
  187. */
  188. extern DECLSPEC SDL_threadID SDLCALL SDL_ThreadID(void);
  189. /**
  190. * Get the thread identifier for the specified thread.
  191. *
  192. * Equivalent to SDL_ThreadID() if the specified thread is NULL.
  193. */
  194. extern DECLSPEC SDL_threadID SDLCALL SDL_GetThreadID(SDL_Thread * thread);
  195. /**
  196. * Set the priority for the current thread
  197. */
  198. extern DECLSPEC int SDLCALL SDL_SetThreadPriority(SDL_ThreadPriority priority);
  199. /**
  200. * Wait for a thread to finish. Threads that haven't been detached will
  201. * remain (as a "zombie") until this function cleans them up. Not doing so
  202. * is a resource leak.
  203. *
  204. * Once a thread has been cleaned up through this function, the SDL_Thread
  205. * that references it becomes invalid and should not be referenced again.
  206. * As such, only one thread may call SDL_WaitThread() on another.
  207. *
  208. * The return code for the thread function is placed in the area
  209. * pointed to by \c status, if \c status is not NULL.
  210. *
  211. * You may not wait on a thread that has been used in a call to
  212. * SDL_DetachThread(). Use either that function or this one, but not
  213. * both, or behavior is undefined.
  214. *
  215. * It is safe to pass NULL to this function; it is a no-op.
  216. */
  217. extern DECLSPEC void SDLCALL SDL_WaitThread(SDL_Thread * thread, int *status);
  218. /**
  219. * A thread may be "detached" to signify that it should not remain until
  220. * another thread has called SDL_WaitThread() on it. Detaching a thread
  221. * is useful for long-running threads that nothing needs to synchronize
  222. * with or further manage. When a detached thread is done, it simply
  223. * goes away.
  224. *
  225. * There is no way to recover the return code of a detached thread. If you
  226. * need this, don't detach the thread and instead use SDL_WaitThread().
  227. *
  228. * Once a thread is detached, you should usually assume the SDL_Thread isn't
  229. * safe to reference again, as it will become invalid immediately upon
  230. * the detached thread's exit, instead of remaining until someone has called
  231. * SDL_WaitThread() to finally clean it up. As such, don't detach the same
  232. * thread more than once.
  233. *
  234. * If a thread has already exited when passed to SDL_DetachThread(), it will
  235. * stop waiting for a call to SDL_WaitThread() and clean up immediately.
  236. * It is not safe to detach a thread that might be used with SDL_WaitThread().
  237. *
  238. * You may not call SDL_WaitThread() on a thread that has been detached.
  239. * Use either that function or this one, but not both, or behavior is
  240. * undefined.
  241. *
  242. * It is safe to pass NULL to this function; it is a no-op.
  243. */
  244. extern DECLSPEC void SDLCALL SDL_DetachThread(SDL_Thread * thread);
  245. /**
  246. * \brief Create an identifier that is globally visible to all threads but refers to data that is thread-specific.
  247. *
  248. * \return The newly created thread local storage identifier, or 0 on error
  249. *
  250. * \code
  251. * static SDL_SpinLock tls_lock;
  252. * static SDL_TLSID thread_local_storage;
  253. *
  254. * void SetMyThreadData(void *value)
  255. * {
  256. * if (!thread_local_storage) {
  257. * SDL_AtomicLock(&tls_lock);
  258. * if (!thread_local_storage) {
  259. * thread_local_storage = SDL_TLSCreate();
  260. * }
  261. * SDL_AtomicUnlock(&tls_lock);
  262. * }
  263. * SDL_TLSSet(thread_local_storage, value, 0);
  264. * }
  265. *
  266. * void *GetMyThreadData(void)
  267. * {
  268. * return SDL_TLSGet(thread_local_storage);
  269. * }
  270. * \endcode
  271. *
  272. * \sa SDL_TLSGet()
  273. * \sa SDL_TLSSet()
  274. */
  275. extern DECLSPEC SDL_TLSID SDLCALL SDL_TLSCreate(void);
  276. /**
  277. * \brief Get the value associated with a thread local storage ID for the current thread.
  278. *
  279. * \param id The thread local storage ID
  280. *
  281. * \return The value associated with the ID for the current thread, or NULL if no value has been set.
  282. *
  283. * \sa SDL_TLSCreate()
  284. * \sa SDL_TLSSet()
  285. */
  286. extern DECLSPEC void * SDLCALL SDL_TLSGet(SDL_TLSID id);
  287. /**
  288. * \brief Set the value associated with a thread local storage ID for the current thread.
  289. *
  290. * \param id The thread local storage ID
  291. * \param value The value to associate with the ID for the current thread
  292. * \param destructor A function called when the thread exits, to free the value.
  293. *
  294. * \return 0 on success, -1 on error
  295. *
  296. * \sa SDL_TLSCreate()
  297. * \sa SDL_TLSGet()
  298. */
  299. extern DECLSPEC int SDLCALL SDL_TLSSet(SDL_TLSID id, const void *value, void (SDLCALL *destructor)(void*));
  300. /* Ends C function definitions when using C++ */
  301. #ifdef __cplusplus
  302. }
  303. #endif
  304. #include "close_code.h"
  305. #endif /* SDL_thread_h_ */
  306. /* vi: set ts=4 sw=4 expandtab: */