]> granicus.if.org Git - icinga2/commitdiff
Implement global mutex (for use by scripting languages).
authorGunnar Beutner <gunnar.beutner@netways.de>
Thu, 14 Feb 2013 09:55:47 +0000 (10:55 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Thu, 14 Feb 2013 09:55:47 +0000 (10:55 +0100)
lib/base/application.cpp
lib/base/application.h
lib/base/event.cpp
lib/base/utility.cpp

index b7a99b6cb0da19f078db19e85cf1babe440723c7..b0d179d5f8e1a709b0882ffc6e5599c3d6dd5731 100644 (file)
@@ -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;
+}
index fb92f5025de5fd9395e08da13579c7a51ed7cd64..d693d00e7029d8dd540c475c63799675792def82 100644 (file)
@@ -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
index e09290fcd74fabb13ca7339e272d653ba8a045dc..b536ebea416cbf38c3bc3cc7b83bbf22dfa1a9a7 100644 (file)
@@ -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();
 
index 380a035ad935ab63ad9283fab0148d70e5335b69..8b702ad1297febd60171c9b907f667fd57ac9b9f 100644 (file)
@@ -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();
 }
 
 /**