SDL_systhread.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #ifdef SDL_THREAD_WINDOWS
  20. /* Win32 thread management routines for SDL */
  21. #include "../SDL_thread_c.h"
  22. #include "../SDL_systhread.h"
  23. #include "SDL_systhread_c.h"
  24. #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
  25. #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
  26. #endif
  27. typedef void (__cdecl * SDL_EndThreadExCallback) (unsigned retval);
  28. typedef uintptr_t (__cdecl * SDL_BeginThreadExCallback)
  29. (void *security, unsigned stacksize, unsigned (__stdcall *startaddr)(void *),
  30. void * arglist, unsigned initflag, unsigned *threadaddr);
  31. static DWORD RunThread(void *data)
  32. {
  33. SDL_Thread *thread = (SDL_Thread *)data;
  34. SDL_EndThreadExCallback pfnEndThread = (SDL_EndThreadExCallback)thread->endfunc;
  35. SDL_RunThread(thread);
  36. if (pfnEndThread) {
  37. pfnEndThread(0);
  38. }
  39. return 0;
  40. }
  41. static DWORD WINAPI MINGW32_FORCEALIGN RunThreadViaCreateThread(LPVOID data)
  42. {
  43. return RunThread(data);
  44. }
  45. static unsigned __stdcall MINGW32_FORCEALIGN RunThreadViaBeginThreadEx(void *data)
  46. {
  47. return (unsigned)RunThread(data);
  48. }
  49. int SDL_SYS_CreateThread(SDL_Thread *thread,
  50. SDL_FunctionPointer vpfnBeginThread,
  51. SDL_FunctionPointer vpfnEndThread)
  52. {
  53. SDL_BeginThreadExCallback pfnBeginThread = (SDL_BeginThreadExCallback) vpfnBeginThread;
  54. const DWORD flags = thread->stacksize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0;
  55. /* Save the function which we will have to call to clear the RTL of calling app! */
  56. thread->endfunc = vpfnEndThread;
  57. /* thread->stacksize == 0 means "system default", same as win32 expects */
  58. if (pfnBeginThread) {
  59. unsigned threadid = 0;
  60. thread->handle = (SYS_ThreadHandle)((size_t)pfnBeginThread(NULL, (unsigned int)thread->stacksize,
  61. RunThreadViaBeginThreadEx,
  62. thread, flags, &threadid));
  63. } else {
  64. DWORD threadid = 0;
  65. thread->handle = CreateThread(NULL, thread->stacksize,
  66. RunThreadViaCreateThread,
  67. thread, flags, &threadid);
  68. }
  69. if (!thread->handle) {
  70. return SDL_SetError("Not enough resources to create thread");
  71. }
  72. return 0;
  73. }
  74. #pragma pack(push, 8)
  75. typedef struct tagTHREADNAME_INFO
  76. {
  77. DWORD dwType; /* must be 0x1000 */
  78. LPCSTR szName; /* pointer to name (in user addr space) */
  79. DWORD dwThreadID; /* thread ID (-1=caller thread) */
  80. DWORD dwFlags; /* reserved for future use, must be zero */
  81. } THREADNAME_INFO;
  82. #pragma pack(pop)
  83. static LONG NTAPI EmptyVectoredExceptionHandler(EXCEPTION_POINTERS *ExceptionInfo)
  84. {
  85. (void)ExceptionInfo;
  86. return EXCEPTION_CONTINUE_EXECUTION;
  87. }
  88. typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
  89. void SDL_SYS_SetupThread(const char *name)
  90. {
  91. if (name) {
  92. PVOID exceptionHandlerHandle;
  93. #ifndef SDL_PLATFORM_WINRT /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
  94. static pfnSetThreadDescription pSetThreadDescription = NULL;
  95. static HMODULE kernel32 = NULL;
  96. if (!kernel32) {
  97. kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
  98. if (kernel32) {
  99. pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
  100. }
  101. if (!kernel32 || !pSetThreadDescription) {
  102. HMODULE kernelBase = GetModuleHandle(TEXT("KernelBase.dll"));
  103. if (kernelBase) {
  104. pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernelBase, "SetThreadDescription");
  105. }
  106. }
  107. }
  108. if (pSetThreadDescription) {
  109. WCHAR *strw = WIN_UTF8ToStringW(name);
  110. if (strw) {
  111. pSetThreadDescription(GetCurrentThread(), strw);
  112. SDL_free(strw);
  113. }
  114. }
  115. #endif
  116. /* Presumably some version of Visual Studio will understand SetThreadDescription(),
  117. but we still need to deal with older OSes and debuggers. Set it with the arcane
  118. exception magic, too. */
  119. exceptionHandlerHandle = AddVectoredExceptionHandler(1, EmptyVectoredExceptionHandler);
  120. if (exceptionHandlerHandle) {
  121. THREADNAME_INFO inf;
  122. /* This magic tells the debugger to name a thread if it's listening. */
  123. SDL_zero(inf);
  124. inf.dwType = 0x1000;
  125. inf.szName = name;
  126. inf.dwThreadID = (DWORD)-1;
  127. inf.dwFlags = 0;
  128. /* The debugger catches this, renames the thread, continues on. */
  129. RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR *)&inf);
  130. RemoveVectoredExceptionHandler(exceptionHandlerHandle);
  131. }
  132. }
  133. }
  134. SDL_ThreadID SDL_GetCurrentThreadID(void)
  135. {
  136. return (SDL_ThreadID)GetCurrentThreadId();
  137. }
  138. int SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
  139. {
  140. int value;
  141. if (priority == SDL_THREAD_PRIORITY_LOW) {
  142. value = THREAD_PRIORITY_LOWEST;
  143. } else if (priority == SDL_THREAD_PRIORITY_HIGH) {
  144. value = THREAD_PRIORITY_HIGHEST;
  145. } else if (priority == SDL_THREAD_PRIORITY_TIME_CRITICAL) {
  146. value = THREAD_PRIORITY_TIME_CRITICAL;
  147. } else {
  148. value = THREAD_PRIORITY_NORMAL;
  149. }
  150. if (!SetThreadPriority(GetCurrentThread(), value)) {
  151. return WIN_SetError("SetThreadPriority()");
  152. }
  153. return 0;
  154. }
  155. void SDL_SYS_WaitThread(SDL_Thread *thread)
  156. {
  157. WaitForSingleObjectEx(thread->handle, INFINITE, FALSE);
  158. CloseHandle(thread->handle);
  159. }
  160. void SDL_SYS_DetachThread(SDL_Thread *thread)
  161. {
  162. CloseHandle(thread->handle);
  163. }
  164. #endif /* SDL_THREAD_WINDOWS */