21 #include "../../SDL_internal.h"
23 #if SDL_THREAD_WINDOWS
29 #include "../SDL_thread_c.h"
30 #include "../SDL_systhread.h"
31 #include "SDL_systhread_c.h"
33 #ifndef SDL_PASSED_BEGINTHREAD_ENDTHREAD
37 #ifndef STACK_SIZE_PARAM_IS_A_RESERVATION
38 #define STACK_SIZE_PARAM_IS_A_RESERVATION 0x00010000
42 #if (defined(__MINGW32__) && (__GNUC__ < 4))
43 typedef unsigned long (__cdecl *pfnSDL_CurrentBeginThread) (
void *, unsigned,
44 unsigned (__stdcall *
func)(
void *),
void *arg,
45 unsigned,
unsigned *threadID);
46 typedef void (__cdecl *pfnSDL_CurrentEndThread)(
unsigned code);
48 #elif defined(__WATCOMC__)
50 #if __WATCOMC__ < 1240
53 typedef unsigned long (__watcall * pfnSDL_CurrentBeginThread) (
void *,
63 typedef void (__watcall * pfnSDL_CurrentEndThread) (
unsigned code);
66 typedef uintptr_t(__cdecl * pfnSDL_CurrentBeginThread) (
void *, unsigned,
72 typedef void (__cdecl * pfnSDL_CurrentEndThread) (
unsigned code);
77 typedef struct ThreadStartParms
80 pfnSDL_CurrentEndThread pfnCurrentEndThread;
81 } tThreadStartParms, *pThreadStartParms;
86 pThreadStartParms pThreadParms = (pThreadStartParms)
data;
87 pfnSDL_CurrentEndThread pfnEndThread = pThreadParms->pfnCurrentEndThread;
88 void *args = pThreadParms->args;
91 if (pfnEndThread !=
NULL)
97 RunThreadViaCreateThread(LPVOID
data)
102 static unsigned __stdcall
103 RunThreadViaBeginThreadEx(
void *
data)
108 #ifdef SDL_PASSED_BEGINTHREAD_ENDTHREAD
111 pfnSDL_CurrentBeginThread pfnBeginThread,
112 pfnSDL_CurrentEndThread pfnEndThread)
114 #elif defined(__CYGWIN__) || defined(__WINRT__)
118 pfnSDL_CurrentBeginThread pfnBeginThread =
NULL;
119 pfnSDL_CurrentEndThread pfnEndThread =
NULL;
124 pfnSDL_CurrentBeginThread pfnBeginThread = (pfnSDL_CurrentBeginThread)_beginthreadex;
125 pfnSDL_CurrentEndThread pfnEndThread = (pfnSDL_CurrentEndThread)_endthreadex;
127 pThreadStartParms pThreadParms =
128 (pThreadStartParms)
SDL_malloc(
sizeof(tThreadStartParms));
129 const DWORD
flags = thread->
stacksize ? STACK_SIZE_PARAM_IS_A_RESERVATION : 0;
134 pThreadParms->pfnCurrentEndThread = pfnEndThread;
136 pThreadParms->args = args;
139 if (pfnBeginThread) {
140 unsigned threadid = 0;
142 ((
size_t) pfnBeginThread(
NULL, (
unsigned int) thread->
stacksize,
143 RunThreadViaBeginThreadEx,
144 pThreadParms,
flags, &threadid));
148 RunThreadViaCreateThread,
149 pThreadParms,
flags, &threadid);
152 return SDL_SetError(
"Not enough resources to create thread");
158 typedef struct tagTHREADNAME_INFO
168 typedef HRESULT (WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
175 static pfnSetThreadDescription pSetThreadDescription =
NULL;
176 static HMODULE kernel32 = 0;
179 kernel32 = LoadLibraryW(L
"kernel32.dll");
181 pSetThreadDescription = (pfnSetThreadDescription) GetProcAddress(kernel32,
"SetThreadDescription");
185 if (pSetThreadDescription !=
NULL) {
188 pSetThreadDescription(GetCurrentThread(), strw);
198 if (IsDebuggerPresent()) {
210 inf.dwThreadID = (DWORD) -1;
214 RaiseException(0x406D1388, 0,
sizeof(inf) /
sizeof(ULONG), (
const ULONG_PTR*) &inf);
231 value = THREAD_PRIORITY_LOWEST;
233 value = THREAD_PRIORITY_HIGHEST;
235 value = THREAD_PRIORITY_TIME_CRITICAL;
237 value = THREAD_PRIORITY_NORMAL;
239 if (!SetThreadPriority(GetCurrentThread(),
value)) {
248 WaitForSingleObjectEx(thread->
handle, INFINITE,
FALSE);
249 CloseHandle(thread->
handle);
255 CloseHandle(thread->
handle);