2013-09-18 09:09:16 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2013-09-25 07:43:57 +02:00
|
|
|
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
|
2013-09-18 09:09:16 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/workqueue.h"
|
|
|
|
#include "base/utility.h"
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-09-21 17:53:14 +02:00
|
|
|
WorkQueue::WorkQueue(size_t maxItems)
|
2013-09-26 10:24:47 +02:00
|
|
|
: m_MaxItems(maxItems), m_Executing(false)
|
2013-09-21 17:53:14 +02:00
|
|
|
{ }
|
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
WorkQueue::~WorkQueue(void)
|
|
|
|
{
|
|
|
|
Join();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a work item. Work items are guaranteed to be executed in the order
|
|
|
|
* they were enqueued in.
|
|
|
|
*/
|
|
|
|
void WorkQueue::Enqueue(const WorkCallback& item)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-09-21 17:53:14 +02:00
|
|
|
|
|
|
|
while (m_Items.size() >= m_MaxItems)
|
|
|
|
m_CV.wait(lock);
|
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
m_Items.push_back(item);
|
|
|
|
m_CV.notify_all();
|
|
|
|
|
2013-09-18 17:38:26 +02:00
|
|
|
if (!m_Executing) {
|
|
|
|
m_Executing = true;
|
2013-09-18 09:09:16 +02:00
|
|
|
Utility::QueueAsyncCallback(boost::bind(&WorkQueue::ExecuteItem, this));
|
2013-09-18 17:38:26 +02:00
|
|
|
}
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::Join(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
while (m_Executing || !m_Items.empty())
|
|
|
|
m_CV.wait(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::Clear(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
m_Items.clear();
|
|
|
|
m_CV.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::ExecuteItem(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
while (!m_Items.empty()) {
|
|
|
|
try {
|
|
|
|
WorkCallback wi = m_Items.front();
|
|
|
|
m_Items.pop_front();
|
2013-09-21 17:53:14 +02:00
|
|
|
m_CV.notify_all();
|
2013-09-18 09:09:16 +02:00
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
wi();
|
|
|
|
lock.lock();
|
|
|
|
} catch (...) {
|
|
|
|
lock.lock();
|
|
|
|
m_Executing = false;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_Executing = false;
|
|
|
|
}
|