2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/threadpool.hpp"
|
2019-04-01 17:05:16 +02:00
|
|
|
#include <boost/thread/locks.hpp>
|
2012-06-24 02:56:48 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2019-04-01 17:05:16 +02:00
|
|
|
ThreadPool::ThreadPool(size_t threads)
|
2019-08-14 17:12:59 +02:00
|
|
|
: m_Threads(threads), m_Pending(0)
|
2013-02-18 14:40:24 +01:00
|
|
|
{
|
2013-12-06 21:46:50 +01:00
|
|
|
Start();
|
2013-02-18 14:40:24 +01:00
|
|
|
}
|
2012-07-13 23:33:30 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
ThreadPool::~ThreadPool()
|
2013-02-15 06:47:26 +01:00
|
|
|
{
|
2013-02-17 19:14:34 +01:00
|
|
|
Stop();
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ThreadPool::Start()
|
2013-12-06 21:46:50 +01:00
|
|
|
{
|
2019-04-01 17:05:16 +02:00
|
|
|
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
|
2013-12-06 21:46:50 +01:00
|
|
|
|
2019-04-01 17:05:16 +02:00
|
|
|
if (!m_Pool) {
|
|
|
|
m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(m_Threads));
|
2013-12-06 21:46:50 +01:00
|
|
|
}
|
2013-02-17 19:14:34 +01:00
|
|
|
}
|
2012-06-24 02:56:48 +02:00
|
|
|
|
2019-04-01 17:05:16 +02:00
|
|
|
void ThreadPool::Stop()
|
2013-03-25 16:12:25 +01:00
|
|
|
{
|
2019-04-01 17:05:16 +02:00
|
|
|
boost::unique_lock<decltype(m_Mutex)> lock (m_Mutex);
|
2013-03-28 13:14:39 +01:00
|
|
|
|
2019-04-01 17:05:16 +02:00
|
|
|
if (m_Pool) {
|
|
|
|
m_Pool->join();
|
|
|
|
m_Pool = nullptr;
|
2013-03-28 13:14:39 +01:00
|
|
|
}
|
2013-03-25 16:12:25 +01:00
|
|
|
}
|