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 you to subscribe
31/// to OS signals `SIGUSR1` and `SIGUSR2`.
32///
33/// Usually retrieved from os_signals::ProcessorComponent. 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 /// @brief Listens for a specific signal during the resource scope lifetime.
42 /// @param scopes storage that owns the subscription lifetime.
43 /// See @ref resource_scopes_parameter "where to obtain scopes".
44 template <class Class>
46 utils::ResourceScopeStorage& scopes,
47 Class* obj,
48 std::string_view name,
49 int signum,
50 void (Class::*func)()
51 ) {
52 channel_.AddListener(scopes, concurrent::FunctionId(obj), name, [obj, func, signum](int sig) {
53 if (sig == signum) {
54 (obj->*func)();
55 }
56 });
57 }
58
59 /// @brief Listens for all OS signals during the resource scope lifetime.
60 /// @param scopes storage that owns the subscription lifetime.
61 /// See @ref resource_scopes_parameter "where to obtain scopes".
62 template <class Class>
63 void AddListener(utils::ResourceScopeStorage& scopes, Class* obj, std::string_view name, void (Class::*func)(int)) {
64 channel_.AddListener(scopes, obj, name, func);
65 }
66
67#ifndef ARCADIA_ROOT
68 /// Listen for a specific signal `signum`. See the other overload for
69 /// subscription to both signals.
70 /// @deprecated Use the overload that takes @ref utils::ResourceScopeStorage.
71 template <class Class>
72 Subscriber AddListener(Class* obj, std::string_view name, int signum, void (Class::*func)()) {
73 auto execute = [obj, func, signum](int sig) {
74 if (sig == signum) {
75 (obj->*func)();
76 }
77 };
78
79 return channel_.AddListener(concurrent::FunctionId(obj), name, std::move(execute));
80 }
81
82 /// Listen for all the OS signals.
83 /// @deprecated Use the overload that takes @ref utils::ResourceScopeStorage.
84 template <class Class>
85 Subscriber AddListener(Class* obj, std::string_view name, void (Class::*func)(int /*signum*/)) {
86 auto execute = [obj, func](int sig) { (obj->*func)(sig); };
87 return channel_.AddListener(concurrent::FunctionId(obj), name, std::move(execute));
88 }
89
90#endif
91
92 /// @cond
93 // For internal use
94 void Notify(int signum, utils::impl::InternalTag);
95 /// @endcond
96private:
97 concurrent::AsyncEventChannel<int> channel_;
98 engine::TaskProcessor& task_processor_;
99};
100
101} // namespace os_signals
102
103USERVER_NAMESPACE_END