OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/threading/thread_id_name_manager.h" | 5 #include "base/threading/thread_id_name_manager.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace { | 15 namespace { |
16 | 16 |
17 static const char kDefaultName[] = ""; | 17 static const char kDefaultName[] = ""; |
18 static std::string* g_default_name; | 18 static std::string* g_default_name; |
19 | 19 |
20 typedef std::map<PlatformThreadId, std::string*>::iterator | 20 typedef std::map<PlatformThreadId, PlatformThreadHandle*>::iterator |
jar (doing other things)
2013/05/22 19:37:56
nit: My preference is to typedef the actual map ty
dsinclair
2013/05/22 20:22:31
Done.
| |
21 ThreadIdToInternedNameIterator; | 21 ThreadIdToHandleIterator; |
22 typedef std::map<PlatformThreadHandle*, std::string*>::iterator | |
23 ThreadHandleToInternedNameIterator; | |
22 typedef std::map<std::string, std::string*>::iterator | 24 typedef std::map<std::string, std::string*>::iterator |
23 NameToInternedNameIterator; | 25 NameToInternedNameIterator; |
24 } | 26 } |
25 | 27 |
26 ThreadIdNameManager::ThreadIdNameManager() { | 28 ThreadIdNameManager::ThreadIdNameManager() { |
27 g_default_name = new std::string(kDefaultName); | 29 g_default_name = new std::string(kDefaultName); |
28 | 30 |
29 AutoLock locked(lock_); | 31 AutoLock locked(lock_); |
30 name_to_interned_name_[kDefaultName] = g_default_name; | 32 name_to_interned_name_[kDefaultName] = g_default_name; |
31 } | 33 } |
32 | 34 |
33 ThreadIdNameManager::~ThreadIdNameManager() { | 35 ThreadIdNameManager::~ThreadIdNameManager() { |
34 } | 36 } |
35 | 37 |
36 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { | 38 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { |
37 return Singleton<ThreadIdNameManager, | 39 return Singleton<ThreadIdNameManager, |
38 LeakySingletonTraits<ThreadIdNameManager> >::get(); | 40 LeakySingletonTraits<ThreadIdNameManager> >::get(); |
39 } | 41 } |
40 | 42 |
41 const char* ThreadIdNameManager::GetDefaultInternedString() { | 43 const char* ThreadIdNameManager::GetDefaultInternedString() { |
42 return g_default_name->c_str(); | 44 return g_default_name->c_str(); |
43 } | 45 } |
44 | 46 |
47 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle* handle, | |
48 PlatformThreadId id) { | |
49 AutoLock locked(lock_); | |
50 thread_id_to_handle_[id] = handle; | |
51 } | |
52 | |
45 void ThreadIdNameManager::SetName(PlatformThreadId id, const char* name) { | 53 void ThreadIdNameManager::SetName(PlatformThreadId id, const char* name) { |
46 std::string str_name(name); | 54 std::string str_name(name); |
47 | 55 |
48 AutoLock locked(lock_); | 56 AutoLock locked(lock_); |
49 NameToInternedNameIterator iter = name_to_interned_name_.find(str_name); | 57 NameToInternedNameIterator iter = name_to_interned_name_.find(str_name); |
50 std::string* leaked_str = NULL; | 58 std::string* leaked_str = NULL; |
51 if (iter != name_to_interned_name_.end()) { | 59 if (iter != name_to_interned_name_.end()) { |
52 leaked_str = iter->second; | 60 leaked_str = iter->second; |
53 } else { | 61 } else { |
54 leaked_str = new std::string(str_name); | 62 leaked_str = new std::string(str_name); |
55 name_to_interned_name_[str_name] = leaked_str; | 63 name_to_interned_name_[str_name] = leaked_str; |
56 } | 64 } |
57 thread_id_to_interned_name_[id] = leaked_str; | 65 |
66 ThreadIdToHandleIterator id_to_handle_iter = thread_id_to_handle_.find(id); | |
67 | |
68 // The main thread of a process will not be created as a Thread object which | |
69 // means there is no PlatformThreadHandler registerd. | |
70 if (id_to_handle_iter == thread_id_to_handle_.end()) { | |
71 main_process_name_ = leaked_str; | |
72 main_process_id_ = id; | |
jar (doing other things)
2013/05/22 19:37:56
Assuming there are no misunderstanding (and races)
dsinclair
2013/05/22 20:22:31
It could be set multiple times. I think in practic
| |
73 return; | |
74 } | |
75 thread_handle_to_interned_name_[id_to_handle_iter->second] = leaked_str; | |
58 } | 76 } |
59 | 77 |
60 const char* ThreadIdNameManager::GetName(PlatformThreadId id) { | 78 const char* ThreadIdNameManager::GetName(PlatformThreadId id) { |
61 AutoLock locked(lock_); | 79 AutoLock locked(lock_); |
62 ThreadIdToInternedNameIterator iter = thread_id_to_interned_name_.find(id); | 80 |
63 // A platform thread may not have a name set, so return blank. | 81 if (id == main_process_id_) |
64 if (iter == thread_id_to_interned_name_.end()) | 82 return main_process_name_->c_str(); |
jar (doing other things)
2013/05/22 19:37:56
If we had put it in the map.... we could just use
dsinclair
2013/05/22 20:22:31
I can't put it into the map without a PlatformThre
| |
83 | |
84 ThreadIdToHandleIterator id_to_handle_iter = | |
85 thread_id_to_handle_.find(id); | |
86 if (id_to_handle_iter == thread_id_to_handle_.end()) | |
65 return name_to_interned_name_[kDefaultName]->c_str(); | 87 return name_to_interned_name_[kDefaultName]->c_str(); |
66 return iter->second->c_str(); | 88 |
89 ThreadHandleToInternedNameIterator handle_to_name_iter = | |
90 thread_handle_to_interned_name_.find(id_to_handle_iter->second); | |
91 if (handle_to_name_iter == thread_handle_to_interned_name_.end()) | |
92 return name_to_interned_name_[kDefaultName]->c_str(); | |
93 | |
94 return handle_to_name_iter->second->c_str(); | |
67 } | 95 } |
68 | 96 |
69 void ThreadIdNameManager::RemoveName(PlatformThreadId id) { | 97 void ThreadIdNameManager::RemoveName(PlatformThreadHandle* handle, |
98 PlatformThreadId id) { | |
99 if (*handle == base::kNullThreadHandle) | |
jar (doing other things)
2013/05/22 21:36:48
I see you deleted this (presumably because it can
dsinclair
2013/05/23 18:21:53
I dropped it originally because base::kNullThreadH
| |
100 return; | |
70 if (id == kInvalidThreadId) | 101 if (id == kInvalidThreadId) |
71 return; | 102 return; |
72 | 103 |
73 AutoLock locked(lock_); | 104 AutoLock locked(lock_); |
74 ThreadIdToInternedNameIterator iter = thread_id_to_interned_name_.find(id); | 105 ThreadHandleToInternedNameIterator handle_to_name_iter = |
75 DCHECK((iter != thread_id_to_interned_name_.end())); | 106 thread_handle_to_interned_name_.find(handle); |
76 thread_id_to_interned_name_.erase(iter); | 107 DCHECK(handle_to_name_iter != thread_handle_to_interned_name_.end()); |
108 thread_handle_to_interned_name_.erase(handle_to_name_iter); | |
109 | |
110 ThreadIdToHandleIterator id_to_handle_iter = thread_id_to_handle_.find(id); | |
111 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); | |
112 // The given |id| may have been re-used by the system. Make sure the | |
113 // mapping points to the provided |handle| before removal. | |
114 if (id_to_handle_iter->second != handle) | |
115 return; | |
116 | |
117 thread_id_to_handle_.erase(id_to_handle_iter); | |
77 } | 118 } |
78 | 119 |
79 } // namespace base | 120 } // namespace base |
OLD | NEW |