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

Unified Diff: base/threading/thread_id_name_manager.cc

Issue 11438022: Add ability to retrieve a thread_name given a thread_id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review cleanups. Created 8 years 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
Index: base/threading/thread_id_name_manager.cc
diff --git a/base/threading/thread_id_name_manager.cc b/base/threading/thread_id_name_manager.cc
new file mode 100644
index 0000000000000000000000000000000000000000..79bed389f26baab249bea243385126083ebd3e04
--- /dev/null
+++ b/base/threading/thread_id_name_manager.cc
@@ -0,0 +1,72 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/threading/thread_id_name_manager.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "base/logging.h"
+#include "base/memory/singleton.h"
+
+namespace base {
+
+ThreadIdNameManager::ThreadIdNameManager()
+ : current_version_(1) {
+}
+
+ThreadIdNameManager::~ThreadIdNameManager() {
+}
+
+ThreadIdNameManager* ThreadIdNameManager::GetInstance() {
+ return Singleton<ThreadIdNameManager,
+ LeakySingletonTraits<ThreadIdNameManager> >::get();
+}
+
+const char* ThreadIdNameManager::GetNameForId(PlatformThreadId id) {
+ base::AutoLock locked(lock_);
+ DCHECK(id_to_name_.count(id));
+ return id_to_name_[id];
+}
+
+void ThreadIdNameManager::SetNameForId(PlatformThreadId id, const char* name) {
+ char* dup_name = strdup(name);
+ char* orig_name = NULL;
+ {
+ base::AutoLock locked(lock_);
+ if (id_to_name_.count(id))
+ orig_name = id_to_name_[id];
+
+ id_to_name_[id] = dup_name;
+ id_to_version_[id] = current_version_++;
+ }
+ free(orig_name);
+}
+
+void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) {
+ if (id == base::kInvalidThreadId)
+ return;
+
+ char* old_name = NULL;
+ {
+ base::AutoLock locked(lock_);
+ DCHECK(id_to_name_.count(id));
+
+ old_name = id_to_name_[id];
+ id_to_name_.erase(id);
+ id_to_version_.erase(id);
+ }
+ free(old_name);
+}
+
+uint32 ThreadIdNameManager::GetVersionForId(PlatformThreadId id) {
+ if (id == base::kInvalidThreadId)
+ return 0;
+
+ base::AutoLock locked(lock_);
+ DCHECK(id_to_version_.count(id));
+ return id_to_version_[id];
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698