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

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..8b55af4229f526a980cc5c0e0971ca10c7018fa6
--- /dev/null
+++ b/base/threading/thread_id_name_manager.cc
@@ -0,0 +1,64 @@
+// 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) {
+ base::AutoLock locked(lock_);
+ if (id_to_name_.count(id))
+ free(id_to_name_[id]);
+
+ id_to_name_[id] = strdup(name);
jar (doing other things) 2012/12/21 00:17:00 It is always good form to do as little work inside
dsinclair 2012/12/21 16:28:59 Done.
+ id_to_version_[id] = current_version_++;
+}
+
+void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) {
+ if (id == base::kInvalidThreadId)
+ return;
+
+ base::AutoLock locked(lock_);
+ DCHECK(id_to_name_.count(id));
+
+ char* old_name = id_to_name_[id];
+ id_to_name_.erase(id);
+ id_to_version_.erase(id);
+ free(old_name);
jar (doing other things) 2012/12/21 00:17:00 Here again, you can do the free after you release
dsinclair 2012/12/21 16:28:59 Done.
+}
+
+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