2019-05-24 16:25:32 +02:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2019-02-08 10:05:24 +01:00
|
|
|
|
|
|
|
#include "base/exception.hpp"
|
|
|
|
#include "base/io-engine.hpp"
|
|
|
|
#include "base/lazy-init.hpp"
|
|
|
|
#include "base/logger.hpp"
|
|
|
|
#include <exception>
|
|
|
|
#include <memory>
|
|
|
|
#include <thread>
|
2019-09-09 15:11:38 +02:00
|
|
|
#include <boost/asio/io_context.hpp>
|
2019-02-08 10:05:24 +01:00
|
|
|
#include <boost/asio/spawn.hpp>
|
2019-09-09 15:11:38 +02:00
|
|
|
#include <boost/asio/post.hpp>
|
2019-02-08 10:05:24 +01:00
|
|
|
#include <boost/date_time/posix_time/ptime.hpp>
|
2019-02-22 16:13:28 +01:00
|
|
|
#include <boost/system/error_code.hpp>
|
2019-02-08 10:05:24 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc)
|
2019-02-14 13:10:04 +01:00
|
|
|
: m_Done(false)
|
2019-02-08 10:05:24 +01:00
|
|
|
{
|
|
|
|
auto& ioEngine (IoEngine::Get());
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto availableSlots (ioEngine.m_CpuBoundSemaphore.fetch_sub(1));
|
|
|
|
|
|
|
|
if (availableSlots < 1) {
|
|
|
|
ioEngine.m_CpuBoundSemaphore.fetch_add(1);
|
|
|
|
ioEngine.m_AlreadyExpiredTimer.async_wait(yc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CpuBoundWork::~CpuBoundWork()
|
|
|
|
{
|
2019-02-14 13:10:04 +01:00
|
|
|
if (!m_Done) {
|
|
|
|
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CpuBoundWork::Done()
|
|
|
|
{
|
|
|
|
if (!m_Done) {
|
|
|
|
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1);
|
|
|
|
|
|
|
|
m_Done = true;
|
|
|
|
}
|
2019-02-08 10:05:24 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 12:25:25 +01:00
|
|
|
IoBoundWorkSlot::IoBoundWorkSlot(boost::asio::yield_context yc)
|
|
|
|
: yc(yc)
|
|
|
|
{
|
|
|
|
IoEngine::Get().m_CpuBoundSemaphore.fetch_add(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
IoBoundWorkSlot::~IoBoundWorkSlot()
|
|
|
|
{
|
|
|
|
auto& ioEngine (IoEngine::Get());
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
auto availableSlots (ioEngine.m_CpuBoundSemaphore.fetch_sub(1));
|
|
|
|
|
|
|
|
if (availableSlots < 1) {
|
|
|
|
ioEngine.m_CpuBoundSemaphore.fetch_add(1);
|
|
|
|
ioEngine.m_AlreadyExpiredTimer.async_wait(yc);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 10:05:24 +01:00
|
|
|
LazyInit<std::unique_ptr<IoEngine>> IoEngine::m_Instance ([]() { return std::unique_ptr<IoEngine>(new IoEngine()); });
|
|
|
|
|
|
|
|
IoEngine& IoEngine::Get()
|
|
|
|
{
|
|
|
|
return *m_Instance.Get();
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:11:38 +02:00
|
|
|
boost::asio::io_context& IoEngine::GetIoContext()
|
2019-02-08 10:05:24 +01:00
|
|
|
{
|
2019-09-09 15:11:38 +02:00
|
|
|
return m_IoContext;
|
2019-02-08 10:05:24 +01:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:11:38 +02:00
|
|
|
IoEngine::IoEngine() : m_IoContext(), m_KeepAlive(boost::asio::make_work_guard(m_IoContext)), m_Threads(decltype(m_Threads)::size_type(std::thread::hardware_concurrency() * 2u)), m_AlreadyExpiredTimer(m_IoContext)
|
2019-02-08 10:05:24 +01:00
|
|
|
{
|
|
|
|
m_AlreadyExpiredTimer.expires_at(boost::posix_time::neg_infin);
|
2019-02-21 10:08:38 +01:00
|
|
|
m_CpuBoundSemaphore.store(std::thread::hardware_concurrency() * 3u / 2u);
|
2019-02-08 10:05:24 +01:00
|
|
|
|
2019-02-15 15:43:58 +01:00
|
|
|
for (auto& thread : m_Threads) {
|
|
|
|
thread = std::thread(&IoEngine::RunEventLoop, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IoEngine::~IoEngine()
|
|
|
|
{
|
|
|
|
for (auto& thread : m_Threads) {
|
2019-09-09 15:11:38 +02:00
|
|
|
boost::asio::post(m_IoContext, []() {
|
2019-02-15 15:43:58 +01:00
|
|
|
throw TerminateIoThread();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& thread : m_Threads) {
|
|
|
|
thread.join();
|
2019-02-08 10:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IoEngine::RunEventLoop()
|
|
|
|
{
|
|
|
|
for (;;) {
|
|
|
|
try {
|
2019-09-09 15:11:38 +02:00
|
|
|
m_IoContext.run();
|
2019-02-08 10:05:24 +01:00
|
|
|
|
2019-02-15 15:43:58 +01:00
|
|
|
break;
|
|
|
|
} catch (const TerminateIoThread&) {
|
2019-02-08 10:05:24 +01:00
|
|
|
break;
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
Log(LogCritical, "IoEngine", "Exception during I/O operation!");
|
|
|
|
Log(LogDebug, "IoEngine") << "Exception during I/O operation: " << DiagnosticInformation(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 16:13:28 +01:00
|
|
|
|
2019-09-09 15:11:38 +02:00
|
|
|
AsioConditionVariable::AsioConditionVariable(boost::asio::io_context& io, bool init)
|
2019-02-22 16:13:28 +01:00
|
|
|
: m_Timer(io)
|
|
|
|
{
|
|
|
|
m_Timer.expires_at(init ? boost::posix_time::neg_infin : boost::posix_time::pos_infin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsioConditionVariable::Set()
|
|
|
|
{
|
|
|
|
m_Timer.expires_at(boost::posix_time::neg_infin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsioConditionVariable::Clear()
|
|
|
|
{
|
|
|
|
m_Timer.expires_at(boost::posix_time::pos_infin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AsioConditionVariable::Wait(boost::asio::yield_context yc)
|
|
|
|
{
|
|
|
|
boost::system::error_code ec;
|
|
|
|
m_Timer.async_wait(yc[ec]);
|
|
|
|
}
|