thread_id_name_manager.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #ifndef BASE_THREADING_THREAD_ID_NAME_MANAGER_H_
  5. #define BASE_THREADING_THREAD_ID_NAME_MANAGER_H_
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/base_export.h"
  10. #include "base/callback.h"
  11. #include "base/macros.h"
  12. #include "base/observer_list.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/threading/platform_thread.h"
  15. namespace base {
  16. template <typename T>
  17. struct DefaultSingletonTraits;
  18. class BASE_EXPORT ThreadIdNameManager {
  19. public:
  20. static ThreadIdNameManager* GetInstance();
  21. static const char* GetDefaultInternedString();
  22. class BASE_EXPORT Observer {
  23. public:
  24. virtual ~Observer();
  25. // Called on the thread whose name is changing, immediately after the name
  26. // is set. |name| is a pointer to a C string that is guaranteed to remain
  27. // valid for the duration of the process.
  28. //
  29. // NOTE: Will be called while ThreadIdNameManager's lock is held, so don't
  30. // call back into it.
  31. virtual void OnThreadNameChanged(const char* name) = 0;
  32. };
  33. // Register the mapping between a thread |id| and |handle|.
  34. void RegisterThread(PlatformThreadHandle::Handle handle, PlatformThreadId id);
  35. void AddObserver(Observer*);
  36. void RemoveObserver(Observer*);
  37. // Set the name for the current thread.
  38. void SetName(const std::string& name);
  39. // Get the name for the given id.
  40. const char* GetName(PlatformThreadId id);
  41. // Unlike |GetName|, this method using TLS and avoids touching |lock_|.
  42. const char* GetNameForCurrentThread();
  43. // Remove the name for the given id.
  44. void RemoveName(PlatformThreadHandle::Handle handle, PlatformThreadId id);
  45. private:
  46. friend struct DefaultSingletonTraits<ThreadIdNameManager>;
  47. typedef std::map<PlatformThreadId, PlatformThreadHandle::Handle>
  48. ThreadIdToHandleMap;
  49. typedef std::map<PlatformThreadHandle::Handle, std::string*>
  50. ThreadHandleToInternedNameMap;
  51. typedef std::map<std::string, std::string*> NameToInternedNameMap;
  52. ThreadIdNameManager();
  53. ~ThreadIdNameManager();
  54. // lock_ protects the name_to_interned_name_, thread_id_to_handle_ and
  55. // thread_handle_to_interned_name_ maps.
  56. Lock lock_;
  57. NameToInternedNameMap name_to_interned_name_;
  58. ThreadIdToHandleMap thread_id_to_handle_;
  59. ThreadHandleToInternedNameMap thread_handle_to_interned_name_;
  60. // Treat the main process specially as there is no PlatformThreadHandle.
  61. std::string* main_process_name_;
  62. PlatformThreadId main_process_id_;
  63. // There's no point using a base::ObserverList behind a lock, so we just use
  64. // an std::vector instead.
  65. std::vector<Observer*> observers_;
  66. DISALLOW_COPY_AND_ASSIGN(ThreadIdNameManager);
  67. };
  68. } // namespace base
  69. #endif // BASE_THREADING_THREAD_ID_NAME_MANAGER_H_