gcc does not support __try / __except syntax, so manually register SEH handler.
This 32-bit x86 code should work with also other compilers as it uses only
functions and structures provided by Windows SDK header files.
---
 mingw-w64-libraries/winpthreads/src/thread.c | 30 +++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/mingw-w64-libraries/winpthreads/src/thread.c 
b/mingw-w64-libraries/winpthreads/src/thread.c
index f32dcc6ecb02..e2f9806ad579 100644
--- a/mingw-w64-libraries/winpthreads/src/thread.c
+++ b/mingw-w64-libraries/winpthreads/src/thread.c
@@ -86,6 +86,22 @@ SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
   return EXCEPTION_CONTINUE_SEARCH;
 }
 
+#if !defined(_MSC_VER) && defined(__i386__)
+static EXCEPTION_DISPOSITION __cdecl
+SetThreadName_SEH (EXCEPTION_RECORD *ExceptionRecord, PVOID EstablisherFrame, 
CONTEXT *ContextRecord, PVOID DispatcherContext)
+{
+  /* Do not be confused with VEH handlers and CRT except filters which returns 
LONG value with UPPER_CASE constants.
+   * SEH handlers like this one return value from EXCEPTION_DISPOSITION enum 
which has CamelCase constants.
+   * UPPER_CASE EXCEPTION_CONTINUE_SEARCH and CamelCase 
ExceptionContinueSearch are different constants.
+   */
+  if (ExceptionRecord != NULL &&
+      ExceptionRecord->ExceptionCode == EXCEPTION_SET_THREAD_NAME)
+    return ExceptionContinueExecution;
+
+  return ExceptionContinueSearch;
+}
+#endif
+
 typedef struct _THREADNAME_INFO
 {
   DWORD  dwType;       /* must be 0x1000 */
@@ -126,7 +142,19 @@ SetThreadName (DWORD dwThreadID, LPCSTR szThreadName)
      {
      }
 #else
-   /* FIXME: SEH exception handling is not support by gcc yet */
+   /* gcc does not support __try / __except syntax, so manually register SEH 
handler */
+#if defined(__i386__)
+   /* On 32-bit x86 is SEH handler registered and unregistered at runtime */
+   EXCEPTION_REGISTRATION_RECORD exception_record = {
+     .Next = (EXCEPTION_REGISTRATION_RECORD *) __readfsdword (0), /* current 
SEH handler */
+     .Handler = SetThreadName_SEH,
+   };
+   __writefsdword (0, (DWORD) &exception_record); /* register our SEH handler 
*/
+   RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (ULONG_PTR *) 
&info);
+   __writefsdword (0, (DWORD) exception_record.Next); /* unregister our SEH 
handler */
+#else
+   /* TODO: SEH exception handling is not implemented for other platforms yet 
*/
+#endif
 #endif
 }
 
-- 
2.20.1



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

Reply via email to