blob: fa870b8d3fbd18c7c8788d90b945602a07c59f8b [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;
55
56 TimerHandler *AddTimer(std::function<void()> callback) override;
57 aos::PhasedLoopHandler *AddPhasedLoop(
58 std::function<void(int)> callback,
59 const monotonic_clock::duration interval,
60 const monotonic_clock::duration offset =
61 std::chrono::seconds(0)) override;
62
63 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070064
65 void SetRuntimeRealtimePriority(int priority) override;
66
James Kuszmaul57c2baa2020-01-19 14:52:52 -080067 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080068 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080069 const Node *node() const override { return node_; }
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
Brian Silverman5120afb2020-01-31 17:44:35 -080073 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080074 internal::EPoll *epoll() { return &epoll_; }
75
Brian Silverman5120afb2020-01-31 17:44:35 -080076 // Returns the local mapping of the shared memory used by the watcher on the
77 // specified channel. A watcher must be created on this channel before calling
78 // this.
79 absl::Span<char> GetWatcherSharedMemory(const Channel *channel);
80
81 // Returns the local mapping of the shared memory used by the provided Sender
82 template <typename T>
83 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
84 return GetShmSenderSharedMemory(GetRawSender(sender));
85 }
86
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 private:
88 friend class internal::WatcherState;
89 friend class internal::TimerHandlerState;
90 friend class internal::PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080091 friend class internal::ShmSender;
92 friend class internal::ShmFetcher;
93
Austin Schuh7d87b672019-12-01 20:23:49 -080094 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -070095
Austin Schuh39788ff2019-12-01 18:22:57 -080096 // Returns the TID of the event loop.
97 pid_t GetTid() override;
98
Brian Silverman5120afb2020-01-31 17:44:35 -080099 // Private method to access the shared memory mapping of a ShmSender
100 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
101
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 std::vector<std::function<void()>> on_run_;
103 int priority_ = 0;
104 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800105 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106
107 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700108};
109
Alex Perrycb7da4b2019-08-28 19:35:56 -0700110} // namespace aos
111
112#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_