| 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..ea47dbb5746840b3f8b02276693a8402bc0c501a
|
| --- /dev/null
|
| +++ b/base/threading/thread_id_name_manager.cc
|
| @@ -0,0 +1,81 @@
|
| +// 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"
|
| +#include "base/string_util.h"
|
| +
|
| +namespace base {
|
| +
|
| +const base::InternedString kDefaultInternedString = 0;
|
| +
|
| +ThreadIdNameManager::ThreadIdNameManager()
|
| + : current_interned_name_(1) {
|
| +}
|
| +
|
| +ThreadIdNameManager::~ThreadIdNameManager() {
|
| +}
|
| +
|
| +ThreadIdNameManager* ThreadIdNameManager::GetInstance() {
|
| + return Singleton<ThreadIdNameManager,
|
| + LeakySingletonTraits<ThreadIdNameManager> >::get();
|
| +}
|
| +
|
| +void ThreadIdNameManager::SetNameForId(PlatformThreadId id, const char* name) {
|
| + std::string str_name(name);
|
| + {
|
| + base::AutoLock locked(lock_);
|
| +
|
| + if (name_to_interned_name_.count(str_name)) {
|
| + id_to_interned_name_[id] = name_to_interned_name_[str_name];
|
| + return;
|
| + }
|
| +
|
| + base::InternedString interned_id = current_interned_name_++;
|
| + id_to_interned_name_[id] = interned_id;
|
| + interned_name_to_name_[interned_id] = str_name;
|
| + name_to_interned_name_[str_name] = interned_id;
|
| + }
|
| +}
|
| +
|
| +const char* ThreadIdNameManager::GetNameForId(PlatformThreadId id) {
|
| + return GetInternedStringValue(GetInternedName(id));
|
| +}
|
| +
|
| +void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) {
|
| + if (id == base::kInvalidThreadId)
|
| + return;
|
| +
|
| + {
|
| + base::AutoLock locked(lock_);
|
| + DCHECK(id_to_interned_name_.count(id));
|
| + id_to_interned_name_.erase(id);
|
| + }
|
| +}
|
| +
|
| +base::InternedString ThreadIdNameManager::GetInternedName(PlatformThreadId id) {
|
| + if (id == base::kInvalidThreadId)
|
| + return base::kDefaultInternedString;
|
| +
|
| + base::AutoLock locked(lock_);
|
| + if (!id_to_interned_name_.count(id))
|
| + return base::kDefaultInternedString;
|
| +
|
| + return id_to_interned_name_[id];
|
| +}
|
| +
|
| +const char* ThreadIdNameManager::GetInternedStringValue(
|
| + base::InternedString name) {
|
| + base::AutoLock locked(lock_);
|
| + if (!interned_name_to_name_.count(name))
|
| + return NULL;
|
| + return interned_name_to_name_[name].c_str();
|
| +}
|
| +
|
| +} // namespace base
|
|
|