Branch data Line data Source code
1 : : // Copyright (c) The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef MP_PROXY_IO_H
6 : : #define MP_PROXY_IO_H
7 : :
8 : : #include <mp/proxy.h>
9 : : #include <mp/util.h>
10 : :
11 : : #include <mp/proxy.capnp.h>
12 : :
13 : : #include <capnp/rpc-twoparty.h>
14 : :
15 : : #include <any>
16 : : #include <assert.h>
17 : : #include <algorithm>
18 : : #include <condition_variable>
19 : : #include <cstdlib>
20 : : #include <functional>
21 : : #include <kj/function.h>
22 : : #include <kj/io.h>
23 : : #include <map>
24 : : #include <memory>
25 : : #include <optional>
26 : : #include <ranges>
27 : : #include <sstream>
28 : : #include <string>
29 : : #include <thread>
30 : :
31 : : namespace mp {
32 : : struct ThreadContext;
33 : : struct Listener;
34 : :
35 : : struct InvokeContext
36 : : {
37 : : Connection& connection;
38 : : };
39 : :
40 : : struct ClientInvokeContext : InvokeContext
41 : : {
42 : : ThreadContext& thread_context;
43 : 51 : ClientInvokeContext(Connection& conn, ThreadContext& thread_context)
44 [ + - + - : 51 : : InvokeContext{conn}, thread_context{thread_context}
+ - + - +
- + - ][ +
- + - # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ + - -
- - - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
45 : : {
46 : : }
47 : : };
48 : :
49 : : template <typename ProxyServer, typename CallContext_>
50 : : struct ServerInvokeContext : InvokeContext
51 : : {
52 : : using CallContext = CallContext_;
53 : :
54 : : ProxyServer& proxy_server;
55 : : CallContext& call_context;
56 : : int req;
57 : : //! For IPC methods that execute asynchronously, not on the event-loop
58 : : //! thread: lock preventing the event-loop thread from freeing the params or
59 : : //! results structs if the request is canceled while the worker thread is
60 : : //! reading params (`call_context.getParams()`) or writing results
61 : : //! (`call_context.getResults()`).
62 : : Lock* cancel_lock{nullptr};
63 : : //! For IPC methods that execute asynchronously, not on the event-loop
64 : : //! thread, this is set to true if the IPC call was canceled by the client
65 : : //! or canceled by a disconnection. If the call runs on the event-loop
66 : : //! thread, it can't be canceled. This should be accessed with cancel_lock
67 : : //! held if it is not null, since in the asynchronous case it is accessed
68 : : //! from multiple threads.
69 : : bool request_canceled{false};
70 : :
71 : 83 : ServerInvokeContext(ProxyServer& proxy_server, CallContext& call_context, int req)
72 [ + - + - : 83 : : InvokeContext{*proxy_server.m_context.connection}, proxy_server{proxy_server}, call_context{call_context}, req{req}
+ - + - +
- + - + -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- - - - -
- - + - ]
[ + - + -
+ - - - -
- - - + -
+ - ][ # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
73 : : {
74 : : }
75 : : };
76 : :
77 : : template <typename Interface, typename Params, typename Results>
78 : : using ServerContext = ServerInvokeContext<ProxyServer<Interface>, ::capnp::CallContext<Params, Results>>;
79 : :
80 : : template <>
81 : : struct ProxyClient<Thread> : public ProxyClientBase<Thread, ::capnp::Void>
82 : : {
83 [ + - ]: 56 : using ProxyClientBase::ProxyClientBase;
84 : : // https://stackoverflow.com/questions/22357887/comparing-two-mapiterators-why-does-it-need-the-copy-constructor-of-stdpair
85 : : ProxyClient(const ProxyClient&) = delete;
86 : : ~ProxyClient();
87 : :
88 : : //! Reference to callback function that is run if there is a sudden
89 : : //! disconnect and the Connection object is destroyed before this
90 : : //! ProxyClient<Thread> object. The callback will destroy this object and
91 : : //! remove its entry from the thread's request_threads or callback_threads
92 : : //! map. It will also reset m_disconnect_cb so the destructor does not
93 : : //! access it. In the normal case where there is no sudden disconnect, the
94 : : //! destructor will unregister m_disconnect_cb so the callback is never run.
95 : : //! Since this variable is accessed from multiple threads, accesses should
96 : : //! be guarded with the associated Waiter::m_mutex.
97 : : std::optional<CleanupIt> m_disconnect_cb;
98 : : };
99 : :
100 : : template <>
101 : : struct ProxyServer<Thread> final : public Thread::Server
102 : : {
103 : : public:
104 : : ProxyServer(Connection& connection, ThreadContext& thread_context, std::thread&& thread);
105 : : ~ProxyServer();
106 : : kj::Promise<void> getName(GetNameContext context) override;
107 : :
108 : : //! Run a callback function fn returning T on this thread. The function will
109 : : //! be queued and executed as soon as the thread is idle, and when fn
110 : : //! returns, the promise returned by this method will be fulfilled with the
111 : : //! value fn returned.
112 : : template<typename T, typename Fn>
113 : : kj::Promise<T> post(Fn&& fn);
114 : :
115 : : EventLoopRef m_loop;
116 : : ThreadContext& m_thread_context;
117 : : std::thread m_thread;
118 : : //! Promise signaled when m_thread_context.waiter is ready and there is no
119 : : //! post() callback function waiting to execute.
120 : : kj::Promise<void> m_thread_ready{kj::READY_NOW};
121 : : };
122 : :
123 : : //! Handler for kj::TaskSet failed task events.
124 : : class LoggingErrorHandler : public kj::TaskSet::ErrorHandler
125 : : {
126 : : public:
127 [ + - ]: 43 : LoggingErrorHandler(EventLoop& loop) : m_loop(loop) {}
128 : : void taskFailed(kj::Exception&& exception) override;
129 : : EventLoop& m_loop;
130 : : };
131 : :
132 : : //! Log flags. Update stringify function if changed!
133 : : enum class Log {
134 : : Trace = 0,
135 : : Debug,
136 : : Info,
137 : : Warning,
138 : : Error,
139 : : Raise,
140 : : };
141 : :
142 : : kj::StringPtr KJ_STRINGIFY(Log flags);
143 : :
144 [ + - ]: 1632 : struct LogMessage {
[ # # # # ]
145 : :
146 : : //! Message to be logged
147 : : std::string message;
148 : :
149 : : //! The severity level of this message
150 : : Log level;
151 : : };
152 : :
153 : : using LogFn = std::function<void(LogMessage)>;
154 : :
155 [ + - ]: 51 : struct LogOptions {
156 : :
157 : : //! External logging callback.
158 : : LogFn log_fn;
159 : :
160 : : //! Maximum number of characters to use when representing
161 : : //! request and response structs as strings.
162 : : size_t max_chars{200};
163 : :
164 : : //! Messages with a severity level less than log_level will not be
165 : : //! reported.
166 : : Log log_level{Log::Trace};
167 : : };
168 : :
169 : : class Logger
170 : : {
171 : : public:
172 [ - - - - : 644 : Logger(const LogOptions& options, Log log_level) : m_options(options), m_log_level(log_level) {}
+ - + - -
- - - + -
+ - - - -
- + - + -
- - - - +
- + - - -
- - + - +
- - - - -
+ - + - -
- - - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ # #
# # # # ]
[ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - -
- ][ - - -
- + - + -
- - - - +
- + - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - ][ - -
- - - - -
- - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - -
- - - + -
+ - # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
173 : :
174 : : Logger(Logger&&) = delete;
175 : : Logger& operator=(Logger&&) = delete;
176 : : Logger(const Logger&) = delete;
177 : : Logger& operator=(const Logger&) = delete;
178 : :
179 : 920 : ~Logger() noexcept(false)
180 : : {
181 [ + - ]: 1632 : if (enabled()) m_options.log_fn({std::move(m_buffer).str(), m_log_level});
182 [ + - ]: 1736 : }
183 : :
184 : : template <typename T>
185 : 4562 : friend Logger& operator<<(Logger& logger, T&& value)
186 : : {
187 [ - + ]: 4562 : if (logger.enabled()) logger.m_buffer << std::forward<T>(value);
188 : 4562 : return logger;
189 : : }
190 : :
191 : : template <typename T>
192 : : friend Logger& operator<<(Logger&& logger, T&& value)
193 : : {
194 : : return logger << std::forward<T>(value);
195 : : }
196 : :
197 : 920 : explicit operator bool() const
198 : : {
199 [ - - - - : 1306 : return enabled();
- - - - -
- - - + -
+ - + - +
- + - + -
- - - - -
- - - - -
- - + - +
- + - + -
+ - + - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - + - +
- + - + -
+ - + - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + -
- - - - -
- - - - -
+ - + - +
- + - - -
- - - - -
- - - + -
+ - + - +
- - - - -
- - - - -
- + - + -
+ - + - -
- - - - -
- - - - +
- + - + -
+ - - - -
- - - - -
- - + - +
- + - + -
- - - - -
- - - - -
+ - + - +
- + - - -
- - - - -
- - - + -
+ - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - - - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- + - + -
+ - + - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ # #
# # # # #
# # # # #
# # ][ + -
+ - # # #
# ][ + - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - + -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - + - -
- - - - -
- - + - +
- - - - -
- - - - -
- + - + -
+ - + - -
- - - - -
- - + - +
- + - - -
- - - - -
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - - -
- - - - -
- - - + -
+ - ][ - -
- - - - +
- - - - -
- - - - -
- - - + -
- - - - -
- - - + -
+ - - - -
- - - - -
- - + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - -
- - - - -
+ - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
- - - - -
- - - - -
+ - + - +
- + - - -
- - - - -
- - - + -
+ - ][ - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - + - +
- # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - - - -
- + - - -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - + - -
- + - ][ -
- - - + -
+ - + - +
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
200 : : }
201 : :
202 : : private:
203 : 6402 : bool enabled() const
204 : : {
205 [ - - - - : 6402 : return m_options.log_fn && m_log_level >= m_options.log_level;
- - - - -
- - - + -
+ - + - +
- + - + -
- - - - +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - - -
- - + - +
- + - + -
+ - + - +
- + - - -
- - - - -
- - - - -
- - - - +
- + - + -
+ - + - +
- + - + -
- - - - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- - - - -
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - - - -
- + - + -
+ - + - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - + - +
- + - + -
- - - - -
- - - - -
- - + - +
- + - + -
+ - + - +
- + - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - - - -
- # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - + -
- - - - -
- - - - -
- - + - +
- + - + -
- - - - #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ - - -
- - - - -
- - - - +
+ - - - -
- - - - +
- + - - -
- - + + +
+ - - - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - + -
- - - - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - - -
- - - - +
- - - - -
- - - - +
- + - - -
- - + - +
- - - - -
+ - + - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - + -
- - - - -
- - - ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- + - + -
+ - + - +
- + - + -
+ - - - -
- # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
- - - - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
+ - + - +
- - - - ]
[ + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - + - +
- + - + -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - -
- - - - +
- + - + -
- - - - -
+ - + + #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - - -
- - - - -
- - - - -
- - - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- - - - -
- - + - +
- - - - -
- - - - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - + - +
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- + - + -
+ - + - -
- - - - -
- - - - -
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - - -
- - - - +
- + - + -
+ - - - +
- + - - -
- - + - +
- + - + -
- - - - -
- - - - -
- - + - +
- + - + -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - + -
+ - - - -
- - - - -
- - - - +
- + - + -
+ - - - -
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- + - + -
- - - - +
- + - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - + - +
- - - - -
- - - - -
- + - - +
+ - + + +
- + + + -
+ + - - -
- - - - -
- - # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ -
- - - - -
- - - - -
- - - - -
- - - - +
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - +
- + - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - + - +
- - - - -
- - - - -
- - - + -
+ - + - +
- + - + -
+ - + - -
- - - - -
- - - - -
- + - + -
+ - + - -
- - - # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - + - -
- - - - -
- - - - -
- - - - -
- - - - -
+ - + - +
- + - + -
+ - + - +
- - - - -
- - + - +
- + - + -
- - + - +
- + - + -
+ - + - +
- + - - +
+ - - + +
- - + + -
- + - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - - -
- - - + -
+ - - - -
- - - - -
- - - - +
- - + + -
- + + - -
+ + - - +
- - - - ]
206 : : }
207 : :
208 : : const LogOptions& m_options;
209 : : Log m_log_level;
210 : : std::ostringstream m_buffer;
211 : : };
212 : :
213 : : #define MP_LOGPLAIN(loop, ...) if (mp::Logger logger{(loop).m_log_opts, __VA_ARGS__}; logger) logger
214 : :
215 : : #define MP_LOG(loop, ...) MP_LOGPLAIN(loop, __VA_ARGS__) << "{" << LongThreadName((loop).m_exe_name) << "} "
216 : :
217 : : std::string LongThreadName(const char* exe_name);
218 : :
219 : : //! Wrap a socket file descriptor as an async stream, taking ownership of the fd.
220 : : Stream MakeStream(EventLoop&loop, SocketId socket);
221 : :
222 : : //! Event loop implementation.
223 : : //!
224 : : //! Cap'n Proto threading model is very simple: all I/O operations are
225 : : //! asynchronous and must be performed on a single thread. This includes:
226 : : //!
227 : : //! - Code starting an asynchronous operation (calling a function that returns a
228 : : //! promise object)
229 : : //! - Code notifying that an asynchronous operation is complete (code using a
230 : : //! fulfiller object)
231 : : //! - Code handling a completed operation (code chaining or waiting for a promise)
232 : : //!
233 : : //! All of this code needs to access shared state, and there is no mutex that
234 : : //! can be acquired to lock this state because Cap'n Proto
235 : : //! assumes it will only be accessed from one thread. So all this code needs to
236 : : //! actually run on one thread, and the EventLoop::loop() method is the entry point for
237 : : //! this thread. ProxyClient and ProxyServer objects that use other threads and
238 : : //! need to perform I/O operations post to this thread using the
239 : : //! EventLoop::sync() method.
240 : : //!
241 : : //! Specifically, because ProxyClient methods can be called from arbitrary
242 : : //! threads, and ProxyServer methods can run on arbitrary threads, ProxyClient
243 : : //! methods use the EventLoop thread to send requests, and ProxyServer methods
244 : : //! use the thread to return results.
245 : : //!
246 : : //! Based on https://groups.google.com/d/msg/capnproto/TuQFF1eH2-M/g81sHaTAAQAJ
247 : : class EventLoop
248 : : {
249 : : public:
250 : : //! Construct event loop object with default logging options.
251 : 1 : EventLoop(const char* exe_name, LogFn log_fn, void* context = nullptr)
252 [ + - ]: 2 : : EventLoop(exe_name, LogOptions{std::move(log_fn)}, context){}
253 : :
254 : : //! Construct event loop object with specified logging options.
255 : : EventLoop(const char* exe_name, LogOptions log_opts, void* context = nullptr);
256 : :
257 : : //! Backwards-compatible constructor for previous (deprecated) logging callback signature
258 : 1 : EventLoop(const char* exe_name, std::function<void(bool, std::string)> old_callback, void* context = nullptr)
259 : 1 : : EventLoop(exe_name,
260 [ - - + - : 138 : LogFn{[old_callback = std::move(old_callback)](LogMessage log_data) {old_callback(log_data.level == Log::Raise, std::move(log_data.message));}},
+ - ]
261 [ + - ]: 2 : context){}
262 : :
263 : : ~EventLoop();
264 : :
265 : : //! Run event loop. Does not return until shutdown. This should only be
266 : : //! called once from the m_thread_id thread. This will block until
267 : : //! the m_num_refs reference count is 0.
268 : : void loop();
269 : :
270 : : //! Run function on event loop thread. Does not return until function completes.
271 : : //! Must be called while the loop() function is active.
272 : : void sync(kj::FunctionParam<void()> fn);
273 : :
274 : : //! Register cleanup function to run on asynchronous worker thread without
275 : : //! blocking the event loop thread.
276 : : void addAsyncCleanup(std::function<void()> fn);
277 : :
278 : : //! Start asynchronous worker thread if necessary. This is only done if
279 : : //! there are ProxyServerBase::m_impl objects that need to be destroyed
280 : : //! asynchronously, without tying up the event loop thread. This can happen
281 : : //! when an interface does not declare a destroy() method that would allow
282 : : //! the client to wait for the destructor to finish and run it on a
283 : : //! dedicated thread. It can also happen whenever this is a broken
284 : : //! connection and the client is no longer around to call the destructors
285 : : //! and the server objects need to be garbage collected. In both cases, it
286 : : //! is important that ProxyServer::m_impl destructors do not run on the
287 : : //! eventloop thread because they may need it to do I/O if they perform
288 : : //! other IPC calls.
289 : : void startAsyncThread() MP_REQUIRES(m_mutex);
290 : :
291 : : //! Check if loop should exit.
292 : : bool done() const MP_REQUIRES(m_mutex);
293 : :
294 : : //! Type of m_incoming_connections list.
295 : : using Connections = std::list<Connection>;
296 : :
297 : : //! View of incoming connections yielding Connection& for each entry.
298 : : auto incomingConnections() { return std::views::all(m_incoming_connections); }
299 : :
300 : : //! Process name included in thread names so combined debug output from
301 : : //! multiple processes is easier to understand.
302 : : const char* m_exe_name;
303 : :
304 : : //! ID of the event loop thread
305 : : std::thread::id m_thread_id = std::this_thread::get_id();
306 : :
307 : : //! Handle of an async worker thread. Joined on destruction. Unset if async
308 : : //! method has not been called.
309 : : std::thread m_async_thread;
310 : :
311 : : //! Callback function to run on event loop thread during sync() call.
312 : : kj::FunctionParam<void()>* m_sync_fn MP_GUARDED_BY(m_mutex) = nullptr;
313 : :
314 : : //! Callback functions to run on async thread.
315 : : std::optional<CleanupList> m_async_fns MP_GUARDED_BY(m_mutex);
316 : :
317 : : //! Socket pair used to post and wait for wakeups to the event loop thread.
318 : : kj::Own<kj::AsyncIoStream> m_wait_stream;
319 : : kj::Own<kj::AsyncIoStream> m_post_stream;
320 : :
321 : : //! Synchronous writer used to write to m_post_stream.
322 : : kj::Own<kj::OutputStream> m_post_writer;
323 : :
324 : : //! Number of EventLoopRef instances referencing this event loop. This is a
325 : : //! sum of the number of client and server objects (Connection, ProxyClient,
326 : : //! ProxyServer) using the loop, plus temporary references held while
327 : : //! posting functions to the loop, plus any references held by external code
328 : : //! to keep the loop running. The loop() method exits when this count drops
329 : : //! to 0 (and m_async_fns is empty).
330 : : int m_num_refs MP_GUARDED_BY(m_mutex) = 0;
331 : :
332 : : //! Mutex and condition variable used to post tasks to event loop and async
333 : : //! thread.
334 : : Mutex m_mutex;
335 : : std::condition_variable m_cv;
336 : :
337 : : //! Capnp IO context.
338 : : kj::AsyncIoContext m_io_context;
339 : :
340 : : //! Capnp error handler. Needs to outlive m_task_set.
341 : : LoggingErrorHandler m_error_handler{*this};
342 : :
343 : : //! Capnp list of pending promises.
344 : : std::unique_ptr<kj::TaskSet> m_task_set;
345 : :
346 : : //! List of connections.
347 : : Connections m_incoming_connections;
348 : :
349 : : //! Logging options
350 : : LogOptions m_log_opts;
351 : :
352 : : //! External context pointer.
353 : : void* m_context;
354 : :
355 : : //! Hook called when ProxyServer<ThreadMap>::makeThread() is called.
356 : : std::function<void()> testing_hook_makethread;
357 : :
358 : : //! Hook called on the worker thread inside makeThread(), after the thread
359 : : //! context is set up and thread_context promise is fulfilled, but before it
360 : : //! starts waiting for requests.
361 : : std::function<void()> testing_hook_makethread_created;
362 : :
363 : : //! Hook called on the worker thread when it starts to execute an async
364 : : //! request. Used by tests to control timing or inject behavior at this
365 : : //! point in execution.
366 : : std::function<void()> testing_hook_async_request_start;
367 : :
368 : : //! Hook called on the worker thread just before returning results.
369 : : std::function<void()> testing_hook_async_request_done;
370 : :
371 : : //! Hook called on the event loop thread when a client has connected.
372 : : std::function<void()> testing_hook_connected;
373 : :
374 : : //! Hook called on the event loop thread when a client has disconnected.
375 : : std::function<void()> testing_hook_disconnected;
376 : :
377 : : //! Miscellaneous testing hook. Called from various places with an
378 : : //! argument identifying the call site (typically a string literal), so
379 : : //! tests can control timing or inject behavior at specific points without
380 : : //! requiring a dedicated hook for each one.
381 : : std::function<void(std::any)> testing_hook_misc;
382 : : };
383 : :
384 : : //! Single element task queue used to handle recursive capnp calls. (If the
385 : : //! server makes a callback into the client in the middle of a request, while the client
386 : : //! thread is blocked waiting for server response, this is what allows the
387 : : //! client to run the request in the same thread, the same way code would run in a
388 : : //! single process, with the callback sharing the same thread stack as the original
389 : : //! call.) To support this, the clientInvoke function calls Waiter::wait() to
390 : : //! block the client IPC thread while initial request is in progress. Then if
391 : : //! there is a callback, it is executed with Waiter::post().
392 : : //!
393 : : //! The Waiter class is also used server-side by `ProxyServer<Thread>::post()`
394 : : //! to execute IPC calls on worker threads.
395 : 18 : struct Waiter
396 : : {
397 : 18 : Waiter() = default;
398 : :
399 : : template <typename Fn>
400 : 32 : bool post(Fn&& fn)
401 : : {
402 : 32 : const Lock lock(m_mutex);
403 [ + - ]: 32 : if (m_fn) return false;
404 [ + - ]: 32 : m_fn = std::forward<Fn>(fn);
405 : 32 : m_cv.notify_all();
406 : 32 : return true;
407 : 32 : }
408 : :
409 : : template <class Predicate>
410 : 63 : void wait(Lock& lock, Predicate pred) MP_REQUIRES(m_mutex)
411 : : {
412 : 198 : m_cv.wait(lock.m_lock, [&]() MP_REQUIRES(m_mutex) {
413 : : // Important for this to be "while (m_fn)", not "if (m_fn)" to avoid
414 : : // a lost-wakeup bug. A new m_fn and m_cv notification might be sent
415 : : // after the fn() call and before the lock.lock() call in this loop
416 : : // in the case where a capnp response is sent and a brand new
417 : : // request is immediately received.
418 [ + + - + : 175 : while (m_fn) {
- + - + -
+ - + -
+ ][ + + -
+ # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ - + -
- - - - -
- + # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - - - -
- - - + -
+ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
419 : 32 : auto fn = std::move(*m_fn);
420 : 32 : m_fn.reset();
421 [ + - - - : 32 : Unlock(lock, fn);
# # # # #
# # # #
# ][ + - -
- # # # #
# # ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
422 : : }
423 : 143 : const bool done = pred();
424 : 143 : return done;
425 : : });
426 : 63 : }
427 : :
428 : : //! Mutex mainly used internally by waiter class, but also used externally
429 : : //! to guard access to related state. Specifically, since the thread_local
430 : : //! ThreadContext struct owns a Waiter, the Waiter::m_mutex is used to guard
431 : : //! access to other parts of the struct to avoid needing to deal with more
432 : : //! mutexes than necessary. This mutex can be held at the same time as
433 : : //! EventLoop::m_mutex as long as Waiter::mutex is locked first and
434 : : //! EventLoop::m_mutex is locked second.
435 : : Mutex m_mutex;
436 : : std::condition_variable m_cv MP_GUARDED_BY(m_mutex);
437 : : std::optional<kj::Function<void()>> m_fn MP_GUARDED_BY(m_mutex);
438 : : };
439 : :
440 : : //! Object holding network & rpc state associated with either an incoming server
441 : : //! connection, or an outgoing client connection. It must be created and destroyed
442 : : //! on the event loop thread.
443 : : //! In addition to Cap'n Proto state, it also holds lists of callbacks to run
444 : : //! when the connection is closed.
445 : : class Connection
446 : : {
447 : : public:
448 : 13 : Connection(EventLoop& loop, kj::Own<kj::AsyncIoStream>&& stream_)
449 : 13 : : m_loop(loop), m_stream(kj::mv(stream_)),
450 [ + - + - ]: 13 : m_network(*m_stream, ::capnp::rpc::twoparty::Side::CLIENT, ::capnp::ReaderOptions()),
451 [ + - + - : 39 : m_rpc_system(::capnp::makeRpcClient(m_network)) {}
+ - + - ]
452 : 17 : Connection(EventLoop& loop,
453 : : kj::Own<kj::AsyncIoStream>&& stream_,
454 : : const std::function<::capnp::Capability::Client(Connection&)>& make_client)
455 : 17 : : m_loop(loop), m_stream(kj::mv(stream_)),
456 [ + - + - ]: 17 : m_network(*m_stream, ::capnp::rpc::twoparty::Side::SERVER, ::capnp::ReaderOptions()),
457 [ + - + - : 51 : m_rpc_system(::capnp::makeRpcServer(m_network, make_client(*this))) {}
+ - + - +
- + - ]
458 : :
459 : : //! Run cleanup functions. Must be called from the event loop thread. First
460 : : //! calls synchronous cleanup functions while blocked (to free capnp
461 : : //! Capability::Client handles owned by ProxyClient objects), then schedules
462 : : //! asynchronous cleanup functions to run in a worker thread (to run
463 : : //! destructors of m_impl instances owned by ProxyServer objects).
464 : : ~Connection() noexcept(false);
465 : :
466 : : //! Register synchronous cleanup function to run on event loop thread (with
467 : : //! access to capnp thread local variables) when disconnect() is called.
468 : : //! any new i/o.
469 : : CleanupIt addSyncCleanup(std::function<void()> fn);
470 : : void removeSyncCleanup(CleanupIt it);
471 : :
472 : : //! Add disconnect handler.
473 : : template <typename F>
474 : 30 : void onDisconnect(F&& f)
475 : : {
476 : : // Add disconnect handler to local TaskSet to ensure it is canceled and
477 : : // will never run after connection object is destroyed. But when disconnect
478 : : // handler fires, do not call the function f right away, instead add it
479 : : // to the EventLoop TaskSet to avoid "Promise callback destroyed itself"
480 : : // error in the typical case where f deletes this Connection object.
481 [ + - - + ]: 75 : m_on_disconnect.add(m_network.onDisconnect().then(
482 [ + - ]: 124 : [f = std::forward<F>(f), this]() mutable { m_loop->m_task_set->add(kj::evalLater(kj::mv(f))); }));
[ # # # # ]
483 : 30 : }
484 : :
485 : : EventLoopRef m_loop;
486 : : kj::Own<kj::AsyncIoStream> m_stream;
487 : : LoggingErrorHandler m_error_handler{*m_loop};
488 : : //! TaskSet used to cancel the m_network.onDisconnect() handler for remote
489 : : //! disconnections, if the connection is closed locally first by deleting
490 : : //! this Connection object.
491 : : kj::TaskSet m_on_disconnect{m_error_handler};
492 : : ::capnp::TwoPartyVatNetwork m_network;
493 : : std::optional<::capnp::RpcSystem<::capnp::rpc::twoparty::VatId>> m_rpc_system;
494 : :
495 : : // ThreadMap interface client, used to create a remote server thread when an
496 : : // client IPC call is being made for the first time from a new thread.
497 : : ThreadMap::Client m_thread_map{nullptr};
498 : :
499 : : //! Collection of server-side IPC worker threads (ProxyServer<Thread> objects previously returned by
500 : : //! ThreadMap.makeThread) used to service requests to clients.
501 : : ::capnp::CapabilityServerSet<Thread> m_threads;
502 : :
503 : : //! A thread created by makePool with associated pending work queue. Vector is filled once by makePool() and never resized.
504 [ # # # # ]: 0 : struct PoolSlot {
505 : : Thread::Client client;
506 : : size_t depth{0};
507 : : };
508 : : std::vector<PoolSlot> m_thread_pool;
509 : :
510 : : //! Canceler for canceling promises that we want to discard when the
511 : : //! connection is destroyed. This is used to interrupt method calls that are
512 : : //! still executing at time of disconnection.
513 : : kj::Canceler m_canceler;
514 : :
515 : : //! Cleanup functions to run if connection is broken unexpectedly. List
516 : : //! will be empty if all ProxyClient are destroyed cleanly before the
517 : : //! connection is destroyed.
518 : : CleanupList m_sync_cleanup_fns;
519 : : };
520 : :
521 : : //! Vat id for server side of connection. Required argument to RpcSystem::bootStrap()
522 : : //!
523 : : //! "Vat" is Cap'n Proto nomenclature for a host of various objects that facilitates
524 : : //! bidirectional communication with other vats; it is often but not always 1-1 with
525 : : //! processes. Cap'n Proto doesn't reference clients or servers per se; instead everything
526 : : //! is just a vat.
527 : : //!
528 : : //! See also: https://github.com/capnproto/capnproto/blob/9021f0c722b36cb11e3690b0860939255ebad39c/c%2B%2B/src/capnp/rpc.capnp#L42-L56
529 [ + - + - ]: 1 : struct ServerVatId
530 : : {
531 : : ::capnp::word scratch[4]{};
532 : : ::capnp::MallocMessageBuilder message{scratch};
533 : : ::capnp::rpc::twoparty::VatId::Builder vat_id{message.getRoot<::capnp::rpc::twoparty::VatId>()};
534 [ + - ]: 13 : ServerVatId() { vat_id.setSide(::capnp::rpc::twoparty::Side::SERVER); }
535 : : };
536 : :
537 : : template <typename Interface, typename Impl>
538 : 81 : ProxyClientBase<Interface, Impl>::ProxyClientBase(typename Interface::Client client,
539 : : Connection* connection,
540 : : bool destroy_connection)
541 [ + - ]: 81 : : m_client(std::move(client)), m_context(connection)
542 : :
543 : : {
544 [ + - + - : 227 : MP_LOG(*m_context.loop, Log::Debug) << "Creating " << CxxTypeName(*this) << " " << this;
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
545 : : // Handler for the connection getting destroyed before this client object.
546 [ + - ]: 81 : auto disconnect_cb = m_context.connection->addSyncCleanup([this]() {
547 : : // Release client capability by move-assigning to temporary.
548 : : {
549 : 0 : typename Interface::Client(std::move(m_client));
550 : : }
551 : 0 : Lock lock{m_context.loop->m_mutex};
552 : 0 : m_context.connection = nullptr;
553 : 0 : });
554 : :
555 : : // Two shutdown sequences are supported:
556 : : //
557 : : // - A normal sequence where client proxy objects are deleted by external
558 : : // code that no longer needs them
559 : : //
560 : : // - A garbage collection sequence where the connection or event loop shuts
561 : : // down while external code is still holding client references.
562 : : //
563 : : // The first case is handled here when m_context.connection is not null. The
564 : : // second case is handled by the disconnect_cb function, which sets
565 : : // m_context.connection to null so nothing happens here.
566 [ + - ]: 162 : m_context.cleanup_fns.emplace_front([this, destroy_connection, disconnect_cb]{
567 : : {
568 : : // If the capnp interface defines a destroy method, call it to destroy
569 : : // the remote object, waiting for it to be deleted server side. If the
570 : : // capnp interface does not define a destroy method, this will just call
571 : : // an empty stub defined in the ProxyClientBase class and do nothing.
572 : : // Exceptions are caught and logged rather than propagated because
573 : : // ~ProxyClientBase is noexcept and the peer may be gone by the time
574 : : // this runs.
575 [ - + - + ]: 89 : if (kj::runCatchingExceptions([&]{ Sub::destroy(*this); }) != nullptr) {
[ # # # #
# # # # #
# # # #
# ][ + - -
+ - + - -
- - - + -
+ ][ # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # #
# ]
576 [ # # # # : 0 : MP_LOG(*m_context.loop, Log::Warning) << "Remote destroy call failed during cleanup. Continuing.";
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
577 : : }
578 : :
579 : : // FIXME: Could just invoke removed addCleanup fn here instead of duplicating code
580 : 162 : m_context.loop->sync([&]() {
581 : : // Remove disconnect callback on cleanup so it doesn't run and try
582 : : // to access this object after it's destroyed. This call needs to
583 : : // run inside loop->sync() on the event loop thread because
584 : : // otherwise, if there were an ill-timed disconnect, the
585 : : // onDisconnect handler could fire and delete the Connection object
586 : : // before the removeSyncCleanup call.
587 [ + - ][ + - : 81 : if (m_context.connection) m_context.connection->removeSyncCleanup(disconnect_cb);
- - + - ]
[ # # # # ]
588 : :
589 : : // Release client capability by move-assigning to temporary.
590 : : {
591 : 81 : typename Interface::Client(std::move(m_client));
592 : : }
593 [ + + ][ - + : 81 : if (destroy_connection) {
- - - + ]
[ # # # # ]
594 [ + - + - ]: 13 : delete m_context.connection;
[ # # # #
# # # # #
# # # ][ #
# # # # #
# # ]
595 : 13 : m_context.connection = nullptr;
596 : : }
597 : : });
598 : : }
599 : : });
600 : : // If construct() fails, run the cleanup functions before rethrowing,
601 : : // because ~ProxyClientBase will not run for an object whose constructor
602 : : // threw, and the connection would otherwise be leaked.
603 : : try {
604 [ + - ]: 81 : Sub::construct(*this);
605 : 0 : } catch (...) {
606 [ - - - - : 0 : MP_LOG(*m_context.loop, Log::Debug) << "Cleaning up " << CxxTypeName(*this) << " " << this << " after construct() failure";
- - - - -
- - - - -
- - - - -
- - - - -
- - ]
607 [ - - ]: 0 : CleanupRun(m_context.cleanup_fns);
608 : 0 : throw;
609 : : }
610 : :
611 : : // If this client owns the connection, delete the connection on disconnect.
612 [ + + ]: 81 : if (destroy_connection) {
613 [ + - ]: 13 : m_context.loop->sync([&] {
614 : 13 : EventLoop& loop = *m_context.loop;
615 : 13 : Connection* connection = m_context.connection;
616 : 13 : connection->onDisconnect([&loop, connection] {
617 [ # # # # : 0 : MP_LOG(loop, Log::Warning) << "IPC client: unexpected network disconnect.";
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
618 [ # # # # ]: 0 : delete connection;
[ # # # #
# # # # #
# # # ][ #
# # # # #
# # ]
619 : : });
620 : : });
621 : : }
622 : 81 : }
623 : :
624 : : template <typename Interface, typename Impl>
625 : 81 : ProxyClientBase<Interface, Impl>::~ProxyClientBase() noexcept
626 : : {
627 [ + - ]: 162 : MP_LOG(*m_context.loop, Log::Debug) << "Cleaning up " << CxxTypeName(*this) << " " << this;
628 : 81 : CleanupRun(m_context.cleanup_fns);
629 [ + - ]: 162 : MP_LOG(*m_context.loop, Log::Debug) << "Destroying " << CxxTypeName(*this) << " " << this;
630 : 105 : }
631 : :
632 : : template <typename Interface, typename Impl>
633 : 29 : ProxyServerBase<Interface, Impl>::ProxyServerBase(std::shared_ptr<Impl> impl, Connection& connection)
634 [ + - ]: 29 : : m_impl(std::move(impl)), m_context(&connection)
635 : : {
636 [ + - + - : 87 : MP_LOG(*m_context.loop, Log::Debug) << "Creating " << CxxTypeName(*this) << " " << this;
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
637 [ - + ]: 29 : assert(m_impl);
638 [ - - ]: 29 : }
639 : :
640 : : //! ProxyServer destructor, called from the EventLoop thread by Cap'n Proto
641 : : //! garbage collection code after there are no more references to this object.
642 : : //! This will typically happen when the corresponding ProxyClient object on the
643 : : //! other side of the connection is destroyed. It can also happen earlier if the
644 : : //! connection is broken or destroyed. In the latter case this destructor will
645 : : //! typically be called inside m_rpc_system.reset() call in the ~Connection
646 : : //! destructor while the Connection object still exists. However, because
647 : : //! ProxyServer objects are refcounted, and the Connection object could be
648 : : //! destroyed while asynchronous IPC calls are still in-flight, it's possible
649 : : //! for this destructor to be called after the Connection object no longer
650 : : //! exists, so it is NOT valid to dereference the m_context.connection pointer
651 : : //! from this function.
652 : : template <typename Interface, typename Impl>
653 : 29 : ProxyServerBase<Interface, Impl>::~ProxyServerBase()
654 : : {
655 [ + - ]: 58 : MP_LOG(*m_context.loop, Log::Debug) << "Cleaning up " << CxxTypeName(*this) << " " << this;
656 [ + + ]: 29 : if (m_impl) {
657 : : // If impl is non-null at this point, it means no client is waiting for
658 : : // the m_impl server object to be destroyed synchronously. This can
659 : : // happen either if the interface did not define a "destroy" method (see
660 : : // invokeDestroy method below), or if a destroy method was defined, but
661 : : // the connection was broken before it could be called.
662 : : //
663 : : // In either case, be conservative and run the cleanup on an
664 : : // asynchronous thread, to avoid destructors or cleanup functions
665 : : // blocking or deadlocking the current EventLoop thread, since they
666 : : // could be making IPC calls.
667 : : //
668 : : // Technically this is a little too conservative since if the interface
669 : : // defines a "destroy" method, but the destroy method does not accept a
670 : : // Context parameter specifying a worker thread, the cleanup method
671 : : // would run on the EventLoop thread normally (when connection is
672 : : // unbroken), but will not run on the EventLoop thread now (when
673 : : // connection is broken). Probably some refactoring of the destructor
674 : : // and invokeDestroy function is possible to make this cleaner and more
675 : : // consistent.
676 : 21 : m_context.loop->addAsyncCleanup([impl=std::move(m_impl), fns=std::move(m_context.cleanup_fns)]() mutable {
677 : 21 : impl.reset();
678 : 21 : CleanupRun(fns);
679 : : });
680 : : }
681 [ - + ]: 29 : assert(m_context.cleanup_fns.empty());
682 [ + - ]: 58 : MP_LOG(*m_context.loop, Log::Debug) << "Destroying " << CxxTypeName(*this) << " " << this;
683 [ - + ]: 58 : }
684 : :
685 : : //! If the capnp interface defined a special "destroy" method, as described the
686 : : //! ProxyClientBase class, this method will be called and synchronously destroy
687 : : //! m_impl before returning to the client.
688 : : //!
689 : : //! If the capnp interface does not define a "destroy" method, this will never
690 : : //! be called and the ~ProxyServerBase destructor will be responsible for
691 : : //! deleting m_impl asynchronously, whenever the ProxyServer object gets garbage
692 : : //! collected by Cap'n Proto.
693 : : //!
694 : : //! This method is called in the same way other proxy server methods are called,
695 : : //! via the serverInvoke function. Basically serverInvoke just calls this as a
696 : : //! substitute for a non-existent m_impl->destroy() method. If the destroy
697 : : //! method has any parameters or return values they will be handled in the
698 : : //! normal way by PassField/ReadField/BuildField functions. Particularly if a
699 : : //! Context.thread parameter was passed, this method will run on the worker
700 : : //! thread specified by the client. Otherwise it will run on the EventLoop
701 : : //! thread, like other server methods without an assigned thread.
702 : : template <typename Interface, typename Impl>
703 : 8 : void ProxyServerBase<Interface, Impl>::invokeDestroy()
704 : : {
705 : 8 : m_impl.reset();
706 : 8 : CleanupRun(m_context.cleanup_fns);
707 : 8 : }
708 : :
709 : : //! Map from Connection to local or remote thread handle which will be used over
710 : : //! that connection. This map will typically only contain one entry, but can
711 : : //! contain multiple if a single thread makes IPC calls over multiple
712 : : //! connections. A std::optional value type is used to avoid the map needing to
713 : : //! be locked while ProxyClient<Thread> objects are constructed, see
714 : : //! ThreadContext "Synchronization note" below.
715 : : using ConnThreads = std::map<Connection*, std::optional<ProxyClient<Thread>>>;
716 : : using ConnThread = ConnThreads::iterator;
717 : :
718 : : // Retrieve ProxyClient<Thread> object associated with this connection from a
719 : : // map, or create a new one and insert it into the map. Return map iterator and
720 : : // inserted bool.
721 : : std::tuple<ConnThread, bool> SetThread(GuardedRef<ConnThreads> threads, Connection* connection, const std::function<Thread::Client()>& make_thread);
722 : :
723 : : //! The thread_local ThreadContext struct (see CurrentThread()) provides information
724 : : //! about individual threads and a way of communicating between them. Because
725 : : //! it's a thread local struct, each ThreadContext instance is initialized by
726 : : //! the thread that owns it.
727 : : //!
728 : : //! ThreadContext is used for any client threads created externally which make
729 : : //! IPC calls, and for server threads created by
730 : : //! ProxyServer<ThreadMap>::makeThread() which execute IPC calls for clients.
731 : : //!
732 : : //! In both cases, the struct holds information like the thread name, and a
733 : : //! Waiter object where the EventLoop can post incoming IPC requests to execute
734 : : //! on the thread. The struct also holds ConnThread maps associating the thread
735 : : //! with local and remote ProxyClient<Thread> objects.
736 : : struct ThreadContext
737 : : {
738 : : //! Identifying string for debug.
739 : : std::string thread_name;
740 : :
741 : : //! Waiter object used to allow remote clients to execute code on this
742 : : //! thread. For server threads created by
743 : : //! ProxyServer<ThreadMap>::makeThread(), this is initialized in that
744 : : //! function. Otherwise, for client threads created externally, this is
745 : : //! initialized the first time the thread tries to make an IPC call. Having
746 : : //! a waiter is necessary for threads making IPC calls in case a server they
747 : : //! are calling expects them to execute a callback during the call, before
748 : : //! it sends a response.
749 : : //!
750 : : //! For IPC client threads, the Waiter pointer is never cleared and the Waiter
751 : : //! just gets destroyed when the thread does. For server threads created by
752 : : //! makeThread(), this pointer is set to null in the ~ProxyServer<Thread> as
753 : : //! a signal for the thread to exit and destroy itself. In both cases, the
754 : : //! same Waiter object is used across different calls and only created and
755 : : //! destroyed once for the lifetime of the thread.
756 : : std::unique_ptr<Waiter> waiter = nullptr;
757 : :
758 : : //! When client is making a request to a server, this is the
759 : : //! `callbackThread` argument it passes in the request, used by the server
760 : : //! in case it needs to make callbacks into the client that need to execute
761 : : //! while the client is waiting. This will be set to a local thread object.
762 : : //!
763 : : //! Synchronization note: The callback_thread and request_thread maps are
764 : : //! only ever accessed internally by this thread's destructor and externally
765 : : //! by Cap'n Proto event loop threads. Since it's possible for IPC client
766 : : //! threads to make calls over different connections that could have
767 : : //! different event loops, these maps are guarded by Waiter::m_mutex in case
768 : : //! different event loop threads add or remove map entries simultaneously.
769 : : //! However, individual ProxyClient<Thread> objects in the maps will only be
770 : : //! associated with one event loop and guarded by EventLoop::m_mutex. So
771 : : //! Waiter::m_mutex does not need to be held while accessing individual
772 : : //! ProxyClient<Thread> instances, and may even need to be released to
773 : : //! respect lock order and avoid locking Waiter::m_mutex before
774 : : //! EventLoop::m_mutex.
775 : : ConnThreads callback_threads MP_GUARDED_BY(waiter->m_mutex);
776 : :
777 : : //! When client is making a request to a server, this is the `thread`
778 : : //! argument it passes in the request, used to control which thread on
779 : : //! server will be responsible for executing it. If client call is being
780 : : //! made from a local thread, this will be a remote thread object returned
781 : : //! by makeThread. If a client call is being made from a thread currently
782 : : //! handling a server request, this will be set to the `callbackThread`
783 : : //! request thread argument passed in that request.
784 : : //!
785 : : //! Synchronization note: \ref callback_threads note applies here as well.
786 : : ConnThreads request_threads MP_GUARDED_BY(waiter->m_mutex);
787 : :
788 : : //! Whether this thread is a capnp event loop thread. Not really used except
789 : : //! to assert false if there's an attempt to execute a blocking operation
790 : : //! which could deadlock the thread.
791 : : bool loop_thread = false;
792 : : };
793 : :
794 : : template<typename T, typename Fn>
795 : 32 : kj::Promise<T> ProxyServer<Thread>::post(Fn&& fn)
796 : : {
797 : 32 : auto ready = kj::newPromiseAndFulfiller<void>(); // Signaled when waiter is ready to post again.
798 [ + - ]: 32 : auto cancel_monitor_ptr = kj::heap<CancelMonitor>();
799 : 32 : CancelMonitor& cancel_monitor = *cancel_monitor_ptr;
800 : : // Keep a reference to the ProxyServer<Thread> instance by assigning it to
801 : : // the self variable. ProxyServer instances are reference-counted and if the
802 : : // client drops its reference, this variable keeps the instance alive until
803 : : // the thread finishes executing. The self variable needs to be destroyed on
804 : : // the event loop thread so it is freed in a sync() call below.
805 [ + - ]: 32 : auto self = thisCap();
806 [ + - + - ]: 92 : auto ret = m_thread_ready.then([this, self = std::move(self), fn = std::forward<Fn>(fn), ready_fulfiller = kj::mv(ready.fulfiller), cancel_monitor_ptr = kj::mv(cancel_monitor_ptr)]() mutable {
807 : 32 : auto result = kj::newPromiseAndFulfiller<T>(); // Signaled when fn() is called, with its return value.
808 [ # # # # : 88 : bool posted = m_thread_context.waiter->post([this, self = std::move(self), fn = std::forward<Fn>(fn), ready_fulfiller = kj::mv(ready_fulfiller), result_fulfiller = kj::mv(result.fulfiller), cancel_monitor_ptr = kj::mv(cancel_monitor_ptr)]() mutable {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- ][ + - +
- - - - -
+ - + - ]
[ + - + -
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
809 : : // Fulfill ready.promise now, as soon as the Waiter starts executing
810 : : // this lambda, so the next ProxyServer<Thread>::post() call can
811 : : // immediately call waiter->post(). It is important to do this
812 : : // before calling fn() because fn() can make an IPC call back to the
813 : : // client, which can make another IPC call to this server thread.
814 : : // (This typically happens when IPC methods take std::function
815 : : // parameters.) When this happens the second call to the server
816 : : // thread should not be blocked waiting for the first call.
817 [ # # # # : 88 : m_loop->sync([ready_fulfiller = kj::mv(ready_fulfiller)]() mutable {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + - ]
[ + - - -
+ - ][ + -
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
818 : 32 : ready_fulfiller->fulfill();
819 : 32 : ready_fulfiller = nullptr;
820 : : });
821 : 32 : std::optional<T> result_value;
822 [ - + ]: 64 : kj::Maybe<kj::Exception> exception{kj::runCatchingExceptions([&]{ result_value.emplace(fn(*cancel_monitor_ptr)); })};
823 [ # # # # : 88 : m_loop->sync([this, &result_value, &exception, self = kj::mv(self), result_fulfiller = kj::mv(result_fulfiller), cancel_monitor_ptr = kj::mv(cancel_monitor_ptr)]() mutable {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ][ + -
+ - + - +
- ][ + - +
- - - - -
+ - + - ]
[ + - + -
# # # # #
# # # ][ #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
824 : : // Destroy CancelMonitor here before fulfilling or rejecting the
825 : : // promise so it doesn't get triggered when the promise is
826 : : // destroyed.
827 : 32 : cancel_monitor_ptr = nullptr;
828 : : // Send results to the fulfiller. Technically it would be ok to
829 : : // skip this if promise was canceled, but it's simpler to just
830 : : // do it unconditionally.
831 [ # # # # : 32 : KJ_IF_MAYBE(e, exception) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + - ]
[ + - - -
+ - ][ + -
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
832 [ # # # # : 0 : assert(!result_value);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ # # # # ]
[ # # # #
# # ][ # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
833 : 0 : result_fulfiller->reject(kj::mv(*e));
834 : : } else {
835 [ # # # # : 32 : assert(result_value);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - + - + ]
[ - + - -
- + ][ - +
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
836 : 32 : result_fulfiller->fulfill(kj::mv(*result_value));
837 [ # # # # : 32 : result_value.reset();
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + - ]
[ + - - -
+ - ][ + -
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
838 : : }
839 : 32 : result_fulfiller = nullptr;
840 : : // Use evalLater to destroy the ProxyServer<Thread> self
841 : : // reference, if it is the last reference, because the
842 : : // ProxyServer<Thread> destructor needs to join the thread,
843 : : // which can't happen until this sync() block has exited.
844 [ # # # # : 128 : m_loop->m_task_set->add(kj::evalLater([self = kj::mv(self)] {}));
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ][ + - +
- + - + -
+ - + - ]
[ + - - -
+ - + - +
- - - - -
+ - + - ]
[ + - + -
+ - # # #
# # # # #
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
845 : : });
846 : 32 : });
847 : : // Assert that calling Waiter::post did not fail. It could only return
848 : : // false if a new function was posted before the previous one finished
849 : : // executing, but new functions are only posted when m_thread_ready is
850 : : // signaled, so this should never happen.
851 [ # # # # : 32 : assert(posted);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ - + - + ]
[ - + - -
- + ][ - +
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
852 [ # # # # : 32 : return kj::mv(result.promise);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + - ]
[ + - - -
+ - ][ + -
# # # # ]
[ # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
853 [ # # # # : 64 : }).attach(kj::heap<CancelProbe>(cancel_monitor));
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # ]
[ + - + - ]
[ + - - -
+ - ][ # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
854 [ - + ]: 32 : m_thread_ready = kj::mv(ready.promise);
855 [ + - ]: 64 : return ret;
856 [ + - + - : 32 : }
- - ]
857 : :
858 : : //! Given a stream, make a new ProxyClient object to send requests over it.
859 : : //! Also create a new Connection object embedded in the client that is freed
860 : : //! when the client is closed.
861 : : //!
862 : : //! If the init interface declares a construct() method, creating the client
863 : : //! calls it, so this function may block making an IPC call and may throw if
864 : : //! the call fails.
865 : : //!
866 : : //! If destroy_connection is false, the returned client does not take
867 : : //! ownership of the connection, and the caller is responsible for
868 : : //! disconnecting and freeing it (accessible as client->m_context.connection)
869 : : //! whenever it is done with it.
870 : : template <typename InitInterface>
871 : 12 : std::unique_ptr<ProxyClient<InitInterface>> ConnectStream(EventLoop& loop, Stream stream, bool destroy_connection = true)
872 : : {
873 : 12 : typename InitInterface::Client init_client(nullptr);
874 : 12 : std::unique_ptr<Connection> connection;
875 [ + - ]: 12 : loop.sync([&] {
876 : 12 : connection = std::make_unique<Connection>(loop, kj::mv(stream));
877 [ + - + - : 24 : init_client = connection->m_rpc_system->bootstrap(ServerVatId().vat_id).castAs<InitInterface>();
+ - + - +
- ][ # # #
# # # # #
# # # # #
# # # # #
# # ]
878 : : });
879 [ + - ]: 12 : return std::make_unique<ProxyClient<InitInterface>>(kj::mv(init_client), connection.release(), destroy_connection);
880 [ + - ]: 12 : }
881 : :
882 : : //! Given stream and init objects, construct a new ProxyServer object that
883 : : //! handles requests from the stream by calling the init object. Embed the
884 : : //! ProxyServer in a Connection object that is stored in
885 : : //! loop.m_incoming_connections. This should be called from the event loop
886 : : //! thread. Returns the new ProxyServer along with an iterator to its
887 : : //! Connection.
888 : : //!
889 : : //! If destroy_connection is false, the connection is not erased
890 : : //! automatically when the peer disconnects, and the caller is responsible
891 : : //! for erasing loop.m_incoming_connections at the returned iterator
892 : : //! whenever it is done with the connection.
893 : : template <typename InitInterface, typename InitImpl, typename OnDisconnect>
894 : 16 : std::pair<ProxyServer<InitInterface>*, EventLoop::Connections::iterator> _Serve(EventLoop& loop,
895 : : kj::Own<kj::AsyncIoStream>&& stream,
896 : : std::shared_ptr<InitImpl> init,
897 : : OnDisconnect&& on_disconnect,
898 : : bool destroy_connection = true)
899 : : {
900 : 16 : ProxyServer<InitInterface>* server = nullptr;
901 : 32 : loop.m_incoming_connections.emplace_front(loop, kj::mv(stream), [&](Connection& connection) {
902 : 16 : auto proxy_server = kj::heap<ProxyServer<InitInterface>>(std::move(init), connection);
903 [ + - + - ]: 16 : server = proxy_server;
[ # # ]
904 [ + - + - : 16 : return capnp::Capability::Client(kj::mv(proxy_server));
+ - + - ]
[ # # # # ]
905 : 16 : });
906 : 16 : auto it = loop.m_incoming_connections.begin();
907 [ + - + - : 48 : MP_LOG(loop, Log::Info) << "IPC server: socket connected.";
+ - + - +
- + - ]
908 [ - + ]: 16 : if (loop.testing_hook_connected) loop.testing_hook_connected();
909 [ + - ]: 62 : it->onDisconnect([&loop, it, on_disconnect = std::forward<OnDisconnect>(on_disconnect), destroy_connection]() mutable {
[ # # # # ]
910 [ + - + - : 48 : MP_LOG(loop, Log::Info) << "IPC server: socket disconnected.";
+ - + - +
- + - + -
+ - + - +
- + - +
- ][ # # #
# # # # #
# # # # ]
911 [ + - + - ]: 16 : if (destroy_connection) loop.m_incoming_connections.erase(it);
[ # # ]
912 : 15 : on_disconnect();
913 [ - + - + ]: 16 : if (loop.testing_hook_disconnected) loop.testing_hook_disconnected();
[ # # ]
914 : : });
915 : 16 : return {server, it};
916 : : }
917 : :
918 : : //! Overload of _Serve that takes a reference to the init object instead of a
919 : : //! shared_ptr. ProxyServer objects use shared_ptr's internally to optionally
920 : : //! take ownership of interfaces being served, and free them when clients are
921 : : //! no longer using them. Some libmultiprocess callers take advantage of this
922 : : //! and pass shared_ptr's directly. But other callers that do not give away
923 : : //! ownership are not required to use shared_ptr, so this overload converts
924 : : //! references they pass into shared_ptr's with empty deleters. This prevents
925 : : //! the ProxyServer object from deleting the init object when the client is
926 : : //! disconnected.
927 : : template <typename InitInterface, typename InitImpl, typename OnDisconnect>
928 : 16 : std::pair<ProxyServer<InitInterface>*, EventLoop::Connections::iterator> _Serve(EventLoop& loop,
929 : : kj::Own<kj::AsyncIoStream>&& stream,
930 : : InitImpl& init,
931 : : OnDisconnect&& on_disconnect,
932 : : bool destroy_connection = true)
933 : : {
934 [ + - ]: 16 : return _Serve<InitInterface>(loop, kj::mv(stream), std::shared_ptr<InitImpl>(&init, [](InitImpl*){}),
935 : 16 : std::forward<OnDisconnect>(on_disconnect), destroy_connection);
936 : : }
937 : :
938 : 9 : struct Listener
939 : : {
940 : 9 : explicit Listener(kj::Own<kj::ConnectionReceiver>&& receiver, std::optional<size_t> max_connections)
941 : 9 : : m_receiver(kj::mv(receiver)), m_max_connections(max_connections) {}
942 : :
943 : 45 : bool atCapacity() const
944 : : {
945 [ + + + + : 38 : return m_max_connections && m_active_connections >= *m_max_connections;
+ + ]
946 : : }
947 : :
948 : : kj::Own<kj::ConnectionReceiver> m_receiver;
949 : : std::optional<size_t> m_max_connections;
950 : : size_t m_active_connections{0};
951 : : };
952 : :
953 : : template <typename InitInterface, typename InitImpl>
954 [ + + ]: 30 : void _Listen(const std::shared_ptr<Listener>& listener, EventLoop& loop, InitImpl& init)
955 : : {
956 [ + + ]: 77 : if (listener->atCapacity()) return;
957 : :
958 : 24 : auto* receiver = listener->m_receiver.get();
959 [ + - ]: 72 : loop.m_task_set->add(receiver->accept().then(
960 [ + - - - : 159 : [&loop, &init, listener](kj::Own<kj::AsyncIoStream>&& stream) {
+ - + - +
- - + -
- ]
961 [ + - ]: 15 : ++listener->m_active_connections;
962 [ + - - - : 150 : _Serve<InitInterface>(loop, kj::mv(stream), init, [&loop, &init, listener] {
+ - - - +
- - + - -
+ - - - +
- + - + -
- - ]
963 [ + + ]: 15 : const bool resume_accept{listener->atCapacity()};
964 [ - + ]: 15 : assert(listener->m_active_connections > 0);
965 [ + + ]: 15 : --listener->m_active_connections;
966 [ + + ]: 15 : if (resume_accept) _Listen<InitInterface>(listener, loop, init);
967 : : });
968 : 15 : _Listen<InitInterface>(listener, loop, init);
969 : : }));
970 : : }
971 : :
972 : : //! Given a stream and an init object, handle requests on the stream by calling
973 : : //! methods on the Init object. See _Serve for details on the return value and
974 : : //! the destroy_connection parameter.
975 : : template <typename InitInterface, typename InitImpl>
976 : 1 : std::pair<ProxyServer<InitInterface>*, EventLoop::Connections::iterator> ServeStream(
977 : : EventLoop& loop, Stream stream, InitImpl&& init, bool destroy_connection = true)
978 : : {
979 [ # # ]: 1 : return _Serve<InitInterface>(loop, kj::mv(stream), std::forward<InitImpl>(init), /*on_disconnect=*/ [] {}, destroy_connection);
980 : : }
981 : :
982 : : //! Given listening socket identifier and an init object, handle incoming
983 : : //! connections and requests by calling methods on the Init object.
984 : : template <typename InitInterface, typename InitImpl>
985 : 9 : void ListenConnections(EventLoop& loop, SocketId fd, InitImpl& init, std::optional<size_t> max_connections = std::nullopt)
986 : : {
987 : 9 : loop.sync([&]() {
988 [ + - ]: 9 : auto listener{std::make_shared<Listener>(
989 [ + - ]: 9 : loop.m_io_context.lowLevelProvider->wrapListenSocketFd(fd, kj::LowLevelAsyncIoProvider::TAKE_OWNERSHIP),
990 : : max_connections)};
991 [ + - ]: 9 : _Listen<InitInterface>(listener, loop, init);
992 : 9 : });
993 : 9 : }
994 : :
995 : : extern thread_local ThreadContext g_thread_context; // NOLINT(bitcoin-nontrivial-threadlocal)
996 : : // Silence nonstandard bitcoin tidy error "Variable with non-trivial destructor
997 : : // cannot be thread_local" which should not be a problem on modern platforms, and
998 : : // could lead to a small memory leak at worst on older ones.
999 : :
1000 : : //! Return the current thread's ThreadContext.
1001 : : //!
1002 : : //! Why per-thread state is needed at all: libmultiprocess has no control over
1003 : : //! which threads the C++ application uses to call ProxyClient methods after
1004 : : //! the proxy objects are returned to it. The thread-mapping model (see
1005 : : //! "Thread Mapping" in doc/design.md) gives each application thread making
1006 : : //! IPC calls a dedicated server-side thread that executes its requests, so
1007 : : //! thread-local state and recursive mutexes work as expected across the
1008 : : //! process boundary and callbacks from the server run on the originating
1009 : : //! client thread. The client-side handles for those dedicated server threads
1010 : : //! (the ProxyClient<Thread> objects returned by ThreadMap.makeThread, stored
1011 : : //! per connection in the request_threads / callback_threads maps below) are
1012 : : //! state that must be keyed implicitly by the calling thread, and must be
1013 : : //! released when the client thread exits so the corresponding server threads
1014 : : //! are freed. A thread_local object is the C++ mechanism that provides both
1015 : : //! of these: per-thread storage plus a destructor that runs at thread exit
1016 : : //! (the C equivalent would be a pthread key destructor). This is why
1017 : : //! ThreadContext is thread_local and why its destructor is nontrivial.
1018 : : ThreadContext& CurrentThread();
1019 : :
1020 : : } // namespace mp
1021 : :
1022 : : #endif // MP_PROXY_IO_H
|