summaryrefslogtreecommitdiff
path: root/src/utilcode
diff options
context:
space:
mode:
authorAlois-xx <akraus1@gmx.de>2017-08-14 19:26:28 +0200
committerJan Kotas <jkotas@microsoft.com>2017-08-14 10:26:28 -0700
commit8d5b762c7b97f635594d4281fca709632d6180c5 (patch)
treeda912ae1cc807b6b24949d1b46aaf5059f33e553 /src/utilcode
parentb24d46b2fed0166cfe3bc2ae39e95b6a049f52fe (diff)
downloadcoreclr-8d5b762c7b97f635594d4281fca709632d6180c5.tar.gz
coreclr-8d5b762c7b97f635594d4281fca709632d6180c5.tar.bz2
coreclr-8d5b762c7b97f635594d4281fca709632d6180c5.zip
Added SetThreadDescription to set the unmanaged thread name (#12593)
* Added SetThreadDescription to set the unmanaged thread name as well when a managed thread name was set. This will show up in future debuggers which know how to read that information or in ETW traces in the Thread Name column. * use printf instead of wprintf which exists on all platforms. * Removed printf Ensure that GetProceAddress is only called once to when the method is not present. Potential perf hit should be negligible since setting a thread name can only happen once per managed thread. * - Moved SetThreadName code to winfix.cpp as proposed - Finalizer and threadpool threads get their name - GCToEEInterface::CreateBackgroundThread is also named - but regular GC threads have no name because when I included utilcode.h things did break apart. * Fix for data race in g_pfnSetThreadDescription * Fix string literals on unix builds. * Fixed nits Settled thread name on ".NET Core ThreadPool"
Diffstat (limited to 'src/utilcode')
-rw-r--r--src/utilcode/winfix.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/utilcode/winfix.cpp b/src/utilcode/winfix.cpp
index 3a044865ec..c914fb6edd 100644
--- a/src/utilcode/winfix.cpp
+++ b/src/utilcode/winfix.cpp
@@ -426,6 +426,45 @@ lExit:
}
+typedef HRESULT(WINAPI *pfnSetThreadDescription)(HANDLE hThread, PCWSTR lpThreadDescription);
+extern pfnSetThreadDescription g_pfnSetThreadDescription;
+
+// Dummy method if windows version does not support it
+HRESULT SetThreadDescriptionDummy(HANDLE hThread, PCWSTR lpThreadDescription)
+{
+ return NOERROR;
+}
+
+HRESULT WINAPI InitializeSetThreadDescription(HANDLE hThread, PCWSTR lpThreadDescription)
+{
+ HMODULE hKernel32 = WszLoadLibrary(W("kernel32.dll"));
+
+ pfnSetThreadDescription pLocal = NULL;
+ if (hKernel32 != NULL)
+ {
+ // store to thread local variable to prevent data race
+ pLocal = (pfnSetThreadDescription)GetProcAddress(hKernel32, "SetThreadDescription");
+ }
+
+ if (pLocal == NULL) // method is only available with Windows 10 Creators Update or later
+ {
+ g_pfnSetThreadDescription = SetThreadDescriptionDummy;
+ }
+ else
+ {
+ g_pfnSetThreadDescription = pLocal;
+ }
+
+ return g_pfnSetThreadDescription(hThread, lpThreadDescription);
+}
+
+pfnSetThreadDescription g_pfnSetThreadDescription = &InitializeSetThreadDescription;
+
+// Set unmanaged thread name which will show up in ETW and Debuggers which know how to read this data.
+HRESULT SetThreadName(HANDLE hThread, PCWSTR lpThreadDescription)
+{
+ return g_pfnSetThreadDescription(hThread, lpThreadDescription);
+}
DWORD
WszGetWorkingSet()