SDL_systhread.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 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. #include "../../SDL_internal.h"
  19. #if SDL_THREAD_WINDOWS
  20. /* Win32 thread management routines for SDL */
  21. #include "SDL_hints.h"
  22. #include "SDL_thread.h"
  23. #include "../SDL_thread_c.h"
  24. #include "../SDL_systhread.h"
  25. #include "SDL_systhread_c.h"
  26. #ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
  27. /* We'll use the C library from this DLL */
  28. #include <process.h>
  29. #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
  30. #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
  31. #endif
  32. /* Cygwin gcc-3 ... MingW64 (even with a i386 host) does this like MSVC. */
  33. #if (defined(__MINGW32__) && (__GNUC__ < 4))
  34. typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (void *, unsigned,
  35. unsigned (__stdcall *func)(void *), void *arg,
  36. unsigned, unsigned *threadID);
  37. typedef void (__cdecl *pfnSDL_CurrentEndThread)(unsigned code);
  38. #elif defined(__WATCOMC__)
  39. /* This is for Watcom targets except OS2 */
  40. #if __WATCOMC__ < 1240
  41. #define __watcall
  42. #endif
  43. typedef unsigned long (__watcall * pfnSDL_CurrentBeginThread) (void *,
  44. unsigned,
  45. unsigned
  46. (__stdcall *
  47. func) (void
  48. *),
  49. void *arg,
  50. unsigned,
  51. unsigned
  52. *threadID);
  53. typedef void (__watcall * pfnSDL_CurrentEndThread) (unsigned code);
  54. #else
  55. typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (void *, unsigned,
  56. unsigned (__stdcall *
  57. func) (void
  58. *),
  59. void *arg, unsigned,
  60. unsigned *threadID);
  61. typedef void (__cdecl * pfnSDL_CurrentEndThread) (unsigned code);
  62. #endif
  63. #endif /* !SDL_PASSED_BEGINTHREAD_ENDTHREAD */
  64. typedef struct ThreadStartParms
  65. {
  66. void *args;
  67. pfnSDL_CurrentEndThread pfnCurrentEndThread;
  68. } tThreadStartParms, *pThreadStartParms;
  69. static DWORD
  70. RunThread(void *data)
  71. {
  72. pThreadStartParms pThreadParms = (pThreadStartParms) data;
  73. pfnSDL_CurrentEndThread pfnEndThread = pThreadParms->pfnCurrentEndThread;
  74. void *args = pThreadParms->args;
  75. SDL_free(pThreadParms);
  76. SDL_RunThread(args);
  77. if (pfnEndThread != NULL)
  78. pfnEndThread(0);
  79. return (0);
  80. }
  81. static DWORD WINAPI
  82. RunThreadViaCreateThread(LPVOID data)
  83. {
  84. return RunThread(data);
  85. }
  86. static unsigned __stdcall
  87. RunThreadViaBeginThreadEx(void *data)
  88. {
  89. return (unsigned) RunThread(data);
  90. }
  91. #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
  92. int
  93. SDL_SYS_CreateThread(SDL_Thread * thread, void *args,
  94. pfnSDL_CurrentBeginThread pfnBeginThread,
  95. pfnSDL_CurrentEndThread pfnEndThread)
  96. {
  97. #elif defined(__CYGWIN__) || defined(__WINRT__)
  98. int
  99. SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
  100. {
  101. pfnSDL_CurrentBeginThread pfnBeginThread = NULL;
  102. pfnSDL_CurrentEndThread pfnEndThread = NULL;
  103. #else
  104. int
  105. SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
  106. {
  107. pfnSDL_CurrentBeginThread pfnBeginThread = (pfnSDL_CurrentBeginThread)_beginthreadex;
  108. pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread)_endthreadex;
  109. #endif /* SDL_PASSED_BEGINTHREAD_ENDTHREAD */
  110. pThreadStartParms pThreadParms =
  111. (pThreadStartParms) SDL_malloc(sizeof(tThreadStartParms));
  112. const DWORD flags = thread->stacksize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0;
  113. if (!pThreadParms) {
  114. return SDL_OutOfMemory();
  115. }
  116. /* Save the function which we will have to call to clear the RTL of calling app! */
  117. pThreadParms->pfnCurrentEndThread = pfnEndThread;
  118. /* Also save the real parameters we have to pass to thread function */
  119. pThreadParms->args = args;
  120. /* thread->stacksize == 0 means "system default", same as win32 expects */
  121. if (pfnBeginThread) {
  122. unsigned threadid = 0;
  123. thread->handle = (SYS_ThreadHandle)
  124. ((size_t) pfnBeginThread(NULL, (unsigned int) thread->stacksize,
  125. RunThreadViaBeginThreadEx,
  126. pThreadParms, flags, &threadid));
  127. } else {
  128. DWORD threadid = 0;
  129. thread->handle = CreateThread(NULL, thread->stacksize,
  130. RunThreadViaCreateThread,
  131. pThreadParms, flags, &threadid);
  132. }
  133. if (thread->handle == NULL) {
  134. return SDL_SetError("Not enough resources to create thread");
  135. }
  136. return 0;
  137. }
  138. #pragma pack(push,8)
  139. typedef struct tagTHREADNAME_INFO
  140. {
  141. DWORD dwType; /* must be 0x1000 */
  142. LPCSTR szName; /* pointer to name (in user addr space) */
  143. DWORD dwThreadID; /* thread ID (-1=caller thread) */
  144. DWORD dwFlags; /* reserved for future use, must be zero */
  145. } THREADNAME_INFO;
  146. #pragma pack(pop)
  147. typedef HRESULT (WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
  148. void
  149. SDL_SYS_SetupThread(const char *name)
  150. {
  151. if (name != NULL) {
  152. #ifndef __WINRT__ /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
  153. static pfnSetThreadDescription pSetThreadDescription = NULL;
  154. static HMODULE kernel32 = 0;
  155. if (!kernel32) {
  156. kernel32 = LoadLibraryW(L"kernel32.dll");
  157. if (kernel32) {
  158. pSetThreadDescription = (pfnSetThreadDescription) GetProcAddress(kernel32, "SetThreadDescription");
  159. }
  160. }
  161. if (pSetThreadDescription != NULL) {
  162. WCHAR *strw = WIN_UTF8ToString(name);
  163. if (strw) {
  164. pSetThreadDescription(GetCurrentThread(), strw);
  165. SDL_free(strw);
  166. }
  167. }
  168. #endif
  169. /* Presumably some version of Visual Studio will understand SetThreadDescription(),
  170. but we still need to deal with older OSes and debuggers. Set it with the arcane
  171. exception magic, too. */
  172. if (IsDebuggerPresent()) {
  173. THREADNAME_INFO inf;
  174. /* C# and friends will try to catch this Exception, let's avoid it. */
  175. if (SDL_GetHintBoolean(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, SDL_TRUE)) {
  176. return;
  177. }
  178. /* This magic tells the debugger to name a thread if it's listening. */
  179. SDL_zero(inf);
  180. inf.dwType = 0x1000;
  181. inf.szName = name;
  182. inf.dwThreadID = (DWORD) -1;
  183. inf.dwFlags = 0;
  184. /* The debugger catches this, renames the thread, continues on. */
  185. RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf);
  186. }
  187. }
  188. }
  189. SDL_threadID
  190. SDL_ThreadID(void)
  191. {
  192. return ((SDL_threadID) GetCurrentThreadId());
  193. }
  194. int
  195. SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  196. {
  197. int value;
  198. if (priority == SDL_THREAD_PRIORITY_LOW) {
  199. value = THREAD_PRIORITY_LOWEST;
  200. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  201. value = THREAD_PRIORITY_HIGHEST;
  202. } else {
  203. value = THREAD_PRIORITY_NORMAL;
  204. }
  205. if (!SetThreadPriority(GetCurrentThread(), value)) {
  206. return WIN_SetError("SetThreadPriority()");
  207. }
  208. return 0;
  209. }
  210. void
  211. SDL_SYS_WaitThread(SDL_Thread * thread)
  212. {
  213. WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
  214. CloseHandle(thread->handle);
  215. }
  216. void
  217. SDL_SYS_DetachThread(SDL_Thread * thread)
  218. {
  219. CloseHandle(thread->handle);
  220. }
  221. #endif /* SDL_THREAD_WINDOWS */
  222. /* vi: set ts=4 sw=4 expandtab: */