Commit 39d916d8 authored by jar@chromium.org's avatar jar@chromium.org

Set the profiler's thread name at a central place

This will automatically catch thread names in several of the
child processes we are after, and it also catches the name
for a webkcore thread.

The underlying function uses a try/catch clause, which
contaminates the entire function.  As a result, I needed
to factor out that section, so that I could still call
to set the profiler thread name.  Without this, the
shared dbg build will fail to compile.

r=rtenneti
Review URL: http://codereview.chromium.org/8565036

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110003 0039d316-1c4b-4281-b951-d872f2087c98
parent 999df889
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/safe_strerror_posix.h" #include "base/safe_strerror_posix.h"
#include "base/threading/thread_local.h" #include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/tracked_objects.h"
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
#include <mach/mach.h> #include <mach/mach.h>
...@@ -175,6 +176,7 @@ void PlatformThread::SetName(const char* name) { ...@@ -175,6 +176,7 @@ void PlatformThread::SetName(const char* name) {
// have to cast away const because ThreadLocalPointer does not support const // have to cast away const because ThreadLocalPointer does not support const
// void* // void*
current_thread_name.Pointer()->Set(const_cast<char*>(name)); current_thread_name.Pointer()->Set(const_cast<char*>(name));
tracked_objects::ThreadData::InitializeThreadContext(name);
// http://0pointer.de/blog/projects/name-your-threads.html // http://0pointer.de/blog/projects/name-your-threads.html
...@@ -210,6 +212,7 @@ void PlatformThread::SetName(const char* name) { ...@@ -210,6 +212,7 @@ void PlatformThread::SetName(const char* name) {
// have to cast away const because ThreadLocalPointer does not support const // have to cast away const because ThreadLocalPointer does not support const
// void* // void*
current_thread_name.Pointer()->Set(const_cast<char*>(name)); current_thread_name.Pointer()->Set(const_cast<char*>(name));
tracked_objects::ThreadData::InitializeThreadContext(name);
// (This should be relatively simple to implement for the BSDs; I // (This should be relatively simple to implement for the BSDs; I
// just don't have one handy to test the code on.) // just don't have one handy to test the code on.)
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/threading/thread_local.h" #include "base/threading/thread_local.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/tracked_objects.h"
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
...@@ -28,6 +29,21 @@ typedef struct tagTHREADNAME_INFO { ...@@ -28,6 +29,21 @@ typedef struct tagTHREADNAME_INFO {
DWORD dwFlags; // Reserved for future use, must be zero. DWORD dwFlags; // Reserved for future use, must be zero.
} THREADNAME_INFO; } THREADNAME_INFO;
// This function has try handling, so it is separated out of its caller.
void SetNameInternal(PlatformThreadId thread_id, const char* name) {
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = thread_id;
info.dwFlags = 0;
__try {
RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
reinterpret_cast<DWORD_PTR*>(&info));
} __except(EXCEPTION_CONTINUE_EXECUTION) {
}
}
struct ThreadParams { struct ThreadParams {
PlatformThread::Delegate* delegate; PlatformThread::Delegate* delegate;
bool joinable; bool joinable;
...@@ -100,23 +116,14 @@ void PlatformThread::Sleep(int duration_ms) { ...@@ -100,23 +116,14 @@ void PlatformThread::Sleep(int duration_ms) {
// static // static
void PlatformThread::SetName(const char* name) { void PlatformThread::SetName(const char* name) {
current_thread_name.Set(const_cast<char*>(name)); current_thread_name.Set(const_cast<char*>(name));
tracked_objects::ThreadData::InitializeThreadContext(name);
// The debugger needs to be around to catch the name in the exception. If // 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. // there isn't a debugger, we are just needlessly throwing an exception.
if (!::IsDebuggerPresent()) if (!::IsDebuggerPresent())
return; return;
THREADNAME_INFO info; SetNameInternal(CurrentId(), name);
info.dwType = 0x1000;
info.szName = name;
info.dwThreadID = CurrentId();
info.dwFlags = 0;
__try {
RaiseException(kVCThreadNameException, 0, sizeof(info)/sizeof(DWORD),
reinterpret_cast<DWORD_PTR*>(&info));
} __except(EXCEPTION_CONTINUE_EXECUTION) {
}
} }
// static // static
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "base/third_party/dynamic_annotations/dynamic_annotations.h" #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
#include "base/threading/thread_local.h" #include "base/threading/thread_local.h"
#include "base/synchronization/waitable_event.h" #include "base/synchronization/waitable_event.h"
#include "base/tracked_objects.h"
namespace base { namespace base {
...@@ -152,7 +151,6 @@ void Thread::ThreadMain() { ...@@ -152,7 +151,6 @@ void Thread::ThreadMain() {
ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector.
message_loop.set_thread_name(name_); message_loop.set_thread_name(name_);
message_loop_ = &message_loop; message_loop_ = &message_loop;
tracked_objects::ThreadData::InitializeThreadContext(name_);
// Let the thread do extra initialization. // Let the thread do extra initialization.
// Let's do this before signaling we are started. // Let's do this before signaling we are started.
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "base/metrics/field_trial.h" #include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h" #include "base/metrics/histogram.h"
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "base/tracked_objects.h"
#include "content/browser/browser_thread_impl.h" #include "content/browser/browser_thread_impl.h"
#include "content/browser/trace_controller.h" #include "content/browser/trace_controller.h"
#include "content/common/hi_res_timer_manager.h" #include "content/common/hi_res_timer_manager.h"
...@@ -297,7 +296,6 @@ void BrowserMainLoop::InitializeMainThread() { ...@@ -297,7 +296,6 @@ void BrowserMainLoop::InitializeMainThread() {
const char* kThreadName = "CrBrowserMain"; const char* kThreadName = "CrBrowserMain";
base::PlatformThread::SetName(kThreadName); base::PlatformThread::SetName(kThreadName);
main_message_loop_->set_thread_name(kThreadName); main_message_loop_->set_thread_name(kThreadName);
tracked_objects::ThreadData::InitializeThreadContext(kThreadName);
// Register the main thread by instantiating it, but don't call any methods. // Register the main thread by instantiating it, but don't call any methods.
main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment