Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_RUNTIME_H_ |
| 2 | #define AOS_EVENTS_EVENT_LOOP_RUNTIME_H_ |
| 3 | |
| 4 | // Exposes the primitives to implement an async Rust runtime on top of an |
| 5 | // EventLoop. This is not intended to be used directly, so the APIs are not |
| 6 | // particularly ergonomic for C++. See the Rust wrapper for detailed |
| 7 | // documentation. |
| 8 | |
Adam Snaider | cc8c2f7 | 2023-06-25 20:56:13 -0700 | [diff] [blame^] | 9 | #include <chrono> |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <optional> |
| 12 | |
| 13 | #include "aos/events/event_loop.h" |
| 14 | #include "aos/for_rust.h" |
| 15 | #include "cxx.h" |
| 16 | |
| 17 | namespace aos { |
| 18 | |
| 19 | // An alternative version of Context to feed autocxx, to work around |
| 20 | // https://github.com/google/autocxx/issues/787. |
| 21 | /// <div rustbindgen replaces="aos::Context"></div> |
| 22 | struct RustContext { |
| 23 | int64_t monotonic_event_time; |
| 24 | int64_t realtime_event_time; |
| 25 | |
| 26 | int64_t monotonic_remote_time; |
| 27 | int64_t realtime_remote_time; |
| 28 | |
| 29 | uint32_t queue_index; |
| 30 | uint32_t remote_queue_index; |
| 31 | |
| 32 | size_t size; |
| 33 | const void *data; |
| 34 | |
| 35 | int buffer_index; |
| 36 | |
| 37 | // Work around https://github.com/google/autocxx/issues/266. |
| 38 | uint8_t source_boot_uuid[16]; |
| 39 | }; |
| 40 | |
| 41 | static_assert(sizeof(Context) == sizeof(RustContext)); |
| 42 | static_assert(alignof(Context) == alignof(RustContext)); |
| 43 | static_assert(offsetof(Context, monotonic_event_time) == |
| 44 | offsetof(RustContext, monotonic_event_time)); |
| 45 | static_assert(offsetof(Context, realtime_event_time) == |
| 46 | offsetof(RustContext, realtime_event_time)); |
| 47 | static_assert(offsetof(Context, monotonic_remote_time) == |
| 48 | offsetof(RustContext, monotonic_remote_time)); |
| 49 | static_assert(offsetof(Context, realtime_remote_time) == |
| 50 | offsetof(RustContext, realtime_remote_time)); |
| 51 | static_assert(offsetof(Context, queue_index) == |
| 52 | offsetof(RustContext, queue_index)); |
| 53 | static_assert(offsetof(Context, remote_queue_index) == |
| 54 | offsetof(RustContext, remote_queue_index)); |
| 55 | static_assert(offsetof(Context, size) == offsetof(RustContext, size)); |
| 56 | static_assert(offsetof(Context, data) == offsetof(RustContext, data)); |
| 57 | static_assert(offsetof(Context, buffer_index) == |
| 58 | offsetof(RustContext, buffer_index)); |
| 59 | static_assert(offsetof(Context, source_boot_uuid) == |
| 60 | offsetof(RustContext, source_boot_uuid)); |
| 61 | static_assert(sizeof(Context::source_boot_uuid) == |
| 62 | sizeof(RustContext::source_boot_uuid)); |
| 63 | static_assert(sizeof(RustContext) == sizeof(Context), |
| 64 | "Update this when adding or removing fields"); |
| 65 | |
| 66 | // Similar to Rust's `Future<Output = Never>`. |
| 67 | class ApplicationFuture { |
| 68 | public: |
| 69 | ApplicationFuture() = default; |
| 70 | virtual ~ApplicationFuture() = default; |
| 71 | |
| 72 | // Calls a Rust `Future::poll`, with a waker that will panic if used. Because |
| 73 | // our Future's Output is Never, the inner Rust implementation can only return |
| 74 | // Poll::Pending, which is equivalent to void. |
Brian Silverman | 1431a77 | 2022-08-31 20:44:36 -0700 | [diff] [blame] | 75 | // |
| 76 | // Returns true if it succeeded, or false if the Rust code paniced. |
| 77 | virtual bool Poll() = 0; |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // Similar to Rust's `Stream<Item = const Option&>`. |
| 81 | class WatcherForRust { |
| 82 | public: |
| 83 | WatcherForRust(std::unique_ptr<RawFetcher> fetcher) |
| 84 | : fetcher_(std::move(fetcher)) {} |
| 85 | ~WatcherForRust() = default; |
| 86 | |
| 87 | const Context *PollNext() { |
| 88 | if (!fetcher_->FetchNext()) { |
| 89 | return nullptr; |
| 90 | } |
| 91 | return &fetcher_->context(); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | const std::unique_ptr<RawFetcher> fetcher_; |
| 96 | }; |
| 97 | |
| 98 | class SenderForRust { |
| 99 | public: |
| 100 | SenderForRust(std::unique_ptr<RawSender> sender) |
| 101 | : sender_(std::move(sender)) {} |
| 102 | ~SenderForRust() = default; |
| 103 | |
| 104 | uint8_t *data() { return reinterpret_cast<uint8_t *>(sender_->data()); } |
| 105 | size_t size() { return sender_->size(); } |
| 106 | RawSender::Error SendBuffer(size_t size) { return sender_->Send(size); } |
| 107 | RawSender::Error CopyAndSend(const uint8_t *data, size_t size) { |
| 108 | return sender_->Send(data, size); |
| 109 | } |
| 110 | |
| 111 | private: |
| 112 | const std::unique_ptr<RawSender> sender_; |
| 113 | }; |
| 114 | |
| 115 | class FetcherForRust { |
| 116 | public: |
| 117 | FetcherForRust(std::unique_ptr<RawFetcher> fetcher) |
| 118 | : fetcher_(std::move(fetcher)) {} |
| 119 | ~FetcherForRust() = default; |
| 120 | |
| 121 | bool FetchNext() { return fetcher_->FetchNext(); } |
| 122 | bool Fetch() { return fetcher_->Fetch(); } |
| 123 | |
| 124 | const Context &context() const { return fetcher_->context(); } |
| 125 | |
| 126 | private: |
| 127 | const std::unique_ptr<RawFetcher> fetcher_; |
| 128 | }; |
| 129 | |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 130 | class EventLoopRuntime; |
| 131 | |
| 132 | class OnRunForRust { |
| 133 | public: |
| 134 | OnRunForRust(EventLoopRuntime *runtime); |
| 135 | ~OnRunForRust(); |
| 136 | |
| 137 | bool is_running() const; |
| 138 | |
| 139 | private: |
| 140 | EventLoopRuntime *const runtime_; |
| 141 | }; |
| 142 | |
Adam Snaider | cc8c2f7 | 2023-06-25 20:56:13 -0700 | [diff] [blame^] | 143 | class TimerForRust { |
| 144 | public: |
| 145 | static std::unique_ptr<TimerForRust> Make(EventLoopRuntime *runtime); |
| 146 | |
| 147 | TimerForRust(const TimerForRust &) = delete; |
| 148 | TimerForRust(TimerForRust &&) = delete; |
| 149 | |
| 150 | TimerForRust &operator=(const TimerForRust &) = delete; |
| 151 | TimerForRust &operator=(TimerForRust &&) = delete; |
| 152 | |
| 153 | ~TimerForRust() { timer_->Disable(); } |
| 154 | |
| 155 | void Schedule(int64_t base, int64_t repeat_offset) { |
| 156 | timer_->Schedule( |
| 157 | monotonic_clock::time_point(std::chrono::nanoseconds(base)), |
| 158 | std::chrono::nanoseconds(repeat_offset)); |
| 159 | } |
| 160 | |
| 161 | void Disable() { timer_->Disable(); } |
| 162 | |
| 163 | bool IsDisabled() const { return timer_->IsDisabled(); } |
| 164 | |
| 165 | void set_name(rust::Str name) { timer_->set_name(RustStrToStringView(name)); } |
| 166 | rust::Str name() const { return StringViewToRustStr(timer_->name()); } |
| 167 | |
| 168 | // If true, the timer is expired. |
| 169 | bool Poll(); |
| 170 | |
| 171 | private: |
| 172 | TimerForRust() = default; |
| 173 | |
| 174 | TimerHandler *timer_; |
| 175 | bool expired_ = false; |
| 176 | }; |
| 177 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 178 | class EventLoopRuntime { |
| 179 | public: |
| 180 | EventLoopRuntime(EventLoop *event_loop) : event_loop_(event_loop) {} |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 181 | ~EventLoopRuntime() { |
Brian Silverman | 1431a77 | 2022-08-31 20:44:36 -0700 | [diff] [blame] | 182 | // Do this first, because it may hold child objects. |
| 183 | task_.reset(); |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 184 | CHECK_EQ(child_count_, 0) |
| 185 | << ": Some child objects were not destroyed first"; |
| 186 | } |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 187 | |
| 188 | EventLoop *event_loop() { return event_loop_; } |
| 189 | |
Brian Silverman | 1431a77 | 2022-08-31 20:44:36 -0700 | [diff] [blame] | 190 | void Spawn(std::unique_ptr<ApplicationFuture> task) { |
| 191 | CHECK(!task_) << ": May only call Spawn once"; |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 192 | task_ = std::move(task); |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 193 | DoPoll(); |
| 194 | // Just do this unconditionally, so we don't have to keep track of each |
| 195 | // OnRun to only do it once. If Rust doesn't use OnRun, it's harmless to do |
| 196 | // an extra poll. |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 197 | event_loop_->OnRun([this] { DoPoll(); }); |
| 198 | } |
| 199 | |
| 200 | const Configuration *configuration() const { |
| 201 | return event_loop_->configuration(); |
| 202 | } |
| 203 | const Node *node() const { return event_loop_->node(); } |
| 204 | |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 205 | bool is_running() const { return event_loop_->is_running(); } |
| 206 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 207 | // autocxx generates broken C++ code for `time_point`, see |
| 208 | // https://github.com/google/autocxx/issues/787. |
| 209 | int64_t monotonic_now() const { |
| 210 | return std::chrono::nanoseconds( |
| 211 | event_loop_->monotonic_now().time_since_epoch()) |
| 212 | .count(); |
| 213 | } |
| 214 | int64_t realtime_now() const { |
| 215 | return std::chrono::nanoseconds( |
| 216 | event_loop_->realtime_now().time_since_epoch()) |
| 217 | .count(); |
| 218 | } |
| 219 | |
| 220 | rust::Str name() const { return StringViewToRustStr(event_loop_->name()); } |
| 221 | |
| 222 | WatcherForRust MakeWatcher(const Channel *channel) { |
| 223 | event_loop_->MakeRawNoArgWatcher(channel, |
| 224 | [this](const Context &) { DoPoll(); }); |
| 225 | return WatcherForRust(event_loop_->MakeRawFetcher(channel)); |
| 226 | } |
| 227 | |
| 228 | SenderForRust MakeSender(const Channel *channel) { |
| 229 | return SenderForRust(event_loop_->MakeRawSender(channel)); |
| 230 | } |
| 231 | |
| 232 | FetcherForRust MakeFetcher(const Channel *channel) { |
| 233 | return FetcherForRust(event_loop_->MakeRawFetcher(channel)); |
| 234 | } |
| 235 | |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 236 | OnRunForRust MakeOnRun() { return OnRunForRust(this); } |
| 237 | |
Adam Snaider | cc8c2f7 | 2023-06-25 20:56:13 -0700 | [diff] [blame^] | 238 | std::unique_ptr<TimerForRust> AddTimer() { return TimerForRust::Make(this); } |
| 239 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 240 | private: |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 241 | friend class OnRunForRust; |
Adam Snaider | cc8c2f7 | 2023-06-25 20:56:13 -0700 | [diff] [blame^] | 242 | friend class TimerForRust; |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 243 | |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 244 | // Polls the top-level future once. This is what all the callbacks should do. |
| 245 | void DoPoll() { |
| 246 | if (task_) { |
Brian Silverman | 1431a77 | 2022-08-31 20:44:36 -0700 | [diff] [blame] | 247 | CHECK(task_->Poll()) << ": Rust panic, aborting"; |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | EventLoop *const event_loop_; |
| 252 | |
| 253 | std::unique_ptr<ApplicationFuture> task_; |
Brian Silverman | 76f4836 | 2022-08-24 21:09:08 -0700 | [diff] [blame] | 254 | |
| 255 | int child_count_ = 0; |
Brian Silverman | 9809c5f | 2022-07-23 16:12:23 -0700 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | } // namespace aos |
| 259 | |
| 260 | #endif // AOS_EVENTS_EVENT_LOOP_RUNTIME_H_ |