Enabled thread naming on Windows.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 31 Aug 2013 01:36:38 -0400
changeset 772454c06b0f65d9
parent 7723 aea98cc3e696
child 7725 c61ab95113fe
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...)
src/thread/windows/SDL_systhread.c
     1.1 --- a/src/thread/windows/SDL_systhread.c	Thu Aug 29 15:02:32 2013 -0300
     1.2 +++ b/src/thread/windows/SDL_systhread.c	Sat Aug 31 01:36:38 2013 -0400
     1.3 @@ -146,6 +146,7 @@
     1.4  }
     1.5  
     1.6  #ifdef _MSC_VER
     1.7 +#pragma warning(disable : 4733)
     1.8  #pragma pack(push,8)
     1.9  typedef struct tagTHREADNAME_INFO
    1.10  {
    1.11 @@ -155,31 +156,46 @@
    1.12      DWORD dwFlags; /* reserved for future use, must be zero */
    1.13  } THREADNAME_INFO;
    1.14  #pragma pack(pop)
    1.15 +
    1.16 +static EXCEPTION_DISPOSITION
    1.17 +ignore_exception(void *a, void *b, void *c, void *d)
    1.18 +{
    1.19 +    return ExceptionContinueExecution;
    1.20 +}
    1.21  #endif
    1.22  
    1.23  void
    1.24  SDL_SYS_SetupThread(const char *name)
    1.25  {
    1.26      if (name != NULL) {
    1.27 -        #if 0  /* !!! FIXME: __except needs C runtime, which we don't link against. */
    1.28 -        #ifdef _MSC_VER  /* !!! FIXME: can we do SEH on other compilers yet? */
    1.29 -        /* This magic tells the debugger to name a thread if it's listening. */
    1.30 +        #ifdef _MSC_VER
    1.31 +        /* This magic tells the debugger to name a thread if it's listening.
    1.32 +            The inline asm sets up SEH (__try/__except) without C runtime
    1.33 +            support. See Microsoft Systems Journal, January 1997:
    1.34 +            http://www.microsoft.com/msj/0197/exception/exception.aspx */
    1.35 +        INT_PTR handler = (INT_PTR) ignore_exception;
    1.36          THREADNAME_INFO inf;
    1.37 +
    1.38          inf.dwType = 0x1000;
    1.39          inf.szName = name;
    1.40          inf.dwThreadID = (DWORD) -1;
    1.41          inf.dwFlags = 0;
    1.42  
    1.43 -        __try
    1.44 -        {
    1.45 -            RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
    1.46 +        __asm {   /* set up SEH */
    1.47 +            push handler
    1.48 +            push fs:[0]
    1.49 +            mov fs:[0],esp
    1.50          }
    1.51 -        __except(EXCEPTION_CONTINUE_EXECUTION)
    1.52 -        {
    1.53 -            /* The program itself should ignore this bogus exception. */
    1.54 +
    1.55 +        /* The program itself should ignore this bogus exception. */
    1.56 +        RaiseException(0x406D1388, 0, sizeof(inf)/sizeof(DWORD), (DWORD*)&inf);
    1.57 +
    1.58 +        __asm {  /* tear down SEH. */
    1.59 +            mov eax,[esp]
    1.60 +            mov fs:[0], eax
    1.61 +            add esp, 8
    1.62          }
    1.63          #endif
    1.64 -        #endif
    1.65      }
    1.66  }
    1.67