blob: 15759f45ffea21663cacf1396fd880dcb0d94d3d [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;
Brian Silverman6a54ff32020-04-28 16:41:39 -070069 void SetRuntimeAffinity(const cpu_set_t &cpuset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070070
James Kuszmaul57c2baa2020-01-19 14:52:52 -080071 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080072 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080073 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070074
Austin Schuh39788ff2019-12-01 18:22:57 -080075 int priority() const override { return priority_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070076
Brian Silverman5120afb2020-01-31 17:44:35 -080077 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080078 internal::EPoll *epoll() { return &epoll_; }
79
Brian Silverman5120afb2020-01-31 17:44:35 -080080 // Returns the local mapping of the shared memory used by the watcher on the
81 // specified channel. A watcher must be created on this channel before calling
82 // this.
83 absl::Span<char> GetWatcherSharedMemory(const Channel *channel);
84
85 // Returns the local mapping of the shared memory used by the provided Sender
86 template <typename T>
87 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
88 return GetShmSenderSharedMemory(GetRawSender(sender));
89 }
90
Alex Perrycb7da4b2019-08-28 19:35:56 -070091 private:
92 friend class internal::WatcherState;
93 friend class internal::TimerHandlerState;
94 friend class internal::PhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080095 friend class internal::ShmSender;
96 friend class internal::ShmFetcher;
97
Brian Silverman6a54ff32020-04-28 16:41:39 -070098 static cpu_set_t DefaultAffinity() {
99 cpu_set_t result;
100 for (int i = 0; i < CPU_SETSIZE; ++i) {
101 CPU_SET(i, &result);
102 }
103 return result;
104 }
105
Austin Schuh7d87b672019-12-01 20:23:49 -0800106 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700107
Austin Schuh39788ff2019-12-01 18:22:57 -0800108 // Returns the TID of the event loop.
109 pid_t GetTid() override;
110
Brian Silverman5120afb2020-01-31 17:44:35 -0800111 // Private method to access the shared memory mapping of a ShmSender
112 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
113
Alex Perrycb7da4b2019-08-28 19:35:56 -0700114 std::vector<std::function<void()>> on_run_;
115 int priority_ = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700116 cpu_set_t affinity_ = DefaultAffinity();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700117 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800118 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700119
120 internal::EPoll epoll_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121};
122
Alex Perrycb7da4b2019-08-28 19:35:56 -0700123} // namespace aos
124
125#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_