• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BASE_THREADING_THREAD_ID_NAME_MANAGER_H_
6 #define BASE_THREADING_THREAD_ID_NAME_MANAGER_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/base_export.h"
12 #include "base/macros.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/platform_thread.h"
15 
16 namespace base {
17 
18 template <typename T>
19 struct DefaultSingletonTraits;
20 
21 class BASE_EXPORT ThreadIdNameManager {
22  public:
23   static ThreadIdNameManager* GetInstance();
24 
25   static const char* GetDefaultInternedString();
26 
27   // Register the mapping between a thread |id| and |handle|.
28   void RegisterThread(PlatformThreadHandle::Handle handle, PlatformThreadId id);
29 
30   // Set the name for the given id.
31   void SetName(PlatformThreadId id, const std::string& name);
32 
33   // Get the name for the given id.
34   const char* GetName(PlatformThreadId id);
35 
36   // Remove the name for the given id.
37   void RemoveName(PlatformThreadHandle::Handle handle, PlatformThreadId id);
38 
39  private:
40   friend struct DefaultSingletonTraits<ThreadIdNameManager>;
41 
42   typedef std::map<PlatformThreadId, PlatformThreadHandle::Handle>
43       ThreadIdToHandleMap;
44   typedef std::map<PlatformThreadHandle::Handle, std::string*>
45       ThreadHandleToInternedNameMap;
46   typedef std::map<std::string, std::string*> NameToInternedNameMap;
47 
48   ThreadIdNameManager();
49   ~ThreadIdNameManager();
50 
51   // lock_ protects the name_to_interned_name_, thread_id_to_handle_ and
52   // thread_handle_to_interned_name_ maps.
53   Lock lock_;
54 
55   NameToInternedNameMap name_to_interned_name_;
56   ThreadIdToHandleMap thread_id_to_handle_;
57   ThreadHandleToInternedNameMap thread_handle_to_interned_name_;
58 
59   // Treat the main process specially as there is no PlatformThreadHandle.
60   std::string* main_process_name_;
61   PlatformThreadId main_process_id_;
62 
63   DISALLOW_COPY_AND_ASSIGN(ThreadIdNameManager);
64 };
65 
66 }  // namespace base
67 
68 #endif  // BASE_THREADING_THREAD_ID_NAME_MANAGER_H_
69