1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
23 #include "base/i2-base.hpp"
24 #include <boost/function.hpp>
25 #include <boost/thread/thread.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition_variable.hpp>
46 class I2_BASE_API ThreadPool
49 typedef boost::function<void ()> WorkFunction;
51 ThreadPool(size_t max_threads = UINT_MAX);
57 bool Post(const WorkFunction& callback, SchedulerPolicy policy = DefaultScheduler);
70 WorkFunction Callback;
82 boost::thread *Thread;
84 WorkerThread(ThreadState state = ThreadDead)
85 : State(state), Zombie(false), Utilization(0), LastUpdate(0), Thread(NULL)
88 void UpdateUtilization(ThreadState state = ThreadUnspecified);
90 void ThreadProc(Queue& queue);
96 boost::condition_variable CV;
97 boost::condition_variable CVStarved;
99 std::deque<WorkItem> Items;
107 WorkerThread Threads[16];
110 : WaitTime(0), ServiceTime(0), TaskCount(0), Stopped(false)
113 void SpawnWorker(boost::thread_group& group);
114 void KillWorker(boost::thread_group& group);
122 boost::thread_group m_ThreadGroup;
124 boost::thread m_MgmtThread;
125 boost::mutex m_MgmtMutex;
126 boost::condition_variable m_MgmtCV;
129 Queue m_Queues[QUEUECOUNT];
131 void ManagerThreadProc(void);
136 #endif /* EVENTQUEUE_H */