Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(569)

Unified Diff: base/threading/platform_thread_win.cc

Issue 2692213003: Use Windows 10 thread naming API, SetThreadDescription (Closed)
Patch Set: Address nits. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/threading/platform_thread_win.cc
diff --git a/base/threading/platform_thread_win.cc b/base/threading/platform_thread_win.cc
index 2644eee520b82c0520c760addc8e9fc3415aadf2..fb9979fed6e706f4f6dcdf7bbfe41c3d16698f04 100644
--- a/base/threading/platform_thread_win.cc
+++ b/base/threading/platform_thread_win.cc
@@ -10,6 +10,8 @@
#include "base/debug/alias.h"
#include "base/debug/profiler.h"
#include "base/logging.h"
+#include "base/strings/string_piece.h"
+#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_id_name_manager.h"
#include "base/threading/thread_restrictions.h"
#include "base/tracked_objects.h"
@@ -30,6 +32,10 @@ typedef struct tagTHREADNAME_INFO {
DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO;
+// The SetThreadDescription API was brought in version 1607 of Windows 10.
+typedef HRESULT(WINAPI* SETTHREADDESCRIPTION)(HANDLE hThread,
+ PCWSTR lpThreadDescription);
gab 2017/02/23 20:03:24 Prefer PascalCasing for SetThreadDescription
chengx 2017/02/23 22:06:21 Done.
+
// This function has try handling, so it is separated out of its caller.
void SetNameInternal(PlatformThreadId thread_id, const char* name) {
THREADNAME_INFO info;
@@ -172,6 +178,16 @@ void PlatformThread::SetName(const std::string& name) {
if (name != "BrokerEvent")
tracked_objects::ThreadData::InitializeThreadContext(name);
+ // The SetThreadDescription API works even if no debugger is attached.
+ auto set_thread_description_func =
+ reinterpret_cast<SETTHREADDESCRIPTION>(::GetProcAddress(
+ ::GetModuleHandle(L"Kernel32.dll"), "SetThreadDescription"));
+ if (set_thread_description_func) {
+ base::StringPiece name_string(name);
gab 2017/02/23 20:03:24 This is unnecessary (std::string will be implicitl
chengx 2017/02/23 22:06:21 Done.
+ set_thread_description_func(GetCurrentThread(),
gab 2017/02/23 20:03:24 ::GetCurrentThread() (prefer highlighting non-Chr
chengx 2017/02/23 22:06:21 Done.
+ base::UTF8ToWide(name_string).c_str());
+ }
+
// The debugger needs to be around to catch the name in the exception. If
// there isn't a debugger, we are just needlessly throwing an exception.
// If this image file is instrumented, we raise the exception anyway
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698