blob: d3f1295ec23c28f2db4eb140bd49dff7b58bc46a [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
Brian Silverman5120afb2020-01-31 17:44:35 -08006#include "absl/types/span.h"
7
Alex Perrycb7da4b2019-08-28 19:35:56 -07008#include "aos/events/epoll.h"
9#include "aos/events/event_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -080010#include "aos/events/event_loop_generated.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070011
12namespace aos {
13namespace internal {
14
15class WatcherState;
16class TimerHandlerState;
17class PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080018class ShmSender;
19class ShmFetcher;
Alex Perrycb7da4b2019-08-28 19:35:56 -070020
21} // namespace internal
22
23// Specialization of EventLoop that is built from queues running out of shared
Austin Schuh39788ff2019-12-01 18:22:57 -080024// memory.
Alex Perrycb7da4b2019-08-28 19:35:56 -070025//
Austin Schuh39788ff2019-12-01 18:22:57 -080026// TODO(austin): Timing reports break multiple threads. Need to add back in a
27// mutex.
28// This object must be interacted with from one thread, but the Senders
29// and Fetchers may be used from multiple threads afterwords (as long as their
Alex Perrycb7da4b2019-08-28 19:35:56 -070030// destructors are called back in one thread again)
31class ShmEventLoop : public EventLoop {
32 public:
33 ShmEventLoop(const Configuration *configuration);
34 ~ShmEventLoop() override;
35
Austin Schuh39788ff2019-12-01 18:22:57 -080036 // Runs the event loop until Exit is called, or ^C is caught.
37 void Run();
38 // Exits the event loop. Async safe.
39 void Exit();
40
Alex Perrycb7da4b2019-08-28 19:35:56 -070041 aos::monotonic_clock::time_point monotonic_now() override {
42 return aos::monotonic_clock::now();
43 }
44 aos::realtime_clock::time_point realtime_now() override {
45 return aos::realtime_clock::now();
46 }
47
48 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
49 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
50
51 void MakeRawWatcher(
52 const Channel *channel,
53 std::function<void(const Context &context, const void *message)> watcher)
54 override;
Brian Silverman6b8a3c32020-03-06 11:26:14 -080055 void MakeRawNoArgWatcher(
56 const Channel *channel,
57 std::function<void(const Context &context)> watcher) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070058
59 TimerHandler *AddTimer(std::function<void()> callback) override;
60 aos::PhasedLoopHandler *AddPhasedLoop(
61 std::function<void(int)> callback,
62 const monotonic_clock::duration interval,
63 const monotonic_clock::duration offset =
64 std::chrono::seconds(0)) override;
65
66 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070067
68 void SetRuntimeRealtimePriority(int priority) override;
69
James Kuszmaul57c2baa2020-01-19 14:52:52 -080070 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080071 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080072 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070073
Austin Schuh39788ff2019-12-01 18:22:57 -080074 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070075
Brian Silverman5120afb2020-01-31 17:44:35 -080076 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080077 internal::EPoll *epoll() { return &epoll_; }
78
Brian Silverman5120afb2020-01-31 17:44:35 -080079 // Returns the local mapping of the shared memory used by the watcher on the
80 // specified channel. A watcher must be created on this channel before calling
81 // this.
82 absl::Span<char> GetWatcherSharedMemory(const Channel *channel);
83
84 // Returns the local mapping of the shared memory used by the provided Sender
85 template <typename T>
86 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
87 return GetShmSenderSharedMemory(GetRawSender(sender));
88 }
89
Alex Perrycb7da4b2019-08-28 19:35:56 -070090 private:
91 friend class internal::WatcherState;
92 friend class internal::TimerHandlerState;
93 friend class internal::PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080094 friend class internal::ShmSender;
95 friend class internal::ShmFetcher;
96
Austin Schuh7d87b672019-12-01 20:23:49 -080097 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -070098
Austin Schuh39788ff2019-12-01 18:22:57 -080099 // Returns the TID of the event loop.
100 pid_t GetTid() override;
101
Brian Silverman5120afb2020-01-31 17:44:35 -0800102 // Private method to access the shared memory mapping of a ShmSender
103 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
104
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 std::vector<std::function<void()>> on_run_;
106 int priority_ = 0;
107 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800108 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109
110 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700111};
112
Alex Perrycb7da4b2019-08-28 19:35:56 -0700113} // namespace aos
114
115#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_