blob: ea2bf6e626a1271d861ac312a5ce8586e6bcb72f [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include "flatbuffers/flatbuffers.h"
18#include "glog/logging.h"
19
Austin Schuh39788ff2019-12-01 18:22:57 -080020DECLARE_bool(timing_reports);
21DECLARE_int32(timing_report_ms);
22
Alex Perrycb7da4b2019-08-28 19:35:56 -070023namespace aos {
24
Austin Schuh39788ff2019-12-01 18:22:57 -080025class EventLoop;
26class WatcherState;
27
Austin Schuh6231cc32019-12-07 13:06:15 -080028// Struct available on Watchers, Fetchers, Timers, and PhasedLoops with context
29// about the current message.
Alex Perrycb7da4b2019-08-28 19:35:56 -070030struct Context {
Austin Schuhad154822019-12-27 15:45:13 -080031 // Time that the message was sent on this node, or the timer was triggered.
32 monotonic_clock::time_point monotonic_event_time;
33 // Realtime the message was sent on this node. This is set to min_time for
34 // Timers and PhasedLoops.
35 realtime_clock::time_point realtime_event_time;
36
37 // For a single-node configuration, these two are identical to *_event_time.
38 // In a multinode configuration, these are the times that the message was
39 // sent on the original node.
40 monotonic_clock::time_point monotonic_remote_time;
41 realtime_clock::time_point realtime_remote_time;
42
Austin Schuh6231cc32019-12-07 13:06:15 -080043 // The rest are only valid for Watchers and Fetchers.
Alex Perrycb7da4b2019-08-28 19:35:56 -070044 // Index in the queue.
45 uint32_t queue_index;
Austin Schuhad154822019-12-27 15:45:13 -080046 // Index into the remote queue. Useful to determine if data was lost. In a
47 // single-node configuration, this will match queue_index.
48 uint32_t remote_queue_index;
49
Alex Perrycb7da4b2019-08-28 19:35:56 -070050 // Size of the data sent.
51 size_t size;
52 // Pointer to the data.
53 void *data;
54};
55
56// Raw version of fetcher. Contains a local variable that the fetcher will
57// update. This is used for reflection and as an interface to implement typed
58// fetchers.
59class RawFetcher {
60 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080061 RawFetcher(EventLoop *event_loop, const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -070062 RawFetcher(const RawFetcher &) = delete;
63 RawFetcher &operator=(const RawFetcher &) = delete;
Austin Schuh39788ff2019-12-01 18:22:57 -080064 virtual ~RawFetcher();
Alex Perrycb7da4b2019-08-28 19:35:56 -070065
Austin Schuh39788ff2019-12-01 18:22:57 -080066 // Fetches the next message in the queue without blocking. Returns true if
67 // there was a new message and we got it.
68 bool FetchNext();
69
70 // Fetches the latest message without blocking.
71 bool Fetch();
72
73 // Returns the channel this fetcher uses.
74 const Channel *channel() const { return channel_; }
75 // Returns the context for the current message.
76 const Context &context() const { return context_; }
77
78 protected:
79 EventLoop *event_loop() { return event_loop_; }
80
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 Context context_;
Austin Schuh39788ff2019-12-01 18:22:57 -080082
83 private:
84 friend class EventLoop;
85 // Implementation
86 virtual std::pair<bool, monotonic_clock::time_point> DoFetchNext() = 0;
87 virtual std::pair<bool, monotonic_clock::time_point> DoFetch() = 0;
88
89 EventLoop *event_loop_;
Austin Schuh54cf95f2019-11-29 13:14:18 -080090 const Channel *channel_;
Austin Schuh39788ff2019-12-01 18:22:57 -080091
92 internal::RawFetcherTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070093};
94
95// Raw version of sender. Sends a block of data. This is used for reflection
96// and as a building block to implement typed senders.
97class RawSender {
98 public:
Austin Schuh39788ff2019-12-01 18:22:57 -080099 RawSender(EventLoop *event_loop, const Channel *channel);
100 RawSender(const RawSender &) = delete;
101 RawSender &operator=(const RawSender &) = delete;
102
103 virtual ~RawSender();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104
105 // Sends a message without copying it. The users starts by copying up to
106 // size() bytes into the data backed by data(). They then call Send to send.
107 // Returns true on a successful send.
Austin Schuhad154822019-12-27 15:45:13 -0800108 // If provided, monotonic_remote_time, realtime_remote_time, and
109 // remote_queue_index are attached to the message and are available in the
110 // context on the read side. If they are not populated, the read side will
111 // get the sent times instead.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700112 virtual void *data() = 0;
113 virtual size_t size() = 0;
Austin Schuhad154822019-12-27 15:45:13 -0800114 bool Send(size_t size,
115 aos::monotonic_clock::time_point monotonic_remote_time =
116 aos::monotonic_clock::min_time,
117 aos::realtime_clock::time_point realtime_remote_time =
118 aos::realtime_clock::min_time,
119 uint32_t remote_queue_index = 0xffffffffu);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120
121 // Sends a single block of data by copying it.
Austin Schuhad154822019-12-27 15:45:13 -0800122 // The remote arguments have the same meaning as in Send above.
123 bool Send(const void *data, size_t size,
124 aos::monotonic_clock::time_point monotonic_remote_time =
125 aos::monotonic_clock::min_time,
126 aos::realtime_clock::time_point realtime_remote_time =
127 aos::realtime_clock::min_time,
128 uint32_t remote_queue_index = 0xffffffffu);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700129
Austin Schuh54cf95f2019-11-29 13:14:18 -0800130 const Channel *channel() const { return channel_; }
131
Austin Schuhad154822019-12-27 15:45:13 -0800132 // Returns the time_points that the last message was sent at.
133 aos::monotonic_clock::time_point monotonic_sent_time() const {
134 return monotonic_sent_time_;
135 }
136 aos::realtime_clock::time_point realtime_sent_time() const {
137 return realtime_sent_time_;
138 }
139 // Returns the queue index that this was sent with.
140 uint32_t sent_queue_index() const { return sent_queue_index_; }
141
Alex Perrycb7da4b2019-08-28 19:35:56 -0700142 protected:
Austin Schuh39788ff2019-12-01 18:22:57 -0800143 EventLoop *event_loop() { return event_loop_; }
Austin Schuh54cf95f2019-11-29 13:14:18 -0800144
Austin Schuhad154822019-12-27 15:45:13 -0800145 aos::monotonic_clock::time_point monotonic_sent_time_ =
146 aos::monotonic_clock::min_time;
147 aos::realtime_clock::time_point realtime_sent_time_ =
148 aos::realtime_clock::min_time;
149 uint32_t sent_queue_index_ = 0xffffffff;
150
Austin Schuh39788ff2019-12-01 18:22:57 -0800151 private:
152 friend class EventLoop;
153
Austin Schuhad154822019-12-27 15:45:13 -0800154 virtual bool DoSend(const void *data, size_t size,
155 aos::monotonic_clock::time_point monotonic_remote_time,
156 aos::realtime_clock::time_point realtime_remote_time,
157 uint32_t remote_queue_index) = 0;
158 virtual bool DoSend(size_t size,
159 aos::monotonic_clock::time_point monotonic_remote_time,
160 aos::realtime_clock::time_point realtime_remote_time,
161 uint32_t remote_queue_index) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800162
163 EventLoop *event_loop_;
Austin Schuh54cf95f2019-11-29 13:14:18 -0800164 const Channel *channel_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165
Austin Schuh39788ff2019-12-01 18:22:57 -0800166 internal::RawSenderTiming timing_;
167};
Alex Perrycb7da4b2019-08-28 19:35:56 -0700168
169// Fetches the newest message from a channel.
170// This provides a polling based interface for channels.
171template <typename T>
172class Fetcher {
173 public:
174 Fetcher() {}
175
176 // Fetches the next message. Returns true if it fetched a new message. This
177 // method will only return messages sent after the Fetcher was created.
178 bool FetchNext() { return fetcher_->FetchNext(); }
179
180 // Fetches the most recent message. Returns true if it fetched a new message.
181 // This will return the latest message regardless of if it was sent before or
182 // after the fetcher was created.
183 bool Fetch() { return fetcher_->Fetch(); }
184
185 // Returns a pointer to the contained flatbuffer, or nullptr if there is no
186 // available message.
187 const T *get() const {
Austin Schuh39788ff2019-12-01 18:22:57 -0800188 return fetcher_->context().data != nullptr
189 ? flatbuffers::GetRoot<T>(
190 reinterpret_cast<const char *>(fetcher_->context().data))
Alex Perrycb7da4b2019-08-28 19:35:56 -0700191 : nullptr;
192 }
193
194 // Returns the context holding timestamps and other metadata about the
195 // message.
196 const Context &context() const { return fetcher_->context(); }
197
198 const T &operator*() const { return *get(); }
199 const T *operator->() const { return get(); }
200
201 private:
202 friend class EventLoop;
203 Fetcher(::std::unique_ptr<RawFetcher> fetcher)
204 : fetcher_(::std::move(fetcher)) {}
205 ::std::unique_ptr<RawFetcher> fetcher_;
206};
207
208// Sends messages to a channel.
209template <typename T>
210class Sender {
211 public:
212 Sender() {}
213
214 // Represents a single message about to be sent to the queue.
215 // The lifecycle goes:
216 //
217 // Builder builder = sender.MakeBuilder();
218 // T::Builder t_builder = builder.MakeBuilder<T>();
219 // Populate(&t_builder);
220 // builder.Send(t_builder.Finish());
221 class Builder {
222 public:
223 Builder(RawSender *sender, void *data, size_t size)
224 : alloc_(data, size), fbb_(size, &alloc_), sender_(sender) {
225 fbb_.ForceDefaults(1);
226 }
227
228 flatbuffers::FlatBufferBuilder *fbb() { return &fbb_; }
229
230 template <typename T2>
231 typename T2::Builder MakeBuilder() {
232 return typename T2::Builder(fbb_);
233 }
234
235 bool Send(flatbuffers::Offset<T> offset) {
236 fbb_.Finish(offset);
237 return sender_->Send(fbb_.GetSize());
238 }
239
240 // CHECKs that this message was sent.
241 void CheckSent() { fbb_.Finished(); }
242
243 private:
244 PreallocatedAllocator alloc_;
245 flatbuffers::FlatBufferBuilder fbb_;
246 RawSender *sender_;
247 };
248
249 // Constructs an above builder.
250 Builder MakeBuilder();
251
Austin Schuha28cbc32019-12-27 16:28:04 -0800252 // Sends a prebuilt flatbuffer.
253 bool Send(const Flatbuffer<T> &flatbuffer);
254
Austin Schuh39788ff2019-12-01 18:22:57 -0800255 // Returns the name of the underlying queue.
Austin Schuh1e869472019-12-01 13:36:10 -0800256 const Channel *channel() const { return sender_->channel(); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700257
258 private:
259 friend class EventLoop;
260 Sender(std::unique_ptr<RawSender> sender) : sender_(std::move(sender)) {}
261 std::unique_ptr<RawSender> sender_;
262};
263
264// Interface for timers
265class TimerHandler {
266 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800267 virtual ~TimerHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700268
269 // Timer should sleep until base, base + offset, base + offset * 2, ...
270 // If repeat_offset isn't set, the timer only expires once.
271 virtual void Setup(monotonic_clock::time_point base,
272 monotonic_clock::duration repeat_offset =
273 ::aos::monotonic_clock::zero()) = 0;
274
275 // Stop future calls to callback().
276 virtual void Disable() = 0;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800277
278 // Sets and gets the name of the timer. Set this if you want a descriptive
279 // name in the timing report.
280 void set_name(std::string_view name) { name_ = std::string(name); }
281 const std::string_view name() const { return name_; }
282
Austin Schuh39788ff2019-12-01 18:22:57 -0800283 protected:
284 TimerHandler(EventLoop *event_loop, std::function<void()> fn);
285
286 void Call(std::function<monotonic_clock::time_point()> get_time,
287 monotonic_clock::time_point event_time);
288
Austin Schuh1540c2f2019-11-29 21:59:29 -0800289 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800290 friend class EventLoop;
291
292 EventLoop *event_loop_;
293 // The function to call when Call is called.
294 std::function<void()> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800295 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800296
297 internal::TimerTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700298};
299
300// Interface for phased loops. They are built on timers.
301class PhasedLoopHandler {
302 public:
Austin Schuh39788ff2019-12-01 18:22:57 -0800303 virtual ~PhasedLoopHandler();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700304
305 // Sets the interval and offset. Any changes to interval and offset only take
306 // effect when the handler finishes running.
Austin Schuh39788ff2019-12-01 18:22:57 -0800307 void set_interval_and_offset(const monotonic_clock::duration interval,
308 const monotonic_clock::duration offset) {
309 phased_loop_.set_interval_and_offset(interval, offset);
310 }
Austin Schuh1540c2f2019-11-29 21:59:29 -0800311
312 // Sets and gets the name of the timer. Set this if you want a descriptive
313 // name in the timing report.
314 void set_name(std::string_view name) { name_ = std::string(name); }
315 const std::string_view name() const { return name_; }
316
Austin Schuh39788ff2019-12-01 18:22:57 -0800317 protected:
318 void Call(std::function<monotonic_clock::time_point()> get_time,
319 std::function<void(monotonic_clock::time_point)> schedule);
320
321 PhasedLoopHandler(EventLoop *event_loop, std::function<void(int)> fn,
322 const monotonic_clock::duration interval,
323 const monotonic_clock::duration offset);
324
Austin Schuh1540c2f2019-11-29 21:59:29 -0800325 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800326 friend class EventLoop;
327
328 void Reschedule(std::function<void(monotonic_clock::time_point)> schedule,
329 monotonic_clock::time_point monotonic_now) {
330 cycles_elapsed_ += phased_loop_.Iterate(monotonic_now);
331 schedule(phased_loop_.sleep_time());
332 }
333
334 virtual void Schedule(monotonic_clock::time_point sleep_time) = 0;
335
336 EventLoop *event_loop_;
337 std::function<void(int)> fn_;
Austin Schuh1540c2f2019-11-29 21:59:29 -0800338 std::string name_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800339 time::PhasedLoop phased_loop_;
340
341 int cycles_elapsed_ = 0;
342
343 internal::TimerTiming timing_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700344};
345
Alex Perrycb7da4b2019-08-28 19:35:56 -0700346class EventLoop {
347 public:
348 EventLoop(const Configuration *configuration)
Austin Schuh39788ff2019-12-01 18:22:57 -0800349 : timing_report_(flatbuffers::DetachedBuffer()),
350 configuration_(configuration) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700351
Austin Schuh39788ff2019-12-01 18:22:57 -0800352 virtual ~EventLoop();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700353
354 // Current time.
355 virtual monotonic_clock::time_point monotonic_now() = 0;
356 virtual realtime_clock::time_point realtime_now() = 0;
357
358 // Note, it is supported to create:
359 // multiple fetchers, and (one sender or one watcher) per <name, type>
360 // tuple.
361
362 // Makes a class that will always fetch the most recent value
363 // sent to the provided channel.
364 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800365 Fetcher<T> MakeFetcher(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800366 const Channel *channel =
367 configuration::GetChannel(configuration_, channel_name,
368 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700369 CHECK(channel != nullptr)
370 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
371 << T::GetFullyQualifiedName() << "\" } not found in config.";
372
Austin Schuhca4828c2019-12-28 14:21:35 -0800373 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
374 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
375 << "\", \"type\": \"" << T::GetFullyQualifiedName()
376 << "\" } is not able to be fetched on this node. Check your "
377 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800378 }
379
Alex Perrycb7da4b2019-08-28 19:35:56 -0700380 return Fetcher<T>(MakeRawFetcher(channel));
381 }
382
383 // Makes class that allows constructing and sending messages to
384 // the provided channel.
385 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800386 Sender<T> MakeSender(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800387 const Channel *channel =
388 configuration::GetChannel(configuration_, channel_name,
389 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700390 CHECK(channel != nullptr)
391 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
392 << T::GetFullyQualifiedName() << "\" } not found in config.";
393
Austin Schuhca4828c2019-12-28 14:21:35 -0800394 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
395 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
396 << "\", \"type\": \"" << T::GetFullyQualifiedName()
397 << "\" } is not able to be sent on this node. Check your "
398 "configuration.";
Austin Schuh217a9782019-12-21 23:02:50 -0800399 }
400
Alex Perrycb7da4b2019-08-28 19:35:56 -0700401 return Sender<T>(MakeRawSender(channel));
402 }
403
404 // This will watch messages sent to the provided channel.
405 //
406 // Watch is a functor that have a call signature like so:
407 // void Event(const MessageType& type);
408 //
409 // TODO(parker): Need to support ::std::bind. For now, use lambdas.
410 // TODO(austin): Do we need a functor? Or is a std::function good enough?
411 template <typename Watch>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800412 void MakeWatcher(const std::string_view name, Watch &&w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700413
414 // The passed in function will be called when the event loop starts.
415 // Use this to run code once the thread goes into "real-time-mode",
416 virtual void OnRun(::std::function<void()> on_run) = 0;
417
Austin Schuh217a9782019-12-21 23:02:50 -0800418 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800419 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700420
Austin Schuh217a9782019-12-21 23:02:50 -0800421 // Returns the node that this event loop is running on. Returns nullptr if we
422 // are running in single-node mode.
423 virtual const Node *node() const = 0;
424
Alex Perrycb7da4b2019-08-28 19:35:56 -0700425 // Creates a timer that executes callback when the timer expires
426 // Returns a TimerHandle for configuration of the timer
427 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
428
429 // Creates a timer that executes callback periodically at the specified
430 // interval and offset. Returns a PhasedLoopHandler for interacting with the
431 // timer.
432 virtual PhasedLoopHandler *AddPhasedLoop(
433 ::std::function<void(int)> callback,
434 const monotonic_clock::duration interval,
435 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
436
Austin Schuh217a9782019-12-21 23:02:50 -0800437 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700438
439 // Threadsafe.
440 bool is_running() const { return is_running_.load(); }
441
442 // Sets the scheduler priority to run the event loop at. This may not be
443 // called after we go into "real-time-mode".
444 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800445 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700446
Austin Schuh217a9782019-12-21 23:02:50 -0800447 // Fetches new messages from the provided channel (path, type).
448 //
449 // Note: this channel must be a member of the exact configuration object this
450 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
452 const Channel *channel) = 0;
453
Austin Schuh217a9782019-12-21 23:02:50 -0800454 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700455 virtual void MakeRawWatcher(
456 const Channel *channel,
457 std::function<void(const Context &context, const void *message)>
458 watcher) = 0;
459
Austin Schuh217a9782019-12-21 23:02:50 -0800460 // Creates a raw sender for the provided channel. This is used for reflection
461 // based sending.
462 // Note: this ignores any node constraints. Ignore at your own peril.
463 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
464
Austin Schuh6231cc32019-12-07 13:06:15 -0800465 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700466 const Context &context() const { return context_; }
467
468 // Returns the configuration that this event loop was built with.
469 const Configuration *configuration() const { return configuration_; }
470
Austin Schuh39788ff2019-12-01 18:22:57 -0800471 // Prevents the event loop from sending a timing report.
472 void SkipTimingReport() { skip_timing_report_ = true; }
473
Alex Perrycb7da4b2019-08-28 19:35:56 -0700474 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800475 // Sets the name of the event loop. This is the application name.
476 virtual void set_name(const std::string_view name) = 0;
477
Alex Perrycb7da4b2019-08-28 19:35:56 -0700478 void set_is_running(bool value) { is_running_.store(value); }
479
Austin Schuh39788ff2019-12-01 18:22:57 -0800480 // Validates that channel exists inside configuration_ and finds its index.
481 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700482
Austin Schuh6231cc32019-12-07 13:06:15 -0800483 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700484 Context context_;
485
Austin Schuh39788ff2019-12-01 18:22:57 -0800486 friend class RawSender;
487 friend class TimerHandler;
488 friend class RawFetcher;
489 friend class PhasedLoopHandler;
490 friend class WatcherState;
491
492 // Methods used to implement timing reports.
493 void NewSender(RawSender *sender);
494 void DeleteSender(RawSender *sender);
495 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
496 PhasedLoopHandler *NewPhasedLoop(
497 std::unique_ptr<PhasedLoopHandler> phased_loop);
498 void NewFetcher(RawFetcher *fetcher);
499 void DeleteFetcher(RawFetcher *fetcher);
500 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
501
502 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800503 std::vector<RawFetcher *> fetchers_;
504
Austin Schuh39788ff2019-12-01 18:22:57 -0800505 std::vector<std::unique_ptr<TimerHandler>> timers_;
506 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800507 std::vector<std::unique_ptr<WatcherState>> watchers_;
508
509 void SendTimingReport();
510 void UpdateTimingReport();
511 void MaybeScheduleTimingReports();
512
513 std::unique_ptr<RawSender> timing_report_sender_;
514
Austin Schuh7d87b672019-12-01 20:23:49 -0800515 // Tracks which event sources (timers and watchers) have data, and which
516 // don't. Added events may not change their event_time().
517 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
518 // t2 to run, t2 should then be picked up without a context switch.
519 void AddEvent(EventLoopEvent *event);
520 void RemoveEvent(EventLoopEvent *event);
521 size_t EventCount() const { return events_.size(); }
522 EventLoopEvent *PopEvent();
523 EventLoopEvent *PeekEvent() { return events_.front(); }
524 void ReserveEvents();
525
526 std::vector<EventLoopEvent *> events_;
527
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800528 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800529 virtual pid_t GetTid() = 0;
530
531 FlatbufferDetachedBuffer<timing::Report> timing_report_;
532
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800533 ::std::atomic<bool> is_running_{false};
534
Alex Perrycb7da4b2019-08-28 19:35:56 -0700535 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800536
537 // If true, don't send out timing reports.
538 bool skip_timing_report_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700539};
540
541} // namespace aos
542
543#include "aos/events/event_loop_tmpl.h"
544
545#endif // AOS_EVENTS_EVENT_LOOP_H