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"
|
2013-10-17 10:19:17 +02:00
|
|
|
#include "base/debug.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-11-28 12:12:24 +01:00
|
|
|
#include "base/convert.h"
|
2013-09-18 09:09:16 +02:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-10-17 10:19:17 +02:00
|
|
|
int WorkQueue::m_NextID = 1;
|
|
|
|
|
|
|
|
WorkQueue::WorkQueue(size_t maxItems)
|
2013-11-05 12:40:25 +01:00
|
|
|
: m_ID(m_NextID++), m_MaxItems(maxItems), m_Joined(false),
|
2013-11-29 17:07:04 +01:00
|
|
|
m_Stopped(false), m_ExceptionCallback(WorkQueue::DefaultExceptionCallback)
|
2013-10-17 10:19:17 +02:00
|
|
|
{
|
|
|
|
m_Thread = boost::thread(boost::bind(&WorkQueue::WorkerThreadProc, this));
|
2013-11-29 17:07:04 +01:00
|
|
|
|
|
|
|
m_StatusTimer = make_shared<Timer>();
|
|
|
|
m_StatusTimer->SetInterval(10);
|
|
|
|
m_StatusTimer->OnTimerExpired.connect(boost::bind(&WorkQueue::StatusTimerHandler, this));
|
|
|
|
m_StatusTimer->Start();
|
2013-10-17 10:19:17 +02:00
|
|
|
}
|
2013-09-21 17:53:14 +02:00
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
WorkQueue::~WorkQueue(void)
|
|
|
|
{
|
|
|
|
Join();
|
2013-10-17 10:19:17 +02:00
|
|
|
|
|
|
|
ASSERT(m_Stopped);
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueues a work item. Work items are guaranteed to be executed in the order
|
|
|
|
* they were enqueued in.
|
|
|
|
*/
|
2013-11-28 10:36:43 +01:00
|
|
|
void WorkQueue::Enqueue(const WorkCallback& callback, bool allowInterleaved)
|
2013-09-18 09:09:16 +02:00
|
|
|
{
|
2013-11-29 17:07:04 +01:00
|
|
|
WorkItem item;
|
|
|
|
item.Callback = callback;
|
|
|
|
item.AllowInterleaved = allowInterleaved;
|
|
|
|
|
|
|
|
bool wq_thread = (boost::this_thread::get_id() == GetThreadId());
|
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-09-21 17:53:14 +02:00
|
|
|
|
2013-11-03 13:57:18 +01:00
|
|
|
ASSERT(!m_Stopped);
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2013-11-22 10:32:53 +01:00
|
|
|
if (!wq_thread) {
|
2013-11-21 13:54:01 +01:00
|
|
|
while (m_Items.size() >= m_MaxItems)
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVFull.wait(lock);
|
2013-11-21 13:54:01 +01:00
|
|
|
}
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2013-09-18 09:09:16 +02:00
|
|
|
m_Items.push_back(item);
|
2013-11-22 10:32:53 +01:00
|
|
|
|
|
|
|
if (wq_thread)
|
2013-11-28 10:36:43 +01:00
|
|
|
ProcessItems(lock, true);
|
2013-11-22 10:32:53 +01:00
|
|
|
else
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVEmpty.notify_one();
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::Join(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
2013-10-17 10:19:17 +02:00
|
|
|
m_Joined = true;
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVEmpty.notify_all();
|
2013-10-17 15:49:53 +02:00
|
|
|
|
2013-10-17 10:19:17 +02:00
|
|
|
while (!m_Stopped)
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVFull.wait(lock);
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:13:09 +01:00
|
|
|
boost::thread::id WorkQueue::GetThreadId(void) const
|
|
|
|
{
|
|
|
|
return m_Thread.get_id();
|
|
|
|
}
|
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
void WorkQueue::SetExceptionCallback(const ExceptionCallback& callback)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
m_ExceptionCallback = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::DefaultExceptionCallback(boost::exception_ptr exp)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2013-11-29 17:07:04 +01:00
|
|
|
void WorkQueue::StatusTimerHandler(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
Log(LogInformation, "base", "WQ #" + Convert::ToString(m_ID) + " items: " + Convert::ToString(m_Items.size()));
|
|
|
|
}
|
|
|
|
|
2013-11-28 10:36:43 +01:00
|
|
|
void WorkQueue::ProcessItems(boost::mutex::scoped_lock& lock, bool interleaved)
|
2013-09-18 09:09:16 +02:00
|
|
|
{
|
2013-11-22 10:32:53 +01:00
|
|
|
while (!m_Items.empty()) {
|
2013-11-29 17:07:04 +01:00
|
|
|
WorkItem wi = m_Items.front();
|
2013-11-28 10:36:43 +01:00
|
|
|
|
2013-11-29 17:07:04 +01:00
|
|
|
if (interleaved && !wi.AllowInterleaved)
|
|
|
|
return;
|
2013-09-18 09:09:16 +02:00
|
|
|
|
2013-11-29 17:07:04 +01:00
|
|
|
m_Items.pop_front();
|
|
|
|
m_CVFull.notify_all();
|
2013-11-28 12:12:24 +01:00
|
|
|
|
2013-11-29 17:07:04 +01:00
|
|
|
lock.unlock();
|
2013-11-28 12:12:24 +01:00
|
|
|
|
2013-11-29 17:07:04 +01:00
|
|
|
try {
|
2013-11-28 10:36:43 +01:00
|
|
|
wi.Callback();
|
2013-10-17 10:19:17 +02:00
|
|
|
} catch (const std::exception& ex) {
|
2013-11-05 12:40:25 +01:00
|
|
|
lock.lock();
|
|
|
|
|
|
|
|
ExceptionCallback callback = m_ExceptionCallback;
|
|
|
|
|
|
|
|
lock.unlock();
|
2013-10-17 10:19:17 +02:00
|
|
|
|
2013-11-05 12:40:25 +01:00
|
|
|
callback(boost::current_exception());
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
2013-10-17 10:19:17 +02:00
|
|
|
|
|
|
|
lock.lock();
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|
2013-11-22 10:32:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkQueue::WorkerThreadProc(void)
|
|
|
|
{
|
|
|
|
boost::mutex::scoped_lock lock(m_Mutex);
|
|
|
|
|
|
|
|
std::ostringstream idbuf;
|
|
|
|
idbuf << "WQ #" << m_ID;
|
|
|
|
Utility::SetThreadName(idbuf.str());
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
while (m_Items.empty() && !m_Joined)
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVEmpty.wait(lock);
|
2013-11-22 10:32:53 +01:00
|
|
|
|
|
|
|
if (m_Joined)
|
|
|
|
break;
|
|
|
|
|
2013-11-28 10:36:43 +01:00
|
|
|
ProcessItems(lock, false);
|
2013-11-22 10:32:53 +01:00
|
|
|
}
|
2013-09-18 09:09:16 +02:00
|
|
|
|
2013-10-17 10:19:17 +02:00
|
|
|
m_Stopped = true;
|
2013-11-29 17:07:04 +01:00
|
|
|
m_CVFull.notify_all();
|
2013-09-18 09:09:16 +02:00
|
|
|
}
|