ioctx_on_thread_pool.hpp
Go to the documentation of this file.
#pragma once
#include <thread>
#include <restinio/asio_include.hpp>
#include <restinio/exception.hpp>
namespace restinio
{
namespace impl
{
class own_io_context_for_thread_pool_t
{
asio_ns::io_context m_ioctx;
public:
own_io_context_for_thread_pool_t() = default;
auto & io_context() noexcept { return m_ioctx; }
};
class external_io_context_for_thread_pool_t
{
asio_ns::io_context & m_ioctx;
public:
external_io_context_for_thread_pool_t(
asio_ns::io_context & ioctx )
: m_ioctx{ ioctx }
{}
auto & io_context() noexcept { return m_ioctx; }
};
template< typename Io_Context_Holder >
class ioctx_on_thread_pool_t
{
public:
ioctx_on_thread_pool_t( const ioctx_on_thread_pool_t & ) = delete;
ioctx_on_thread_pool_t( ioctx_on_thread_pool_t && ) = delete;
template< typename... Io_Context_Holder_Ctor_Args >
ioctx_on_thread_pool_t(
// Pool size.
//FIXME: better to use not_null from gsl.
std::size_t pool_size,
// Optional arguments for Io_Context_Holder instance.
Io_Context_Holder_Ctor_Args && ...ioctx_holder_args )
: m_ioctx_holder{
std::forward<Io_Context_Holder_Ctor_Args>(ioctx_holder_args)... }
, m_pool( pool_size )
, m_status( status_t::stopped )
{}
// Makes sure the pool is stopped.
~ioctx_on_thread_pool_t()
{
if( started() )
{
stop();
wait();
}
}
void
start()
{
if( started() )
{
throw exception_t{
"io_context_with_thread_pool is already started" };
}
try
{
std::generate(
begin( m_pool ),
end( m_pool ),
[this]{
return
std::thread{ [this] {
auto work{ asio_ns::make_work_guard(
m_ioctx_holder.io_context() ) };
m_ioctx_holder.io_context().run();
} };
} );
// When all thread started successfully
// status can be changed.
m_status = status_t::started;
}
catch( const std::exception & )
{
io_context().stop();
for( auto & t : m_pool )
if( t.joinable() )
t.join();
throw;
}
}
// NOTE: this method is marked as noexcept in v.0.6.7.
// It's because this method can be called from destructors and
// there is no way to recover from an exception thrown from
// this method.
void
stop() noexcept
{
if( started() )
{
io_context().stop();
}
}
// NOTE: this method is marked as noexcept in v.0.6.7.
// It's because this method can be called from destructors and
// there is no way to recover from an exception thrown from
// this method.
void
wait() noexcept
{
if( started() )
{
for( auto & t : m_pool )
t.join();
// When all threads are stopped status can be changed.
m_status = status_t::stopped;
}
}
bool started() const noexcept { return status_t::started == m_status; }
asio_ns::io_context &
io_context() noexcept
{
return m_ioctx_holder.io_context();
}
private:
enum class status_t : std::uint8_t { stopped, started };
Io_Context_Holder m_ioctx_holder;
std::vector< std::thread > m_pool;
status_t m_status;
};
} /* namespace impl */
} /* namespace restinio */
asio_include.hpp
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio::impl::external_io_context_for_thread_pool_t
A class for holding a reference to external Asio's io_context.
Definition: ioctx_on_thread_pool.hpp:47
restinio::impl::external_io_context_for_thread_pool_t::external_io_context_for_thread_pool_t
external_io_context_for_thread_pool_t(asio_ns::io_context &ioctx)
Initializing constructor.
Definition: ioctx_on_thread_pool.hpp:52
restinio::impl::external_io_context_for_thread_pool_t::m_ioctx
asio_ns::io_context & m_ioctx
Definition: ioctx_on_thread_pool.hpp:48
restinio::impl::external_io_context_for_thread_pool_t::io_context
auto & io_context() noexcept
Get access to io_context object.
Definition: ioctx_on_thread_pool.hpp:59
restinio::impl::ioctx_on_thread_pool_t
Definition: ioctx_on_thread_pool.hpp:74
restinio::impl::ioctx_on_thread_pool_t::io_context
asio_ns::io_context & io_context() noexcept
Definition: ioctx_on_thread_pool.hpp:174
restinio::impl::ioctx_on_thread_pool_t::status_t
status_t
Definition: ioctx_on_thread_pool.hpp:180
restinio::impl::ioctx_on_thread_pool_t::status_t::started
@ started
restinio::impl::ioctx_on_thread_pool_t::status_t::stopped
@ stopped
restinio::impl::ioctx_on_thread_pool_t::started
bool started() const noexcept
Definition: ioctx_on_thread_pool.hpp:171
restinio::impl::ioctx_on_thread_pool_t::wait
void wait() noexcept
Definition: ioctx_on_thread_pool.hpp:159
restinio::impl::ioctx_on_thread_pool_t::stop
void stop() noexcept
Definition: ioctx_on_thread_pool.hpp:146
restinio::impl::ioctx_on_thread_pool_t::start
void start()
Definition: ioctx_on_thread_pool.hpp:103
restinio::impl::ioctx_on_thread_pool_t::m_pool
std::vector< std::thread > m_pool
Definition: ioctx_on_thread_pool.hpp:183
restinio::impl::ioctx_on_thread_pool_t::~ioctx_on_thread_pool_t
~ioctx_on_thread_pool_t()
Definition: ioctx_on_thread_pool.hpp:93
restinio::impl::ioctx_on_thread_pool_t::m_status
status_t m_status
Definition: ioctx_on_thread_pool.hpp:184
restinio::impl::ioctx_on_thread_pool_t::ioctx_on_thread_pool_t
ioctx_on_thread_pool_t(const ioctx_on_thread_pool_t &)=delete
restinio::impl::ioctx_on_thread_pool_t::ioctx_on_thread_pool_t
ioctx_on_thread_pool_t(ioctx_on_thread_pool_t &&)=delete
restinio::impl::ioctx_on_thread_pool_t::m_ioctx_holder
Io_Context_Holder m_ioctx_holder
Definition: ioctx_on_thread_pool.hpp:182
restinio::impl::ioctx_on_thread_pool_t::ioctx_on_thread_pool_t
ioctx_on_thread_pool_t(std::size_t pool_size, Io_Context_Holder_Ctor_Args &&...ioctx_holder_args)
Definition: ioctx_on_thread_pool.hpp:80
restinio::impl::own_io_context_for_thread_pool_t
A class for holding actual instance of Asio's io_context.
Definition: ioctx_on_thread_pool.hpp:26
restinio::impl::own_io_context_for_thread_pool_t::io_context
auto & io_context() noexcept
Get access to io_context object.
Definition: ioctx_on_thread_pool.hpp:33
restinio::impl::own_io_context_for_thread_pool_t::own_io_context_for_thread_pool_t
own_io_context_for_thread_pool_t()=default
restinio::impl::own_io_context_for_thread_pool_t::m_ioctx
asio_ns::io_context m_ioctx
Definition: ioctx_on_thread_pool.hpp:27
exception.hpp
restinio
Definition: asio_include.hpp:21
std
STL namespace.
const
#define const
Definition: zconf.h:230