1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2022 Gothel Software e.K.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef JAU_SERVICE_RUNNER_HPP_
#define JAU_SERVICE_RUNNER_HPP_
#include <cstring>
#include <string>
#include <cstdint>
#include <limits>
#include <atomic>
#include <memory>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <algorithm>
#include <jau/cpp_lang_util.hpp>
#include <jau/ordered_atomic.hpp>
#include <jau/fraction_type.hpp>
#include <jau/functional.hpp>
extern "C" {
#include <unistd.h>
#include <pthread.h>
}
namespace jau {
/** \addtogroup Concurrency
*
* @{
*/
/**
* Service runner, a reusable dedicated thread performing custom user services.
*/
class service_runner {
public:
typedef service_runner& service_runner_ref;
typedef function<void(service_runner_ref)> Callback;
static const ::pid_t pid_self;
private:
std::string name_;
/**
* Maximum duration in fractions of seconds to wait for service to stop at stop() and join(), where fractions_i64::zero waits infinitely
*/
fraction_i64 service_shutdown_timeout_;
Callback service_work;
Callback service_init_locked;
Callback service_end_locked;
std::mutex mtx_shall_stop_;
std::condition_variable cv_shall_stop_;
jau::sc_atomic_bool shall_stop_;
jau::sc_atomic_bool running;
::pthread_t thread_id_;
std::mutex mtx_lifecycle;
std::condition_variable cv_init;
void service_thread();
static bool install_sighandler() noexcept;
public:
/**
* Remove the sighandler
*/
static bool remove_sighandler() noexcept;
/**
* Install the singleton SIGALRM sighandler instance.
* - First call will install the sighandler
* - Should be called at least once within the application using jau::service_runner
*/
static bool singleton_sighandler() noexcept {
/**
* Thread safe starting with C++11 6.7:
*
* If control enters the declaration concurrently while the variable is being initialized,
* the concurrent execution shall wait for completion of the initialization.
*
* (Magic Statics)
*
* Avoiding non-working double checked locking.
*/
static bool r = install_sighandler();
return r;
}
/**
* Service runner constructor.
*
* start() shall be issued to kick off this service.
*
* @param name service name
* @param service_shutdown_timeout maximum duration in fractions of seconds to wait for service to stop at stop() and join(), where fractions_i64::zero waits infinitely
* @param service_work service working function
* @param service_init_locked optional service init function, lifecycle mutex is locked
* @param service_end_locked optional service end function, lifecycle mutex is locked
*/
service_runner(std::string name,
fraction_i64 service_shutdown_timeout,
Callback service_work,
Callback service_init_locked = Callback(),
Callback service_end_locked = Callback()) noexcept;
/**
* Service runner destructor.
*
* Issues stop()
*/
~service_runner() noexcept;
/**
* Return the given name of this service
*/
const std::string& name() const noexcept { return name_; }
/**
* Returns maximum duration in fractions of seconds to wait for service to stop at stop() and join(), where fractions_i64::zero waits infinitely
* @see stop()
* @see join()
*/
fraction_i64 service_shutdown_timeout() const noexcept { return service_shutdown_timeout_; }
/**
* Return the thread-id of this service service thread, zero if not running.
*/
pthread_t thread_id() const noexcept { return thread_id_; }
/**
* Returns true if service is running
*
* @see start()
* @see stop()
* @see join()
*/
bool is_running() const noexcept { return running; }
/**
* Returns true if service shall stop.
*
* This flag can be used by the service_work Callback to determine whether to skip lengthly tasks,
* or even to skip stopping this service (again).
*
* @see is_running()
* @see set_shall_stop()
* @see mtx_shall_stop()
* @see cv_shall_stop()
* @see start()
* @see stop()
* @see join()
*/
bool shall_stop() const noexcept { return shall_stop_; }
/**
* Helper function to easy FunctionDef usage w/o creating a lambda alike capture
* with same semantics as shall_stop().
*
* The const qualifier has also been lifted, since free function pointer declarations can't match it.
*
* @param dummy a dummy argument to help FunctionDef template parameter pack, not used
* @see shall_stop()
*/
bool shall_stop2(int dummy) /* const */ noexcept { (void)dummy; return shall_stop_; }
/**
* Marks the service thread to stop in due process by flagging `shall stop` to `true`.
* @see is_running()
* @see shall_stop()
* @see mtx_shall_stop()
* @see cv_shall_stop()
* @see start()
* @see stop()
* @see join()
*/
void set_shall_stop() noexcept;
/** mtx_shall_stop() and cv_shall_stop() allows caller to be notified when shall_stop() changes, i.e. start(), set_shall_stop() or stop() is called. */
std::mutex& mtx_shall_stop() noexcept { return mtx_shall_stop_; }
/** mtx_shall_stop() and cv_shall_stop() allows caller to be notified when shall_stop() changes, i.e. start(), set_shall_stop() or stop() is called. */
std::condition_variable& cv_shall_stop() noexcept { return cv_shall_stop_; }
/**
* Starts this service, if not running already.
*
* Methods blocks the current thread until service is started.
*
* @see is_running()
* @see shall_stop()
* @see set_shall_stop()
* @see mtx_shall_stop()
* @see cv_shall_stop()
* @see stop()
* @see join()
* @see service_shutdown_timeout()
*/
void start() noexcept;
/**
* Stops this service, if running.
*
* If called from the service thread, method just issues set_shall_stop() without blocking,
* otherwise methods blocks the current thread until service is stopped.
*
* Maximum blocked wait period is optionally limited by service_shutdown_timeout().
*
* Method attempts to stop the service thread
* - by flagging `shall stop` via set_shall_stop()
* - if not called from the service thread itself:
* - sending `SIGALRM` to the service thread
* - waiting until service thread has stopped or timeout occurred
*
* Implementation requires a `SIGALRM` handler to be install,
* e.g. using singleton_sighandler().
*
* @returns true if thread has been stopped or false if timeout has been hit
*
* @see is_running()
* @see shall_stop()
* @see set_shall_stop()
* @see mtx_shall_stop()
* @see cv_shall_stop()
* @see start()
* @see join()
* @see service_shutdown_timeout()
* @see singleton_sighandler()
*/
bool stop() noexcept;
/**
* Blocks the current thread until service is stopped
* or returns immediately if not running or called from the service thread.
*
* Maximum blocked wait period is optionally limited by service_shutdown_timeout().
*
* @returns true if thread has been stopped or false if timeout has been hit
* @see is_running()
* @see shall_stop()
* @see set_shall_stop()
* @see mtx_shall_stop()
* @see cv_shall_stop()
* @see start()
* @see stop()
* @see service_shutdown_timeout()
*/
bool join() noexcept;
/**
* Returns a string representation of this service
*/
std::string toString() const noexcept;
};
/**@}*/
} // namespace jau
#endif /* JAU_SERVICE_RUNNER_HPP_ */
|