From 561f79e8e6d508d7061e54756af7487b8d267b95 Mon Sep 17 00:00:00 2001 From: Smirnov Kirill Date: Wed, 17 Feb 2016 18:18:06 +0300 Subject: [PATCH] Some changes to Singleton implementation --- src/profile_manager.cpp | 42 ++++++++++++++++++----------------------- src/profile_manager.h | 4 ++-- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/profile_manager.cpp b/src/profile_manager.cpp index 5321e73..e838a4a 100644 --- a/src/profile_manager.cpp +++ b/src/profile_manager.cpp @@ -3,28 +3,25 @@ using namespace profiler; -ProfileManager* ProfileManager::m_profileManager = nullptr; - extern "C"{ - void PROFILER_API registerMark(Mark* _mark) - { - ProfileManager::instance()->registerMark(_mark); - } + void PROFILER_API registerMark(Mark* _mark) + { + ProfileManager::instance().registerMark(_mark); + } - void PROFILER_API endBlock() - { - ProfileManager::instance()->endBlock(); - } - - void PROFILER_API setEnabled(bool isEnable) - { - ProfileManager::instance()->setEnabled(isEnable); - } + void PROFILER_API endBlock() + { + ProfileManager::instance().endBlock(); + } + void PROFILER_API setEnabled(bool isEnable) + { + ProfileManager::instance().setEnabled(isEnable); + } void PROFILER_API beginBlock(Block* _block) { - ProfileManager::instance()->beginBlock(_block); + ProfileManager::instance().beginBlock(_block); } } @@ -34,15 +31,12 @@ ProfileManager::ProfileManager() } -ProfileManager* ProfileManager::instance() +ProfileManager& ProfileManager::instance() { - if (!m_profileManager) - { - //TODO: thread safety for profiler::instance - //if(!m_profiler)//see paper by Scott Mayers and Alecsandrescu: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf - m_profileManager = new ProfileManager(); - } - return m_profileManager; + ///C++11 makes possible to create Singleton without any warry about thread-safeness + ///http://preshing.com/20130930/double-checked-locking-is-fixed-in-cpp11/ + static ProfileManager m_profileManager; + return m_profileManager; } void ProfileManager::registerMark(profiler::Mark* _mark) diff --git a/src/profile_manager.h b/src/profile_manager.h index 81da5af..849a2d4 100644 --- a/src/profile_manager.h +++ b/src/profile_manager.h @@ -26,11 +26,11 @@ class ProfileManager ProfileManager(); ProfileManager(const ProfileManager& p) = delete; ProfileManager& operator=(const ProfileManager&) = delete; - static ProfileManager* m_profileManager; + static ProfileManager m_profileManager; bool m_isEnabled = false; public: - static ProfileManager* instance(); + static ProfileManager& instance(); void registerMark(profiler::Mark* _mark); void beginBlock(profiler::Block* _block);