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 static const char kDefaultName[] = ""; | |
18 static std::string* g_default_name; | |
19 | |
20 typedef std::map<PlatformThreadId, std::string*>::iterator | |
21 ThreadIdToInternedNameIterator; | |
22 typedef std::map<std::string, std::string*>::iterator | |
23 NameToInternedNameIterator; | |
24 } | |
25 | |
26 ThreadIdNameManager::ThreadIdNameManager() { | |
27 g_default_name = new std::string(kDefaultName); | |
jar (doing other things)
2013/01/25 03:13:25
nit: probably AutoLock locked(lock_); before writi
dsinclair
2013/01/25 14:54:43
Done.
| |
28 name_to_interned_name_[kDefaultName] = g_default_name; | |
29 } | |
30 | |
31 ThreadIdNameManager::~ThreadIdNameManager() { | |
32 } | |
33 | |
34 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { | |
35 return Singleton<ThreadIdNameManager, | |
36 LeakySingletonTraits<ThreadIdNameManager> >::get(); | |
37 } | |
38 | |
39 const char* ThreadIdNameManager::GetDefaultInternedString() { | |
40 return g_default_name->c_str(); | |
41 } | |
42 | |
43 void ThreadIdNameManager::SetName(PlatformThreadId id, const char* name) { | |
44 std::string str_name(name); | |
45 | |
46 AutoLock locked(lock_); | |
47 NameToInternedNameIterator iter = name_to_interned_name_.find(str_name); | |
48 if (iter != name_to_interned_name_.end()) { | |
49 thread_id_to_interned_name_[id] = iter->second; | |
50 return; | |
51 } | |
52 | |
53 std::string* leaked_str = new std::string(str_name); | |
54 name_to_interned_name_[str_name] = leaked_str; | |
55 thread_id_to_interned_name_[id] = leaked_str; | |
jar (doing other things)
2013/01/25 03:13:25
nit: I think this code went back and forth. I tho
dsinclair
2013/01/25 14:54:43
Sorry about that, juggled it around again when I w
| |
56 } | |
57 | |
58 const char* ThreadIdNameManager::GetName(PlatformThreadId id) { | |
59 AutoLock locked(lock_); | |
60 ThreadIdToInternedNameIterator iter = thread_id_to_interned_name_.find(id); | |
61 DCHECK(iter != thread_id_to_interned_name_.end()); | |
62 return iter->second->c_str(); | |
63 } | |
64 | |
65 void ThreadIdNameManager::RemoveName(PlatformThreadId id) { | |
66 if (id == kInvalidThreadId) | |
67 return; | |
68 | |
69 AutoLock locked(lock_); | |
70 ThreadIdToInternedNameIterator iter = thread_id_to_interned_name_.find(id); | |
71 DCHECK((iter != thread_id_to_interned_name_.end())); | |
72 thread_id_to_interned_name_.erase(iter); | |
73 } | |
74 | |
75 } // namespace base | |
OLD | NEW |