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 |
| 13 namespace base { |
| 14 |
| 15 ThreadIdNameManager::ThreadIdNameManager() |
| 16 : current_version_(1) { |
| 17 } |
| 18 |
| 19 ThreadIdNameManager::~ThreadIdNameManager() { |
| 20 } |
| 21 |
| 22 ThreadIdNameManager* ThreadIdNameManager::GetInstance() { |
| 23 return Singleton<ThreadIdNameManager, |
| 24 LeakySingletonTraits<ThreadIdNameManager> >::get(); |
| 25 } |
| 26 |
| 27 const char* ThreadIdNameManager::GetNameForId(PlatformThreadId id) { |
| 28 base::AutoLock locked(lock_); |
| 29 DCHECK(id_to_name_.count(id)); |
| 30 return id_to_name_[id]; |
| 31 } |
| 32 |
| 33 void ThreadIdNameManager::SetNameForId(PlatformThreadId id, const char* name) { |
| 34 char* dup_name = strdup(name); |
| 35 char* orig_name = NULL; |
| 36 { |
| 37 base::AutoLock locked(lock_); |
| 38 if (id_to_name_.count(id)) |
| 39 orig_name = id_to_name_[id]; |
| 40 |
| 41 id_to_name_[id] = dup_name; |
| 42 id_to_version_[id] = current_version_++; |
| 43 } |
| 44 free(orig_name); |
| 45 } |
| 46 |
| 47 void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) { |
| 48 if (id == base::kInvalidThreadId) |
| 49 return; |
| 50 |
| 51 char* old_name = NULL; |
| 52 { |
| 53 base::AutoLock locked(lock_); |
| 54 DCHECK(id_to_name_.count(id)); |
| 55 |
| 56 old_name = id_to_name_[id]; |
| 57 id_to_name_.erase(id); |
| 58 id_to_version_.erase(id); |
| 59 } |
| 60 free(old_name); |
| 61 } |
| 62 |
| 63 uint32 ThreadIdNameManager::GetVersionForId(PlatformThreadId id) { |
| 64 if (id == base::kInvalidThreadId) |
| 65 return 0; |
| 66 |
| 67 base::AutoLock locked(lock_); |
| 68 DCHECK(id_to_version_.count(id)); |
| 69 return id_to_version_[id]; |
| 70 } |
| 71 |
| 72 } // namespace base |
OLD | NEW |