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 base::AutoLock locked(lock_); | |
35 if (id_to_name_.count(id)) | |
36 free(id_to_name_[id]); | |
37 | |
38 id_to_name_[id] = strdup(name); | |
jar (doing other things)
2012/12/21 00:17:00
It is always good form to do as little work inside
dsinclair
2012/12/21 16:28:59
Done.
| |
39 id_to_version_[id] = current_version_++; | |
40 } | |
41 | |
42 void ThreadIdNameManager::RemoveNameForId(PlatformThreadId id) { | |
43 if (id == base::kInvalidThreadId) | |
44 return; | |
45 | |
46 base::AutoLock locked(lock_); | |
47 DCHECK(id_to_name_.count(id)); | |
48 | |
49 char* old_name = id_to_name_[id]; | |
50 id_to_name_.erase(id); | |
51 id_to_version_.erase(id); | |
52 free(old_name); | |
jar (doing other things)
2012/12/21 00:17:00
Here again, you can do the free after you release
dsinclair
2012/12/21 16:28:59
Done.
| |
53 } | |
54 | |
55 uint32 ThreadIdNameManager::GetVersionForId(PlatformThreadId id) { | |
56 if (id == base::kInvalidThreadId) | |
57 return 0; | |
58 | |
59 base::AutoLock locked(lock_); | |
60 DCHECK(id_to_version_.count(id)); | |
61 return id_to_version_[id]; | |
62 } | |
63 | |
64 } // namespace base | |
OLD | NEW |