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" |
(...skipping 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle, | 43 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle, |
44 PlatformThreadId id) { | 44 PlatformThreadId id) { |
45 AutoLock locked(lock_); | 45 AutoLock locked(lock_); |
46 thread_id_to_handle_[id] = handle; | 46 thread_id_to_handle_[id] = handle; |
47 thread_handle_to_interned_name_[handle] = | 47 thread_handle_to_interned_name_[handle] = |
48 name_to_interned_name_[kDefaultName]; | 48 name_to_interned_name_[kDefaultName]; |
49 } | 49 } |
50 | 50 |
51 void ThreadIdNameManager::SetName(PlatformThreadId id, | 51 void ThreadIdNameManager::SetName(PlatformThreadId id, const char* name) { |
52 const std::string& name) { | 52 std::string str_name(name); |
| 53 |
53 AutoLock locked(lock_); | 54 AutoLock locked(lock_); |
54 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(name); | 55 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(str_name); |
55 std::string* leaked_str = NULL; | 56 std::string* leaked_str = NULL; |
56 if (iter != name_to_interned_name_.end()) { | 57 if (iter != name_to_interned_name_.end()) { |
57 leaked_str = iter->second; | 58 leaked_str = iter->second; |
58 } else { | 59 } else { |
59 leaked_str = new std::string(name); | 60 leaked_str = new std::string(str_name); |
60 name_to_interned_name_[name] = leaked_str; | 61 name_to_interned_name_[str_name] = leaked_str; |
61 } | 62 } |
62 | 63 |
63 ThreadIdToHandleMap::iterator id_to_handle_iter = | 64 ThreadIdToHandleMap::iterator id_to_handle_iter = |
64 thread_id_to_handle_.find(id); | 65 thread_id_to_handle_.find(id); |
65 | 66 |
66 // The main thread of a process will not be created as a Thread object which | 67 // The main thread of a process will not be created as a Thread object which |
67 // means there is no PlatformThreadHandler registered. | 68 // means there is no PlatformThreadHandler registered. |
68 if (id_to_handle_iter == thread_id_to_handle_.end()) { | 69 if (id_to_handle_iter == thread_id_to_handle_.end()) { |
69 main_process_name_ = leaked_str; | 70 main_process_name_ = leaked_str; |
70 main_process_id_ = id; | 71 main_process_id_ = id; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); | 104 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); |
104 // The given |id| may have been re-used by the system. Make sure the | 105 // The given |id| may have been re-used by the system. Make sure the |
105 // mapping points to the provided |handle| before removal. | 106 // mapping points to the provided |handle| before removal. |
106 if (id_to_handle_iter->second != handle) | 107 if (id_to_handle_iter->second != handle) |
107 return; | 108 return; |
108 | 109 |
109 thread_id_to_handle_.erase(id_to_handle_iter); | 110 thread_id_to_handle_.erase(id_to_handle_iter); |
110 } | 111 } |
111 | 112 |
112 } // namespace base | 113 } // namespace base |
OLD | NEW |