From c91191e70193d474c74173ec92fd9892ed4f9f83 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 14 Feb 2013 10:55:47 +0100 Subject: [PATCH] Implement global mutex (for use by scripting languages). --- lib/base/application.cpp | 13 +++++++++++++ lib/base/application.h | 3 +++ lib/base/event.cpp | 4 ++++ lib/base/utility.cpp | 6 ++++++ 4 files changed, 26 insertions(+) diff --git a/lib/base/application.cpp b/lib/base/application.cpp index b7a99b6cb..b0d179d5f 100644 --- a/lib/base/application.cpp +++ b/lib/base/application.cpp @@ -21,6 +21,7 @@ using namespace icinga; +boost::mutex Application::m_Mutex; Application *Application::m_Instance = NULL; bool Application::m_ShuttingDown = false; bool Application::m_Debugging = false; @@ -113,6 +114,8 @@ bool Application::ProcessEvents(void) */ void Application::RunEventLoop(void) const { + boost::mutex::scoped_lock lock(m_Mutex); + #ifdef _DEBUG double nextProfile = 0; #endif /* _DEBUG */ @@ -565,3 +568,13 @@ void Application::SetPkgDataDir(const String& path) { m_PkgDataDir = path; } + +/** + * Returns the global mutex for the main thread. + * + * @returns The mutex. + */ +boost::mutex& Application::GetMutex(void) +{ + return m_Mutex; +} diff --git a/lib/base/application.h b/lib/base/application.h index fb92f5025..d693d00e7 100644 --- a/lib/base/application.h +++ b/lib/base/application.h @@ -78,10 +78,13 @@ public: static bool ProcessEvents(void); + static boost::mutex& GetMutex(void); + protected: void RunEventLoop(void) const; private: + static boost::mutex m_Mutex; /**< The main thread mutex. */ static Application *m_Instance; /**< The application instance. */ static bool m_ShuttingDown; /**< Whether the application is in the process of diff --git a/lib/base/event.cpp b/lib/base/event.cpp index e09290fcd..b536ebea4 100644 --- a/lib/base/event.cpp +++ b/lib/base/event.cpp @@ -46,6 +46,8 @@ void Event::ProcessEvents(millisec timeout) assert(Application::IsMainThread()); + Application::GetMutex().unlock(); + { boost::mutex::scoped_lock lock(m_Mutex); @@ -57,6 +59,8 @@ void Event::ProcessEvents(millisec timeout) events.swap(m_Events); } + Application::GetMutex().lock(); + BOOST_FOREACH(const Event& ev, events) { double st = Utility::GetTime(); diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 380a035ad..8b702ad12 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -398,11 +398,17 @@ pid_t Utility::GetPid(void) */ void Utility::Sleep(double timeout) { + if (Application::IsMainThread()) + Application::GetMutex().unlock(); + #ifndef _WIN32 usleep(timeout * 1000 * 1000); #else /* _WIN32 */ ::Sleep(timeout * 1000); #endif /* _WIN32 */ + + if (Application::IsMainThread()) + Application::GetMutex().lock(); } /**