Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(401)

Side by Side Diff: base/threading/thread_id_name_manager.cc

Issue 11438022: Add ability to retrieve a thread_name given a thread_id. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review cleanups. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698