RESTinio
Toggle main menu visibility
Loading...
Searching...
No Matches
restinio
impl
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
15
/*!
16
* \brief A class for holding actual instance of Asio's io_context.
17
*
18
* \note
19
* This class is intended to be used as template argument for
20
* ioctx_on_thread_pool_t template.
21
*
22
* \since
23
* v.0.4.2
24
*/
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
32
//! Get access to io_context object.
33
auto
&
io_context
()
noexcept
{
return
m_ioctx
; }
34
};
35
36
/*!
37
* \brief A class for holding a reference to external Asio's io_context.
38
*
39
* \note
40
* This class is intended to be used as template argument for
41
* ioctx_on_thread_pool_t template.
42
*
43
* \since
44
* v.0.4.2
45
*/
46
class
external_io_context_for_thread_pool_t
47
{
48
asio_ns::io_context &
m_ioctx
;
49
50
public
:
51
//! Initializing constructor.
52
external_io_context_for_thread_pool_t
(
53
//! External io_context to be used.
54
asio_ns::io_context & ioctx )
55
:
m_ioctx
{ ioctx }
56
{}
57
58
//! Get access to io_context object.
59
auto
&
io_context
()
noexcept
{
return
m_ioctx
; }
60
};
61
62
namespace
pool_size_checking
63
{
64
65
[[
nodiscard
]]
66
inline
std::size_t
67
ensure_pool_size_non_zero
( std::size_t pool_size )
68
{
69
if
( !pool_size )
70
throw
exception_t
{
"pool_size can't be 0"
}
;
71
72
return
pool_size;
73
}
74
75
}
/* namespace pool_size_checking */
76
77
/*!
78
* Helper class for creating io_context and running it
79
* (via `io_context::run()`) on a thread pool.
80
*
81
* \note class is not thread-safe (except `io_context()` method).
82
* Expected usage scenario is to start and stop it on the same thread.
83
*
84
* \tparam Io_Context_Holder A type which actually holds io_context object
85
* or a reference to an external io_context object.
86
*/
87
template
<
typename
Io_Context_Holder >
88
class
ioctx_on_thread_pool_t
89
{
90
public
:
91
ioctx_on_thread_pool_t
(
const
ioctx_on_thread_pool_t
& ) =
delete
;
92
ioctx_on_thread_pool_t
(
ioctx_on_thread_pool_t
&& ) =
delete
;
93
94
template
<
typename
... Io_Context_Holder_Ctor_Args >
95
ioctx_on_thread_pool_t
(
96
// Pool size.
97
std::size_t pool_size,
98
// Optional arguments for Io_Context_Holder instance.
99
Io_Context_Holder_Ctor_Args && ...ioctx_holder_args )
100
:
m_ioctx_holder
{
101
std::forward<Io_Context_Holder_Ctor_Args>(ioctx_holder_args)... }
102
,
m_pool
(
pool_size_checking
::
ensure_pool_size_non_zero
(
pool_size
) )
103
,
m_status
(
status_t
::stopped )
104
{}
105
106
// Makes sure the pool is stopped.
107
~
ioctx_on_thread_pool_t
()
108
{
109
if
(
started
(
)
)
110
{
111
stop
(
)
;
112
wait
(
)
;
113
}
114
}
115
116
void
117
start
()
118
{
119
if
(
started
(
)
)
120
{
121
throw
exception_t
{
122
"io_context_with_thread_pool is already started"
}
;
123
}
124
125
try
126
{
127
std::generate(
128
begin( m_pool ),
129
end( m_pool ),
130
[
this
]{
131
return
132
std::thread{ [
this
] {
133
auto
work{ asio_ns::make_work_guard(
134
m_ioctx_holder
.io_context() ) };
135
136
m_ioctx_holder
.io_context().run();
137
} };
138
} );
139
140
// When all thread started successfully
141
// status can be changed.
142
m_status
=
status_t
::started;
143
}
144
catch
(
const
std::exception & )
145
{
146
io_context
(
)
.stop();
147
for
(
auto
& t : m_pool )
148
if
( t.joinable() )
149
t.join();
150
151
throw
;
152
}
153
}
154
155
// NOTE: this method is marked as noexcept in v.0.6.7.
156
// It's because this method can be called from destructors and
157
// there is no way to recover from an exception thrown from
158
// this method.
159
void
160
stop
()
noexcept
161
{
162
if
(
started
(
)
)
163
{
164
io_context
(
)
.stop();
165
}
166
}
167
168
// NOTE: this method is marked as noexcept in v.0.6.7.
169
// It's because this method can be called from destructors and
170
// there is no way to recover from an exception thrown from
171
// this method.
172
void
173
wait
()
noexcept
174
{
175
if
(
started
(
)
)
176
{
177
for
(
auto
& t : m_pool )
178
t.join();
179
180
// When all threads are stopped status can be changed.
181
m_status
=
status_t
::stopped;
182
}
183
}
184
185
bool
started
()
const
noexcept
{
return
status_t
::started ==
m_status
; }
186
187
asio_ns::io_context &
188
io_context
()
noexcept
189
{
190
return
m_ioctx_holder
.io_context();
191
}
192
193
private
:
194
enum
class
status_t
:
std
::
uint8_t
{
stopped
,
started
};
195
196
Io_Context_Holder
m_ioctx_holder
;
197
std
::
vector
<
std
::
thread
>
m_pool
;
198
status_t
m_status
;
199
};
200
201
}
/* namespace impl */
202
203
}
/* namespace restinio */
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition
exception.hpp:26
restinio::exception_t::exception_t
exception_t(const char *err)
Definition
exception.hpp:29
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:89
restinio::impl::ioctx_on_thread_pool_t::io_context
asio_ns::io_context & io_context() noexcept
Definition
ioctx_on_thread_pool.hpp:188
restinio::impl::ioctx_on_thread_pool_t::status_t
status_t
Definition
ioctx_on_thread_pool.hpp:194
restinio::impl::ioctx_on_thread_pool_t::status_t::started
@ started
Definition
ioctx_on_thread_pool.hpp:194
restinio::impl::ioctx_on_thread_pool_t::status_t::stopped
@ stopped
Definition
ioctx_on_thread_pool.hpp:194
restinio::impl::ioctx_on_thread_pool_t::started
bool started() const noexcept
Definition
ioctx_on_thread_pool.hpp:185
restinio::impl::ioctx_on_thread_pool_t::wait
void wait() noexcept
Definition
ioctx_on_thread_pool.hpp:173
restinio::impl::ioctx_on_thread_pool_t::stop
void stop() noexcept
Definition
ioctx_on_thread_pool.hpp:160
restinio::impl::ioctx_on_thread_pool_t::start
void start()
Definition
ioctx_on_thread_pool.hpp:117
restinio::impl::ioctx_on_thread_pool_t::m_pool
std::vector< std::thread > m_pool
Definition
ioctx_on_thread_pool.hpp:197
restinio::impl::ioctx_on_thread_pool_t::~ioctx_on_thread_pool_t
~ioctx_on_thread_pool_t()
Definition
ioctx_on_thread_pool.hpp:107
restinio::impl::ioctx_on_thread_pool_t::m_status
status_t m_status
Definition
ioctx_on_thread_pool.hpp:198
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:196
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:95
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
restinio::impl::pool_size_checking
Definition
ioctx_on_thread_pool.hpp:63
restinio::impl::pool_size_checking::ensure_pool_size_non_zero
std::size_t ensure_pool_size_non_zero(std::size_t pool_size)
Definition
ioctx_on_thread_pool.hpp:67
restinio::impl
Definition
sendfile_operation_default.ipp:15
restinio
Definition
sendfile_operation_default.ipp:12
Generated by
1.17.0