]> granicus.if.org Git - icinga2/commitdiff
base: Limit work queue size.
authorGunnar Beutner <gunnar@beutner.name>
Sat, 21 Sep 2013 15:53:14 +0000 (17:53 +0200)
committerGunnar Beutner <gunnar@beutner.name>
Sat, 21 Sep 2013 15:53:14 +0000 (17:53 +0200)
lib/base/workqueue.cpp
lib/base/workqueue.h

index 01af5938ee21b05bc6a28fed1c0d660fcc13fcfd..6984efa6baf194a6fd7968a32a7d08b0f319f1a9 100644 (file)
 
 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();
index 4572384f11b4f0b08fead118e4c555abd4027253..b7ad56986dd523a9a500c7a9c6a9940d740cf97c 100644 (file)
@@ -39,6 +39,7 @@ class I2_BASE_API WorkQueue
 public:
        typedef boost::function<void (void)> 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<WorkCallback> m_Items;