Sort wakeups and add send latency.
We don't want to rely on epoll to trigger wakeups in order. Timers will
likely wake up before watchers due to less kernel delays, etc. So,
maintain a sorted list of events and see which should be triggered first
and trigger it first. It also merges wakeups nicely.
This gives us the infrastructure to handle the same problem in
simulation. We can have perfect timers, and senders with latency. But,
if a timer wakes up while something is being sent, we will move up the
wakeup and handle it in order.
Change-Id: I8675ec5221dd2603b4aa5b1a3729907a599813b4
diff --git a/aos/events/event_loop.h b/aos/events/event_loop.h
index 16980dd..68b1b5e 100644
--- a/aos/events/event_loop.h
+++ b/aos/events/event_loop.h
@@ -7,6 +7,7 @@
#include "aos/configuration.h"
#include "aos/configuration_generated.h"
+#include "aos/events/event_loop_event.h"
#include "aos/events/event_loop_generated.h"
#include "aos/events/timing_statistics.h"
#include "aos/flatbuffers.h"
@@ -424,9 +425,10 @@
WatcherState *NewWatcher(std::unique_ptr<WatcherState> watcher);
std::vector<RawSender *> senders_;
+ std::vector<RawFetcher *> fetchers_;
+
std::vector<std::unique_ptr<TimerHandler>> timers_;
std::vector<std::unique_ptr<PhasedLoopHandler>> phased_loops_;
- std::vector<RawFetcher *> fetchers_;
std::vector<std::unique_ptr<WatcherState>> watchers_;
void SendTimingReport();
@@ -435,6 +437,19 @@
std::unique_ptr<RawSender> timing_report_sender_;
+ // Tracks which event sources (timers and watchers) have data, and which
+ // don't. Added events may not change their event_time().
+ // TODO(austin): Test case 1: timer triggers at t1, handler takes until after
+ // t2 to run, t2 should then be picked up without a context switch.
+ void AddEvent(EventLoopEvent *event);
+ void RemoveEvent(EventLoopEvent *event);
+ size_t EventCount() const { return events_.size(); }
+ EventLoopEvent *PopEvent();
+ EventLoopEvent *PeekEvent() { return events_.front(); }
+ void ReserveEvents();
+
+ std::vector<EventLoopEvent *> events_;
+
private:
virtual pid_t GetTid() = 0;