Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
sys
Runner.hpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2013-2019 Swirly Cloud Limited
3
// Copyright (C) 2022 Reactive Markets Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#ifndef TOOLBOX_SYS_RUNNER_HPP
18
#define TOOLBOX_SYS_RUNNER_HPP
19
20
#include <
toolbox/sys/Log.hpp
>
21
#include <
toolbox/sys/Signal.hpp
>
22
#include <
toolbox/sys/Thread.hpp
>
23
24
namespace
toolbox
{
25
inline
namespace
sys {
26
27
namespace
{
28
template
<
typename
T>
29
// clang-format off
30
concept
Runnable
=
requires
(T
r
) {
31
{
r
.run() } -> std::convertible_to<bool>;
32
r
.stop();
33
};
34
// clang-format on
35
}
// namespace
36
37
template
<
typename
RunnableT>
38
requires
Runnable<RunnableT>
39
class
Runner
{
40
public
:
41
Runner
(
RunnableT
&
r
,
ThreadConfig
config
)
42
: r_{
r
}
43
, thread_{run,
std
::
ref
(
r
),
config
}
44
{
45
}
46
~Runner
()
47
{
48
r_.stop();
49
thread_.join();
50
}
51
52
// Copy.
53
Runner
(
const
Runner
&) =
delete
;
54
Runner
&
operator=
(
const
Runner
&) =
delete
;
55
56
// Move.
57
Runner
(
Runner
&&) =
delete
;
58
Runner
&
operator=
(
Runner
&&) =
delete
;
59
60
private
:
61
static
void
run(
RunnableT
&
r
,
ThreadConfig
config
)
62
{
63
sig_block_all
();
64
try
{
65
set_thread_attrs
(
config
);
66
TOOLBOX_NOTICE
<<
"started "
<<
config
.name <<
" thread"
;
67
// The run() function returns false when the runnable is stopped.
68
while
(
r
.run()) {
69
}
70
}
catch
(
const
std::exception& e) {
71
TOOLBOX_CRIT
<<
"exception on "
<<
config
.name <<
" thread: "
<< e.what();
72
kill
(
getpid
(),
SIGTERM
);
73
}
74
TOOLBOX_NOTICE
<<
"stopping "
<<
config
.name <<
" thread"
;
75
}
76
77
RunnableT
& r_;
78
std::thread thread_;
79
};
80
81
}
// namespace sys
82
}
// namespace toolbox
83
84
#endif
// TOOLBOX_SYS_RUNNER_HPP
Log.hpp
TOOLBOX_CRIT
#define TOOLBOX_CRIT
Definition
Log.hpp:92
TOOLBOX_NOTICE
#define TOOLBOX_NOTICE
Definition
Log.hpp:96
Signal.hpp
Thread.hpp
toolbox::Runner
Definition
Runner.hpp:39
toolbox::sys::Runner::Runner
Runner(Runner &&)=delete
toolbox::sys::Runner::Runner
Runner(const Runner &)=delete
toolbox::sys::Runner::operator=
Runner & operator=(const Runner &)=delete
toolbox::sys::Runner::operator=
Runner & operator=(Runner &&)=delete
toolbox::sys::Runner::Runner
Runner(RunnableT &r, ThreadConfig config)
Definition
Runner.hpp:41
toolbox::sys::Runner::~Runner
~Runner()
Definition
Runner.hpp:46
toolbox::sys::@150::Runnable
Definition
Runner.hpp:30
std
STL namespace.
toolbox::sys::set_thread_attrs
void set_thread_attrs(const ThreadConfig &config)
Definition
Thread.cpp:70
toolbox::sys::sig_block_all
void sig_block_all()
Block all signals.
Definition
Signal.cpp:82
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
toolbox::sys::ThreadConfig
ThreadConfig holds the thread attributes.
Definition
Thread.hpp:29
Generated by
1.9.8