Commit e1188564 authored by LRN's avatar LRN

Add g_system_thread_set_name() implementation for W32 threads

This works by using semi-documented[1] exception to tell the debugger
that a thread needs to have its name changed.

If this exception is not caught and handled by something, it will crash
the process, so we need to set up our own handler in case there's no
debugger attached or the debugger can't handle this type of exception.

Since SEH is not supported by gcc on i686 (at the moment), we need to use VEH
instead. For completeness the MSVC-oriented code still uses SEH, although
there is no reason why it shouldn't work with the VEH variant used by MinGW.

VEH handler has to be set up somewhere (g_thread_win32_init () works nicely)
and removed once it's not needed (g_thread_win32_process_detach () is added
expressly for that purpose). Note that g_thread_win32_process_detach() is
only called when glib is unloaded by FreeLibrary(), not when glib-using
process is terminating.

This exception is known to work with WinDbg, and adding support for it into
GDB proved to be feasible (GDB patch will be sent upstream, eventually).

parent 99bdfd1b
......@@ -273,6 +273,13 @@ DllMain (HINSTANCE hinstDLL,
#ifdef THREADS_WIN32
if (lpvReserved == NULL)
g_thread_win32_process_detach ();
/* do nothing */
......@@ -31,6 +31,7 @@ void g_quark_init (void);
#ifdef G_OS_WIN32
#include <windows.h>
void g_thread_win32_process_detach (void);
void g_thread_win32_thread_detach (void);
void g_thread_win32_init (void);
void g_clock_win32_init (void);
......@@ -496,10 +496,67 @@ g_system_thread_wait (GRealThread *thread)
win32_check_for_error (WAIT_FAILED != WaitForSingleObject (wt->handle, INFINITE));
#ifndef _MSC_VER
static void *SetThreadName_VEH_handle = NULL;
static LONG __stdcall
SetThreadName_VEH (PEXCEPTION_POINTERS ExceptionInfo)
if (ExceptionInfo->ExceptionRecord != NULL &&
ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SET_THREAD_NAME)
typedef struct _THREADNAME_INFO
DWORD dwType; /* must be 0x1000 */
LPCSTR szName; /* pointer to name (in user addr space) */
DWORD dwThreadID; /* thread ID (-1=caller thread) */
DWORD dwFlags; /* reserved for future use, must be zero */
static void
SetThreadName (DWORD dwThreadID,
LPCSTR szThreadName)
DWORD infosize;
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
infosize = sizeof (info) / sizeof (DWORD);
#ifdef _MSC_VER
RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info);
/* Without a debugger we *must* have an exception handler,
* otherwise raising an exception will crash the process.
if ((!IsDebuggerPresent ()) && (SetThreadName_VEH_handle == NULL))
RaiseException (EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *) &info);
g_system_thread_set_name (const gchar *name)
/* FIXME: implement */
SetThreadName ((DWORD) -1, name);
/* {{{1 SRWLock and CONDITION_VARIABLE emulation (for Windows XP) */
......@@ -988,6 +1045,14 @@ g_thread_win32_init (void)
g_thread_xp_init ();
InitializeCriticalSection (&g_private_lock);
#ifndef _MSC_VER
SetThreadName_VEH_handle = AddVectoredExceptionHandler (1, &SetThreadName_VEH);
if (SetThreadName_VEH_handle == NULL)
/* This is bad, but what can we do? */
......@@ -1028,4 +1093,16 @@ g_thread_win32_thread_detach (void)
g_thread_impl_vtable.CallThisOnThreadExit ();
g_thread_win32_process_detach (void)
#ifndef _MSC_VER
if (SetThreadName_VEH_handle != NULL)
RemoveVectoredExceptionHandler (SetThreadName_VEH_handle);
SetThreadName_VEH_handle = NULL;
/* vim:set foldmethod=marker: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment