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 | |
| 42 | static task<> loop(context& ctx); |
| 43 | static void wait_once(context& ctx); |
| 44 | }; |
| 45 | |
| 46 | /* The completion template based on receiver type. |
| 47 | * |
| 48 | * The type of the receivers (typically the co_awaiter) is only known by |
| 49 | * a template, so we need a sub-class of completion to hold the receiver. |
| 50 | */ |
| 51 | template <execution::receiver R> |
| 52 | struct wait_process_operation : public wait_process_completion |
| 53 | { |
| 54 | wait_process_operation(context& ctx, R r) : |
| 55 | wait_process_completion(ctx), receiver(std::move(r)) |
| 56 | {} |
| 57 | |
| 58 | wait_process_operation(wait_process_operation&&) = delete; |
| 59 | |
| 60 | void complete() noexcept override final |
| 61 | { |
| 62 | execution::set_value(std::move(this->receiver)); |
| 63 | } |
| 64 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 65 | void stop() noexcept override final |
| 66 | { |
| 67 | // Stop can be called when the context is shutting down, |
| 68 | // so treat it as if the receiver completed. |
| 69 | execution::set_value(std::move(this->receiver)); |
| 70 | } |
| 71 | |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 72 | friend void tag_invoke(execution::start_t, |
| 73 | wait_process_operation& self) noexcept |
| 74 | { |
| 75 | self.arm(); |
| 76 | } |
| 77 | |
| 78 | R receiver; |
| 79 | }; |
| 80 | |
| 81 | /* The sender for the wait/process event. */ |
| 82 | struct wait_process_sender |
| 83 | { |
| 84 | explicit wait_process_sender(context& ctx) : ctx(ctx){}; |
| 85 | |
| 86 | friend auto tag_invoke(execution::get_completion_signatures_t, |
| 87 | const wait_process_sender&, auto) |
| 88 | -> execution::completion_signatures<execution::set_value_t()>; |
| 89 | |
| 90 | template <execution::receiver R> |
| 91 | friend auto tag_invoke(execution::connect_t, wait_process_sender&& self, |
| 92 | R r) -> wait_process_operation<R> |
| 93 | { |
| 94 | // Create the completion for the wait. |
| 95 | return {self.ctx, std::move(r)}; |
| 96 | } |
| 97 | |
| 98 | private: |
| 99 | context& ctx; |
| 100 | }; |
| 101 | |
| 102 | task<> wait_process_completion::loop(context& ctx) |
| 103 | { |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 104 | while (!ctx.final_stop.stop_requested()) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 105 | { |
| 106 | // Handle the next sdbus event. |
| 107 | co_await wait_process_sender(ctx); |
| 108 | |
| 109 | // Completion likely happened on the context 'caller' thread, so |
| 110 | // we need to switch to the worker thread. |
| 111 | co_await execution::schedule(ctx.loop.get_scheduler()); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | } // namespace details |
| 116 | |
| 117 | context::~context() noexcept(false) |
| 118 | { |
| 119 | if (worker_thread.joinable()) |
| 120 | { |
| 121 | throw std::logic_error( |
| 122 | "sdbusplus::async::context destructed without completion."); |
| 123 | } |
| 124 | } |
| 125 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 126 | bool context::request_stop() noexcept |
| 127 | { |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 128 | auto first_stop = initial_stop.request_stop(); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 129 | |
| 130 | if (first_stop) |
| 131 | { |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 132 | // Now that the workers have been requested to stop, we need to wait |
| 133 | // until they all drain and then stop the internal tasks. |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame^] | 134 | |
| 135 | auto complete = [this]() { |
| 136 | final_stop.request_stop(); |
| 137 | caller_wait.notify_one(); |
| 138 | event_loop.break_run(); |
| 139 | }; |
| 140 | |
| 141 | internal_tasks.spawn(pending_tasks.empty() | execution::then(complete) | |
| 142 | execution::upon_error([=](auto&& e) { |
| 143 | complete(); |
| 144 | std::rethrow_exception(e); |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 145 | })); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return first_stop; |
| 149 | } |
| 150 | |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 151 | void context::caller_run(task<> startup) |
| 152 | { |
| 153 | // Start up the worker thread. |
| 154 | worker_thread = std::thread{[this, startup = std::move(startup)]() mutable { |
| 155 | worker_run(std::move(startup)); |
| 156 | }}; |
| 157 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 158 | // Run until the context requested to stop. |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 159 | while (!final_stop.stop_requested()) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 160 | { |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 161 | // Handle waiting on all the sd-events. |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 162 | details::wait_process_completion::wait_once(*this); |
| 163 | } |
| 164 | |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame^] | 165 | std::exception_ptr pending_exception{}; |
| 166 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 167 | // Wait for all the internal tasks to complete. |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame^] | 168 | std::this_thread::sync_wait(internal_tasks.empty() | |
| 169 | execution::upon_error([&](auto&& e) { |
| 170 | pending_exception = std::move(e); |
| 171 | })); |
Patrick Williams | c5b5ff5 | 2022-09-21 08:34:22 -0500 | [diff] [blame] | 172 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 173 | // Stop has been requested, so finish up the loop. |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 174 | loop.finish(); |
| 175 | if (worker_thread.joinable()) |
| 176 | { |
| 177 | worker_thread.join(); |
| 178 | } |
Patrick Williams | 4cfc284 | 2022-09-22 09:53:33 -0500 | [diff] [blame^] | 179 | |
| 180 | // If there was an exception inside the context, rethrow it. |
| 181 | if (pending_exception) |
| 182 | { |
| 183 | std::rethrow_exception(std::move(pending_exception)); |
| 184 | } |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void context::worker_run(task<> startup) |
| 188 | { |
| 189 | // Begin the 'startup' task. |
| 190 | // This shouldn't start detached because we want to be able to forward |
| 191 | // failures back to the 'run'. execution::ensure_started isn't |
| 192 | // implemented yet, so we don't have a lot of other options. |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 193 | spawn(std::move(startup)); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 194 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 195 | // Also start the sdbus 'wait/process' loop; treat it as an internal task. |
| 196 | internal_tasks.spawn(details::wait_process_completion::loop(*this)); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 197 | |
| 198 | // Run the execution::run_loop to handle all the tasks. |
| 199 | loop.run(); |
| 200 | } |
| 201 | |
| 202 | void details::wait_process_completion::arm() noexcept |
| 203 | { |
| 204 | // Call process. True indicates something was handled and we do not |
| 205 | // need to `wait`, because there might be yet another pending operation |
| 206 | // to process, so immediately signal the operation as complete. |
| 207 | if (ctx.get_bus().process_discard()) |
| 208 | { |
| 209 | this->complete(); |
| 210 | return; |
| 211 | } |
| 212 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 213 | // We need to call wait now, get the current timeout and stage ourselves |
| 214 | // as the next completion. |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 215 | |
| 216 | // Get the bus' timeout. |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 217 | uint64_t to_usec = 0; |
| 218 | sd_bus_get_timeout(get_busp(ctx.get_bus()), &to_usec); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 219 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 220 | if (to_usec == UINT64_MAX) |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 221 | { |
| 222 | // sd_bus_get_timeout returns UINT64_MAX to indicate 'wait forever'. |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 223 | // Turn this into -1 for sd-event. |
| 224 | timeout = std::chrono::microseconds{-1}; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 225 | } |
| 226 | else |
| 227 | { |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 228 | timeout = std::chrono::microseconds(to_usec); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // Assign ourselves as the pending completion and release the caller. |
| 232 | std::lock_guard lock{ctx.lock}; |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 233 | ctx.staged = this; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 234 | ctx.caller_wait.notify_one(); |
| 235 | } |
| 236 | |
| 237 | void details::wait_process_completion::wait_once(context& ctx) |
| 238 | { |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 239 | // Scope for lock. |
| 240 | { |
| 241 | std::unique_lock lock{ctx.lock}; |
| 242 | |
| 243 | // If there isn't a completion waiting already, wait on the condition |
| 244 | // variable for one to show up (we can't call `poll` yet because we |
| 245 | // don't have the required parameters). |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 246 | ctx.caller_wait.wait(lock, [&] { |
| 247 | return (ctx.pending != nullptr) || (ctx.staged != nullptr) || |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 248 | (ctx.final_stop.stop_requested()); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 249 | }); |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 250 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 251 | // Save the waiter as pending. |
| 252 | if (ctx.pending == nullptr) |
| 253 | { |
| 254 | ctx.pending = std::exchange(ctx.staged, nullptr); |
| 255 | } |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 258 | // If the context has been requested to be stopped, exit now instead of |
| 259 | // running the context event loop. |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 260 | if (ctx.final_stop.stop_requested()) |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 261 | { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // Run the event loop to process one request. |
| 266 | ctx.event_loop.run_one(ctx.pending->timeout); |
| 267 | |
| 268 | // If there is a stop requested, we need to stop the pending operation. |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 269 | if (ctx.final_stop.stop_requested()) |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 270 | { |
| 271 | decltype(ctx.pending) pending = nullptr; |
| 272 | |
| 273 | { |
| 274 | std::lock_guard lock{ctx.lock}; |
| 275 | pending = std::exchange(ctx.pending, nullptr); |
| 276 | } |
| 277 | |
| 278 | // Do the stop outside the lock to prevent potential deadlocks due to |
| 279 | // the stop handler running. |
| 280 | if (pending != nullptr) |
| 281 | { |
| 282 | pending->stop(); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | int context::dbus_event_handle(sd_event_source*, int, uint32_t, void* data) |
| 288 | { |
| 289 | auto self = static_cast<context*>(data); |
| 290 | |
| 291 | decltype(self->pending) pending = nullptr; |
| 292 | { |
| 293 | std::lock_guard lock{self->lock}; |
| 294 | pending = std::exchange(self->pending, nullptr); |
| 295 | } |
| 296 | |
| 297 | // Outside the lock complete the pending operation. |
| 298 | // |
| 299 | // This can cause the Receiver task (the worker) to start executing (on |
| 300 | // this thread!), hence we do not want the lock held in order to avoid |
| 301 | // deadlocks. |
| 302 | if (pending != nullptr) |
| 303 | { |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 304 | if (self->final_stop.stop_requested()) |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 305 | { |
| 306 | pending->stop(); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | pending->complete(); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return 0; |
Patrick Williams | 7418766 | 2022-08-26 19:28:24 -0500 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | } // namespace sdbusplus::async |