blob: bb4ad77f4f65714ab635cb50e71fddbca16606c7 [file] [log] [blame]
Alex Perrycb7da4b2019-08-28 19:35:56 -07001#ifndef AOS_EVENTS_SHM_EVENT_LOOP_H_
2#define AOS_EVENTS_SHM_EVENT_LOOP_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include <vector>
5
6#include "aos/events/epoll.h"
7#include "aos/events/event_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -08008#include "aos/events/event_loop_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07009
10namespace aos {
11namespace internal {
12
13class WatcherState;
14class TimerHandlerState;
15class PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080016class ShmSender;
17class ShmFetcher;
Alex Perrycb7da4b2019-08-28 19:35:56 -070018
19} // namespace internal
20
21// Specialization of EventLoop that is built from queues running out of shared
Austin Schuh39788ff2019-12-01 18:22:57 -080022// memory.
Alex Perrycb7da4b2019-08-28 19:35:56 -070023//
Austin Schuh39788ff2019-12-01 18:22:57 -080024// TODO(austin): Timing reports break multiple threads. Need to add back in a
25// mutex.
26// This object must be interacted with from one thread, but the Senders
27// and Fetchers may be used from multiple threads afterwords (as long as their
Alex Perrycb7da4b2019-08-28 19:35:56 -070028// destructors are called back in one thread again)
29class ShmEventLoop : public EventLoop {
30 public:
31 ShmEventLoop(const Configuration *configuration);
32 ~ShmEventLoop() override;
33
Austin Schuh39788ff2019-12-01 18:22:57 -080034 // Runs the event loop until Exit is called, or ^C is caught.
35 void Run();
36 // Exits the event loop. Async safe.
37 void Exit();
38
Alex Perrycb7da4b2019-08-28 19:35:56 -070039 aos::monotonic_clock::time_point monotonic_now() override {
40 return aos::monotonic_clock::now();
41 }
42 aos::realtime_clock::time_point realtime_now() override {
43 return aos::realtime_clock::now();
44 }
45
46 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
47 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
48
49 void MakeRawWatcher(
50 const Channel *channel,
51 std::function<void(const Context &context, const void *message)> watcher)
52 override;
53
54 TimerHandler *AddTimer(std::function<void()> callback) override;
55 aos::PhasedLoopHandler *AddPhasedLoop(
56 std::function<void(int)> callback,
57 const monotonic_clock::duration interval,
58 const monotonic_clock::duration offset =
59 std::chrono::seconds(0)) override;
60
61 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070062
63 void SetRuntimeRealtimePriority(int priority) override;
64
James Kuszmaul3ae42262019-11-08 12:33:41 -080065 void set_name(const std::string_view name) override {
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 name_ = std::string(name);
Austin Schuh39788ff2019-12-01 18:22:57 -080067 UpdateTimingReport();
Alex Perrycb7da4b2019-08-28 19:35:56 -070068 }
James Kuszmaul3ae42262019-11-08 12:33:41 -080069 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080070 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070071
Austin Schuh39788ff2019-12-01 18:22:57 -080072 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070073
Austin Schuhe84c3ed2019-12-14 15:29:48 -080074 internal::EPoll *epoll() { return &epoll_; }
75
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 private:
77 friend class internal::WatcherState;
78 friend class internal::TimerHandlerState;
79 friend class internal::PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080080 friend class internal::ShmSender;
81 friend class internal::ShmFetcher;
82
Austin Schuh7d87b672019-12-01 20:23:49 -080083 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -070084
85 // Tracks that we can't have multiple watchers or a sender and a watcher (or
86 // multiple senders) on a single queue (path).
87 void Take(const Channel *channel);
88
Austin Schuh39788ff2019-12-01 18:22:57 -080089 // Returns the TID of the event loop.
90 pid_t GetTid() override;
91
Alex Perrycb7da4b2019-08-28 19:35:56 -070092 std::vector<std::function<void()>> on_run_;
93 int priority_ = 0;
94 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -080095 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 std::vector<std::string> taken_;
97
98 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070099};
100
Austin Schuh7d87b672019-12-01 20:23:49 -0800101
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102} // namespace aos
103
104#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_