| 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { | 34 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { |
| 35 return Singleton<ThreadIdNameManager, | 35 return Singleton<ThreadIdNameManager, |
| 36 LeakySingletonTraits<ThreadIdNameManager> >::get(); | 36 LeakySingletonTraits<ThreadIdNameManager> >::get(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 const char* ThreadIdNameManager::GetDefaultInternedString() { | 39 const char* ThreadIdNameManager::GetDefaultInternedString() { |
| 40 return g_default_name->c_str(); | 40 return g_default_name->c_str(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle::Handle handle, | 43 void ThreadIdNameManager::RegisterThread(PlatformThreadHandle 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.handle_; |
| 47 thread_handle_to_interned_name_[handle] = | 47 thread_handle_to_interned_name_[handle.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, |
| 52 const std::string& name) { | 52 const std::string& name) { |
| 53 AutoLock locked(lock_); | 53 AutoLock locked(lock_); |
| 54 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(name); | 54 NameToInternedNameMap::iterator iter = name_to_interned_name_.find(name); |
| 55 std::string* leaked_str = NULL; | 55 std::string* leaked_str = NULL; |
| 56 if (iter != name_to_interned_name_.end()) { | 56 if (iter != name_to_interned_name_.end()) { |
| 57 leaked_str = iter->second; | 57 leaked_str = iter->second; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 82 ThreadIdToHandleMap::iterator id_to_handle_iter = | 82 ThreadIdToHandleMap::iterator id_to_handle_iter = |
| 83 thread_id_to_handle_.find(id); | 83 thread_id_to_handle_.find(id); |
| 84 if (id_to_handle_iter == thread_id_to_handle_.end()) | 84 if (id_to_handle_iter == thread_id_to_handle_.end()) |
| 85 return name_to_interned_name_[kDefaultName]->c_str(); | 85 return name_to_interned_name_[kDefaultName]->c_str(); |
| 86 | 86 |
| 87 ThreadHandleToInternedNameMap::iterator handle_to_name_iter = | 87 ThreadHandleToInternedNameMap::iterator handle_to_name_iter = |
| 88 thread_handle_to_interned_name_.find(id_to_handle_iter->second); | 88 thread_handle_to_interned_name_.find(id_to_handle_iter->second); |
| 89 return handle_to_name_iter->second->c_str(); | 89 return handle_to_name_iter->second->c_str(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void ThreadIdNameManager::RemoveName(PlatformThreadHandle::Handle handle, | 92 void ThreadIdNameManager::RemoveName(PlatformThreadHandle handle, |
| 93 PlatformThreadId id) { | 93 PlatformThreadId id) { |
| 94 AutoLock locked(lock_); | 94 AutoLock locked(lock_); |
| 95 ThreadHandleToInternedNameMap::iterator handle_to_name_iter = | 95 ThreadHandleToInternedNameMap::iterator handle_to_name_iter = |
| 96 thread_handle_to_interned_name_.find(handle); | 96 thread_handle_to_interned_name_.find(handle.handle_); |
| 97 | 97 |
| 98 DCHECK(handle_to_name_iter != thread_handle_to_interned_name_.end()); | 98 DCHECK(handle_to_name_iter != thread_handle_to_interned_name_.end()); |
| 99 thread_handle_to_interned_name_.erase(handle_to_name_iter); | 99 thread_handle_to_interned_name_.erase(handle_to_name_iter); |
| 100 | 100 |
| 101 ThreadIdToHandleMap::iterator id_to_handle_iter = | 101 ThreadIdToHandleMap::iterator id_to_handle_iter = |
| 102 thread_id_to_handle_.find(id); | 102 thread_id_to_handle_.find(id); |
| 103 DCHECK((id_to_handle_iter!= thread_id_to_handle_.end())); | 103 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 | 104 // The given |id| may have been re-used by the system. Make sure the |
| 105 // mapping points to the provided |handle| before removal. | 105 // mapping points to the provided |handle| before removal. |
| 106 if (id_to_handle_iter->second != handle) | 106 if (id_to_handle_iter->second != handle.handle_) |
| 107 return; | 107 return; |
| 108 | 108 |
| 109 thread_id_to_handle_.erase(id_to_handle_iter); | 109 thread_id_to_handle_.erase(id_to_handle_iter); |
| 110 } | 110 } |
| 111 | 111 |
| 112 } // namespace base | 112 } // namespace base |
| OLD | NEW |