michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef BASE_THREADING_THREAD_ID_NAME_MANAGER_H_ michael@0: #define BASE_THREADING_THREAD_ID_NAME_MANAGER_H_ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "base/base_export.h" michael@0: #include "base/basictypes.h" michael@0: #include "base/synchronization/lock.h" michael@0: #include "base/threading/platform_thread.h" michael@0: michael@0: template struct DefaultSingletonTraits; michael@0: michael@0: namespace base { michael@0: michael@0: class BASE_EXPORT ThreadIdNameManager { michael@0: public: michael@0: static ThreadIdNameManager* GetInstance(); michael@0: michael@0: static const char* GetDefaultInternedString(); michael@0: michael@0: // Register the mapping between a thread |id| and |handle|. michael@0: void RegisterThread(PlatformThreadHandle::Handle handle, PlatformThreadId id); michael@0: michael@0: // Set the name for the given id. michael@0: void SetName(PlatformThreadId id, const char* name); michael@0: michael@0: // Get the name for the given id. michael@0: const char* GetName(PlatformThreadId id); michael@0: michael@0: // Remove the name for the given id. michael@0: void RemoveName(PlatformThreadHandle::Handle handle, PlatformThreadId id); michael@0: michael@0: private: michael@0: friend struct DefaultSingletonTraits; michael@0: michael@0: typedef std::map michael@0: ThreadIdToHandleMap; michael@0: typedef std::map michael@0: ThreadHandleToInternedNameMap; michael@0: typedef std::map NameToInternedNameMap; michael@0: michael@0: ThreadIdNameManager(); michael@0: ~ThreadIdNameManager(); michael@0: michael@0: // lock_ protects the name_to_interned_name_, thread_id_to_handle_ and michael@0: // thread_handle_to_interned_name_ maps. michael@0: Lock lock_; michael@0: michael@0: NameToInternedNameMap name_to_interned_name_; michael@0: ThreadIdToHandleMap thread_id_to_handle_; michael@0: ThreadHandleToInternedNameMap thread_handle_to_interned_name_; michael@0: michael@0: // Treat the main process specially as there is no PlatformThreadHandle. michael@0: std::string* main_process_name_; michael@0: PlatformThreadId main_process_id_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(ThreadIdNameManager); michael@0: }; michael@0: michael@0: } // namespace base michael@0: michael@0: #endif // BASE_THREADING_THREAD_ID_NAME_MANAGER_H_