mirror of
https://github.com/Icinga/icinga2.git
synced 2026-02-19 02:29:16 -05:00
https://github.com/boostorg/asio/issues/110 https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/example/cpp03/services/logger_service.hpp
118 lines
2.3 KiB
C++
118 lines
2.3 KiB
C++
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
|
|
|
#ifndef IO_ENGINE_H
|
|
#define IO_ENGINE_H
|
|
|
|
#include "base/lazy-init.hpp"
|
|
#include <atomic>
|
|
#include <exception>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <boost/asio/deadline_timer.hpp>
|
|
#include <boost/asio/io_context.hpp>
|
|
#include <boost/asio/spawn.hpp>
|
|
|
|
namespace icinga
|
|
{
|
|
|
|
/**
|
|
* 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;
|
|
};
|
|
|
|
/**
|
|
* 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;
|
|
};
|
|
|
|
/**
|
|
* Async I/O engine
|
|
*
|
|
* @ingroup base
|
|
*/
|
|
class IoEngine
|
|
{
|
|
friend CpuBoundWork;
|
|
friend IoBoundWorkSlot;
|
|
|
|
public:
|
|
IoEngine(const IoEngine&) = delete;
|
|
IoEngine(IoEngine&&) = delete;
|
|
IoEngine& operator=(const IoEngine&) = delete;
|
|
IoEngine& operator=(IoEngine&&) = delete;
|
|
~IoEngine();
|
|
|
|
static IoEngine& Get();
|
|
|
|
boost::asio::io_context& GetIoContext();
|
|
|
|
private:
|
|
IoEngine();
|
|
|
|
void RunEventLoop();
|
|
|
|
static LazyInit<std::unique_ptr<IoEngine>> m_Instance;
|
|
|
|
boost::asio::io_context m_IoContext;
|
|
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_KeepAlive;
|
|
std::vector<std::thread> m_Threads;
|
|
boost::asio::deadline_timer m_AlreadyExpiredTimer;
|
|
std::atomic_int_fast32_t m_CpuBoundSemaphore;
|
|
};
|
|
|
|
class TerminateIoThread : public std::exception
|
|
{
|
|
};
|
|
|
|
/**
|
|
* Condition variable which doesn't block I/O threads
|
|
*
|
|
* @ingroup base
|
|
*/
|
|
class AsioConditionVariable
|
|
{
|
|
public:
|
|
AsioConditionVariable(boost::asio::io_context& io, bool init = false);
|
|
|
|
void Set();
|
|
void Clear();
|
|
void Wait(boost::asio::yield_context yc);
|
|
|
|
private:
|
|
boost::asio::deadline_timer m_Timer;
|
|
};
|
|
|
|
}
|
|
|
|
#endif /* IO_ENGINE_H */
|