2012-06-24 02:56:48 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
#ifndef THREADPOOL_H
|
|
|
|
#define THREADPOOL_H
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/i2-base.h"
|
|
|
|
#include <stack>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/function.hpp>
|
|
|
|
#include <boost/thread/thread.hpp>
|
|
|
|
#include <boost/thread/mutex.hpp>
|
|
|
|
#include <boost/thread/condition_variable.hpp>
|
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2012-09-17 13:35:55 +02:00
|
|
|
/**
|
2013-03-25 18:36:15 +01:00
|
|
|
* A thread pool.
|
2012-09-17 13:35:55 +02:00
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2013-03-25 18:36:15 +01:00
|
|
|
class I2_BASE_API ThreadPool
|
2012-06-24 02:56:48 +02:00
|
|
|
{
|
|
|
|
public:
|
2013-03-25 18:36:15 +01:00
|
|
|
typedef boost::function<void ()> WorkFunction;
|
|
|
|
|
|
|
|
ThreadPool(void);
|
|
|
|
~ThreadPool(void);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
void Stop(void);
|
2013-02-18 14:40:24 +01:00
|
|
|
void Join(void);
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
void Post(const WorkFunction& callback);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
private:
|
2013-03-25 18:36:15 +01:00
|
|
|
enum ThreadState
|
|
|
|
{
|
2013-03-28 13:14:39 +01:00
|
|
|
ThreadUnspecified,
|
2013-03-25 18:36:15 +01:00
|
|
|
ThreadDead,
|
|
|
|
ThreadIdle,
|
|
|
|
ThreadBusy
|
|
|
|
};
|
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
struct ThreadStats
|
|
|
|
{
|
|
|
|
ThreadState State;
|
|
|
|
double Utilization;
|
|
|
|
double LastUpdate;
|
|
|
|
|
|
|
|
ThreadStats(ThreadState state = ThreadDead)
|
|
|
|
: State(state), Utilization(0), LastUpdate(0)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
ThreadStats m_ThreadStats[512];
|
2013-03-23 12:23:13 +01:00
|
|
|
int m_ThreadDeaths;
|
|
|
|
|
2013-03-25 16:12:25 +01:00
|
|
|
double m_WaitTime;
|
|
|
|
double m_ServiceTime;
|
|
|
|
int m_TaskCount;
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-03-23 20:38:41 +01:00
|
|
|
double m_MaxLatency;
|
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
boost::mutex m_Mutex;
|
2013-03-15 18:21:29 +01:00
|
|
|
boost::condition_variable m_CV;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
bool m_Stopped;
|
2013-03-25 18:36:15 +01:00
|
|
|
|
|
|
|
struct WorkItem
|
|
|
|
{
|
|
|
|
WorkFunction Callback;
|
|
|
|
double Timestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
std::deque<WorkItem> m_WorkItems;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-03-23 00:26:56 +01:00
|
|
|
void QueueThreadProc(int tid);
|
2013-03-23 20:38:41 +01:00
|
|
|
void ManagerThreadProc(void);
|
2013-03-28 13:14:39 +01:00
|
|
|
void StatsThreadProc(void);
|
2013-03-23 12:23:13 +01:00
|
|
|
|
|
|
|
void SpawnWorker(void);
|
|
|
|
void KillWorker(void);
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2013-03-28 13:14:39 +01:00
|
|
|
void UpdateThreadUtilization(int tid, ThreadState state = ThreadUnspecified);
|
2012-06-24 02:56:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
#endif /* EVENTQUEUE_H */
|