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 | |
| 16 const base::InternedString kDefaultInternedString = 0; | |
|
jar (doing other things)
2013/01/17 23:18:39
nit: avoid using base:: thoughout, since we're in
dsinclair
2013/01/21 19:14:18
Done.
| |
| 17 | |
| 18 ThreadIdNameManager::ThreadIdNameManager() | |
| 19 : current_interned_name_(1) { | |
| 20 } | |
| 21 | |
| 22 ThreadIdNameManager::~ThreadIdNameManager() { | |
| 23 } | |
| 24 | |
| 25 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { | |
| 26 return Singleton<ThreadIdNameManager, | |
| 27 LeakySingletonTraits<ThreadIdNameManager> >::get(); | |
| 28 } | |
| 29 | |
| 30 void ThreadIdNameManager::SetNameForId(PlatformThreadId id, const char* name) { | |
| 31 std::string str_name(name); | |
| 32 { | |
|
jar (doing other things)
2013/01/17 23:18:39
nit: curlies not needed, since we return when we r
dsinclair
2013/01/21 19:14:18
Done.
| |
| 33 base::AutoLock locked(lock_); | |
| 34 | |
| 35 if (name_to_interned_name_.count(str_name)) { | |
| 36 id_to_interned_name_[id] = name_to_interned_name_[str_name]; | |
| 37 return; | |
| 38 } | |
| 39 | |
| 40 base::InternedString interned_id = current_interned_name_++; | |
| 41 id_to_interned_name_[id] = interned_id; | |
|
jar (doing other things)
2013/01/17 23:18:39
nit: code might be cleaner if line 41 and line 36
dsinclair
2013/01/21 19:14:18
Done.
| |
| 42 interned_name_to_name_[interned_id] = str_name; | |
| 43 name_to_interned_name_[str_name] = interned_id; | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 const char* ThreadIdNameManager::GetNameForId(PlatformThreadId id) { | |
| 48 return GetInternedStringValue(GetInternedName(id)); | |
| 49 } | |
| 50 | |
| 51 void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) { | |
| 52 if (id == base::kInvalidThreadId) | |
| 53 return; | |
| 54 | |
| 55 { | |
|
jar (doing other things)
2013/01/17 23:18:39
nit: remove curlies.
dsinclair
2013/01/21 19:14:18
Done.
| |
| 56 base::AutoLock locked(lock_); | |
| 57 DCHECK(id_to_interned_name_.count(id)); | |
| 58 id_to_interned_name_.erase(id); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 base::InternedString ThreadIdNameManager::GetInternedName(PlatformThreadId id) { | |
| 63 if (id == base::kInvalidThreadId) | |
| 64 return base::kDefaultInternedString; | |
| 65 | |
| 66 base::AutoLock locked(lock_); | |
| 67 if (!id_to_interned_name_.count(id)) | |
|
jar (doing other things)
2013/01/17 23:18:39
nit: a consistently better pattern is probably to
dsinclair
2013/01/21 19:14:18
Done.
| |
| 68 return base::kDefaultInternedString; | |
| 69 | |
| 70 return id_to_interned_name_[id]; | |
| 71 } | |
| 72 | |
| 73 const char* ThreadIdNameManager::GetInternedStringValue( | |
| 74 base::InternedString name) { | |
| 75 base::AutoLock locked(lock_); | |
| 76 if (!interned_name_to_name_.count(name)) | |
| 77 return NULL; | |
|
jar (doing other things)
2013/01/17 23:18:39
At first I thought this was a result of a very inv
dsinclair
2013/01/21 19:14:18
Done.
| |
| 78 return interned_name_to_name_[name].c_str(); | |
| 79 } | |
| 80 | |
| 81 } // namespace base | |
| OLD | NEW |