icinga2/lib/base/io-engine.hpp

136 lines
3.6 KiB
C++
Raw Normal View History

2019-02-08 10:05:24 +01:00
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
* *
* 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. *
******************************************************************************/
#ifndef IO_ENGINE_H
#define IO_ENGINE_H
#include "base/lazy-init.hpp"
#include <atomic>
2019-02-15 15:43:58 +01:00
#include <exception>
2019-02-08 10:05:24 +01:00
#include <memory>
2019-02-15 15:43:58 +01:00
#include <thread>
#include <vector>
2019-02-08 10:05:24 +01:00
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/spawn.hpp>
2019-02-22 16:16:59 +01:00
namespace icinga
{
2019-02-08 10:05:24 +01:00
/**
* Scope lock for CPU-bound work done in an I/O thread
*
* @ingroup base
*/
class CpuBoundWork
{
public:
CpuBoundWork(boost::asio::yield_context yc);
CpuBoundWork(const CpuBoundWork&) = delete;
CpuBoundWork(CpuBoundWork&&) = delete;
CpuBoundWork& operator=(const CpuBoundWork&) = delete;
CpuBoundWork& operator=(CpuBoundWork&&) = delete;
~CpuBoundWork();
void Done();
private:
bool m_Done;
2019-02-08 10:05:24 +01:00
};
2019-02-15 12:25:25 +01:00
/**
* Scope break for CPU-bound work done in an I/O thread
*
* @ingroup base
*/
class IoBoundWorkSlot
{
public:
IoBoundWorkSlot(boost::asio::yield_context yc);
IoBoundWorkSlot(const IoBoundWorkSlot&) = delete;
IoBoundWorkSlot(IoBoundWorkSlot&&) = delete;
IoBoundWorkSlot& operator=(const IoBoundWorkSlot&) = delete;
IoBoundWorkSlot& operator=(IoBoundWorkSlot&&) = delete;
~IoBoundWorkSlot();
private:
boost::asio::yield_context yc;
};
2019-02-08 10:05:24 +01:00
/**
* Async I/O engine
*
* @ingroup base
*/
class IoEngine
{
friend CpuBoundWork;
2019-02-15 12:25:25 +01:00
friend IoBoundWorkSlot;
2019-02-08 10:05:24 +01:00
public:
IoEngine(const IoEngine&) = delete;
IoEngine(IoEngine&&) = delete;
IoEngine& operator=(const IoEngine&) = delete;
IoEngine& operator=(IoEngine&&) = delete;
2019-02-15 15:43:58 +01:00
~IoEngine();
2019-02-08 10:05:24 +01:00
static IoEngine& Get();
boost::asio::io_service& GetIoService();
private:
IoEngine();
void RunEventLoop();
static LazyInit<std::unique_ptr<IoEngine>> m_Instance;
boost::asio::io_service m_IoService;
boost::asio::io_service::work m_KeepAlive;
2019-02-15 15:43:58 +01:00
std::vector<std::thread> m_Threads;
2019-02-08 10:05:24 +01:00
boost::asio::deadline_timer m_AlreadyExpiredTimer;
std::atomic_int_fast32_t m_CpuBoundSemaphore;
2019-02-08 10:05:24 +01:00
};
2019-02-15 15:43:58 +01:00
class TerminateIoThread : public std::exception
{
};
2019-02-22 16:13:28 +01:00
/**
* Condition variable which doesn't block I/O threads
*
* @ingroup base
*/
class AsioConditionVariable
{
public:
AsioConditionVariable(boost::asio::io_service& io, bool init = false);
void Set();
void Clear();
void Wait(boost::asio::yield_context yc);
private:
boost::asio::deadline_timer m_Timer;
};
2019-02-22 16:16:59 +01:00
}
2019-02-08 10:05:24 +01:00
#endif /* IO_ENGINE_H */