RESTinio

ioctx_on_thread_pool.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <thread>
4
5#include <restinio/asio_include.hpp>
6
7#include <restinio/exception.hpp>
8
9 namespace restinio
10{
11
12 namespace impl
13{
14
25 class own_io_context_for_thread_pool_t
26{
27 asio_ns::io_context m_ioctx;
28
29 public:
30 own_io_context_for_thread_pool_t() = default;
31
33 auto & io_context() noexcept { return m_ioctx; }
34};
35
46 class external_io_context_for_thread_pool_t
47{
48 asio_ns::io_context & m_ioctx;
49
50 public:
52 external_io_context_for_thread_pool_t(
54 asio_ns::io_context & ioctx )
55 : m_ioctx{ ioctx }
56 {}
57
59 auto & io_context() noexcept { return m_ioctx; }
60};
61
72 template< typename Io_Context_Holder >
73 class ioctx_on_thread_pool_t
74{
75 public:
76 ioctx_on_thread_pool_t( const ioctx_on_thread_pool_t & ) = delete;
77 ioctx_on_thread_pool_t( ioctx_on_thread_pool_t && ) = delete;
78
79 template< typename... Io_Context_Holder_Ctor_Args >
80 ioctx_on_thread_pool_t(
81 // Pool size.
82 //FIXME: better to use not_null from gsl.
83 std::size_t pool_size,
84 // Optional arguments for Io_Context_Holder instance.
85 Io_Context_Holder_Ctor_Args && ...ioctx_holder_args )
86 : m_ioctx_holder{
87 std::forward<Io_Context_Holder_Ctor_Args>(ioctx_holder_args)... }
88 , m_pool( pool_size )
89 , m_status( status_t::stopped )
90 {}
91
92 // Makes sure the pool is stopped.
93 ~ioctx_on_thread_pool_t()
94 {
95 if( started() )
96 {
97 stop();
98 wait();
99 }
100 }
101
102 void
103 start()
104 {
105 if( started() )
106 {
107 throw exception_t{
108 "io_context_with_thread_pool is already started" };
109 }
110
111 try
112 {
113 std::generate(
114 begin( m_pool ),
115 end( m_pool ),
116 [this]{
117 return
118 std::thread{ [this] {
119 auto work{ asio_ns::make_work_guard(
120 m_ioctx_holder.io_context() ) };
121
122 m_ioctx_holder.io_context().run();
123 } };
124 } );
125
126 // When all thread started successfully
127 // status can be changed.
128 m_status = status_t::started;
129 }
130 catch( const std::exception & )
131 {
132 io_context().stop();
133 for( auto & t : m_pool )
134 if( t.joinable() )
135 t.join();
136
137 throw;
138 }
139 }
140
141 // NOTE: this method is marked as noexcept in v.0.6.7.
142 // It's because this method can be called from destructors and
143 // there is no way to recover from an exception thrown from
144 // this method.
145 void
146 stop() noexcept
147 {
148 if( started() )
149 {
150 io_context().stop();
151 }
152 }
153
154 // NOTE: this method is marked as noexcept in v.0.6.7.
155 // It's because this method can be called from destructors and
156 // there is no way to recover from an exception thrown from
157 // this method.
158 void
159 wait() noexcept
160 {
161 if( started() )
162 {
163 for( auto & t : m_pool )
164 t.join();
165
166 // When all threads are stopped status can be changed.
167 m_status = status_t::stopped;
168 }
169 }
170
171 bool started() const noexcept { return status_t::started == m_status; }
172
173 asio_ns::io_context &
174 io_context() noexcept
175 {
176 return m_ioctx_holder.io_context();
177 }
178
179 private:
180 enum class status_t : std::uint8_t { stopped, started };
181
182 Io_Context_Holder m_ioctx_holder;
183 std::vector< std::thread > m_pool;
184 status_t m_status;
185};
186
187} /* namespace impl */
188
189} /* namespace restinio */
190
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