diff -r 6b3cc8b7c589 src/thread/windows/SDL_systhread.c --- a/src/thread/windows/SDL_systhread.c Tue Oct 01 08:47:06 2013 -0300 +++ b/src/thread/windows/SDL_systhread.c Wed Oct 02 10:49:55 2013 +0200 @@ -145,35 +145,51 @@ return 0; } -#ifdef _MSC_VER -#pragma warning(disable : 4733) -#pragma pack(push,8) +/* These macros can be used to disable thread names or force a certain behavior. + * SDL_THREAD_NAMES_DISABLED Disable thread names. + * SDL_THREAD_FORCE_VECTORED_EH Always use vectored exception handling. + */ +#ifndef SDL_THREAD_NAMES_DISABLED +/* The size of this struct should be 16 bytes for x86 and 24 bytes for x64. */ typedef struct tagTHREADNAME_INFO { - DWORD dwType; /* must be 0x1000 */ + ULONG_PTR dwType; /* must be 0x1000 */ LPCSTR szName; /* pointer to name (in user addr space) */ DWORD dwThreadID; /* thread ID (-1=caller thread) */ DWORD dwFlags; /* reserved for future use, must be zero */ } THREADNAME_INFO; -#pragma pack(pop) -static EXCEPTION_DISPOSITION -ignore_exception(void *a, void *b, void *c, void *d) +#if defined(SDL_THREAD_FORCE_VECTORED_EH) || !defined(HAVE_LIBC) +/* Do the exception handling with a vectored exception handler. */ +#define SDL_THREAD_VECTORED_EH +#elif defined(_MSC_VER) && defined(HAVE_LIBC) +/* Do the exception handling with Visual C++'s __try/__except extension. */ +#define SDL_THREAD_MSVC_TRY_EXCEPT +#else +/* No way of handling the exception. Disable thread names. */ +#define SDL_THREAD_NAMES_DISABLED +#endif + +#ifdef SDL_THREAD_VECTORED_EH +static LONG CALLBACK +ignore_exception(PEXCEPTION_POINTERS ExceptionInfo) { - return ExceptionContinueExecution; + if (ExceptionInfo->ExceptionRecord->ExceptionCode == 0x406D1388) { + return EXCEPTION_CONTINUE_EXECUTION; /* -1 */ + } + return EXCEPTION_CONTINUE_SEARCH; /* 0 */ } + +/* This acts as a flag that the handler has been added. It's not pretty. Any Ideas? */ +static void *vectored_exception_handler = NULL; +#endif #endif void SDL_SYS_SetupThread(const char *name) { + #ifndef SDL_THREAD_NAMES_DISABLED if (name != NULL) { - #if (defined(_MSC_VER) && defined(_M_IX86)) - /* This magic tells the debugger to name a thread if it's listening. - The inline asm sets up SEH (__try/__except) without C runtime - support. See Microsoft Systems Journal, January 1997: - http://www.microsoft.com/msj/0197/exception/exception.aspx */ - INT_PTR handler = (INT_PTR) ignore_exception; THREADNAME_INFO inf; inf.dwType = 0x1000; @@ -181,22 +197,23 @@ inf.dwThreadID = (DWORD) -1; inf.dwFlags = 0; - __asm { /* set up SEH */ - push handler - push fs:[0] - mov fs:[0],esp + #if defined(SDL_THREAD_VECTORED_EH) + if (vectored_exception_handler == NULL) { + vectored_exception_handler = ignore_exception; + AddVectoredExceptionHandler(0, ignore_exception); } + #elif defined(SDL_THREAD_MSVC_TRY_EXCEPT) + __try { + #endif /* The program itself should ignore this bogus exception. */ - RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf); + RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (const ULONG_PTR*)&inf); - __asm { /* tear down SEH. */ - mov eax,[esp] - mov fs:[0], eax - add esp, 8 - } + #if defined(SDL_THREAD_MSVC_TRY_EXCEPT) + } __except(EXCEPTION_EXECUTE_HANDLER) {} #endif } + #endif } SDL_threadID