Enabled thread naming on Windows. This is now done without compiler or C runtime support for __try/__except. (Granted, it uses Visual Studio-style inline asm, but still...)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c
index 6cf826e..7117ba3 100644
--- a/src/thread/windows/SDL_systhread.c
+++ b/src/thread/windows/SDL_systhread.c
@@ -146,6 +146,7 @@ SDL_SYS_CreateThread(SDL_Thread * thread, void *args)
}
#ifdef _MSC_VER
+#pragma warning(disable : 4733)
#pragma pack(push,8)
typedef struct tagTHREADNAME_INFO
{
@@ -155,31 +156,46 @@ typedef struct tagTHREADNAME_INFO
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)
+{
+ return ExceptionContinueExecution;
+}
#endif
void
SDL_SYS_SetupThread(const char *name)
{
if (name != NULL) {
- #if 0 /* !!! FIXME: __except needs C runtime, which we don't link against. */
- #ifdef _MSC_VER /* !!! FIXME: can we do SEH on other compilers yet? */
- /* This magic tells the debugger to name a thread if it's listening. */
+ #ifdef _MSC_VER
+ /* 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;
inf.szName = name;
inf.dwThreadID = (DWORD) -1;
inf.dwFlags = 0;
- __try
- {
- RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
+ __asm { /* set up SEH */
+ push handler
+ push fs:[0]
+ mov fs:[0],esp
}
- __except(EXCEPTION_CONTINUE_EXECUTION)
- {
- /* The program itself should ignore this bogus exception. */
+
+ /* The program itself should ignore this bogus exception. */
+ RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
+
+ __asm { /* tear down SEH. */
+ mov eax,[esp]
+ mov fs:[0], eax
+ add esp, 8
}
#endif
- #endif
}
}