windows: first shot at naming threads with SetThreadDescription().

This is a bleeding edge API, added to Windows 10 Anniversary Edition (build
1607, specifically).

https://msdn.microsoft.com/en-us/library/windows/desktop/mt774976(v=vs.85).aspx

Nothing supports this yet, including WinDbg, Visual Studio, minidumps, etc,
so we still need to also use the RaiseException hack. But presumably tools
will use this API as a more robust and universal way to get thread names
sooner or later, so we'll start broadcasting to it now.
This commit is contained in:
Ryan C. Gordon 2017-01-27 20:50:30 -05:00
parent 95ab9dc73c
commit 8fa9b57f75
1 changed files with 40 additions and 13 deletions

View File

@ -164,10 +164,36 @@ typedef struct tagTHREADNAME_INFO
} THREADNAME_INFO; } THREADNAME_INFO;
#pragma pack(pop) #pragma pack(pop)
typedef HRESULT (WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
void void
SDL_SYS_SetupThread(const char *name) SDL_SYS_SetupThread(const char *name)
{ {
if ((name != NULL) && IsDebuggerPresent()) { if (name != NULL) {
static HMODULE kernel32 = 0;
static pfnSetThreadDescription pSetThreadDescription = NULL;
if (!kernel32) {
kernel32 = LoadLibrary(L"kernel32.dll");
if (kernel32) {
pSetThreadDescription = (pfnSetThreadDescription) GetProcAddress(kernel32, "SetThreadDescription");
}
}
if (pSetThreadDescription != NULL) {
WCHAR *strw = WIN_UTF8ToString(name);
if (strw) {
pSetThreadDescription(GetCurrentThread(), strw);
SDL_free(strw);
}
}
/* Presumably some version of Visual Studio will understand SetThreadDescription(),
but we still need to deal with older OSes and debuggers. Set it with the arcane
exception magic, too. */
if (IsDebuggerPresent()) {
THREADNAME_INFO inf; THREADNAME_INFO inf;
/* C# and friends will try to catch this Exception, let's avoid it. */ /* C# and friends will try to catch this Exception, let's avoid it. */
@ -186,6 +212,7 @@ SDL_SYS_SetupThread(const char *name)
RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf); RaiseException(0x406D1388, 0, sizeof(inf) / sizeof(ULONG), (const ULONG_PTR*) &inf);
} }
} }
}
SDL_threadID SDL_threadID
SDL_ThreadID(void) SDL_ThreadID(void)