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/internal_tag_fwd.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 {
38 public:
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,
45 void (Class::*func)()) {
46 auto execute = [obj, func, signum](int sig) {
47 if (sig == signum) {
48 (obj->*func)();
49 }
50 };
51
52 return channel_.AddListener(concurrent::FunctionId(obj), name,
53 std::move(execute));
54 }
55
56 /// Listen for all the OS signals.
57 template <class Class>
58 Subscriber AddListener(Class* obj, std::string_view name,
59 void (Class::*func)(int /*signum*/)) {
60 auto execute = [obj, func](int sig) { (obj->*func)(sig); };
61 return channel_.AddListener(concurrent::FunctionId(obj), name,
62 std::move(execute));
63 }
64
65 /// @cond
66 // For internal use
67 void Notify(int signum, utils::InternalTag);
68 /// @endcond
69 private:
70 concurrent::AsyncEventChannel<int> channel_;
71 engine::TaskProcessor& task_processor_;
72};
73
74} // namespace os_signals
75
76USERVER_NAMESPACE_END