]> granicus.if.org Git - icinga2/commitdiff
Fixed lock contention issue.
authorGunnar Beutner <gunnar@beutner.name>
Sun, 24 Jun 2012 15:07:38 +0000 (17:07 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Sun, 24 Jun 2012 15:07:38 +0000 (17:07 +0200)
icinga/nagioschecktask.cpp
icinga/nagioschecktask.h

index d0452b00e9945890071954ca9857700ba2db1e22..800dca309cd3e98cb3ff6fe42c1165d6d8299655 100644 (file)
@@ -7,6 +7,7 @@
 using namespace icinga;
 
 boost::mutex NagiosCheckTask::m_Mutex;
+vector<NagiosCheckTask::Ptr> NagiosCheckTask::m_PendingTasks;
 deque<NagiosCheckTask::Ptr> NagiosCheckTask::m_Tasks;
 condition_variable NagiosCheckTask::m_TasksCV;
 
@@ -20,16 +21,17 @@ NagiosCheckTask::NagiosCheckTask(const Service& service)
 void NagiosCheckTask::Enqueue(void)
 {
        time(&m_Result.StartTime);
-
-       {
-                       mutex::scoped_lock lock(m_Mutex);
-                       m_Tasks.push_back(GetSelf());
-       }
+       m_PendingTasks.push_back(GetSelf());
 }
 
 void NagiosCheckTask::FlushQueue(void)
 {
-       m_TasksCV.notify_all();
+       {
+               mutex::scoped_lock lock(m_Mutex);
+               std::copy(m_PendingTasks.begin(), m_PendingTasks.end(), back_inserter(m_Tasks));
+               m_PendingTasks.clear();
+               m_TasksCV.notify_all();
+       }
 }
 
 CheckResult NagiosCheckTask::GetResult(void)
@@ -200,7 +202,9 @@ void NagiosCheckTask::Register(void)
 {
        CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask, NagiosCheckTask::FlushQueue);
 
-       for (int i = 0; i < 1; i++) {
+       int numThreads = max(4, boost::thread::hardware_concurrency());
+
+       for (int i = 0; i < numThreads; i++) {
                thread t(&NagiosCheckTask::CheckThreadProc);
                t.detach();
        }
index c0601c3737427928682b87f513fd4ad5c5ca7335..16141857dbb8e2dd737d441055c8f897c7281f32 100644 (file)
@@ -33,6 +33,7 @@ private:
 
        static boost::mutex m_Mutex;
        static deque<NagiosCheckTask::Ptr> m_Tasks;
+       static vector<NagiosCheckTask::Ptr> m_PendingTasks;
        static condition_variable m_TasksCV;
 
        static void CheckThreadProc(void);