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

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

Issue 2692213003: Use Windows 10 thread naming API, SetThreadDescription (Closed)
Patch Set: Fix 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/utf_string_conversions.h"
13 #include "base/threading/thread_id_name_manager.h" 14 #include "base/threading/thread_id_name_manager.h"
14 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
15 #include "base/tracked_objects.h" 16 #include "base/tracked_objects.h"
16 #include "base/win/scoped_handle.h" 17 #include "base/win/scoped_handle.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 namespace { 21 namespace {
21 22
22 // The information on how to set the thread name comes from 23 // The information on how to set the thread name comes from
23 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx 24 // a MSDN article: http://msdn2.microsoft.com/en-us/library/xcb2z8hs.aspx
24 const DWORD kVCThreadNameException = 0x406D1388; 25 const DWORD kVCThreadNameException = 0x406D1388;
25 26
26 typedef struct tagTHREADNAME_INFO { 27 typedef struct tagTHREADNAME_INFO {
27 DWORD dwType; // Must be 0x1000. 28 DWORD dwType; // Must be 0x1000.
28 LPCSTR szName; // Pointer to name (in user addr space). 29 LPCSTR szName; // Pointer to name (in user addr space).
29 DWORD dwThreadID; // Thread ID (-1=caller thread). 30 DWORD dwThreadID; // Thread ID (-1=caller thread).
30 DWORD dwFlags; // Reserved for future use, must be zero. 31 DWORD dwFlags; // Reserved for future use, must be zero.
31 } THREADNAME_INFO; 32 } THREADNAME_INFO;
32 33
34 // The SetThreadDescription API was brought in version 1607 of Windows 10.
35 typedef HRESULT(WINAPI* SetThreadDescription)(HANDLE hThread,
36 PCWSTR lpThreadDescription);
37
33 // This function has try handling, so it is separated out of its caller. 38 // This function has try handling, so it is separated out of its caller.
34 void SetNameInternal(PlatformThreadId thread_id, const char* name) { 39 void SetNameInternal(PlatformThreadId thread_id, const char* name) {
35 THREADNAME_INFO info; 40 THREADNAME_INFO info;
36 info.dwType = 0x1000; 41 info.dwType = 0x1000;
37 info.szName = name; 42 info.szName = name;
38 info.dwThreadID = thread_id; 43 info.dwThreadID = thread_id;
39 info.dwFlags = 0; 44 info.dwFlags = 0;
40 45
41 __try { 46 __try {
42 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD), 47 RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 170
166 // On Windows only, we don't need to tell the profiler about the "BrokerEvent" 171 // 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 172 // 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, 173 // 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 174 // which would also (as a side effect) initialize the profiler in this unused
170 // context, including setting up thread local storage, etc. The performance 175 // context, including setting up thread local storage, etc. The performance
171 // impact is not terrible, but there is no reason to do initialize it. 176 // impact is not terrible, but there is no reason to do initialize it.
172 if (name != "BrokerEvent") 177 if (name != "BrokerEvent")
173 tracked_objects::ThreadData::InitializeThreadContext(name); 178 tracked_objects::ThreadData::InitializeThreadContext(name);
174 179
180 // The SetThreadDescription API works even if no debugger is attached.
181 auto set_thread_description_func =
182 reinterpret_cast<SetThreadDescription>(::GetProcAddress(
183 ::GetModuleHandle(L"Kernel32.dll"), "SetThreadDescription"));
184 if (set_thread_description_func) {
185 set_thread_description_func(::GetCurrentThread(),
186 base::UTF8ToWide(name).c_str());
187 }
188
175 // The debugger needs to be around to catch the name in the exception. If 189 // 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. 190 // there isn't a debugger, we are just needlessly throwing an exception.
177 // If this image file is instrumented, we raise the exception anyway 191 // If this image file is instrumented, we raise the exception anyway
178 // to provide the profiler with human-readable thread names. 192 // to provide the profiler with human-readable thread names.
179 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) 193 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented())
180 return; 194 return;
181 195
182 SetNameInternal(CurrentId(), name.c_str()); 196 SetNameInternal(CurrentId(), name.c_str());
183 } 197 }
184 198
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return ThreadPriority::REALTIME_AUDIO; 301 return ThreadPriority::REALTIME_AUDIO;
288 case THREAD_PRIORITY_ERROR_RETURN: 302 case THREAD_PRIORITY_ERROR_RETURN:
289 DPCHECK(false) << "GetThreadPriority error"; // Falls through. 303 DPCHECK(false) << "GetThreadPriority error"; // Falls through.
290 default: 304 default:
291 NOTREACHED() << "Unexpected priority: " << priority; 305 NOTREACHED() << "Unexpected priority: " << priority;
292 return ThreadPriority::NORMAL; 306 return ThreadPriority::NORMAL;
293 } 307 }
294 } 308 }
295 309
296 } // namespace base 310 } // 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