thread_id_name_manager.h 3.0 KB

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