blob: 8ddbe5838c670bb0e58c185196e094f746e1e5aa [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 {
104 // Handle the next sdbus event.
105 co_await wait_process_sender(ctx);
106
107 // Completion likely happened on the context 'caller' thread, so
108 // we need to switch to the worker thread.
109 co_await execution::schedule(ctx.loop.get_scheduler());
110 }
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 Williams3c242ba2022-09-23 09:51:55 -0500151void context::worker_run()
Patrick Williams74187662022-08-26 19:28:24 -0500152{
Patrick Williams3c242ba2022-09-23 09:51:55 -0500153 // Start the sdbus 'wait/process' loop; treat it as an internal task.
Patrick Williams97c31c82023-05-25 11:04:46 -0500154 internal_tasks.spawn(details::wait_process_completion::loop(*this));
Patrick Williams74187662022-08-26 19:28:24 -0500155
156 // Run the execution::run_loop to handle all the tasks.
157 loop.run();
158}
159
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500160void context::spawn_complete()
Patrick Williams10483c92022-09-23 11:33:39 -0500161{
162 {
163 std::lock_guard l{lock};
164 spawn_watcher_running = false;
Patrick Williams10483c92022-09-23 11:33:39 -0500165 }
166
167 if (stop_requested())
168 {
169 final_stop.request_stop();
170 }
171
172 caller_wait.notify_one();
173 event_loop.break_run();
174}
175
176void context::check_stop_requested()
177{
178 if (stop_requested())
179 {
180 throw std::logic_error(
181 "sdbusplus::async::context spawn called while already stopped.");
182 }
183}
184
185void context::spawn_watcher()
186{
187 {
188 std::lock_guard l{lock};
189 if (spawn_watcher_running)
190 {
191 return;
192 }
193
194 spawn_watcher_running = true;
195 }
196
197 // Spawn the watch for completion / exceptions.
Patrick Williams97c31c82023-05-25 11:04:46 -0500198 internal_tasks.spawn(pending_tasks.on_empty() |
199 execution::then([this]() { spawn_complete(); }));
Patrick Williams10483c92022-09-23 11:33:39 -0500200}
201
202void context::caller_run()
203{
204 // We are able to run the loop until the context is requested to stop or
205 // we get an exception.
206 auto keep_running = [this]() {
207 std::lock_guard l{lock};
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500208 return !final_stop.stop_requested();
Patrick Williams10483c92022-09-23 11:33:39 -0500209 };
210
211 // If we are suppose to keep running, start the run loop.
212 if (keep_running())
213 {
214 // Start up the worker thread.
215 if (!worker_thread.joinable())
216 {
217 worker_thread = std::thread{[this]() { worker_run(); }};
218 }
219 else
220 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500221 // We've already been running and there might a completion pending.
222 // Spawn a new watcher that checks for these.
Patrick Williams10483c92022-09-23 11:33:39 -0500223 spawn_watcher();
224 }
225
226 while (keep_running())
227 {
228 // Handle waiting on all the sd-events.
229 details::wait_process_completion::wait_once(*this);
230 }
231 }
232 else
233 {
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500234 // There might be pending completions still, so spawn a watcher for
235 // them.
Patrick Williams10483c92022-09-23 11:33:39 -0500236 spawn_watcher();
237 }
238}
239
240void context::wait_for_wait_process_stopped()
241{
242 auto worker = std::exchange(pending, nullptr);
243 while (worker == nullptr)
244 {
245 std::lock_guard l{lock};
246 if (wait_process_stopped)
247 {
248 break;
249 }
250
251 worker = std::exchange(staged, nullptr);
252 if (!worker)
253 {
254 std::this_thread::yield();
255 }
256 }
257 if (worker)
258 {
259 worker->stop();
260 wait_process_stopped = true;
261 }
262}
263
Patrick Williams74187662022-08-26 19:28:24 -0500264void details::wait_process_completion::arm() noexcept
265{
266 // Call process. True indicates something was handled and we do not
267 // need to `wait`, because there might be yet another pending operation
268 // to process, so immediately signal the operation as complete.
269 if (ctx.get_bus().process_discard())
270 {
271 this->complete();
272 return;
273 }
274
Patrick Williams73e278b2022-09-16 08:31:36 -0500275 // We need to call wait now, get the current timeout and stage ourselves
276 // as the next completion.
Patrick Williams74187662022-08-26 19:28:24 -0500277
278 // Get the bus' timeout.
Patrick Williams73e278b2022-09-16 08:31:36 -0500279 uint64_t to_usec = 0;
Patrick Williams1ee60d62023-08-18 12:59:05 -0500280 sd_bus_get_timeout(get_busp(ctx), &to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500281
Patrick Williams73e278b2022-09-16 08:31:36 -0500282 if (to_usec == UINT64_MAX)
Patrick Williams74187662022-08-26 19:28:24 -0500283 {
284 // sd_bus_get_timeout returns UINT64_MAX to indicate 'wait forever'.
Patrick Williams73e278b2022-09-16 08:31:36 -0500285 // Turn this into -1 for sd-event.
286 timeout = std::chrono::microseconds{-1};
Patrick Williams74187662022-08-26 19:28:24 -0500287 }
288 else
289 {
Patrick Williams73e278b2022-09-16 08:31:36 -0500290 timeout = std::chrono::microseconds(to_usec);
Patrick Williams74187662022-08-26 19:28:24 -0500291 }
292
293 // Assign ourselves as the pending completion and release the caller.
294 std::lock_guard lock{ctx.lock};
Patrick Williams73e278b2022-09-16 08:31:36 -0500295 ctx.staged = this;
Patrick Williams74187662022-08-26 19:28:24 -0500296 ctx.caller_wait.notify_one();
297}
298
299void details::wait_process_completion::wait_once(context& ctx)
300{
Patrick Williams74187662022-08-26 19:28:24 -0500301 // Scope for lock.
302 {
303 std::unique_lock lock{ctx.lock};
304
305 // If there isn't a completion waiting already, wait on the condition
306 // variable for one to show up (we can't call `poll` yet because we
307 // don't have the required parameters).
Patrick Williams73e278b2022-09-16 08:31:36 -0500308 ctx.caller_wait.wait(lock, [&] {
309 return (ctx.pending != nullptr) || (ctx.staged != nullptr) ||
Patrick Williams5d16a8e2023-05-25 12:13:24 -0500310 ctx.final_stop.stop_requested();
Patrick Williams73e278b2022-09-16 08:31:36 -0500311 });
Patrick Williams74187662022-08-26 19:28:24 -0500312
Patrick Williams73e278b2022-09-16 08:31:36 -0500313 // Save the waiter as pending.
314 if (ctx.pending == nullptr)
315 {
316 ctx.pending = std::exchange(ctx.staged, nullptr);
317 }
Patrick Williams74187662022-08-26 19:28:24 -0500318 }
319
Patrick Williams73e278b2022-09-16 08:31:36 -0500320 // Run the event loop to process one request.
Patrick Williams10483c92022-09-23 11:33:39 -0500321 // If the context has been requested to be stopped, skip the event loop.
322 if (!ctx.final_stop.stop_requested() && ctx.pending)
Patrick Williams73e278b2022-09-16 08:31:36 -0500323 {
Patrick Williams10483c92022-09-23 11:33:39 -0500324 ctx.event_loop.run_one(ctx.pending->timeout);
Patrick Williams73e278b2022-09-16 08:31:36 -0500325 }
326}
327
328int context::dbus_event_handle(sd_event_source*, int, uint32_t, void* data)
329{
330 auto self = static_cast<context*>(data);
331
Patrick Williams10483c92022-09-23 11:33:39 -0500332 auto pending = std::exchange(self->pending, nullptr);
Patrick Williams73e278b2022-09-16 08:31:36 -0500333 if (pending != nullptr)
334 {
Patrick Williams10483c92022-09-23 11:33:39 -0500335 pending->complete();
Patrick Williams73e278b2022-09-16 08:31:36 -0500336 }
337
338 return 0;
Patrick Williams74187662022-08-26 19:28:24 -0500339}
340
341} // namespace sdbusplus::async