Bug 1362976 - Use SetThreadDescription in _PR_MD_SET_CURRENT_THREAD_NAME. portions by David Major, r=mayhemer NSPR_4_18_BETA4
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Mon, 18 Dec 2017 10:56:50 +0100
changeset 4770 ca06d079331872fa43ed183b617c806b8cf23dc1
parent 4769 9061ac5e94d4a9edac999a8b961098931ed982ed
child 4771 8a898490facde48cbf9b61fc319e0af92746939e
push id282
push userkaie@kuix.de
push dateMon, 18 Dec 2017 09:57:11 +0000
reviewersmayhemer
bugs1362976
Bug 1362976 - Use SetThreadDescription in _PR_MD_SET_CURRENT_THREAD_NAME. portions by David Major, r=mayhemer
pr/src/md/windows/ntthread.c
pr/src/md/windows/w95thred.c
--- a/pr/src/md/windows/ntthread.c
+++ b/pr/src/md/windows/ntthread.c
@@ -22,16 +22,19 @@ DWORD _pr_intsOffIndex;
 
 _MDLock                       _nt_idleLock;
 PRCList                       _nt_idleList;
 PRUint32                        _nt_idleCount;
 
 extern __declspec(thread) PRThread *_pr_io_restarted_io;
 extern DWORD _pr_io_restartedIOIndex;
 
+typedef HRESULT (WINAPI *SETTHREADDESCRIPTION)(HANDLE, PCWSTR);
+static SETTHREADDESCRIPTION sSetThreadDescription = NULL;
+
 /* Must check the restarted_io *before* decrementing no_sched to 0 */
 #define POST_SWITCH_WORK() \
     PR_BEGIN_MACRO \
         PRThread *restarted_io = \
             (_pr_use_static_tls ? _pr_io_restarted_io \
             : (PRThread *) TlsGetValue(_pr_io_restartedIOIndex)); \
         if (restarted_io) { \
             _nt_handle_restarted_io(restarted_io); \
@@ -74,16 +77,18 @@ void
     } else {
         TlsSetValue(_pr_io_restartedIOIndex, NULL);
     }
 }
 
 void
 _PR_MD_EARLY_INIT()
 {
+    HMODULE hModule;
+
     _MD_NEW_LOCK( &_nt_idleLock );
     _nt_idleCount = 0;
     PR_INIT_CLIST(&_nt_idleList);
 
 #if 0
     /* Make the clock tick at least once per millisecond */
     if ( timeBeginPeriod(1) == TIMERR_NOCANDO) {
         /* deep yoghurt; clock doesn't tick fast enough! */
@@ -93,16 +98,25 @@ void
 
     if (!_pr_use_static_tls) {
         _pr_currentFiberIndex = TlsAlloc();
         _pr_lastFiberIndex = TlsAlloc();
         _pr_currentCPUIndex = TlsAlloc();
         _pr_intsOffIndex = TlsAlloc();
         _pr_io_restartedIOIndex = TlsAlloc();
     }
+
+    // SetThreadDescription is Windows 10 build 1607+
+    hModule = GetModuleHandleW(L"kernel32.dll");
+    if (hModule) {
+        sSetThreadDescription =
+            (SETTHREADDESCRIPTION) GetProcAddress(
+                                       hModule,
+                                       "SetThreadDescription");
+    }
 }
 
 void _PR_MD_CLEANUP_BEFORE_EXIT(void)
 {
     _PR_NT_FreeSids();
 
     WSACleanup();
 
@@ -288,17 +302,26 @@ typedef struct tagTHREADNAME_INFO
 } THREADNAME_INFO;
 #pragma pack(pop)
 
 void
 _PR_MD_SET_CURRENT_THREAD_NAME(const char *name)
 {
 #ifdef _MSC_VER
    THREADNAME_INFO info;
+#endif
 
+   if (sSetThreadDescription) {
+      WCHAR wideName[MAX_PATH];
+      if (MultiByteToWideChar(CP_ACP, 0, name, -1, wideName, MAX_PATH)) {
+         sSetThreadDescription(GetCurrentThread(), wideName);
+      }
+   }
+
+#ifdef _MSC_VER
    if (!IsDebuggerPresent())
       return;
 
    info.dwType = 0x1000;
    info.szName = (char*) name;
    info.dwThreadID = -1;
    info.dwFlags = 0;
 
--- a/pr/src/md/windows/w95thred.c
+++ b/pr/src/md/windows/w95thred.c
@@ -1,8 +1,9 @@
+
 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "primpl.h"
 #include <process.h>  /* for _beginthreadex() */
 
@@ -22,28 +23,42 @@ typedef DWORD DWORD_PTR;
 #else
 DWORD _pr_currentThreadIndex;
 DWORD _pr_lastThreadIndex;
 DWORD _pr_currentCPUIndex;
 #endif
 int                           _pr_intsOff = 0; 
 _PRInterruptTable             _pr_interruptTable[] = { { 0 } };
 
+typedef HRESULT (WINAPI *SETTHREADDESCRIPTION)(HANDLE, PCWSTR);
+static SETTHREADDESCRIPTION sSetThreadDescription = NULL;
+
 void
 _PR_MD_EARLY_INIT()
 {
+    HMODULE hModule;
+
 #ifndef _PR_USE_STATIC_TLS
     _pr_currentThreadIndex = TlsAlloc();
     _pr_lastThreadIndex = TlsAlloc();
     _pr_currentCPUIndex = TlsAlloc();
 #endif
 
 #if defined(_WIN64) && defined(WIN95)
     _fd_waiting_for_overlapped_done_lock = PR_NewLock();
 #endif
+
+    // SetThreadDescription is Windows 10 build 1607+
+    hModule = GetModuleHandleW(L"kernel32.dll");
+    if (hModule) {
+        sSetThreadDescription =
+            (SETTHREADDESCRIPTION) GetProcAddress(
+                                       hModule,
+                                       "SetThreadDescription");
+    }
 }
 
 void _PR_MD_CLEANUP_BEFORE_EXIT(void)
 {
     _PR_NT_FreeSids();
 
     _PR_MD_CleanupSockets();
 
@@ -212,17 +227,26 @@ typedef struct tagTHREADNAME_INFO
 } THREADNAME_INFO;
 #pragma pack(pop)
 
 void
 _PR_MD_SET_CURRENT_THREAD_NAME(const char *name)
 {
 #ifdef _MSC_VER
    THREADNAME_INFO info;
+#endif
 
+   if (sSetThreadDescription) {
+      WCHAR wideName[MAX_PATH];
+      if (MultiByteToWideChar(CP_ACP, 0, name, -1, wideName, MAX_PATH)) {
+         sSetThreadDescription(GetCurrentThread(), wideName);
+      }
+   }
+
+#ifdef _MSC_VER
    if (!IsDebuggerPresent())
       return;
 
    info.dwType = 0x1000;
    info.szName = (char*) name;
    info.dwThreadID = -1;
    info.dwFlags = 0;