Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 1 | #include <systemd/sd-bus.h> |
| 2 | |
| 3 | #include <sdbusplus/async/context.hpp> |
| 4 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 5 | #include <chrono> |
| 6 | |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 7 | namespace sdbusplus::async |
| 8 | { |
| 9 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 10 | context::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 Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 16 | namespace 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 | */ |
| 25 | struct wait_process_completion : bus::details::bus_friend |
| 26 | { |
| 27 | explicit wait_process_completion(context& ctx) : ctx(ctx) {} |
| 28 | virtual ~wait_process_completion() = default; |
| 29 | |
| 30 | // Called by the `caller` to indicate the Sender is completed. |
| 31 | virtual void complete() noexcept = 0; |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 32 | // Called by the `caller` to indicate the Sender should be stopped. |
| 33 | virtual void stop() noexcept = 0; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 34 | |
| 35 | // Arm the completion event. |
| 36 | void arm() noexcept; |
| 37 | |
| 38 | // Data to share with the worker. |
| 39 | context& ctx; |
Patrick Williams | 435eb1b | 2022-09-16 16:22:07 -0500 | [diff] [blame] | 40 | event_t::time_resolution timeout{}; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 41 | |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 42 | // TODO: It seems like we should be able to do a normal `task<>` here |
| 43 | // but spawn on it compile fails. |
| 44 | static exec::basic_task<void, exec::__task::__raw_task_context<void>> |
| 45 | loop(context& ctx); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 46 | static void wait_once(context& ctx); |
| 47 | }; |
| 48 | |
| 49 | /* The completion template based on receiver type. |
| 50 | * |
| 51 | * The type of the receivers (typically the co_awaiter) is only known by |
| 52 | * a template, so we need a sub-class of completion to hold the receiver. |
| 53 | */ |
| 54 | template <execution::receiver R> |
| 55 | struct wait_process_operation : public wait_process_completion |
| 56 | { |
| 57 | wait_process_operation(context& ctx, R r) : |
| 58 | wait_process_completion(ctx), receiver(std::move(r)) |
| 59 | {} |
| 60 | |
| 61 | wait_process_operation(wait_process_operation&&) = delete; |
| 62 | |
| 63 | void complete() noexcept override final |
| 64 | { |
| 65 | execution::set_value(std::move(this->receiver)); |
| 66 | } |
| 67 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 68 | void stop() noexcept override final |
| 69 | { |
| 70 | // Stop can be called when the context is shutting down, |
| 71 | // so treat it as if the receiver completed. |
| 72 | execution::set_value(std::move(this->receiver)); |
| 73 | } |
| 74 | |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 75 | friend void tag_invoke(execution::start_t, |
| 76 | wait_process_operation& self) noexcept |
| 77 | { |
| 78 | self.arm(); |
| 79 | } |
| 80 | |
| 81 | R receiver; |
| 82 | }; |
| 83 | |
| 84 | /* The sender for the wait/process event. */ |
| 85 | struct wait_process_sender |
| 86 | { |
Patrick Williams | 9c6ec9b | 2023-06-23 15:47:42 -0500 | [diff] [blame] | 87 | using is_sender = void; |
| 88 | |
Ed Tanous | b8be599 | 2023-01-04 15:56:20 -0800 | [diff] [blame] | 89 | explicit wait_process_sender(context& ctx) : ctx(ctx) {} |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 90 | |
| 91 | friend auto tag_invoke(execution::get_completion_signatures_t, |
| 92 | const wait_process_sender&, auto) |
| 93 | -> execution::completion_signatures<execution::set_value_t()>; |
| 94 | |
| 95 | template <execution::receiver R> |
| 96 | friend auto tag_invoke(execution::connect_t, wait_process_sender&& self, |
| 97 | R r) -> wait_process_operation<R> |
| 98 | { |
| 99 | // Create the completion for the wait. |
| 100 | return {self.ctx, std::move(r)}; |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | context& ctx; |
| 105 | }; |
| 106 | |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 107 | exec::basic_task<void, exec::__task::__raw_task_context<void>> |
| 108 | wait_process_completion::loop(context& ctx) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 109 | { |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 110 | while (!ctx.final_stop.stop_requested()) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 111 | { |
| 112 | // Handle the next sdbus event. |
| 113 | co_await wait_process_sender(ctx); |
| 114 | |
| 115 | // Completion likely happened on the context 'caller' thread, so |
| 116 | // we need to switch to the worker thread. |
| 117 | co_await execution::schedule(ctx.loop.get_scheduler()); |
| 118 | } |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 119 | |
| 120 | { |
| 121 | std::lock_guard lock{ctx.lock}; |
| 122 | ctx.wait_process_stopped = true; |
| 123 | } |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace details |
| 127 | |
| 128 | context::~context() noexcept(false) |
| 129 | { |
| 130 | if (worker_thread.joinable()) |
| 131 | { |
| 132 | throw std::logic_error( |
| 133 | "sdbusplus::async::context destructed without completion."); |
| 134 | } |
| 135 | } |
| 136 | |
Patrick Williams | 3c242ba | 2022-09-23 09:51:55 -0500 | [diff] [blame] | 137 | void context::run() |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 138 | { |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 139 | // Run the primary portion of the run-loop. |
| 140 | caller_run(); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 141 | |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 142 | // This should be final_stop... |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 143 | |
| 144 | // We need to wait for the pending wait process and stop it. |
| 145 | wait_for_wait_process_stopped(); |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame] | 146 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 147 | // Wait for all the internal tasks to complete. |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 148 | stdexec::sync_wait(internal_tasks.on_empty()); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 149 | |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 150 | // Finish up the loop and join the thread. |
| 151 | // (There shouldn't be anything going on by this point anyhow.) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 152 | loop.finish(); |
| 153 | if (worker_thread.joinable()) |
| 154 | { |
| 155 | worker_thread.join(); |
| 156 | } |
| 157 | } |
| 158 | |
Patrick Williams | 3c242ba | 2022-09-23 09:51:55 -0500 | [diff] [blame] | 159 | void context::worker_run() |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 160 | { |
Patrick Williams | 3c242ba | 2022-09-23 09:51:55 -0500 | [diff] [blame] | 161 | // Start the sdbus 'wait/process' loop; treat it as an internal task. |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 162 | internal_tasks.spawn(details::wait_process_completion::loop(*this)); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 163 | |
| 164 | // Run the execution::run_loop to handle all the tasks. |
| 165 | loop.run(); |
| 166 | } |
| 167 | |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 168 | void context::spawn_complete() |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 169 | { |
| 170 | { |
| 171 | std::lock_guard l{lock}; |
| 172 | spawn_watcher_running = false; |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (stop_requested()) |
| 176 | { |
| 177 | final_stop.request_stop(); |
| 178 | } |
| 179 | |
| 180 | caller_wait.notify_one(); |
| 181 | event_loop.break_run(); |
| 182 | } |
| 183 | |
| 184 | void context::check_stop_requested() |
| 185 | { |
| 186 | if (stop_requested()) |
| 187 | { |
| 188 | throw std::logic_error( |
| 189 | "sdbusplus::async::context spawn called while already stopped."); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void context::spawn_watcher() |
| 194 | { |
| 195 | { |
| 196 | std::lock_guard l{lock}; |
| 197 | if (spawn_watcher_running) |
| 198 | { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | spawn_watcher_running = true; |
| 203 | } |
| 204 | |
| 205 | // Spawn the watch for completion / exceptions. |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 206 | internal_tasks.spawn(pending_tasks.on_empty() | |
| 207 | execution::then([this]() { spawn_complete(); })); |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void context::caller_run() |
| 211 | { |
| 212 | // We are able to run the loop until the context is requested to stop or |
| 213 | // we get an exception. |
| 214 | auto keep_running = [this]() { |
| 215 | std::lock_guard l{lock}; |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 216 | return !final_stop.stop_requested(); |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | // If we are suppose to keep running, start the run loop. |
| 220 | if (keep_running()) |
| 221 | { |
| 222 | // Start up the worker thread. |
| 223 | if (!worker_thread.joinable()) |
| 224 | { |
| 225 | worker_thread = std::thread{[this]() { worker_run(); }}; |
| 226 | } |
| 227 | else |
| 228 | { |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 229 | // We've already been running and there might a completion pending. |
| 230 | // Spawn a new watcher that checks for these. |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 231 | spawn_watcher(); |
| 232 | } |
| 233 | |
| 234 | while (keep_running()) |
| 235 | { |
| 236 | // Handle waiting on all the sd-events. |
| 237 | details::wait_process_completion::wait_once(*this); |
| 238 | } |
| 239 | } |
| 240 | else |
| 241 | { |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 242 | // There might be pending completions still, so spawn a watcher for |
| 243 | // them. |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 244 | spawn_watcher(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | void context::wait_for_wait_process_stopped() |
| 249 | { |
| 250 | auto worker = std::exchange(pending, nullptr); |
| 251 | while (worker == nullptr) |
| 252 | { |
| 253 | std::lock_guard l{lock}; |
| 254 | if (wait_process_stopped) |
| 255 | { |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | worker = std::exchange(staged, nullptr); |
| 260 | if (!worker) |
| 261 | { |
| 262 | std::this_thread::yield(); |
| 263 | } |
| 264 | } |
| 265 | if (worker) |
| 266 | { |
| 267 | worker->stop(); |
| 268 | wait_process_stopped = true; |
| 269 | } |
| 270 | } |
| 271 | |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 272 | void details::wait_process_completion::arm() noexcept |
| 273 | { |
| 274 | // Call process. True indicates something was handled and we do not |
| 275 | // need to `wait`, because there might be yet another pending operation |
| 276 | // to process, so immediately signal the operation as complete. |
| 277 | if (ctx.get_bus().process_discard()) |
| 278 | { |
| 279 | this->complete(); |
| 280 | return; |
| 281 | } |
| 282 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 283 | // We need to call wait now, get the current timeout and stage ourselves |
| 284 | // as the next completion. |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 285 | |
| 286 | // Get the bus' timeout. |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 287 | uint64_t to_usec = 0; |
| 288 | sd_bus_get_timeout(get_busp(ctx.get_bus()), &to_usec); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 289 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 290 | if (to_usec == UINT64_MAX) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 291 | { |
| 292 | // sd_bus_get_timeout returns UINT64_MAX to indicate 'wait forever'. |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 293 | // Turn this into -1 for sd-event. |
| 294 | timeout = std::chrono::microseconds{-1}; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 295 | } |
| 296 | else |
| 297 | { |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 298 | timeout = std::chrono::microseconds(to_usec); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | // Assign ourselves as the pending completion and release the caller. |
| 302 | std::lock_guard lock{ctx.lock}; |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 303 | ctx.staged = this; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 304 | ctx.caller_wait.notify_one(); |
| 305 | } |
| 306 | |
| 307 | void details::wait_process_completion::wait_once(context& ctx) |
| 308 | { |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 309 | // Scope for lock. |
| 310 | { |
| 311 | std::unique_lock lock{ctx.lock}; |
| 312 | |
| 313 | // If there isn't a completion waiting already, wait on the condition |
| 314 | // variable for one to show up (we can't call `poll` yet because we |
| 315 | // don't have the required parameters). |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 316 | ctx.caller_wait.wait(lock, [&] { |
| 317 | return (ctx.pending != nullptr) || (ctx.staged != nullptr) || |
Patrick Williams | 5d16a8e | 2023-05-25 12:13:24 -0500 | [diff] [blame] | 318 | ctx.final_stop.stop_requested(); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 319 | }); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 320 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 321 | // Save the waiter as pending. |
| 322 | if (ctx.pending == nullptr) |
| 323 | { |
| 324 | ctx.pending = std::exchange(ctx.staged, nullptr); |
| 325 | } |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 326 | } |
| 327 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 328 | // Run the event loop to process one request. |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 329 | // If the context has been requested to be stopped, skip the event loop. |
| 330 | if (!ctx.final_stop.stop_requested() && ctx.pending) |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 331 | { |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 332 | ctx.event_loop.run_one(ctx.pending->timeout); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | int context::dbus_event_handle(sd_event_source*, int, uint32_t, void* data) |
| 337 | { |
| 338 | auto self = static_cast<context*>(data); |
| 339 | |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 340 | auto pending = std::exchange(self->pending, nullptr); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 341 | if (pending != nullptr) |
| 342 | { |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 343 | pending->complete(); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return 0; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | } // namespace sdbusplus::async |