blob: 89a272136890ed4aecb82cc97d07722c468393b0 [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_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070070
Austin Schuh39788ff2019-12-01 18:22:57 -080071 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070072
73 private:
74 friend class internal::WatcherState;
75 friend class internal::TimerHandlerState;
76 friend class internal::PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080077 friend class internal::ShmSender;
78 friend class internal::ShmFetcher;
79
80 void HandleWatcherSignal();
Alex Perrycb7da4b2019-08-28 19:35:56 -070081
82 // Tracks that we can't have multiple watchers or a sender and a watcher (or
83 // multiple senders) on a single queue (path).
84 void Take(const Channel *channel);
85
Austin Schuh39788ff2019-12-01 18:22:57 -080086 // Returns the TID of the event loop.
87 pid_t GetTid() override;
88
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 std::vector<std::function<void()>> on_run_;
90 int priority_ = 0;
91 std::string name_;
92 std::vector<std::string> taken_;
93
94 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070095};
96
97} // namespace aos
98
99#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_