Browse Source

Update thread naming on windows

Edoardo Lolletti 1 year ago
parent
commit
42d6cf9ce7
1 changed files with 16 additions and 7 deletions
  1. 16 7
      src/thread/windows/SDL_systhread.c

+ 16 - 7
src/thread/windows/SDL_systhread.c

@@ -112,11 +112,18 @@ typedef struct tagTHREADNAME_INFO
 } THREADNAME_INFO;
 } THREADNAME_INFO;
 #pragma pack(pop)
 #pragma pack(pop)
 
 
+static LONG NTAPI EmptyVectoredExceptionHandler(EXCEPTION_POINTERS *ExceptionInfo)
+{
+    (void)ExceptionInfo;
+    return EXCEPTION_CONTINUE_EXECUTION;
+}
+
 typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
 typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
 
 
 void SDL_SYS_SetupThread(const char *name)
 void SDL_SYS_SetupThread(const char *name)
 {
 {
     if (name) {
     if (name) {
+        PVOID exceptionHandlerHandle;
 #ifndef __WINRT__ /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
 #ifndef __WINRT__ /* !!! FIXME: There's no LoadLibrary() in WinRT; don't know if SetThreadDescription is available there at all at the moment. */
         static pfnSetThreadDescription pSetThreadDescription = NULL;
         static pfnSetThreadDescription pSetThreadDescription = NULL;
         static HMODULE kernel32 = NULL;
         static HMODULE kernel32 = NULL;
@@ -126,6 +133,12 @@ void SDL_SYS_SetupThread(const char *name)
             if (kernel32) {
             if (kernel32) {
                 pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
                 pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
             }
             }
+            if (!kernel32 || !pSetThreadDescription) {
+                HMODULE kernelBase = GetModuleHandle(TEXT("KernelBase.dll"));
+                if (kernelBase) {
+                    pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernelBase, "SetThreadDescription");
+                }
+            }
         }
         }
 
 
         if (pSetThreadDescription) {
         if (pSetThreadDescription) {
@@ -141,14 +154,9 @@ void SDL_SYS_SetupThread(const char *name)
            but we still need to deal with older OSes and debuggers. Set it with the arcane
            but we still need to deal with older OSes and debuggers. Set it with the arcane
            exception magic, too. */
            exception magic, too. */
 
 
-        if (IsDebuggerPresent()) {
+        exceptionHandlerHandle = AddVectoredExceptionHandler(1, EmptyVectoredExceptionHandler);
+        if (exceptionHandlerHandle) {
             THREADNAME_INFO inf;
             THREADNAME_INFO inf;
-
-            /* C# and friends will try to catch this Exception, let's avoid it. */
-            if (SDL_GetHintBoolean(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, SDL_TRUE)) {
-                return;
-            }
-
             /* This magic tells the debugger to name a thread if it's listening. */
             /* This magic tells the debugger to name a thread if it's listening. */
             SDL_zero(inf);
             SDL_zero(inf);
             inf.dwType = 0x1000;
             inf.dwType = 0x1000;
@@ -158,6 +166,7 @@ void SDL_SYS_SetupThread(const char *name)
 
 
             /* The debugger catches this, renames the thread, continues on. */
             /* The debugger catches this, renames the thread, continues on. */
             RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR *)&inf);
             RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR *)&inf);
+            RemoveVectoredExceptionHandler(exceptionHandlerHandle);
         }
         }
     }
     }
 }
 }