SDL_systhread.c 7.4 KB

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