2012-06-24 02:56:48 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2017-01-10 15:54:22 +01:00
|
|
|
* Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
|
2012-06-24 02:56:48 +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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-25 18:36:15 +01:00
|
|
|
#ifndef THREADPOOL_H
|
|
|
|
#define THREADPOOL_H
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/i2-base.hpp"
|
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>
|
2014-05-11 17:14:35 +02:00
|
|
|
#include <deque>
|
2013-03-15 18:21:29 +01:00
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
2014-12-06 19:21:30 +01:00
|
|
|
#define QUEUECOUNT 4U
|
2014-05-28 15:57:48 +02:00
|
|
|
|
2014-09-11 11:45:21 +02:00
|
|
|
enum SchedulerPolicy
|
|
|
|
{
|
|
|
|
DefaultScheduler,
|
|
|
|
LowLatencyScheduler
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
2014-05-11 08:27:42 +02:00
|
|
|
ThreadPool(size_t max_threads = UINT_MAX);
|
2013-03-25 18:36:15 +01:00
|
|
|
~ThreadPool(void);
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
void Start(void);
|
2013-02-15 06:47:26 +01:00
|
|
|
void Stop(void);
|
2013-02-18 14:40:24 +01:00
|
|
|
|
2014-09-11 11:45:21 +02:00
|
|
|
bool Post(const WorkFunction& callback, SchedulerPolicy policy = DefaultScheduler);
|
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-12-06 21:46:50 +01:00
|
|
|
struct WorkItem
|
|
|
|
{
|
|
|
|
WorkFunction Callback;
|
|
|
|
double Timestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Queue;
|
|
|
|
|
2013-11-21 07:46:12 +01:00
|
|
|
struct WorkerThread
|
2013-03-28 13:14:39 +01:00
|
|
|
{
|
|
|
|
ThreadState State;
|
2013-08-27 15:57:00 +02:00
|
|
|
bool Zombie;
|
2013-03-28 13:14:39 +01:00
|
|
|
double Utilization;
|
|
|
|
double LastUpdate;
|
2013-11-21 07:46:12 +01:00
|
|
|
boost::thread *Thread;
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2013-11-21 07:46:12 +01:00
|
|
|
WorkerThread(ThreadState state = ThreadDead)
|
|
|
|
: State(state), Zombie(false), Utilization(0), LastUpdate(0), Thread(NULL)
|
2013-03-28 13:14:39 +01:00
|
|
|
{ }
|
2013-12-06 21:46:50 +01:00
|
|
|
|
|
|
|
void UpdateUtilization(ThreadState state = ThreadUnspecified);
|
|
|
|
|
|
|
|
void ThreadProc(Queue& queue);
|
2013-03-28 13:14:39 +01:00
|
|
|
};
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
struct Queue
|
|
|
|
{
|
|
|
|
boost::mutex Mutex;
|
|
|
|
boost::condition_variable CV;
|
|
|
|
boost::condition_variable CVStarved;
|
2013-08-30 10:19:32 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
std::deque<WorkItem> Items;
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
double WaitTime;
|
|
|
|
double ServiceTime;
|
|
|
|
int TaskCount;
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
bool Stopped;
|
2013-03-23 20:38:41 +01:00
|
|
|
|
2014-03-12 10:52:07 +01:00
|
|
|
WorkerThread Threads[16];
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
Queue(void)
|
|
|
|
: WaitTime(0), ServiceTime(0), TaskCount(0), Stopped(false)
|
|
|
|
{ }
|
2013-03-25 18:36:15 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
void SpawnWorker(boost::thread_group& group);
|
|
|
|
void KillWorker(boost::thread_group& group);
|
2013-03-25 18:36:15 +01:00
|
|
|
};
|
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
int m_ID;
|
|
|
|
static int m_NextID;
|
2013-03-25 18:36:15 +01:00
|
|
|
|
2014-05-11 08:27:42 +02:00
|
|
|
size_t m_MaxThreads;
|
2013-02-17 19:14:34 +01:00
|
|
|
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::thread_group m_ThreadGroup;
|
2013-03-23 12:23:13 +01:00
|
|
|
|
2014-11-13 10:00:49 +01:00
|
|
|
boost::thread m_MgmtThread;
|
2013-12-06 21:46:50 +01:00
|
|
|
boost::mutex m_MgmtMutex;
|
|
|
|
boost::condition_variable m_MgmtCV;
|
|
|
|
bool m_Stopped;
|
2013-03-25 16:12:25 +01:00
|
|
|
|
2014-05-28 15:57:48 +02:00
|
|
|
Queue m_Queues[QUEUECOUNT];
|
2013-12-06 21:46:50 +01:00
|
|
|
|
|
|
|
void ManagerThreadProc(void);
|
2012-06-24 02:56:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-15 06:47:26 +01:00
|
|
|
#endif /* EVENTQUEUE_H */
|