Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_EVENT_LOOP_H_ |
| 3 | |
| 4 | #include <atomic> |
| 5 | #include <string> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 6 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/configuration.h" |
| 9 | #include "aos/configuration_generated.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop_event.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 11 | #include "aos/events/event_loop_generated.h" |
| 12 | #include "aos/events/timing_statistics.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 13 | #include "aos/flatbuffers.h" |
| 14 | #include "aos/json_to_flatbuffer.h" |
| 15 | #include "aos/time/time.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 16 | #include "aos/util/phased_loop.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include "flatbuffers/flatbuffers.h" |
| 18 | #include "glog/logging.h" |
| 19 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 20 | DECLARE_bool(timing_reports); |
| 21 | DECLARE_int32(timing_report_ms); |
| 22 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | namespace aos { |
| 24 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 25 | class EventLoop; |
| 26 | class WatcherState; |
| 27 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 28 | // Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context |
| 29 | // about the current message. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | struct Context { |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 31 | // Time that the message was sent, or the timer was triggered. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 32 | monotonic_clock::time_point monotonic_sent_time; |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 33 | // Realtime the message was sent. This is set to min_time for Timers and |
| 34 | // PhasedLoops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | realtime_clock::time_point realtime_sent_time; |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 36 | // The rest are only valid for Watchers and Fetchers. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | // Index in the queue. |
| 38 | uint32_t queue_index; |
| 39 | // Size of the data sent. |
| 40 | size_t size; |
| 41 | // Pointer to the data. |
| 42 | void *data; |
| 43 | }; |
| 44 | |
| 45 | // Raw version of fetcher. Contains a local variable that the fetcher will |
| 46 | // update. This is used for reflection and as an interface to implement typed |
| 47 | // fetchers. |
| 48 | class RawFetcher { |
| 49 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 50 | RawFetcher(EventLoop *event_loop, const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 51 | RawFetcher(const RawFetcher &) = delete; |
| 52 | RawFetcher &operator=(const RawFetcher &) = delete; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 53 | virtual ~RawFetcher(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 54 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 55 | // Fetches the next message in the queue without blocking. Returns true if |
| 56 | // there was a new message and we got it. |
| 57 | bool FetchNext(); |
| 58 | |
| 59 | // Fetches the latest message without blocking. |
| 60 | bool Fetch(); |
| 61 | |
| 62 | // Returns the channel this fetcher uses. |
| 63 | const Channel *channel() const { return channel_; } |
| 64 | // Returns the context for the current message. |
| 65 | const Context &context() const { return context_; } |
| 66 | |
| 67 | protected: |
| 68 | EventLoop *event_loop() { return event_loop_; } |
| 69 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | Context context_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 71 | |
| 72 | private: |
| 73 | friend class EventLoop; |
| 74 | // Implementation |
| 75 | virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0; |
| 76 | virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0; |
| 77 | |
| 78 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 79 | const Channel *channel_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 80 | |
| 81 | internal::RawFetcherTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Raw version of sender. Sends a block of data. This is used for reflection |
| 85 | // and as a building block to implement typed senders. |
| 86 | class RawSender { |
| 87 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 88 | RawSender(EventLoop *event_loop, const Channel *channel); |
| 89 | RawSender(const RawSender &) = delete; |
| 90 | RawSender &operator=(const RawSender &) = delete; |
| 91 | |
| 92 | virtual ~RawSender(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 93 | |
| 94 | // Sends a message without copying it. The users starts by copying up to |
| 95 | // size() bytes into the data backed by data(). They then call Send to send. |
| 96 | // Returns true on a successful send. |
| 97 | virtual void *data() = 0; |
| 98 | virtual size_t size() = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 99 | bool Send(size_t size); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 100 | |
| 101 | // Sends a single block of data by copying it. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 102 | bool Send(const void *data, size_t size); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 103 | |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 104 | const Channel *channel() const { return channel_; } |
| 105 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | protected: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 107 | EventLoop *event_loop() { return event_loop_; } |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 108 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 109 | private: |
| 110 | friend class EventLoop; |
| 111 | |
| 112 | virtual bool DoSend(const void *data, size_t size) = 0; |
| 113 | virtual bool DoSend(size_t size) = 0; |
| 114 | |
| 115 | EventLoop *event_loop_; |
Austin Schuh | 54cf95f | 2019-11-29 13:14:18 -0800 | [diff] [blame] | 116 | const Channel *channel_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 117 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 118 | internal::RawSenderTiming timing_; |
| 119 | }; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | |
| 121 | // Fetches the newest message from a channel. |
| 122 | // This provides a polling based interface for channels. |
| 123 | template <typename T> |
| 124 | class Fetcher { |
| 125 | public: |
| 126 | Fetcher() {} |
| 127 | |
| 128 | // Fetches the next message. Returns true if it fetched a new message. This |
| 129 | // method will only return messages sent after the Fetcher was created. |
| 130 | bool FetchNext() { return fetcher_->FetchNext(); } |
| 131 | |
| 132 | // Fetches the most recent message. Returns true if it fetched a new message. |
| 133 | // This will return the latest message regardless of if it was sent before or |
| 134 | // after the fetcher was created. |
| 135 | bool Fetch() { return fetcher_->Fetch(); } |
| 136 | |
| 137 | // Returns a pointer to the contained flatbuffer, or nullptr if there is no |
| 138 | // available message. |
| 139 | const T *get() const { |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 140 | return fetcher_->context().data != nullptr |
| 141 | ? flatbuffers::GetRoot<T>( |
| 142 | reinterpret_cast<const char *>(fetcher_->context().data)) |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | : nullptr; |
| 144 | } |
| 145 | |
| 146 | // Returns the context holding timestamps and other metadata about the |
| 147 | // message. |
| 148 | const Context &context() const { return fetcher_->context(); } |
| 149 | |
| 150 | const T &operator*() const { return *get(); } |
| 151 | const T *operator->() const { return get(); } |
| 152 | |
| 153 | private: |
| 154 | friend class EventLoop; |
| 155 | Fetcher(::std::unique_ptr<RawFetcher> fetcher) |
| 156 | : fetcher_(::std::move(fetcher)) {} |
| 157 | ::std::unique_ptr<RawFetcher> fetcher_; |
| 158 | }; |
| 159 | |
| 160 | // Sends messages to a channel. |
| 161 | template <typename T> |
| 162 | class Sender { |
| 163 | public: |
| 164 | Sender() {} |
| 165 | |
| 166 | // Represents a single message about to be sent to the queue. |
| 167 | // The lifecycle goes: |
| 168 | // |
| 169 | // Builder builder = sender.MakeBuilder(); |
| 170 | // T::Builder t_builder = builder.MakeBuilder<T>(); |
| 171 | // Populate(&t_builder); |
| 172 | // builder.Send(t_builder.Finish()); |
| 173 | class Builder { |
| 174 | public: |
| 175 | Builder(RawSender *sender, void *data, size_t size) |
| 176 | : alloc_(data, size), fbb_(size, &alloc_), sender_(sender) { |
| 177 | fbb_.ForceDefaults(1); |
| 178 | } |
| 179 | |
| 180 | flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; } |
| 181 | |
| 182 | template <typename T2> |
| 183 | typename T2::Builder MakeBuilder() { |
| 184 | return typename T2::Builder(fbb_); |
| 185 | } |
| 186 | |
| 187 | bool Send(flatbuffers::Offset<T> offset) { |
| 188 | fbb_.Finish(offset); |
| 189 | return sender_->Send(fbb_.GetSize()); |
| 190 | } |
| 191 | |
| 192 | // CHECKs that this message was sent. |
| 193 | void CheckSent() { fbb_.Finished(); } |
| 194 | |
| 195 | private: |
| 196 | PreallocatedAllocator alloc_; |
| 197 | flatbuffers::FlatBufferBuilder fbb_; |
| 198 | RawSender *sender_; |
| 199 | }; |
| 200 | |
| 201 | // Constructs an above builder. |
| 202 | Builder MakeBuilder(); |
| 203 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 204 | // Returns the name of the underlying queue. |
Austin Schuh | 1e86947 | 2019-12-01 13:36:10 -0800 | [diff] [blame] | 205 | const Channel *channel() const { return sender_->channel(); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 206 | |
| 207 | private: |
| 208 | friend class EventLoop; |
| 209 | Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {} |
| 210 | std::unique_ptr<RawSender> sender_; |
| 211 | }; |
| 212 | |
| 213 | // Interface for timers |
| 214 | class TimerHandler { |
| 215 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 216 | virtual ~TimerHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 217 | |
| 218 | // Timer should sleep until base, base + offset, base + offset * 2, ... |
| 219 | // If repeat_offset isn't set, the timer only expires once. |
| 220 | virtual void Setup(monotonic_clock::time_point base, |
| 221 | monotonic_clock::duration repeat_offset = |
| 222 | ::aos::monotonic_clock::zero()) = 0; |
| 223 | |
| 224 | // Stop future calls to callback(). |
| 225 | virtual void Disable() = 0; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 226 | |
| 227 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 228 | // name in the timing report. |
| 229 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 230 | const std::string_view name() const { return name_; } |
| 231 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 232 | protected: |
| 233 | TimerHandler(EventLoop *event_loop, std::function<void()> fn); |
| 234 | |
| 235 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 236 | monotonic_clock::time_point event_time); |
| 237 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 238 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 239 | friend class EventLoop; |
| 240 | |
| 241 | EventLoop *event_loop_; |
| 242 | // The function to call when Call is called. |
| 243 | std::function<void()> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 244 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 245 | |
| 246 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | // Interface for phased loops. They are built on timers. |
| 250 | class PhasedLoopHandler { |
| 251 | public: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 252 | virtual ~PhasedLoopHandler(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 253 | |
| 254 | // Sets the interval and offset. Any changes to interval and offset only take |
| 255 | // effect when the handler finishes running. |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 256 | void set_interval_and_offset(const monotonic_clock::duration interval, |
| 257 | const monotonic_clock::duration offset) { |
| 258 | phased_loop_.set_interval_and_offset(interval, offset); |
| 259 | } |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 260 | |
| 261 | // Sets and gets the name of the timer. Set this if you want a descriptive |
| 262 | // name in the timing report. |
| 263 | void set_name(std::string_view name) { name_ = std::string(name); } |
| 264 | const std::string_view name() const { return name_; } |
| 265 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 266 | protected: |
| 267 | void Call(std::function<monotonic_clock::time_point()> get_time, |
| 268 | std::function<void(monotonic_clock::time_point)> schedule); |
| 269 | |
| 270 | PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn, |
| 271 | const monotonic_clock::duration interval, |
| 272 | const monotonic_clock::duration offset); |
| 273 | |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 274 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 275 | friend class EventLoop; |
| 276 | |
| 277 | void Reschedule(std::function<void(monotonic_clock::time_point)> schedule, |
| 278 | monotonic_clock::time_point monotonic_now) { |
| 279 | cycles_elapsed_ += phased_loop_.Iterate(monotonic_now); |
| 280 | schedule(phased_loop_.sleep_time()); |
| 281 | } |
| 282 | |
| 283 | virtual void Schedule(monotonic_clock::time_point sleep_time) = 0; |
| 284 | |
| 285 | EventLoop *event_loop_; |
| 286 | std::function<void(int)> fn_; |
Austin Schuh | 1540c2f | 2019-11-29 21:59:29 -0800 | [diff] [blame] | 287 | std::string name_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 288 | time::PhasedLoop phased_loop_; |
| 289 | |
| 290 | int cycles_elapsed_ = 0; |
| 291 | |
| 292 | internal::TimerTiming timing_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 295 | class EventLoop { |
| 296 | public: |
| 297 | EventLoop(const Configuration *configuration) |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 298 | : timing_report_(flatbuffers::DetachedBuffer()), |
| 299 | configuration_(configuration) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 300 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 301 | virtual ~EventLoop(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 302 | |
| 303 | // Current time. |
| 304 | virtual monotonic_clock::time_point monotonic_now() = 0; |
| 305 | virtual realtime_clock::time_point realtime_now() = 0; |
| 306 | |
| 307 | // Note, it is supported to create: |
| 308 | // multiple fetchers, and (one sender or one watcher) per <name, type> |
| 309 | // tuple. |
| 310 | |
| 311 | // Makes a class that will always fetch the most recent value |
| 312 | // sent to the provided channel. |
| 313 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 314 | Fetcher<T> MakeFetcher(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame^] | 315 | const Channel *channel = |
| 316 | configuration::GetChannel(configuration_, channel_name, |
| 317 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 318 | CHECK(channel != nullptr) |
| 319 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 320 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 321 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 322 | if (node() != nullptr) { |
| 323 | if (!configuration::ChannelIsReadableOnNode(channel, node())) { |
| 324 | LOG(FATAL) |
| 325 | << "Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 326 | << T::GetFullyQualifiedName() |
| 327 | << "\" } is not able to be fetched on this node. Check your " |
| 328 | "configuration."; |
| 329 | } |
| 330 | } |
| 331 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 332 | return Fetcher<T>(MakeRawFetcher(channel)); |
| 333 | } |
| 334 | |
| 335 | // Makes class that allows constructing and sending messages to |
| 336 | // the provided channel. |
| 337 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 338 | Sender<T> MakeSender(const std::string_view channel_name) { |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame^] | 339 | const Channel *channel = |
| 340 | configuration::GetChannel(configuration_, channel_name, |
| 341 | T::GetFullyQualifiedName(), name(), node()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 342 | CHECK(channel != nullptr) |
| 343 | << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \"" |
| 344 | << T::GetFullyQualifiedName() << "\" } not found in config."; |
| 345 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 346 | if (node() != nullptr) { |
| 347 | if (!configuration::ChannelIsSendableOnNode(channel, node())) { |
| 348 | LOG(FATAL) << "Channel { \"name\": \"" << channel_name |
| 349 | << "\", \"type\": \"" << T::GetFullyQualifiedName() |
| 350 | << "\" } is not able to be sent on this node. Check your " |
| 351 | "configuration."; |
| 352 | } |
| 353 | } |
| 354 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 355 | return Sender<T>(MakeRawSender(channel)); |
| 356 | } |
| 357 | |
| 358 | // This will watch messages sent to the provided channel. |
| 359 | // |
| 360 | // Watch is a functor that have a call signature like so: |
| 361 | // void Event(const MessageType& type); |
| 362 | // |
| 363 | // TODO(parker): Need to support ::std::bind. For now, use lambdas. |
| 364 | // TODO(austin): Do we need a functor? Or is a std::function good enough? |
| 365 | template <typename Watch> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 366 | void MakeWatcher(const std::string_view name, Watch &&w); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 367 | |
| 368 | // The passed in function will be called when the event loop starts. |
| 369 | // Use this to run code once the thread goes into "real-time-mode", |
| 370 | virtual void OnRun(::std::function<void()> on_run) = 0; |
| 371 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 372 | // Gets the name of the event loop. This is the application name. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 373 | virtual const std::string_view name() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 374 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 375 | // Returns the node that this event loop is running on. Returns nullptr if we |
| 376 | // are running in single-node mode. |
| 377 | virtual const Node *node() const = 0; |
| 378 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 379 | // Creates a timer that executes callback when the timer expires |
| 380 | // Returns a TimerHandle for configuration of the timer |
| 381 | virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0; |
| 382 | |
| 383 | // Creates a timer that executes callback periodically at the specified |
| 384 | // interval and offset. Returns a PhasedLoopHandler for interacting with the |
| 385 | // timer. |
| 386 | virtual PhasedLoopHandler *AddPhasedLoop( |
| 387 | ::std::function<void(int)> callback, |
| 388 | const monotonic_clock::duration interval, |
| 389 | const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0; |
| 390 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 391 | // TODO(austin): OnExit for cleanup. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 392 | |
| 393 | // Threadsafe. |
| 394 | bool is_running() const { return is_running_.load(); } |
| 395 | |
| 396 | // Sets the scheduler priority to run the event loop at. This may not be |
| 397 | // called after we go into "real-time-mode". |
| 398 | virtual void SetRuntimeRealtimePriority(int priority) = 0; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 399 | virtual int priority() const = 0; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 400 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 401 | // Fetches new messages from the provided channel (path, type). |
| 402 | // |
| 403 | // Note: this channel must be a member of the exact configuration object this |
| 404 | // was built with. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 405 | virtual std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 406 | const Channel *channel) = 0; |
| 407 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 408 | // Watches channel (name, type) for new messages. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 409 | virtual void MakeRawWatcher( |
| 410 | const Channel *channel, |
| 411 | std::function<void(const Context &context, const void *message)> |
| 412 | watcher) = 0; |
| 413 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 414 | // Creates a raw sender for the provided channel. This is used for reflection |
| 415 | // based sending. |
| 416 | // Note: this ignores any node constraints. Ignore at your own peril. |
| 417 | virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0; |
| 418 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 419 | // Returns the context for the current callback. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 420 | const Context &context() const { return context_; } |
| 421 | |
| 422 | // Returns the configuration that this event loop was built with. |
| 423 | const Configuration *configuration() const { return configuration_; } |
| 424 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 425 | // Prevents the event loop from sending a timing report. |
| 426 | void SkipTimingReport() { skip_timing_report_ = true; } |
| 427 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 428 | protected: |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 429 | // Sets the name of the event loop. This is the application name. |
| 430 | virtual void set_name(const std::string_view name) = 0; |
| 431 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 432 | void set_is_running(bool value) { is_running_.store(value); } |
| 433 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 434 | // Validates that channel exists inside configuration_ and finds its index. |
| 435 | int ChannelIndex(const Channel *channel); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 436 | |
Austin Schuh | 6231cc3 | 2019-12-07 13:06:15 -0800 | [diff] [blame] | 437 | // Context available for watchers, timers, and phased loops. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 438 | Context context_; |
| 439 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 440 | friend class RawSender; |
| 441 | friend class TimerHandler; |
| 442 | friend class RawFetcher; |
| 443 | friend class PhasedLoopHandler; |
| 444 | friend class WatcherState; |
| 445 | |
| 446 | // Methods used to implement timing reports. |
| 447 | void NewSender(RawSender *sender); |
| 448 | void DeleteSender(RawSender *sender); |
| 449 | TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer); |
| 450 | PhasedLoopHandler *NewPhasedLoop( |
| 451 | std::unique_ptr<PhasedLoopHandler> phased_loop); |
| 452 | void NewFetcher(RawFetcher *fetcher); |
| 453 | void DeleteFetcher(RawFetcher *fetcher); |
| 454 | WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher); |
| 455 | |
| 456 | std::vector<RawSender *> senders_; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 457 | std::vector<RawFetcher *> fetchers_; |
| 458 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 459 | std::vector<std::unique_ptr<TimerHandler>> timers_; |
| 460 | std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 461 | std::vector<std::unique_ptr<WatcherState>> watchers_; |
| 462 | |
| 463 | void SendTimingReport(); |
| 464 | void UpdateTimingReport(); |
| 465 | void MaybeScheduleTimingReports(); |
| 466 | |
| 467 | std::unique_ptr<RawSender> timing_report_sender_; |
| 468 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 469 | // Tracks which event sources (timers and watchers) have data, and which |
| 470 | // don't. Added events may not change their event_time(). |
| 471 | // TODO(austin): Test case 1: timer triggers at t1, handler takes until after |
| 472 | // t2 to run, t2 should then be picked up without a context switch. |
| 473 | void AddEvent(EventLoopEvent *event); |
| 474 | void RemoveEvent(EventLoopEvent *event); |
| 475 | size_t EventCount() const { return events_.size(); } |
| 476 | EventLoopEvent *PopEvent(); |
| 477 | EventLoopEvent *PeekEvent() { return events_.front(); } |
| 478 | void ReserveEvents(); |
| 479 | |
| 480 | std::vector<EventLoopEvent *> events_; |
| 481 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 482 | private: |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 483 | virtual pid_t GetTid() = 0; |
| 484 | |
| 485 | FlatbufferDetachedBuffer<timing::Report> timing_report_; |
| 486 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 487 | ::std::atomic<bool> is_running_{false}; |
| 488 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 489 | const Configuration *configuration_; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 490 | |
| 491 | // If true, don't send out timing reports. |
| 492 | bool skip_timing_report_ = false; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | } // namespace aos |
| 496 | |
| 497 | #include "aos/events/event_loop_tmpl.h" |
| 498 | |
| 499 | #endif // AOS_EVENTS_EVENT_LOOP_H |