blob: ad45c4d63738e1fe0f48476d738f0d93feedb33d [file] [log] [blame]
Patrick Williams74187662022-08-26 19:28:24 -05001#include <systemd/sd-bus.h>
2
3#include <sdbusplus/async/context.hpp>
4
Patrick Williams73e278b2022-09-16 08:31:36 -05005#include <chrono>
6
Patrick Williams74187662022-08-26 19:28:24 -05007namespace sdbusplus::async
8{
9
Patrick Williams73e278b2022-09-16 08:31:36 -050010context::context(bus_t&& b) : bus(std::move(b))
11{
12 dbus_source = event_loop.add_io(bus.get_fd(), EPOLLIN, dbus_event_handle,
13 this);
14}
15
Patrick Williams74187662022-08-26 19:28:24 -050016namespace details
17{
18
19/* The sd_bus_wait/process completion event.
20 *
21 * The wait/process handshake is modelled as a Sender with the the worker
22 * task `co_await`ing Senders over and over. This class is the completion
23 * handling for the Sender (to get it back to the Receiver, ie. the worker).
24 */
Patrick Williams4a9e4222023-08-18 09:57:28 -050025struct wait_process_completion : context_ref, bus::details::bus_friend
Patrick Williams74187662022-08-26 19:28:24 -050026{
Patrick Williams4a9e4222023-08-18 09:57:28 -050027 explicit wait_process_completion(context& ctx) : context_ref(ctx) {}
Patrick Williams74187662022-08-26 19:28:24 -050028 virtual ~wait_process_completion() = default;
29
30 // Called by the `caller` to indicate the Sender is completed.
31 virtual void complete() noexcept = 0;
Patrick Williams73e278b2022-09-16 08:31:36 -050032 // Called by the `caller` to indicate the Sender should be stopped.
33 virtual void stop() noexcept = 0;
Patrick Williams74187662022-08-26 19:28:24 -050034
35 // Arm the completion event.
36 void arm() noexcept;
37
38 // Data to share with the worker.
Patrick Williams435eb1b2022-09-16 16:22:07 -050039 event_t::time_resolution timeout{};
Patrick Williams74187662022-08-26 19:28:24 -050040
Patrick Williamsbbc181e2023-11-16 16:29:53 -060041 static auto loop(context& ctx) -> task<>;
Patrick Williams74187662022-08-26 19:28:24 -050042 static void wait_once(context& ctx);
43};
44
45/* The completion template based on receiver type.
46 *
47 * The type of the receivers (typically the co_awaiter) is only known by
48 * a template, so we need a sub-class of completion to hold the receiver.
49 */
50template <execution::receiver R>
51struct wait_process_operation : public wait_process_completion
52{
53 wait_process_operation(context& ctx, R r) :
54 wait_process_completion(ctx), receiver(std::move(r))
55 {}
56
57 wait_process_operation(wait_process_operation&&) = delete;
58
59 void complete() noexcept override final
60 {
61 execution::set_value(std::move(this->receiver));
62 }
63
Patrick Williams73e278b2022-09-16 08:31:36 -050064 void stop() noexcept override final
65 {
66 // Stop can be called when the context is shutting down,
67 // so treat it as if the receiver completed.
68 execution::set_value(std::move(this->receiver));
69 }
70
Patrick Williams74187662022-08-26 19:28:24 -050071 friend void tag_invoke(execution::start_t,
72 wait_process_operation& self) noexcept
73 {
74 self.arm();
75 }
76
77 R receiver;
78};
79
80/* The sender for the wait/process event. */
Patrick Williams4a9e4222023-08-18 09:57:28 -050081struct wait_process_sender : public context_ref
Patrick Williams74187662022-08-26 19:28:24 -050082{
Patrick Williams9c6ec9b2023-06-23 15:47:42 -050083 using is_sender = void;
84
Patrick Williams4a9e4222023-08-18 09:57:28 -050085 explicit wait_process_sender(context& ctx) : context_ref(ctx) {}
Patrick Williams74187662022-08-26 19:28:24 -050086
87 friend auto tag_invoke(execution::get_completion_signatures_t,
88 const wait_process_sender&, auto)
89 -> execution::completion_signatures<execution::set_value_t()>;
90
91 template <execution::receiver R>
92 friend auto tag_invoke(execution::connect_t, wait_process_sender&& self,
93 R r) -> wait_process_operation<R>
94 {
95 // Create the completion for the wait.
96 return {self.ctx, std::move(r)};
97 }
Patrick Williams74187662022-08-26 19:28:24 -050098};
99
Patrick Williamsbbc181e2023-11-16 16:29:53 -0600100auto wait_process_completion::loop(context& ctx) -> task<>
Patrick Williams74187662022-08-26 19:28:24 -0500101{
Patrick Williams78e436f2022-09-21 10:06:20 -0500102 while (!ctx.final_stop.stop_requested())
Patrick Williams74187662022-08-26 19:28:24 -0500103 {
Patrick Williamse8e66312023-12-01 16:57:29 -0600104 // Handle the next sdbus event. Completion likely happened on a
105 // different thread so we need to transfer back to the worker thread.
106 co_await execution::transfer(wait_process_sender(ctx),
107 ctx.loop.get_scheduler());
Patrick Williams74187662022-08-26 19:28:24 -0500108 }
Patrick Williams10483c92022-09-23 11:33:39 -0500109
110 {
111 std::lock_guard lock{ctx.lock};
112 ctx.wait_process_stopped = true;
113 }
Patrick Williams74187662022-08-26 19:28:24 -0500114}
115
116} // namespace details
117
118context::~context() noexcept(false)
119{
120 if (worker_thread.joinable())
121 {
122 throw std::logic_error(
123 "sdbusplus::async::context destructed without completion.");
124 }
125}
126
Patrick Williams3c242ba2022-09-23 09:51:55 -0500127void context::run()
Patrick Williams74187662022-08-26 19:28:24 -0500128{
Patrick Williams10483c92022-09-23 11:33:39 -0500129 // Run the primary portion of the run-loop.
130 caller_run();
Patrick Williams74187662022-08-26 19:28:24 -0500131
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500132 // This should be final_stop...
Patrick Williams10483c92022-09-23 11:33:39 -0500133
134 // We need to wait for the pending wait process and stop it.
135 wait_for_wait_process_stopped();
Patrick Williams4cfc2842022-09-22 09:53:33 -0500136
Patrick Williams78e436f2022-09-21 10:06:20 -0500137 // Wait for all the internal tasks to complete.
Patrick Williams97c31c82023-05-25 11:04:46 -0500138 stdexec::sync_wait(internal_tasks.on_empty());
Patrick Williamsc5b5ff52022-09-21 08:34:22 -0500139
Patrick Williams10483c92022-09-23 11:33:39 -0500140 // Finish up the loop and join the thread.
141 // (There shouldn't be anything going on by this point anyhow.)
Patrick Williams74187662022-08-26 19:28:24 -0500142 loop.finish();
143 if (worker_thread.joinable())
144 {
145 worker_thread.join();
146 }
147}
148
Patrick Williams3c242ba2022-09-23 09:51:55 -0500149void context::worker_run()
Patrick Williams74187662022-08-26 19:28:24 -0500150{
Patrick Williams3c242ba2022-09-23 09:51:55 -0500151 // Start the sdbus 'wait/process' loop; treat it as an internal task.
Patrick Williams97c31c82023-05-25 11:04:46 -0500152 internal_tasks.spawn(details::wait_process_completion::loop(*this));
Patrick Williams74187662022-08-26 19:28:24 -0500153
154 // Run the execution::run_loop to handle all the tasks.
155 loop.run();
156}
157
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500158void context::spawn_complete()
Patrick Williams10483c92022-09-23 11:33:39 -0500159{
160 {
161 std::lock_guard l{lock};
162 spawn_watcher_running = false;
Patrick Williams10483c92022-09-23 11:33:39 -0500163 }
164
165 if (stop_requested())
166 {
167 final_stop.request_stop();
168 }
169
170 caller_wait.notify_one();
171 event_loop.break_run();
172}
173
174void context::check_stop_requested()
175{
176 if (stop_requested())
177 {
178 throw std::logic_error(
179 "sdbusplus::async::context spawn called while already stopped.");
180 }
181}
182
183void context::spawn_watcher()
184{
185 {
186 std::lock_guard l{lock};
187 if (spawn_watcher_running)
188 {
189 return;
190 }
191
192 spawn_watcher_running = true;
193 }
194
195 // Spawn the watch for completion / exceptions.
Patrick Williams97c31c82023-05-25 11:04:46 -0500196 internal_tasks.spawn(pending_tasks.on_empty() |
197 execution::then([this]() { spawn_complete(); }));
Patrick Williams10483c92022-09-23 11:33:39 -0500198}
199
200void context::caller_run()
201{
202 // We are able to run the loop until the context is requested to stop or
203 // we get an exception.
204 auto keep_running = [this]() {
205 std::lock_guard l{lock};
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500206 return !final_stop.stop_requested();
Patrick Williams10483c92022-09-23 11:33:39 -0500207 };
208
209 // If we are suppose to keep running, start the run loop.
210 if (keep_running())
211 {
212 // Start up the worker thread.
213 if (!worker_thread.joinable())
214 {
215 worker_thread = std::thread{[this]() { worker_run(); }};
216 }
217 else
218 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500219 // We've already been running and there might a completion pending.
220 // Spawn a new watcher that checks for these.
Patrick Williams10483c92022-09-23 11:33:39 -0500221 spawn_watcher();
222 }
223
224 while (keep_running())
225 {
226 // Handle waiting on all the sd-events.
227 details::wait_process_completion::wait_once(*this);
228 }
229 }
230 else
231 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500232 // There might be pending completions still, so spawn a watcher for
233 // them.
Patrick Williams10483c92022-09-23 11:33:39 -0500234 spawn_watcher();
235 }
236}
237
238void context::wait_for_wait_process_stopped()
239{
240 auto worker = std::exchange(pending, nullptr);
241 while (worker == nullptr)
242 {
243 std::lock_guard l{lock};
244 if (wait_process_stopped)
245 {
246 break;
247 }
248
249 worker = std::exchange(staged, nullptr);
250 if (!worker)
251 {
252 std::this_thread::yield();
253 }
254 }
255 if (worker)
256 {
257 worker->stop();
258 wait_process_stopped = true;
259 }
260}
261
Patrick Williams74187662022-08-26 19:28:24 -0500262void details::wait_process_completion::arm() noexcept
263{
264 // Call process. True indicates something was handled and we do not
265 // need to `wait`, because there might be yet another pending operation
266 // to process, so immediately signal the operation as complete.
267 if (ctx.get_bus().process_discard())
268 {
269 this->complete();
270 return;
271 }
272
Patrick Williams73e278b2022-09-16 08:31:36 -0500273 // We need to call wait now, get the current timeout and stage ourselves
274 // as the next completion.
Patrick Williams74187662022-08-26 19:28:24 -0500275
276 // Get the bus' timeout.
Patrick Williams73e278b2022-09-16 08:31:36 -0500277 uint64_t to_usec = 0;
Patrick Williams1ee60d62023-08-18 12:59:05 -0500278 sd_bus_get_timeout(get_busp(ctx), &to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500279
Patrick Williams73e278b2022-09-16 08:31:36 -0500280 if (to_usec == UINT64_MAX)
Patrick Williams74187662022-08-26 19:28:24 -0500281 {
282 // sd_bus_get_timeout returns UINT64_MAX to indicate 'wait forever'.
Patrick Williams73e278b2022-09-16 08:31:36 -0500283 // Turn this into -1 for sd-event.
284 timeout = std::chrono::microseconds{-1};
Patrick Williams74187662022-08-26 19:28:24 -0500285 }
286 else
287 {
Patrick Williams73e278b2022-09-16 08:31:36 -0500288 timeout = std::chrono::microseconds(to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500289 }
290
291 // Assign ourselves as the pending completion and release the caller.
292 std::lock_guard lock{ctx.lock};
Patrick Williams73e278b2022-09-16 08:31:36 -0500293 ctx.staged = this;
Patrick Williams74187662022-08-26 19:28:24 -0500294 ctx.caller_wait.notify_one();
295}
296
297void details::wait_process_completion::wait_once(context& ctx)
298{
Patrick Williams74187662022-08-26 19:28:24 -0500299 // Scope for lock.
300 {
301 std::unique_lock lock{ctx.lock};
302
303 // If there isn't a completion waiting already, wait on the condition
304 // variable for one to show up (we can't call `poll` yet because we
305 // don't have the required parameters).
Patrick Williams73e278b2022-09-16 08:31:36 -0500306 ctx.caller_wait.wait(lock, [&] {
307 return (ctx.pending != nullptr) || (ctx.staged != nullptr) ||
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500308 ctx.final_stop.stop_requested();
Patrick Williams73e278b2022-09-16 08:31:36 -0500309 });
Patrick Williams74187662022-08-26 19:28:24 -0500310
Patrick Williams73e278b2022-09-16 08:31:36 -0500311 // Save the waiter as pending.
312 if (ctx.pending == nullptr)
313 {
314 ctx.pending = std::exchange(ctx.staged, nullptr);
315 }
Patrick Williams74187662022-08-26 19:28:24 -0500316 }
317
Patrick Williams73e278b2022-09-16 08:31:36 -0500318 // Run the event loop to process one request.
Patrick Williams10483c92022-09-23 11:33:39 -0500319 // If the context has been requested to be stopped, skip the event loop.
320 if (!ctx.final_stop.stop_requested() && ctx.pending)
Patrick Williams73e278b2022-09-16 08:31:36 -0500321 {
Patrick Williams10483c92022-09-23 11:33:39 -0500322 ctx.event_loop.run_one(ctx.pending->timeout);
Patrick Williams73e278b2022-09-16 08:31:36 -0500323 }
324}
325
326int context::dbus_event_handle(sd_event_source*, int, uint32_t, void* data)
327{
328 auto self = static_cast<context*>(data);
329
Patrick Williams10483c92022-09-23 11:33:39 -0500330 auto pending = std::exchange(self->pending, nullptr);
Patrick Williams73e278b2022-09-16 08:31:36 -0500331 if (pending != nullptr)
332 {
Patrick Williams10483c92022-09-23 11:33:39 -0500333 pending->complete();
Patrick Williams73e278b2022-09-16 08:31:36 -0500334 }
335
336 return 0;
Patrick Williams74187662022-08-26 19:28:24 -0500337}
338
339} // namespace sdbusplus::async