Fixed lock contention issue.

This commit is contained in:
Gunnar Beutner 2012-06-24 17:07:38 +02:00
parent ba4d550fec
commit b414d5a952
2 changed files with 12 additions and 7 deletions

View File

@ -7,6 +7,7 @@
using namespace icinga; using namespace icinga;
boost::mutex NagiosCheckTask::m_Mutex; boost::mutex NagiosCheckTask::m_Mutex;
vector<NagiosCheckTask::Ptr> NagiosCheckTask::m_PendingTasks;
deque<NagiosCheckTask::Ptr> NagiosCheckTask::m_Tasks; deque<NagiosCheckTask::Ptr> NagiosCheckTask::m_Tasks;
condition_variable NagiosCheckTask::m_TasksCV; condition_variable NagiosCheckTask::m_TasksCV;
@ -20,16 +21,17 @@ NagiosCheckTask::NagiosCheckTask(const Service& service)
void NagiosCheckTask::Enqueue(void) void NagiosCheckTask::Enqueue(void)
{ {
time(&m_Result.StartTime); time(&m_Result.StartTime);
m_PendingTasks.push_back(GetSelf());
{
mutex::scoped_lock lock(m_Mutex);
m_Tasks.push_back(GetSelf());
}
} }
void NagiosCheckTask::FlushQueue(void) void NagiosCheckTask::FlushQueue(void)
{ {
{
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(); m_TasksCV.notify_all();
}
} }
CheckResult NagiosCheckTask::GetResult(void) CheckResult NagiosCheckTask::GetResult(void)
@ -200,7 +202,9 @@ void NagiosCheckTask::Register(void)
{ {
CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask, NagiosCheckTask::FlushQueue); 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); thread t(&NagiosCheckTask::CheckThreadProc);
t.detach(); t.detach();
} }

View File

@ -33,6 +33,7 @@ private:
static boost::mutex m_Mutex; static boost::mutex m_Mutex;
static deque<NagiosCheckTask::Ptr> m_Tasks; static deque<NagiosCheckTask::Ptr> m_Tasks;
static vector<NagiosCheckTask::Ptr> m_PendingTasks;
static condition_variable m_TasksCV; static condition_variable m_TasksCV;
static void CheckThreadProc(void); static void CheckThreadProc(void);