new named thread API on win32

pull/13/head
despair86 6 years ago
parent 961a4b1d3c
commit db1989c848

@ -2,8 +2,9 @@
/* /*
* All the user-mode scaffolding necessary to backport GetAdaptersAddresses(2)) * All the user-mode scaffolding necessary to backport GetAdaptersAddresses(2))
* to the NT 5.x series. See further comments for any limitations. * to the NT 5.x series. See further comments for any limitations.
* * NOTE: this is dead code, i haven't had time to debug it yet due to illness.
* -despair86 30/07/18 * For now, downlevel platforms use GetAdaptersInfo(2) which is inet4 only.
* -despair86 20/08/18
*/ */
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
@ -502,7 +503,10 @@ getInterfaceIndexTable(void)
*/ */
#ifdef _MSC_VER #ifdef _MSC_VER
#include <windows.h> #include <windows.h>
typedef HRESULT(FAR PASCAL *p_SetThreadDescription)(void *, const wchar_t *);
#define EXCEPTION_SET_THREAD_NAME ((DWORD)0x406D1388) #define EXCEPTION_SET_THREAD_NAME ((DWORD)0x406D1388)
typedef struct _THREADNAME_INFO typedef struct _THREADNAME_INFO
{ {
DWORD dwType; /* must be 0x1000 */ DWORD dwType; /* must be 0x1000 */
@ -516,20 +520,41 @@ SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
{ {
THREADNAME_INFO info; THREADNAME_INFO info;
DWORD infosize; DWORD infosize;
HANDLE hThread;
info.dwType = 0x1000; /* because loonix is SHIT and limits thread names to 16 bytes */
info.szName = szThreadName; wchar_t thr_name_w[16];
info.dwThreadID = dwThreadID; p_SetThreadDescription _SetThreadDescription;
info.dwFlags = 0;
/* current win10 flights now have a new named-thread API, let's try to use that first! */
infosize = sizeof(info) / sizeof(DWORD); /* first, dlsym(2) the new call from system library */
_SetThreadDescription =
__try (p_SetThreadDescription)GetProcAddress(GetModuleHandle("kernel32"), "SetThreadDescription");
if(_SetThreadDescription)
{ {
RaiseException(EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *)&info); hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID);
MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16);
if(hThread)
_SetThreadDescription(hThread, thr_name_w);
else
goto old; /* for whatever reason, we couldn't get a handle to the thread. Just use the old method. */
} }
__except(EXCEPTION_EXECUTE_HANDLER) else
{ {
old:
info.dwType = 0x1000;
info.szName = szThreadName;
info.dwThreadID = dwThreadID;
info.dwFlags = 0;
infosize = sizeof(info) / sizeof(DWORD);
__try
{
RaiseException(EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *)&info);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}
} }
} }
#endif #endif
Loading…
Cancel
Save