Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/threading/thread_id_name_manager.h" | |
| 6 | |
| 7 #include <stdlib.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/singleton.h" | |
| 12 #include "base/string_util.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace { | |
| 16 | |
| 17 const char* kDefaultName(""); | |
| 18 | |
| 19 typedef std::map<PlatformThreadId, std::string>::iterator | |
| 20 IdToInternedNameIterator; | |
| 21 typedef std::map<std::string, std::string>::iterator NameToInternedNameIterator; | |
| 22 } | |
| 23 | |
| 24 ThreadIdNameManager::ThreadIdNameManager() { | |
| 25 name_to_interned_name_[kDefaultName] = kDefaultName; | |
| 26 } | |
| 27 | |
| 28 ThreadIdNameManager::~ThreadIdNameManager() { | |
| 29 } | |
| 30 | |
| 31 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { | |
| 32 return Singleton<ThreadIdNameManager, | |
| 33 LeakySingletonTraits<ThreadIdNameManager> >::get(); | |
| 34 } | |
| 35 | |
| 36 const char* ThreadIdNameManager::GetDefaultInternedString() { | |
| 37 return kDefaultName; | |
| 38 } | |
| 39 | |
| 40 void ThreadIdNameManager::SetNameForId(PlatformThreadId id, const char* name) { | |
| 41 std::string str_name(name); | |
| 42 | |
| 43 AutoLock locked(lock_); | |
| 44 NameToInternedNameIterator iter = name_to_interned_name_.find(str_name); | |
| 45 if (iter == name_to_interned_name_.end()) | |
| 46 name_to_interned_name_[str_name] = str_name; | |
|
jar (doing other things)
2013/01/24 01:05:14
I'm a little confused now, as you set values into
dsinclair
2013/01/24 15:13:06
Bug, I should have used iter->second in the assign
| |
| 47 | |
| 48 id_to_interned_name_[id] = str_name; | |
|
jar (doing other things)
2013/01/24 01:05:14
nit: suggest thread_id_to_interned_name
I was con
dsinclair
2013/01/24 15:13:06
Done.
| |
| 49 } | |
| 50 | |
| 51 const char* ThreadIdNameManager::GetNameForId(PlatformThreadId id) { | |
|
jar (doing other things)
2013/01/24 01:05:14
nit: Clearer might be GetNameForThread(...), or po
dsinclair
2013/01/24 15:13:06
Done.
| |
| 52 AutoLock locked(lock_); | |
| 53 IdToInternedNameIterator iter = id_to_interned_name_.find(id); | |
| 54 DCHECK(iter != id_to_interned_name_.end()); | |
| 55 return iter->second.c_str(); | |
|
jar (doing other things)
2013/01/24 01:05:14
This sort of return tends to suggest that the stri
dsinclair
2013/01/24 15:13:06
This should be long lived as it will continue to l
| |
| 56 } | |
| 57 | |
| 58 void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) { | |
| 59 if (id == kInvalidThreadId) | |
| 60 return; | |
| 61 | |
| 62 AutoLock locked(lock_); | |
| 63 IdToInternedNameIterator iter = id_to_interned_name_.find(id); | |
| 64 DCHECK((iter != id_to_interned_name_.end())); | |
| 65 id_to_interned_name_.erase(iter); | |
| 66 } | |
| 67 | |
| 68 } // namespace base | |
| OLD | NEW |