blob: d4ec5e9b385854a01aea0549df7454e0f344e3a7 [file] [log] [blame]
Patrick Williams74187662022-08-26 19:28:24 -05001#include <systemd/sd-bus.h>
2
3#include <sdbusplus/async/context.hpp>
Patrick Williamsbc139972025-07-08 16:46:50 -04004#include <sdbusplus/async/task.hpp>
5#include <sdbusplus/async/timer.hpp>
Patrick Williams74187662022-08-26 19:28:24 -05006
Patrick Williams73e278b2022-09-16 08:31:36 -05007#include <chrono>
8
Patrick Williams74187662022-08-26 19:28:24 -05009namespace sdbusplus::async
10{
11
Patrick Williams73e278b2022-09-16 08:31:36 -050012context::context(bus_t&& b) : bus(std::move(b))
13{
Patrick Williams06f265f2024-08-16 15:19:49 -040014 dbus_source =
15 event_loop.add_io(bus.get_fd(), EPOLLIN, dbus_event_handle, this);
Patrick Williams73e278b2022-09-16 08:31:36 -050016}
17
Patrick Williams74187662022-08-26 19:28:24 -050018namespace details
19{
20
21/* The sd_bus_wait/process completion event.
22 *
23 * The wait/process handshake is modelled as a Sender with the the worker
24 * task `co_await`ing Senders over and over. This class is the completion
25 * handling for the Sender (to get it back to the Receiver, ie. the worker).
26 */
Patrick Williams4a9e4222023-08-18 09:57:28 -050027struct wait_process_completion : context_ref, bus::details::bus_friend
Patrick Williams74187662022-08-26 19:28:24 -050028{
Patrick Williams4a9e4222023-08-18 09:57:28 -050029 explicit wait_process_completion(context& ctx) : context_ref(ctx) {}
Patrick Williams74187662022-08-26 19:28:24 -050030 virtual ~wait_process_completion() = default;
31
32 // Called by the `caller` to indicate the Sender is completed.
33 virtual void complete() noexcept = 0;
Patrick Williams73e278b2022-09-16 08:31:36 -050034 // Called by the `caller` to indicate the Sender should be stopped.
35 virtual void stop() noexcept = 0;
Patrick Williams74187662022-08-26 19:28:24 -050036
37 // Arm the completion event.
38 void arm() noexcept;
39
40 // Data to share with the worker.
Patrick Williams435eb1b2022-09-16 16:22:07 -050041 event_t::time_resolution timeout{};
Patrick Williams74187662022-08-26 19:28:24 -050042
Patrick Williamsbbc181e2023-11-16 16:29:53 -060043 static auto loop(context& ctx) -> task<>;
Patrick Williams74187662022-08-26 19:28:24 -050044 static void wait_once(context& ctx);
45};
46
47/* The completion template based on receiver type.
48 *
49 * The type of the receivers (typically the co_awaiter) is only known by
50 * a template, so we need a sub-class of completion to hold the receiver.
51 */
52template <execution::receiver R>
53struct wait_process_operation : public wait_process_completion
54{
55 wait_process_operation(context& ctx, R r) :
56 wait_process_completion(ctx), receiver(std::move(r))
57 {}
58
59 wait_process_operation(wait_process_operation&&) = delete;
60
61 void complete() noexcept override final
62 {
63 execution::set_value(std::move(this->receiver));
64 }
65
Patrick Williams73e278b2022-09-16 08:31:36 -050066 void stop() noexcept override final
67 {
68 // Stop can be called when the context is shutting down,
69 // so treat it as if the receiver completed.
70 execution::set_value(std::move(this->receiver));
71 }
72
Patrick Williams74187662022-08-26 19:28:24 -050073 friend void tag_invoke(execution::start_t,
74 wait_process_operation& self) noexcept
75 {
76 self.arm();
77 }
78
79 R receiver;
80};
81
82/* The sender for the wait/process event. */
Patrick Williams4a9e4222023-08-18 09:57:28 -050083struct wait_process_sender : public context_ref
Patrick Williams74187662022-08-26 19:28:24 -050084{
Patrick Williams9c6ec9b2023-06-23 15:47:42 -050085 using is_sender = void;
86
Patrick Williams4a9e4222023-08-18 09:57:28 -050087 explicit wait_process_sender(context& ctx) : context_ref(ctx) {}
Patrick Williams74187662022-08-26 19:28:24 -050088
Patrick Williams36137e02024-12-18 11:20:12 -050089 friend auto tag_invoke(execution::get_completion_signatures_t,
90 const wait_process_sender&, auto)
91 -> execution::completion_signatures<execution::set_value_t()>;
Patrick Williams74187662022-08-26 19:28:24 -050092
93 template <execution::receiver R>
94 friend auto tag_invoke(execution::connect_t, wait_process_sender&& self,
95 R r) -> wait_process_operation<R>
96 {
97 // Create the completion for the wait.
98 return {self.ctx, std::move(r)};
99 }
Patrick Williams74187662022-08-26 19:28:24 -0500100};
101
Patrick Williamsbbc181e2023-11-16 16:29:53 -0600102auto wait_process_completion::loop(context& ctx) -> task<>
Patrick Williams74187662022-08-26 19:28:24 -0500103{
Patrick Williams78e436f2022-09-21 10:06:20 -0500104 while (!ctx.final_stop.stop_requested())
Patrick Williams74187662022-08-26 19:28:24 -0500105 {
Patrick Williamse8e66312023-12-01 16:57:29 -0600106 // Handle the next sdbus event. Completion likely happened on a
107 // different thread so we need to transfer back to the worker thread.
Patrick Williamsf083bc12024-12-16 16:13:28 -0500108 co_await execution::continues_on(wait_process_sender(ctx),
109 ctx.loop.get_scheduler());
Patrick Williams74187662022-08-26 19:28:24 -0500110 }
Patrick Williams10483c92022-09-23 11:33:39 -0500111
112 {
113 std::lock_guard lock{ctx.lock};
114 ctx.wait_process_stopped = true;
115 }
Patrick Williams74187662022-08-26 19:28:24 -0500116}
117
118} // namespace details
119
120context::~context() noexcept(false)
121{
122 if (worker_thread.joinable())
123 {
124 throw std::logic_error(
125 "sdbusplus::async::context destructed without completion.");
126 }
127}
128
Patrick Williams3c242ba2022-09-23 09:51:55 -0500129void context::run()
Patrick Williams74187662022-08-26 19:28:24 -0500130{
Patrick Williams10483c92022-09-23 11:33:39 -0500131 // Run the primary portion of the run-loop.
132 caller_run();
Patrick Williams74187662022-08-26 19:28:24 -0500133
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500134 // This should be final_stop...
Patrick Williams10483c92022-09-23 11:33:39 -0500135
136 // We need to wait for the pending wait process and stop it.
137 wait_for_wait_process_stopped();
Patrick Williams4cfc2842022-09-22 09:53:33 -0500138
Patrick Williams78e436f2022-09-21 10:06:20 -0500139 // Wait for all the internal tasks to complete.
Patrick Williams97c31c82023-05-25 11:04:46 -0500140 stdexec::sync_wait(internal_tasks.on_empty());
Patrick Williamsc5b5ff52022-09-21 08:34:22 -0500141
Patrick Williams10483c92022-09-23 11:33:39 -0500142 // Finish up the loop and join the thread.
143 // (There shouldn't be anything going on by this point anyhow.)
Patrick Williams74187662022-08-26 19:28:24 -0500144 loop.finish();
145 if (worker_thread.joinable())
146 {
147 worker_thread.join();
148 }
149}
150
Patrick Williamsbc139972025-07-08 16:46:50 -0400151static auto watchdog_loop(sdbusplus::async::context& ctx) -> task<>
152{
153 auto watchdog_time =
154 std::chrono::microseconds(ctx.get_bus().watchdog_enabled());
155 if (watchdog_time.count() == 0)
156 {
157 co_return;
158 }
159
160 // Recommended interval is half of WATCHDOG_USEC
161 watchdog_time /= 2;
162
163 while (!ctx.stop_requested())
164 {
165 ctx.get_bus().watchdog_pet();
166 co_await sleep_for(ctx, watchdog_time);
167 }
168}
169
Patrick Williams3c242ba2022-09-23 09:51:55 -0500170void context::worker_run()
Patrick Williams74187662022-08-26 19:28:24 -0500171{
Patrick Williamsbc139972025-07-08 16:46:50 -0400172 internal_tasks.spawn(watchdog_loop(*this));
173
Patrick Williams3c242ba2022-09-23 09:51:55 -0500174 // Start the sdbus 'wait/process' loop; treat it as an internal task.
Patrick Williams97c31c82023-05-25 11:04:46 -0500175 internal_tasks.spawn(details::wait_process_completion::loop(*this));
Patrick Williams74187662022-08-26 19:28:24 -0500176
177 // Run the execution::run_loop to handle all the tasks.
178 loop.run();
179}
180
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500181void context::spawn_complete()
Patrick Williams10483c92022-09-23 11:33:39 -0500182{
183 {
184 std::lock_guard l{lock};
185 spawn_watcher_running = false;
Patrick Williams10483c92022-09-23 11:33:39 -0500186 }
187
188 if (stop_requested())
189 {
190 final_stop.request_stop();
191 }
192
193 caller_wait.notify_one();
194 event_loop.break_run();
195}
196
197void context::check_stop_requested()
198{
199 if (stop_requested())
200 {
201 throw std::logic_error(
202 "sdbusplus::async::context spawn called while already stopped.");
203 }
204}
205
206void context::spawn_watcher()
207{
208 {
209 std::lock_guard l{lock};
210 if (spawn_watcher_running)
211 {
212 return;
213 }
214
215 spawn_watcher_running = true;
216 }
217
218 // Spawn the watch for completion / exceptions.
Patrick Williams97c31c82023-05-25 11:04:46 -0500219 internal_tasks.spawn(pending_tasks.on_empty() |
220 execution::then([this]() { spawn_complete(); }));
Patrick Williams10483c92022-09-23 11:33:39 -0500221}
222
223void context::caller_run()
224{
225 // We are able to run the loop until the context is requested to stop or
226 // we get an exception.
227 auto keep_running = [this]() {
228 std::lock_guard l{lock};
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500229 return !final_stop.stop_requested();
Patrick Williams10483c92022-09-23 11:33:39 -0500230 };
231
232 // If we are suppose to keep running, start the run loop.
233 if (keep_running())
234 {
235 // Start up the worker thread.
236 if (!worker_thread.joinable())
237 {
238 worker_thread = std::thread{[this]() { worker_run(); }};
239 }
240 else
241 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500242 // We've already been running and there might a completion pending.
243 // Spawn a new watcher that checks for these.
Patrick Williams10483c92022-09-23 11:33:39 -0500244 spawn_watcher();
245 }
246
247 while (keep_running())
248 {
249 // Handle waiting on all the sd-events.
250 details::wait_process_completion::wait_once(*this);
251 }
252 }
253 else
254 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500255 // There might be pending completions still, so spawn a watcher for
256 // them.
Patrick Williams10483c92022-09-23 11:33:39 -0500257 spawn_watcher();
258 }
259}
260
261void context::wait_for_wait_process_stopped()
262{
263 auto worker = std::exchange(pending, nullptr);
264 while (worker == nullptr)
265 {
266 std::lock_guard l{lock};
267 if (wait_process_stopped)
268 {
269 break;
270 }
271
272 worker = std::exchange(staged, nullptr);
273 if (!worker)
274 {
275 std::this_thread::yield();
276 }
277 }
278 if (worker)
279 {
280 worker->stop();
281 wait_process_stopped = true;
282 }
283}
284
Patrick Williams74187662022-08-26 19:28:24 -0500285void details::wait_process_completion::arm() noexcept
286{
287 // Call process. True indicates something was handled and we do not
288 // need to `wait`, because there might be yet another pending operation
289 // to process, so immediately signal the operation as complete.
290 if (ctx.get_bus().process_discard())
291 {
292 this->complete();
293 return;
294 }
295
Patrick Williams73e278b2022-09-16 08:31:36 -0500296 // We need to call wait now, get the current timeout and stage ourselves
297 // as the next completion.
Patrick Williams74187662022-08-26 19:28:24 -0500298
299 // Get the bus' timeout.
Patrick Williams73e278b2022-09-16 08:31:36 -0500300 uint64_t to_usec = 0;
Patrick Williams1ee60d62023-08-18 12:59:05 -0500301 sd_bus_get_timeout(get_busp(ctx), &to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500302
Patrick Williams73e278b2022-09-16 08:31:36 -0500303 if (to_usec == UINT64_MAX)
Patrick Williams74187662022-08-26 19:28:24 -0500304 {
305 // sd_bus_get_timeout returns UINT64_MAX to indicate 'wait forever'.
Patrick Williams73e278b2022-09-16 08:31:36 -0500306 // Turn this into -1 for sd-event.
307 timeout = std::chrono::microseconds{-1};
Patrick Williams74187662022-08-26 19:28:24 -0500308 }
309 else
310 {
Patrick Williams73e278b2022-09-16 08:31:36 -0500311 timeout = std::chrono::microseconds(to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500312 }
313
314 // Assign ourselves as the pending completion and release the caller.
315 std::lock_guard lock{ctx.lock};
Patrick Williams73e278b2022-09-16 08:31:36 -0500316 ctx.staged = this;
Patrick Williams74187662022-08-26 19:28:24 -0500317 ctx.caller_wait.notify_one();
318}
319
320void details::wait_process_completion::wait_once(context& ctx)
321{
Patrick Williams74187662022-08-26 19:28:24 -0500322 // Scope for lock.
323 {
324 std::unique_lock lock{ctx.lock};
325
326 // If there isn't a completion waiting already, wait on the condition
327 // variable for one to show up (we can't call `poll` yet because we
328 // don't have the required parameters).
Patrick Williams73e278b2022-09-16 08:31:36 -0500329 ctx.caller_wait.wait(lock, [&] {
330 return (ctx.pending != nullptr) || (ctx.staged != nullptr) ||
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500331 ctx.final_stop.stop_requested();
Patrick Williams73e278b2022-09-16 08:31:36 -0500332 });
Patrick Williams74187662022-08-26 19:28:24 -0500333
Patrick Williams73e278b2022-09-16 08:31:36 -0500334 // Save the waiter as pending.
335 if (ctx.pending == nullptr)
336 {
337 ctx.pending = std::exchange(ctx.staged, nullptr);
338 }
Patrick Williams74187662022-08-26 19:28:24 -0500339 }
340
Patrick Williams73e278b2022-09-16 08:31:36 -0500341 // Run the event loop to process one request.
Patrick Williams10483c92022-09-23 11:33:39 -0500342 // If the context has been requested to be stopped, skip the event loop.
343 if (!ctx.final_stop.stop_requested() && ctx.pending)
Patrick Williams73e278b2022-09-16 08:31:36 -0500344 {
Patrick Williams10483c92022-09-23 11:33:39 -0500345 ctx.event_loop.run_one(ctx.pending->timeout);
Patrick Williams73e278b2022-09-16 08:31:36 -0500346 }
347}
348
349int context::dbus_event_handle(sd_event_source*, int, uint32_t, void* data)
350{
351 auto self = static_cast<context*>(data);
352
Patrick Williams10483c92022-09-23 11:33:39 -0500353 auto pending = std::exchange(self->pending, nullptr);
Patrick Williams73e278b2022-09-16 08:31:36 -0500354 if (pending != nullptr)
355 {
Patrick Williams10483c92022-09-23 11:33:39 -0500356 pending->complete();
Patrick Williams73e278b2022-09-16 08:31:36 -0500357 }
358
359 return 0;
Patrick Williams74187662022-08-26 19:28:24 -0500360}
361
362} // namespace sdbusplus::async