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

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

Issue 2692213003: Use Windows 10 thread naming API, SetThreadDescription (Closed)
Patch Set: 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 typedef HRESULT(WINAPI* SETTHREADDESCRIPTION)(HANDLE hThread,
brucedawson 2017/02/21 22:12:40 Somewhere you need a comment saying which version
chengx 2017/02/22 21:24:34 Done.
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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
175 // The debugger needs to be around to catch the name in the exception. If 180 // 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. 181 // there isn't a debugger, we are just needlessly throwing an exception.
177 // If this image file is instrumented, we raise the exception anyway 182 // If this image file is instrumented, we raise the exception anyway
178 // to provide the profiler with human-readable thread names. 183 // to provide the profiler with human-readable thread names.
179 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented()) 184 if (!::IsDebuggerPresent() && !base::debug::IsBinaryInstrumented())
180 return; 185 return;
brucedawson 2017/02/21 22:12:40 Oops! This will return early in those cases where
chengx 2017/02/22 21:24:34 Done.
181 186
187 HMODULE kernal32dll = ::GetModuleHandle(L"Kernel32.dll");
brucedawson 2017/02/21 22:12:40 Spelling - should be kernel32dll. Or "kernel32". O
chengx 2017/02/22 21:24:34 Done.
188 if (kernal32dll) {
189 SETTHREADDESCRIPTION set_thread_description_ptr =
brucedawson 2017/02/21 22:12:40 Consider using 'auto' for the type - no need to sp
chengx 2017/02/22 21:24:34 Done.
190 reinterpret_cast<SETTHREADDESCRIPTION>(
191 ::GetProcAddress(kernal32dll, "SetThreadDescription"));
192 base::StringPiece name_string(name);
193 std::wstring name_wstring = base::UTF8ToWide(name_string);
brucedawson 2017/02/21 22:12:40 The initialization of name_wstring should be insid
chengx 2017/02/22 21:24:34 Done.
194 if (set_thread_description_ptr) {
195 set_thread_description_ptr(GetCurrentThread(), name_wstring.c_str());
196 return;
197 }
198 }
199
182 SetNameInternal(CurrentId(), name.c_str()); 200 SetNameInternal(CurrentId(), name.c_str());
183 } 201 }
184 202
185 // static 203 // static
186 const char* PlatformThread::GetName() { 204 const char* PlatformThread::GetName() {
187 return ThreadIdNameManager::GetInstance()->GetName(CurrentId()); 205 return ThreadIdNameManager::GetInstance()->GetName(CurrentId());
188 } 206 }
189 207
190 // static 208 // static
191 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate, 209 bool PlatformThread::CreateWithPriority(size_t stack_size, Delegate* delegate,
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 return ThreadPriority::REALTIME_AUDIO; 305 return ThreadPriority::REALTIME_AUDIO;
288 case THREAD_PRIORITY_ERROR_RETURN: 306 case THREAD_PRIORITY_ERROR_RETURN:
289 DPCHECK(false) << "GetThreadPriority error"; // Falls through. 307 DPCHECK(false) << "GetThreadPriority error"; // Falls through.
290 default: 308 default:
291 NOTREACHED() << "Unexpected priority: " << priority; 309 NOTREACHED() << "Unexpected priority: " << priority;
292 return ThreadPriority::NORMAL; 310 return ThreadPriority::NORMAL;
293 } 311 }
294 } 312 }
295 313
296 } // namespace base 314 } // 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