From 137c726920ee017606199a332615928c25137bd5 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sat, 21 Sep 2013 17:53:14 +0200 Subject: [PATCH] base: Limit work queue size. --- lib/base/workqueue.cpp | 9 +++++++++ lib/base/workqueue.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/lib/base/workqueue.cpp b/lib/base/workqueue.cpp index 01af5938e..6984efa6b 100644 --- a/lib/base/workqueue.cpp +++ b/lib/base/workqueue.cpp @@ -23,6 +23,10 @@ using namespace icinga; +WorkQueue::WorkQueue(size_t maxItems) + : m_MaxItems(maxItems) +{ } + WorkQueue::~WorkQueue(void) { Join(); @@ -35,6 +39,10 @@ WorkQueue::~WorkQueue(void) void WorkQueue::Enqueue(const WorkCallback& item) { boost::mutex::scoped_lock lock(m_Mutex); + + while (m_Items.size() >= m_MaxItems) + m_CV.wait(lock); + m_Items.push_back(item); m_CV.notify_all(); @@ -66,6 +74,7 @@ void WorkQueue::ExecuteItem(void) try { WorkCallback wi = m_Items.front(); m_Items.pop_front(); + m_CV.notify_all(); lock.unlock(); wi(); diff --git a/lib/base/workqueue.h b/lib/base/workqueue.h index 4572384f1..b7ad56986 100644 --- a/lib/base/workqueue.h +++ b/lib/base/workqueue.h @@ -39,6 +39,7 @@ class I2_BASE_API WorkQueue public: typedef boost::function WorkCallback; + WorkQueue(size_t maxItems = 25000); ~WorkQueue(void); void Enqueue(const WorkCallback& item); @@ -48,6 +49,7 @@ public: private: boost::mutex m_Mutex; boost::condition_variable m_CV; + size_t m_MaxItems; bool m_Executing; std::deque m_Items;