Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback when SetThreadDescription isn't available. #1

Closed
mtwilliams opened this issue Oct 28, 2019 · 2 comments
Closed

Fallback when SetThreadDescription isn't available. #1

mtwilliams opened this issue Oct 28, 2019 · 2 comments

Comments

@mtwilliams
Copy link
Member

mtwilliams commented Oct 28, 2019

SetThreadDescription wasn't introduced until Windows 10, 1607. It's currently being used to name threads without a check to determine if it's actually available. Instead, it should be fetched dynamically with a fallback to the older approach that only worked under debuggers.

I suggest a static initializer that does something akin to:

SetThreadDescription set_thread_description;
*(void **)&set_thread_description =
  GetProcAddress(GetModuleHandleA("kernel32.dll"), "SetThreadDescription");
if (!set_thread_description)
  set_thread_description = &set_thread_description_fallback;

Here's the fallback, for Windows 7.

if (IsDebuggerPresent()) {
  #pragma pack(push, 8)
    typedef struct THREAD_NAME_INFO {
      DWORD dwType;
      LPCSTR szName;
      DWORD dwThreadID;
      DWORD dwFlags;
    } THREAD_NAME_INFO;
  #pragma pack(pop)

  THREAD_NAME_INFO thread_name_info;

  thread_name_info.dwType = 0x1000;
  thread_name_info.szName = name;
  thread_name_info.dwThreadID = GetCurrentThreadId();
  thread_name_info.dwFlags = 0;

  #pragma warning(push)
  #pragma warning(disable: 6320 6322)
    __try {
      // This is how you used to name threads in Windows. Talk about a mess.
      RaiseException(0x406D1388, 0, sizeof(THREAD_NAME_INFO)/sizeof(ULONG_PTR), (ULONG_PTR *)&thread_name_info);
    } __except (EXCEPTION_EXECUTE_HANDLER) {
    }
  #pragma warning(pop)
}
@graphitemaster
Copy link
Member

When Thread and ThreadPool are reworked as described in #42 this will be handled. Mentioning it here so that I don't forget to.

@graphitemaster
Copy link
Member

Fixed in 5e9f857

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants