blob: 4a12096b16e089c114e556c76aa8901e8088a56b [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_EVENT_LOOP_H_
2#define AOS_EVENTS_EVENT_LOOP_H_
3
4#include <atomic>
5#include <string>
James Kuszmaul3ae42262019-11-08 12:33:41 -08006#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07007
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/configuration.h"
9#include "aos/configuration_generated.h"
Austin Schuh7d87b672019-12-01 20:23:49 -080010#include "aos/events/event_loop_event.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080011#include "aos/events/event_loop_generated.h"
12#include "aos/events/timing_statistics.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070013#include "aos/flatbuffers.h"
14#include "aos/json_to_flatbuffer.h"
15#include "aos/time/time.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080016#include "aos/util/phased_loop.h"
Brian Silverman0fc69932020-01-24 21:54:02 -080017
18#include "absl/container/btree_set.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070019#include "flatbuffers/flatbuffers.h"
20#include "glog/logging.h"
21
Austin Schuh39788ff2019-12-01 18:22:57 -080022DECLARE_bool(timing_reports);
23DECLARE_int32(timing_report_ms);
24
Alex Perrycb7da4b2019-08-28 19:35:56 -070025namespace aos {
26
Austin Schuh39788ff2019-12-01 18:22:57 -080027class EventLoop;
28class WatcherState;
29
Austin Schuh6231cc32019-12-07 13:06:15 -080030// Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context
31// about the current message.
Alex Perrycb7da4b2019-08-28 19:35:56 -070032struct Context {
Austin Schuhad154822019-12-27 15:45:13 -080033 // Time that the message was sent on this node, or the timer was triggered.
34 monotonic_clock::time_point monotonic_event_time;
35 // Realtime the message was sent on this node. This is set to min_time for
36 // Timers and PhasedLoops.
37 realtime_clock::time_point realtime_event_time;
38
39 // For a single-node configuration, these two are identical to *_event_time.
40 // In a multinode configuration, these are the times that the message was
41 // sent on the original node.
42 monotonic_clock::time_point monotonic_remote_time;
43 realtime_clock::time_point realtime_remote_time;
44
Austin Schuh6231cc32019-12-07 13:06:15 -080045 // The rest are only valid for Watchers and Fetchers.
Alex Perrycb7da4b2019-08-28 19:35:56 -070046 // Index in the queue.
47 uint32_t queue_index;
Austin Schuhad154822019-12-27 15:45:13 -080048 // Index into the remote queue. Useful to determine if data was lost. In a
49 // single-node configuration, this will match queue_index.
50 uint32_t remote_queue_index;
51
Alex Perrycb7da4b2019-08-28 19:35:56 -070052 // Size of the data sent.
53 size_t size;
54 // Pointer to the data.
55 void *data;
56};
57
58// Raw version of fetcher. Contains a local variable that the fetcher will
59// update. This is used for reflection and as an interface to implement typed
60// fetchers.
61class RawFetcher {
62 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080063 RawFetcher(EventLoop *event_loop, const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -070064 RawFetcher(const RawFetcher &) = delete;
65 RawFetcher &operator=(const RawFetcher &) = delete;
Austin Schuh39788ff2019-12-01 18:22:57 -080066 virtual ~RawFetcher();
Alex Perrycb7da4b2019-08-28 19:35:56 -070067
Austin Schuh39788ff2019-12-01 18:22:57 -080068 // Fetches the next message in the queue without blocking. Returns true if
69 // there was a new message and we got it.
70 bool FetchNext();
71
72 // Fetches the latest message without blocking.
73 bool Fetch();
74
75 // Returns the channel this fetcher uses.
76 const Channel *channel() const { return channel_; }
77 // Returns the context for the current message.
78 const Context &context() const { return context_; }
79
80 protected:
81 EventLoop *event_loop() { return event_loop_; }
82
Alex Perrycb7da4b2019-08-28 19:35:56 -070083 Context context_;
Austin Schuh39788ff2019-12-01 18:22:57 -080084
85 private:
86 friend class EventLoop;
87 // Implementation
88 virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0;
89 virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0;
90
91 EventLoop *event_loop_;
Austin Schuh54cf95f2019-11-29 13:14:18 -080092 const Channel *channel_;
Austin Schuh39788ff2019-12-01 18:22:57 -080093
94 internal::RawFetcherTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070095};
96
97// Raw version of sender. Sends a block of data. This is used for reflection
98// and as a building block to implement typed senders.
99class RawSender {
100 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800101 RawSender(EventLoop *event_loop, const Channel *channel);
102 RawSender(const RawSender &) = delete;
103 RawSender &operator=(const RawSender &) = delete;
104
105 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106
107 // Sends a message without copying it. The users starts by copying up to
108 // size() bytes into the data backed by data(). They then call Send to send.
109 // Returns true on a successful send.
Austin Schuhad154822019-12-27 15:45:13 -0800110 // If provided, monotonic_remote_time, realtime_remote_time, and
111 // remote_queue_index are attached to the message and are available in the
112 // context on the read side. If they are not populated, the read side will
113 // get the sent times instead.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700114 virtual void *data() = 0;
115 virtual size_t size() = 0;
Austin Schuhad154822019-12-27 15:45:13 -0800116 bool Send(size_t size,
117 aos::monotonic_clock::time_point monotonic_remote_time =
118 aos::monotonic_clock::min_time,
119 aos::realtime_clock::time_point realtime_remote_time =
120 aos::realtime_clock::min_time,
121 uint32_t remote_queue_index = 0xffffffffu);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700122
123 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800124 // The remote arguments have the same meaning as in Send above.
125 bool Send(const void *data, size_t size,
126 aos::monotonic_clock::time_point monotonic_remote_time =
127 aos::monotonic_clock::min_time,
128 aos::realtime_clock::time_point realtime_remote_time =
129 aos::realtime_clock::min_time,
130 uint32_t remote_queue_index = 0xffffffffu);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700131
Austin Schuh54cf95f2019-11-29 13:14:18 -0800132 const Channel *channel() const { return channel_; }
133
Austin Schuhad154822019-12-27 15:45:13 -0800134 // Returns the time_points that the last message was sent at.
135 aos::monotonic_clock::time_point monotonic_sent_time() const {
136 return monotonic_sent_time_;
137 }
138 aos::realtime_clock::time_point realtime_sent_time() const {
139 return realtime_sent_time_;
140 }
141 // Returns the queue index that this was sent with.
142 uint32_t sent_queue_index() const { return sent_queue_index_; }
143
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800145 EventLoop *event_loop() { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800146
Austin Schuhad154822019-12-27 15:45:13 -0800147 aos::monotonic_clock::time_point monotonic_sent_time_ =
148 aos::monotonic_clock::min_time;
149 aos::realtime_clock::time_point realtime_sent_time_ =
150 aos::realtime_clock::min_time;
151 uint32_t sent_queue_index_ = 0xffffffff;
152
Austin Schuh39788ff2019-12-01 18:22:57 -0800153 private:
154 friend class EventLoop;
155
Austin Schuhad154822019-12-27 15:45:13 -0800156 virtual bool DoSend(const void *data, size_t size,
157 aos::monotonic_clock::time_point monotonic_remote_time,
158 aos::realtime_clock::time_point realtime_remote_time,
159 uint32_t remote_queue_index) = 0;
160 virtual bool DoSend(size_t size,
161 aos::monotonic_clock::time_point monotonic_remote_time,
162 aos::realtime_clock::time_point realtime_remote_time,
163 uint32_t remote_queue_index) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800164
165 EventLoop *event_loop_;
Austin Schuh54cf95f2019-11-29 13:14:18 -0800166 const Channel *channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700167
Austin Schuh39788ff2019-12-01 18:22:57 -0800168 internal::RawSenderTiming timing_;
169};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700170
171// Fetches the newest message from a channel.
172// This provides a polling based interface for channels.
173template <typename T>
174class Fetcher {
175 public:
176 Fetcher() {}
177
178 // Fetches the next message. Returns true if it fetched a new message. This
179 // method will only return messages sent after the Fetcher was created.
180 bool FetchNext() { return fetcher_->FetchNext(); }
181
182 // Fetches the most recent message. Returns true if it fetched a new message.
183 // This will return the latest message regardless of if it was sent before or
184 // after the fetcher was created.
185 bool Fetch() { return fetcher_->Fetch(); }
186
187 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
188 // available message.
189 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800190 return fetcher_->context().data != nullptr
191 ? flatbuffers::GetRoot<T>(
192 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700193 : nullptr;
194 }
195
196 // Returns the context holding timestamps and other metadata about the
197 // message.
198 const Context &context() const { return fetcher_->context(); }
199
200 const T &operator*() const { return *get(); }
201 const T *operator->() const { return get(); }
202
203 private:
204 friend class EventLoop;
205 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
206 : fetcher_(::std::move(fetcher)) {}
207 ::std::unique_ptr<RawFetcher> fetcher_;
208};
209
210// Sends messages to a channel.
211template <typename T>
212class Sender {
213 public:
214 Sender() {}
215
216 // Represents a single message about to be sent to the queue.
217 // The lifecycle goes:
218 //
219 // Builder builder = sender.MakeBuilder();
220 // T::Builder t_builder = builder.MakeBuilder<T>();
221 // Populate(&t_builder);
222 // builder.Send(t_builder.Finish());
223 class Builder {
224 public:
225 Builder(RawSender *sender, void *data, size_t size)
226 : alloc_(data, size), fbb_(size, &alloc_), sender_(sender) {
227 fbb_.ForceDefaults(1);
228 }
229
230 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
231
232 template <typename T2>
233 typename T2::Builder MakeBuilder() {
234 return typename T2::Builder(fbb_);
235 }
236
237 bool Send(flatbuffers::Offset<T> offset) {
238 fbb_.Finish(offset);
239 return sender_->Send(fbb_.GetSize());
240 }
241
242 // CHECKs that this message was sent.
243 void CheckSent() { fbb_.Finished(); }
244
245 private:
246 PreallocatedAllocator alloc_;
247 flatbuffers::FlatBufferBuilder fbb_;
248 RawSender *sender_;
249 };
250
251 // Constructs an above builder.
252 Builder MakeBuilder();
253
Austin Schuha28cbc32019-12-27 16:28:04 -0800254 // Sends a prebuilt flatbuffer.
255 bool Send(const Flatbuffer<T> &flatbuffer);
256
Austin Schuh39788ff2019-12-01 18:22:57 -0800257 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800258 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259
260 private:
261 friend class EventLoop;
262 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
263 std::unique_ptr<RawSender> sender_;
264};
265
266// Interface for timers
267class TimerHandler {
268 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800269 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700270
271 // Timer should sleep until base, base + offset, base + offset * 2, ...
272 // If repeat_offset isn't set, the timer only expires once.
273 virtual void Setup(monotonic_clock::time_point base,
274 monotonic_clock::duration repeat_offset =
275 ::aos::monotonic_clock::zero()) = 0;
276
277 // Stop future calls to callback().
278 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800279
280 // Sets and gets the name of the timer. Set this if you want a descriptive
281 // name in the timing report.
282 void set_name(std::string_view name) { name_ = std::string(name); }
283 const std::string_view name() const { return name_; }
284
Austin Schuh39788ff2019-12-01 18:22:57 -0800285 protected:
286 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
287
288 void Call(std::function<monotonic_clock::time_point()> get_time,
289 monotonic_clock::time_point event_time);
290
Austin Schuh1540c2f2019-11-29 21:59:29 -0800291 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800292 friend class EventLoop;
293
294 EventLoop *event_loop_;
295 // The function to call when Call is called.
296 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800297 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800298
299 internal::TimerTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700300};
301
302// Interface for phased loops. They are built on timers.
303class PhasedLoopHandler {
304 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800305 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700306
307 // Sets the interval and offset. Any changes to interval and offset only take
308 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800309 void set_interval_and_offset(const monotonic_clock::duration interval,
310 const monotonic_clock::duration offset) {
311 phased_loop_.set_interval_and_offset(interval, offset);
312 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800313
314 // Sets and gets the name of the timer. Set this if you want a descriptive
315 // name in the timing report.
316 void set_name(std::string_view name) { name_ = std::string(name); }
317 const std::string_view name() const { return name_; }
318
Austin Schuh39788ff2019-12-01 18:22:57 -0800319 protected:
320 void Call(std::function<monotonic_clock::time_point()> get_time,
321 std::function<void(monotonic_clock::time_point)> schedule);
322
323 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
324 const monotonic_clock::duration interval,
325 const monotonic_clock::duration offset);
326
Austin Schuh1540c2f2019-11-29 21:59:29 -0800327 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800328 friend class EventLoop;
329
330 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
331 monotonic_clock::time_point monotonic_now) {
332 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
333 schedule(phased_loop_.sleep_time());
334 }
335
336 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
337
338 EventLoop *event_loop_;
339 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800340 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800341 time::PhasedLoop phased_loop_;
342
343 int cycles_elapsed_ = 0;
344
345 internal::TimerTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346};
347
Alex Perrycb7da4b2019-08-28 19:35:56 -0700348class EventLoop {
349 public:
350 EventLoop(const Configuration *configuration)
Austin Schuh39788ff2019-12-01 18:22:57 -0800351 : timing_report_(flatbuffers::DetachedBuffer()),
352 configuration_(configuration) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353
Austin Schuh39788ff2019-12-01 18:22:57 -0800354 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700355
356 // Current time.
357 virtual monotonic_clock::time_point monotonic_now() = 0;
358 virtual realtime_clock::time_point realtime_now() = 0;
359
360 // Note, it is supported to create:
361 // multiple fetchers, and (one sender or one watcher) per <name, type>
362 // tuple.
363
364 // Makes a class that will always fetch the most recent value
365 // sent to the provided channel.
366 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800367 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800368 const Channel *channel =
369 configuration::GetChannel(configuration_, channel_name,
370 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700371 CHECK(channel != nullptr)
372 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
373 << T::GetFullyQualifiedName() << "\" } not found in config.";
374
Austin Schuhca4828c2019-12-28 14:21:35 -0800375 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
376 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
377 << "\", \"type\": \"" << T::GetFullyQualifiedName()
378 << "\" } is not able to be fetched on this node. Check your "
379 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800380 }
381
Alex Perrycb7da4b2019-08-28 19:35:56 -0700382 return Fetcher<T>(MakeRawFetcher(channel));
383 }
384
385 // Makes class that allows constructing and sending messages to
386 // the provided channel.
387 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800388 Sender<T> MakeSender(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800389 const Channel *channel =
390 configuration::GetChannel(configuration_, channel_name,
391 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700392 CHECK(channel != nullptr)
393 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
394 << T::GetFullyQualifiedName() << "\" } not found in config.";
395
Austin Schuhca4828c2019-12-28 14:21:35 -0800396 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
397 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
398 << "\", \"type\": \"" << T::GetFullyQualifiedName()
399 << "\" } is not able to be sent on this node. Check your "
400 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800401 }
402
Alex Perrycb7da4b2019-08-28 19:35:56 -0700403 return Sender<T>(MakeRawSender(channel));
404 }
405
406 // This will watch messages sent to the provided channel.
407 //
408 // Watch is a functor that have a call signature like so:
409 // void Event(const MessageType& type);
410 //
411 // TODO(parker): Need to support ::std::bind. For now, use lambdas.
412 // TODO(austin): Do we need a functor? Or is a std::function good enough?
413 template <typename Watch>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800414 void MakeWatcher(const std::string_view name, Watch &&w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700415
416 // The passed in function will be called when the event loop starts.
417 // Use this to run code once the thread goes into "real-time-mode",
418 virtual void OnRun(::std::function<void()> on_run) = 0;
419
Austin Schuh217a9782019-12-21 23:02:50 -0800420 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800421 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700422
Austin Schuh217a9782019-12-21 23:02:50 -0800423 // Returns the node that this event loop is running on. Returns nullptr if we
424 // are running in single-node mode.
425 virtual const Node *node() const = 0;
426
Alex Perrycb7da4b2019-08-28 19:35:56 -0700427 // Creates a timer that executes callback when the timer expires
428 // Returns a TimerHandle for configuration of the timer
429 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
430
431 // Creates a timer that executes callback periodically at the specified
432 // interval and offset. Returns a PhasedLoopHandler for interacting with the
433 // timer.
434 virtual PhasedLoopHandler *AddPhasedLoop(
435 ::std::function<void(int)> callback,
436 const monotonic_clock::duration interval,
437 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
438
Austin Schuh217a9782019-12-21 23:02:50 -0800439 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700440
441 // Threadsafe.
442 bool is_running() const { return is_running_.load(); }
443
444 // Sets the scheduler priority to run the event loop at. This may not be
445 // called after we go into "real-time-mode".
446 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800447 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700448
Austin Schuh217a9782019-12-21 23:02:50 -0800449 // Fetches new messages from the provided channel (path, type).
450 //
451 // Note: this channel must be a member of the exact configuration object this
452 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700453 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
454 const Channel *channel) = 0;
455
Austin Schuh217a9782019-12-21 23:02:50 -0800456 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700457 virtual void MakeRawWatcher(
458 const Channel *channel,
459 std::function<void(const Context &context, const void *message)>
460 watcher) = 0;
461
Austin Schuh217a9782019-12-21 23:02:50 -0800462 // Creates a raw sender for the provided channel. This is used for reflection
463 // based sending.
464 // Note: this ignores any node constraints. Ignore at your own peril.
465 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
466
Austin Schuh6231cc32019-12-07 13:06:15 -0800467 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700468 const Context &context() const { return context_; }
469
470 // Returns the configuration that this event loop was built with.
471 const Configuration *configuration() const { return configuration_; }
472
Austin Schuh39788ff2019-12-01 18:22:57 -0800473 // Prevents the event loop from sending a timing report.
474 void SkipTimingReport() { skip_timing_report_ = true; }
475
Alex Perrycb7da4b2019-08-28 19:35:56 -0700476 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800477 // Sets the name of the event loop. This is the application name.
478 virtual void set_name(const std::string_view name) = 0;
479
Alex Perrycb7da4b2019-08-28 19:35:56 -0700480 void set_is_running(bool value) { is_running_.store(value); }
481
Austin Schuh39788ff2019-12-01 18:22:57 -0800482 // Validates that channel exists inside configuration_ and finds its index.
483 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484
Austin Schuh6231cc32019-12-07 13:06:15 -0800485 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700486 Context context_;
487
Austin Schuh39788ff2019-12-01 18:22:57 -0800488 friend class RawSender;
489 friend class TimerHandler;
490 friend class RawFetcher;
491 friend class PhasedLoopHandler;
492 friend class WatcherState;
493
494 // Methods used to implement timing reports.
495 void NewSender(RawSender *sender);
496 void DeleteSender(RawSender *sender);
497 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
498 PhasedLoopHandler *NewPhasedLoop(
499 std::unique_ptr<PhasedLoopHandler> phased_loop);
500 void NewFetcher(RawFetcher *fetcher);
501 void DeleteFetcher(RawFetcher *fetcher);
502 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
503
Brian Silverman0fc69932020-01-24 21:54:02 -0800504 // Tracks that we have a (single) watcher on the given channel.
505 void TakeWatcher(const Channel *channel);
506 // Tracks that we have at least one sender on the given channel.
507 void TakeSender(const Channel *channel);
508
Austin Schuh39788ff2019-12-01 18:22:57 -0800509 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800510 std::vector<RawFetcher *> fetchers_;
511
Austin Schuh39788ff2019-12-01 18:22:57 -0800512 std::vector<std::unique_ptr<TimerHandler>> timers_;
513 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800514 std::vector<std::unique_ptr<WatcherState>> watchers_;
515
516 void SendTimingReport();
517 void UpdateTimingReport();
518 void MaybeScheduleTimingReports();
519
520 std::unique_ptr<RawSender> timing_report_sender_;
521
Austin Schuh7d87b672019-12-01 20:23:49 -0800522 // Tracks which event sources (timers and watchers) have data, and which
523 // don't. Added events may not change their event_time().
524 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
525 // t2 to run, t2 should then be picked up without a context switch.
526 void AddEvent(EventLoopEvent *event);
527 void RemoveEvent(EventLoopEvent *event);
528 size_t EventCount() const { return events_.size(); }
529 EventLoopEvent *PopEvent();
530 EventLoopEvent *PeekEvent() { return events_.front(); }
531 void ReserveEvents();
532
533 std::vector<EventLoopEvent *> events_;
534
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800535 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800536 virtual pid_t GetTid() = 0;
537
538 FlatbufferDetachedBuffer<timing::Report> timing_report_;
539
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800540 ::std::atomic<bool> is_running_{false};
541
Alex Perrycb7da4b2019-08-28 19:35:56 -0700542 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800543
544 // If true, don't send out timing reports.
545 bool skip_timing_report_ = false;
Brian Silverman0fc69932020-01-24 21:54:02 -0800546
547 absl::btree_set<const Channel *> taken_watchers_, taken_senders_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700548};
549
550} // namespace aos
551
552#include "aos/events/event_loop_tmpl.h"
553
554#endif // AOS_EVENTS_EVENT_LOOP_H