[icinga-checkins] icinga.org: icinga2/master: Implement global mutex ( for use by scripting languages).

git at icinga.org git at icinga.org
Thu Feb 14 10:56:04 CET 2013


Module: icinga2
Branch: master
Commit: c91191e70193d474c74173ec92fd9892ed4f9f83
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=c91191e70193d474c74173ec92fd9892ed4f9f83

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Thu Feb 14 10:55:47 2013 +0100

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(+), 0 deletions(-)

diff --git a/lib/base/application.cpp b/lib/base/application.cpp
index b7a99b6..b0d179d 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 fb92f50..d693d00 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 e09290f..b536ebe 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 380a035..8b702ad 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();
 }
 
 /**





More information about the icinga-checkins mailing list