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

Side by Side Diff: base/threading/platform_thread_win.cc

Issue 2692213003: Use Windows 10 thread naming API, SetThreadDescription (Closed)
Patch Set: Address comments. 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);
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. Once
182 // tool support cataches up, e.g., the debugger in Visual Studio, the
brucedawson 2017/02/22 21:36:10 We won't be retiring the RaiseException technique
chengx 2017/02/22 23:20:28 Done.
183 // SetNameInternal function below may be retired.
184 HMODULE kernel32dll = ::GetModuleHandle(L"Kernel32.dll");
185 if (kernel32dll) {
brucedawson 2017/02/22 21:36:10 Note that this check can be omitted. It often is i
186 auto set_thread_description_func = reinterpret_cast<SETTHREADDESCRIPTION>(
187 ::GetProcAddress(kernel32dll, "SetThreadDescription"));
188 if (set_thread_description_func) {
189 base::StringPiece name_string(name);
190 set_thread_description_func(GetCurrentThread(),
191 base::UTF8ToWide(name_string).c_str());
192 }
193 }
194
175 // The debugger needs to be around to catch the name in the exception. If 195 // 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. 196 // there isn't a debugger, we are just needlessly throwing an exception.
177 // If this image file is instrumented, we raise the exception anyway 197 // If this image file is instrumented, we raise the exception anyway
178 // to provide the profiler with human-readable thread names. 198 // to provide the profiler with human-readable thread names.
179 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) 199 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented())
180 return; 200 return;
181 201
182 SetNameInternal(CurrentId(), name.c_str()); 202 SetNameInternal(CurrentId(), name.c_str());
183 } 203 }
184 204
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return ThreadPriority::REALTIME_AUDIO; 307 return ThreadPriority::REALTIME_AUDIO;
288 case THREAD_PRIORITY_ERROR_RETURN: 308 case THREAD_PRIORITY_ERROR_RETURN:
289 DPCHECK(false) << "GetThreadPriority error"; // Falls through. 309 DPCHECK(false) << "GetThreadPriority error"; // Falls through.
290 default: 310 default:
291 NOTREACHED() << "Unexpected priority: " << priority; 311 NOTREACHED() << "Unexpected priority: " << priority;
292 return ThreadPriority::NORMAL; 312 return ThreadPriority::NORMAL;
293 } 313 }
294 } 314 }
295 315
296 } // namespace base 316 } // 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