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

Unified Diff: base/debug/trace_event.cc

Issue 8470001: TraceEvent fixes for thread name changes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: jbates & nduca comments Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/debug/trace_event.h ('k') | base/debug/trace_event_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/debug/trace_event.cc
diff --git a/base/debug/trace_event.cc b/base/debug/trace_event.cc
index bf59f91e91d0b7e58a99cf47fca2a9a2f22a838b..eb517a32e81e1edf50b524b752242281142b9abf 100644
--- a/base/debug/trace_event.cc
+++ b/base/debug/trace_event.cc
@@ -12,8 +12,10 @@
#include "base/bind.h"
#include "base/format_macros.h"
#include "base/lazy_instance.h"
+#include "base/memory/singleton.h"
#include "base/process_util.h"
#include "base/stringprintf.h"
+#include "base/threading/platform_thread.h"
#include "base/threading/thread_local.h"
#include "base/utf_string_conversions.h"
#include "base/stl_util.h"
@@ -54,10 +56,10 @@ const TraceCategory* const g_category_metadata =
&g_categories[2];
int g_category_index = 3; // skip initial 3 categories
-// Flag to indicate whether we captured the current thread name
-LazyInstance<ThreadLocalBoolean,
- LeakyLazyInstanceTraits<ThreadLocalBoolean> >
- g_current_thread_name_captured(LINKER_INITIALIZED);
+// The most-recently captured name of the current thread
+LazyInstance<ThreadLocalPointer<char>,
+ LeakyLazyInstanceTraits<ThreadLocalPointer<char> > >
+ g_current_thread_name(LINKER_INITIALIZED);
} // namespace
@@ -513,27 +515,31 @@ int TraceLog::AddTraceEvent(TraceEventPhase phase,
PlatformThreadId thread_id = PlatformThread::CurrentId();
- // Record the name of the calling thread, if not done already.
- if (!g_current_thread_name_captured.Pointer()->Get()) {
- g_current_thread_name_captured.Pointer()->Set(true);
- const char* cur_name = PlatformThread::GetName();
+ const char* new_name = PlatformThread::GetName();
+ // Check if the thread name has been set or changed since th previous
jar (doing other things) 2011/11/10 00:18:35 nit typo: th--> the
joth 2011/11/10 10:15:26 Done.
+ // call (if any), but don't bother if the new name is empty. Note this will
+ // not detect a thread name change within the same char* buffer address: we
+ // favor common case performance over corner case correctness.
+ if (new_name != g_current_thread_name.Get().Get() &&
+ new_name && *new_name) {
+ // Benign const cast.
+ g_current_thread_name.Get().Set(const_cast<char*>(new_name));
base::hash_map<PlatformThreadId, std::string>::iterator existing_name =
thread_names_.find(thread_id);
if (existing_name == thread_names_.end()) {
// This is a new thread id, and a new name.
- thread_names_[thread_id] = cur_name ? cur_name : "";
- } else if(cur_name != NULL) {
+ thread_names_[thread_id] = new_name;
+ } else {
// This is a thread id that we've seen before, but potentially with a
// new name.
- std::vector<std::string> existing_names;
+ std::vector<base::StringPiece> existing_names;
Tokenize(existing_name->second, std::string(","), &existing_names);
bool found = std::find(existing_names.begin(),
existing_names.end(),
- cur_name) != existing_names.end();
+ new_name) != existing_names.end();
if (!found) {
- existing_names.push_back(cur_name);
- thread_names_[thread_id] =
- JoinString(existing_names, ',');
+ existing_name->second.push_back(',');
+ existing_name->second.append(new_name);
}
}
}
« no previous file with comments | « base/debug/trace_event.h ('k') | base/debug/trace_event_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698