userver: userver/os_signals/processor.hpp Source File
Loading...
Searching...
No Matches
processor.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/os_signals/processor.hpp
4/// @brief @copybrief os_signals::Processor
5
6#include <userver/concurrent/async_event_channel.hpp>
7#include <userver/engine/task/task_processor_fwd.hpp>
8#include <userver/os_signals/subscriber.hpp>
9#include <userver/utils/impl/internal_tag.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace os_signals {
14
15#if !defined(__APPLE__)
16
17/// @brief Constant for SIGUSR1
18inline constexpr int kSigUsr1 = 10;
19
20/// @brief Constant for SIGUSR2
21inline constexpr int kSigUsr2 = 12;
22
23#else
24inline constexpr int kSigUsr1 = 30;
25inline constexpr int kSigUsr2 = 31;
26#endif
27
28/// @ingroup userver_clients
29///
30/// @brief A client that allows to subscribe
31/// to OS signals `SIGUSR1 and `SIGUSR2`.
32///
33/// Usually retrieved from os_signals::ProcessorComponent component. For tests
34/// use os_signals::ProcessorMock.
35///
36/// @see @ref scripts/docs/en/userver/os_signals.md
37class Processor final {
38public:
39 explicit Processor(engine::TaskProcessor& task_processor);
40
41 /// Listen for a specific signal `signum`. See the other overload for
42 /// subscription to both signals.
43 template <class Class>
44 Subscriber AddListener(Class* obj, std::string_view name, int signum, void (Class::*func)()) {
45 auto execute = [obj, func, signum](int sig) {
46 if (sig == signum) {
47 (obj->*func)();
48 }
49 };
50
51 return channel_.AddListener(concurrent::FunctionId(obj), name, std::move(execute));
52 }
53
54 /// Listen for all the OS signals.
55 template <class Class>
56 Subscriber AddListener(Class* obj, std::string_view name, void (Class::*func)(int /*signum*/)) {
57 auto execute = [obj, func](int sig) { (obj->*func)(sig); };
58 return channel_.AddListener(concurrent::FunctionId(obj), name, std::move(execute));
59 }
60
61 /// @cond
62 // For internal use
63 void Notify(int signum, utils::impl::InternalTag);
64 /// @endcond
65private:
66 concurrent::AsyncEventChannel<int> channel_;
67 engine::TaskProcessor& task_processor_;
68};
69
70} // namespace os_signals
71
72USERVER_NAMESPACE_END