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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/platform_thread.h" 5 #include "base/threading/platform_thread.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/debug/activity_tracker.h" 9 #include "base/debug/activity_tracker.h"
10 #include "base/debug/alias.h" 10 #include "base/debug/alias.h"
11 #include "base/debug/profiler.h" 11 #include "base/debug/profiler.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/strings/string_piece.h"
14 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_id_name_manager.h" 15 #include "base/threading/thread_id_name_manager.h"
14 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
15 #include "base/tracked_objects.h" 17 #include "base/tracked_objects.h"
16 #include "base/win/scoped_handle.h" 18 #include "base/win/scoped_handle.h"
17 19
18 namespace base { 20 namespace base {
19 21
20 namespace { 22 namespace {
21 23
22 // The information on how to set the thread name comes from 24 // The information on how to set the thread name comes from
23 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx 25 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
24 const DWORD kVCThreadNameException = 0x406D1388; 26 const DWORD kVCThreadNameException = 0x406D1388;
25 27
26 typedef struct tagTHREADNAME_INFO { 28 typedef struct tagTHREADNAME_INFO {
27 DWORD dwType; // Must be 0x1000. 29 DWORD dwType; // Must be 0x1000.
28 LPCSTR szName; // Pointer to name (in user addr space). 30 LPCSTR szName; // Pointer to name (in user addr space).
29 DWORD dwThreadID; // Thread ID (-1=caller thread). 31 DWORD dwThreadID; // Thread ID (-1=caller thread).
30 DWORD dwFlags; // Reserved for future use, must be zero. 32 DWORD dwFlags; // Reserved for future use, must be zero.
31 } THREADNAME_INFO; 33 } THREADNAME_INFO;
32 34
35 // The SetThreadDescription API was brought in version 1607 of Windows 10.
36 typedef HRESULT(WINAPI* SETTHREADDESCRIPTION)(HANDLE hThread,
37 PCWSTR lpThreadDescription);
gab 2017/02/23 20:03:24 Prefer PascalCasing for SetThreadDescription
chengx 2017/02/23 22:06:21 Done.
38
33 // This function has try handling, so it is separated out of its caller. 39 // This function has try handling, so it is separated out of its caller.
34 void SetNameInternal(PlatformThreadId thread_id, const char* name) { 40 void SetNameInternal(PlatformThreadId thread_id, const char* name) {
35 THREADNAME_INFO info; 41 THREADNAME_INFO info;
36 info.dwType = 0x1000; 42 info.dwType = 0x1000;
37 info.szName = name; 43 info.szName = name;
38 info.dwThreadID = thread_id; 44 info.dwThreadID = thread_id;
39 info.dwFlags = 0; 45 info.dwFlags = 0;
40 46
41 __try { 47 __try {
42 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), 48 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 171
166 // On Windows only, we don't need to tell the profiler about the "BrokerEvent" 172 // On Windows only, we don't need to tell the profiler about the "BrokerEvent"
167 // thread, as it exists only in the chrome.exe image, and never spawns or runs 173 // thread, as it exists only in the chrome.exe image, and never spawns or runs
168 // tasks (items which could be profiled). This test avoids the notification, 174 // tasks (items which could be profiled). This test avoids the notification,
169 // which would also (as a side effect) initialize the profiler in this unused 175 // which would also (as a side effect) initialize the profiler in this unused
170 // context, including setting up thread local storage, etc. The performance 176 // context, including setting up thread local storage, etc. The performance
171 // impact is not terrible, but there is no reason to do initialize it. 177 // impact is not terrible, but there is no reason to do initialize it.
172 if (name != "BrokerEvent") 178 if (name != "BrokerEvent")
173 tracked_objects::ThreadData::InitializeThreadContext(name); 179 tracked_objects::ThreadData::InitializeThreadContext(name);
174 180
181 // The SetThreadDescription API works even if no debugger is attached.
182 auto set_thread_description_func =
183 reinterpret_cast<SETTHREADDESCRIPTION>(::GetProcAddress(
184 ::GetModuleHandle(L"Kernel32.dll"), "SetThreadDescription"));
185 if (set_thread_description_func) {
186 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.
187 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.
188 base::UTF8ToWide(name_string).c_str());
189 }
190
175 // The debugger needs to be around to catch the name in the exception. If 191 // The debugger needs to be around to catch the name in the exception. If
176 // there isn't a debugger, we are just needlessly throwing an exception. 192 // there isn't a debugger, we are just needlessly throwing an exception.
177 // If this image file is instrumented, we raise the exception anyway 193 // If this image file is instrumented, we raise the exception anyway
178 // to provide the profiler with human-readable thread names. 194 // to provide the profiler with human-readable thread names.
179 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) 195 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented())
180 return; 196 return;
181 197
182 SetNameInternal(CurrentId(), name.c_str()); 198 SetNameInternal(CurrentId(), name.c_str());
183 } 199 }
184 200
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return ThreadPriority::REALTIME_AUDIO; 303 return ThreadPriority::REALTIME_AUDIO;
288 case THREAD_PRIORITY_ERROR_RETURN: 304 case THREAD_PRIORITY_ERROR_RETURN:
289 DPCHECK(false) << "GetThreadPriority error"; // Falls through. 305 DPCHECK(false) << "GetThreadPriority error"; // Falls through.
290 default: 306 default:
291 NOTREACHED() << "Unexpected priority: " << priority; 307 NOTREACHED() << "Unexpected priority: " << priority;
292 return ThreadPriority::NORMAL; 308 return ThreadPriority::NORMAL;
293 } 309 }
294 } 310 }
295 311
296 } // namespace base 312 } // namespace base
OLDNEW
« 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