Now possible unavailable functions AddVectoredExceptionHandler and
RemoveVectoredExceptionHandler are resolved at runtime. So there is no need
to have compile time checks and conditional compilation code.

So when VectoredExceptionHandler is available at runtime, raise
EXCEPTION_SET_THREAD_NAME from SetThreadName(). If VectoredExceptionHandler
is not available then fallback to SEH (currently available only for MSVC 
builds).
---
 mingw-w64-libraries/winpthreads/src/thread.c | 42 ++++++--------------
 1 file changed, 12 insertions(+), 30 deletions(-)

diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index 71dcae261820..80a132d3de09 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -75,15 +75,6 @@ static size_t idListCnt = 0;
 static size_t idListMax = 0;
 static pthread_t idListNextId = 0;
 
-#if !defined(_MSC_VER)
-#define USE_VEH_FOR_MSC_SETTHREADNAME
-#endif
-#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
-/* forbidden RemoveVectoredExceptionHandler/AddVectoredExceptionHandler APIs */
-#undef USE_VEH_FOR_MSC_SETTHREADNAME
-#endif
-
-#if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
 static void *SetThreadName_VEH_handle = NULL;
 
 static LONG __stdcall
@@ -95,7 +86,6 @@ SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
 
   return EXCEPTION_CONTINUE_SEARCH;
 }
-#endif
 
 typedef struct _THREADNAME_INFO
 {
@@ -118,7 +108,17 @@ SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
 
    infosize = sizeof (info) / sizeof (ULONG_PTR);
 
-#if defined(_MSC_VER) && !defined (USE_VEH_FOR_MSC_SETTHREADNAME)
+   /* Exception has to be processed otherwise it will crash the process. */
+
+   /* First try via VEH */
+   if (SetThreadName_VEH_handle != NULL)
+     {
+       RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (ULONG_PTR *) 
&info);
+       return;
+     }
+
+   /* Then fallback via SEH */
+#if defined(_MSC_VER)
    __try
      {
        RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (ULONG_PTR 
*)&info);
@@ -127,17 +127,7 @@ SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
      {
      }
 #else
-   /* Without a debugger we *must* have an exception handler,
-    * otherwise raising an exception will crash the process.
-    */
-#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
-   if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL))
-#else
-   if (!IsDebuggerPresent ())
-#endif
-     return;
-
-   RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (ULONG_PTR *) 
&info);
+   /* FIXME: SEH exception handling is not support by gcc yet */
 #endif
 }
 
@@ -435,28 +425,23 @@ replace_spin_keys (pthread_spinlock_t *old, 
pthread_spinlock_t new)
 static void WINAPI
 __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
 {
-#if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
   static PVOID (WINAPI *AddVectoredExceptionHandlerFuncPtr) (ULONG, PVOID) = 
NULL;
   static ULONG (WINAPI *RemoveVectoredExceptionHandlerFuncPtr) (PVOID) = NULL;
   static BOOL haveVectoredExceptionHandlerFuncs = FALSE;
-#endif
   _pthread_v *t = NULL;
   pthread_spinlock_t new_spin_keys = PTHREAD_SPINLOCK_INITIALIZER;
 
   if (dwReason == DLL_PROCESS_DETACH)
     {
-#if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
       if (lpreserved == NULL && SetThreadName_VEH_handle != NULL && 
haveVectoredExceptionHandlerFuncs)
         {
           RemoveVectoredExceptionHandlerFuncPtr (SetThreadName_VEH_handle);
           SetThreadName_VEH_handle = NULL;
         }
-#endif
       free_pthread_mem ();
     }
   else if (dwReason == DLL_PROCESS_ATTACH)
     {
-#if defined(USE_VEH_FOR_MSC_SETTHREADNAME)
       if (!haveVectoredExceptionHandlerFuncs)
         {
           HMODULE kernel32 = GetModuleHandleA("kernel32.dll");
@@ -470,7 +455,6 @@ __dyn_tls_pthread (HANDLE hDllHandle, DWORD dwReason, 
LPVOID lpreserved)
       if (haveVectoredExceptionHandlerFuncs)
         SetThreadName_VEH_handle = AddVectoredExceptionHandlerFuncPtr (1, 
&SetThreadName_VEH);
       /* Can't do anything on error anyway, check for NULL later */
-#endif
     }
   else if (dwReason == DLL_THREAD_DETACH)
     {
@@ -1290,9 +1274,7 @@ pthread_cancel (pthread_t t)
 #else
 #error Unsupported architecture
 #endif
-#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
          SetThreadContext (tv->h, &ctxt);
-#endif
 
          /* Also try deferred Cancelling */
          tv->cancelled = 1;
-- 
2.20.1



_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to