blob: 4ac6e6d4afb38c1ddedfc9f7ada9df94b2e2f917 [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 Schuh217a9782019-12-21 23:02:50 -0800373 if (node() != nullptr) {
374 if (!configuration::ChannelIsReadableOnNode(channel, node())) {
375 LOG(FATAL)
376 << "Channel { \"name\": \"" << channel_name << "\", \"type\": \""
377 << T::GetFullyQualifiedName()
378 << "\" } is not able to be fetched on this node. Check your "
379 "configuration.";
380 }
381 }
382
Alex Perrycb7da4b2019-08-28 19:35:56 -0700383 return Fetcher<T>(MakeRawFetcher(channel));
384 }
385
386 // Makes class that allows constructing and sending messages to
387 // the provided channel.
388 template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800389 Sender<T> MakeSender(const std::string_view channel_name) {
Austin Schuhbca6cf02019-12-22 17:28:34 -0800390 const Channel *channel =
391 configuration::GetChannel(configuration_, channel_name,
392 T::GetFullyQualifiedName(), name(), node());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700393 CHECK(channel != nullptr)
394 << ": Channel { \"name\": \"" << channel_name << "\", \"type\": \""
395 << T::GetFullyQualifiedName() << "\" } not found in config.";
396
Austin Schuh217a9782019-12-21 23:02:50 -0800397 if (node() != nullptr) {
398 if (!configuration::ChannelIsSendableOnNode(channel, node())) {
399 LOG(FATAL) << "Channel { \"name\": \"" << channel_name
400 << "\", \"type\": \"" << T::GetFullyQualifiedName()
401 << "\" } is not able to be sent on this node. Check your "
402 "configuration.";
403 }
404 }
405
Alex Perrycb7da4b2019-08-28 19:35:56 -0700406 return Sender<T>(MakeRawSender(channel));
407 }
408
409 // This will watch messages sent to the provided channel.
410 //
411 // Watch is a functor that have a call signature like so:
412 // void Event(const MessageType& type);
413 //
414 // TODO(parker): Need to support ::std::bind. For now, use lambdas.
415 // TODO(austin): Do we need a functor? Or is a std::function good enough?
416 template <typename Watch>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800417 void MakeWatcher(const std::string_view name, Watch &&w);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700418
419 // The passed in function will be called when the event loop starts.
420 // Use this to run code once the thread goes into "real-time-mode",
421 virtual void OnRun(::std::function<void()> on_run) = 0;
422
Austin Schuh217a9782019-12-21 23:02:50 -0800423 // Gets the name of the event loop. This is the application name.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800424 virtual const std::string_view name() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700425
Austin Schuh217a9782019-12-21 23:02:50 -0800426 // Returns the node that this event loop is running on. Returns nullptr if we
427 // are running in single-node mode.
428 virtual const Node *node() const = 0;
429
Alex Perrycb7da4b2019-08-28 19:35:56 -0700430 // Creates a timer that executes callback when the timer expires
431 // Returns a TimerHandle for configuration of the timer
432 virtual TimerHandler *AddTimer(::std::function<void()> callback) = 0;
433
434 // Creates a timer that executes callback periodically at the specified
435 // interval and offset. Returns a PhasedLoopHandler for interacting with the
436 // timer.
437 virtual PhasedLoopHandler *AddPhasedLoop(
438 ::std::function<void(int)> callback,
439 const monotonic_clock::duration interval,
440 const monotonic_clock::duration offset = ::std::chrono::seconds(0)) = 0;
441
Austin Schuh217a9782019-12-21 23:02:50 -0800442 // TODO(austin): OnExit for cleanup.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700443
444 // Threadsafe.
445 bool is_running() const { return is_running_.load(); }
446
447 // Sets the scheduler priority to run the event loop at. This may not be
448 // called after we go into "real-time-mode".
449 virtual void SetRuntimeRealtimePriority(int priority) = 0;
Austin Schuh39788ff2019-12-01 18:22:57 -0800450 virtual int priority() const = 0;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700451
Austin Schuh217a9782019-12-21 23:02:50 -0800452 // Fetches new messages from the provided channel (path, type).
453 //
454 // Note: this channel must be a member of the exact configuration object this
455 // was built with.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700456 virtual std::unique_ptr<RawFetcher> MakeRawFetcher(
457 const Channel *channel) = 0;
458
Austin Schuh217a9782019-12-21 23:02:50 -0800459 // Watches channel (name, type) for new messages.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700460 virtual void MakeRawWatcher(
461 const Channel *channel,
462 std::function<void(const Context &context, const void *message)>
463 watcher) = 0;
464
Austin Schuh217a9782019-12-21 23:02:50 -0800465 // Creates a raw sender for the provided channel. This is used for reflection
466 // based sending.
467 // Note: this ignores any node constraints. Ignore at your own peril.
468 virtual std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) = 0;
469
Austin Schuh6231cc32019-12-07 13:06:15 -0800470 // Returns the context for the current callback.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700471 const Context &context() const { return context_; }
472
473 // Returns the configuration that this event loop was built with.
474 const Configuration *configuration() const { return configuration_; }
475
Austin Schuh39788ff2019-12-01 18:22:57 -0800476 // Prevents the event loop from sending a timing report.
477 void SkipTimingReport() { skip_timing_report_ = true; }
478
Alex Perrycb7da4b2019-08-28 19:35:56 -0700479 protected:
Austin Schuh217a9782019-12-21 23:02:50 -0800480 // Sets the name of the event loop. This is the application name.
481 virtual void set_name(const std::string_view name) = 0;
482
Alex Perrycb7da4b2019-08-28 19:35:56 -0700483 void set_is_running(bool value) { is_running_.store(value); }
484
Austin Schuh39788ff2019-12-01 18:22:57 -0800485 // Validates that channel exists inside configuration_ and finds its index.
486 int ChannelIndex(const Channel *channel);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700487
Austin Schuh6231cc32019-12-07 13:06:15 -0800488 // Context available for watchers, timers, and phased loops.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700489 Context context_;
490
Austin Schuh39788ff2019-12-01 18:22:57 -0800491 friend class RawSender;
492 friend class TimerHandler;
493 friend class RawFetcher;
494 friend class PhasedLoopHandler;
495 friend class WatcherState;
496
497 // Methods used to implement timing reports.
498 void NewSender(RawSender *sender);
499 void DeleteSender(RawSender *sender);
500 TimerHandler *NewTimer(std::unique_ptr<TimerHandler> timer);
501 PhasedLoopHandler *NewPhasedLoop(
502 std::unique_ptr<PhasedLoopHandler> phased_loop);
503 void NewFetcher(RawFetcher *fetcher);
504 void DeleteFetcher(RawFetcher *fetcher);
505 WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
506
507 std::vector<RawSender *> senders_;
Austin Schuh7d87b672019-12-01 20:23:49 -0800508 std::vector<RawFetcher *> fetchers_;
509
Austin Schuh39788ff2019-12-01 18:22:57 -0800510 std::vector<std::unique_ptr<TimerHandler>> timers_;
511 std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800512 std::vector<std::unique_ptr<WatcherState>> watchers_;
513
514 void SendTimingReport();
515 void UpdateTimingReport();
516 void MaybeScheduleTimingReports();
517
518 std::unique_ptr<RawSender> timing_report_sender_;
519
Austin Schuh7d87b672019-12-01 20:23:49 -0800520 // Tracks which event sources (timers and watchers) have data, and which
521 // don't. Added events may not change their event_time().
522 // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
523 // t2 to run, t2 should then be picked up without a context switch.
524 void AddEvent(EventLoopEvent *event);
525 void RemoveEvent(EventLoopEvent *event);
526 size_t EventCount() const { return events_.size(); }
527 EventLoopEvent *PopEvent();
528 EventLoopEvent *PeekEvent() { return events_.front(); }
529 void ReserveEvents();
530
531 std::vector<EventLoopEvent *> events_;
532
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800533 private:
Austin Schuh39788ff2019-12-01 18:22:57 -0800534 virtual pid_t GetTid() = 0;
535
536 FlatbufferDetachedBuffer<timing::Report> timing_report_;
537
Austin Schuhde8a8ff2019-11-30 15:25:36 -0800538 ::std::atomic<bool> is_running_{false};
539
Alex Perrycb7da4b2019-08-28 19:35:56 -0700540 const Configuration *configuration_;
Austin Schuh39788ff2019-12-01 18:22:57 -0800541
542 // If true, don't send out timing reports.
543 bool skip_timing_report_ = false;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700544};
545
546} // namespace aos
547
548#include "aos/events/event_loop_tmpl.h"
549
550#endif // AOS_EVENTS_EVENT_LOOP_H