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]; |
jar (doing other things)
2012/12/14 18:23:07
I have a tiny concern about the lifetime of this c
dsinclair
2012/12/14 19:13:46
Currently, this is used in one place, the tracing
jar (doing other things)
2012/12/14 19:58:08
Sorry for my seemingly confusing mention of std::s
dsinclair
2012/12/14 20:18:19
Sorry, I was thinking the copy-on-write semantics
|
+} |
+ |
+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); |
+ 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); |
+} |
+ |
+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 |