blob: 845857c36d81a1393769b87da58ac8e4775879da [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"
Austin Schuh5ca13112021-02-07 22:06:53 -080011#include "aos/ipc_lib/signalfd.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070012
Jim Ostrowski2192ddb2020-06-24 19:07:31 -070013DECLARE_string(application_name);
14DECLARE_string(shm_base);
15
Alex Perrycb7da4b2019-08-28 19:35:56 -070016namespace aos {
Brian Silverman148d43d2020-06-07 18:19:22 -050017namespace shm_event_loop_internal {
Alex Perrycb7da4b2019-08-28 19:35:56 -070018
Brian Silverman148d43d2020-06-07 18:19:22 -050019class ShmWatcherState;
20class ShmTimerHandler;
21class ShmPhasedLoopHandler;
Austin Schuh39788ff2019-12-01 18:22:57 -080022class ShmSender;
Austin Schuh432784f2020-06-23 17:27:35 -070023class SimpleShmFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -080024class ShmFetcher;
Alex Perrycb7da4b2019-08-28 19:35:56 -070025
Brian Silverman148d43d2020-06-07 18:19:22 -050026} // namespace shm_event_loop_internal
Alex Perrycb7da4b2019-08-28 19:35:56 -070027
Brian Silverman4f4e0612020-08-12 19:54:41 -070028// Concrete implementation of EventLoop that is built from queues running out of
29// shared memory.
Alex Perrycb7da4b2019-08-28 19:35:56 -070030//
Austin Schuh39788ff2019-12-01 18:22:57 -080031// TODO(austin): Timing reports break multiple threads. Need to add back in a
32// mutex.
33// This object must be interacted with from one thread, but the Senders
34// and Fetchers may be used from multiple threads afterwords (as long as their
Alex Perrycb7da4b2019-08-28 19:35:56 -070035// destructors are called back in one thread again)
36class ShmEventLoop : public EventLoop {
37 public:
Austin Schuhcaa2a5d2020-11-01 22:38:20 -080038 ShmEventLoop(const Flatbuffer<Configuration> &configuration)
39 : ShmEventLoop(&configuration.message()) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -070040 ShmEventLoop(const Configuration *configuration);
41 ~ShmEventLoop() override;
42
Austin Schuh39788ff2019-12-01 18:22:57 -080043 // Runs the event loop until Exit is called, or ^C is caught.
44 void Run();
45 // Exits the event loop. Async safe.
46 void Exit();
47
Alex Perrycb7da4b2019-08-28 19:35:56 -070048 aos::monotonic_clock::time_point monotonic_now() override {
49 return aos::monotonic_clock::now();
50 }
51 aos::realtime_clock::time_point realtime_now() override {
52 return aos::realtime_clock::now();
53 }
54
55 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
56 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
57
58 void MakeRawWatcher(
59 const Channel *channel,
60 std::function<void(const Context &context, const void *message)> watcher)
61 override;
Brian Silverman6b8a3c32020-03-06 11:26:14 -080062 void MakeRawNoArgWatcher(
63 const Channel *channel,
64 std::function<void(const Context &context)> watcher) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070065
66 TimerHandler *AddTimer(std::function<void()> callback) override;
Brian Silverman148d43d2020-06-07 18:19:22 -050067 PhasedLoopHandler *AddPhasedLoop(std::function<void(int)> callback,
68 const monotonic_clock::duration interval,
69 const monotonic_clock::duration offset =
70 std::chrono::seconds(0)) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070071
72 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070073
74 void SetRuntimeRealtimePriority(int priority) override;
Brian Silverman6a54ff32020-04-28 16:41:39 -070075 void SetRuntimeAffinity(const cpu_set_t &cpuset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070076
James Kuszmaul57c2baa2020-01-19 14:52:52 -080077 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080078 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080079 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070080
Austin Schuh39788ff2019-12-01 18:22:57 -080081 int priority() const override { return priority_; }
Austin Schuh83c7f702021-01-19 22:36:29 -080082 const UUID &boot_uuid() const override { return boot_uuid_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070083
Brian Silverman5120afb2020-01-31 17:44:35 -080084 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080085 internal::EPoll *epoll() { return &epoll_; }
86
Brian Silverman5120afb2020-01-31 17:44:35 -080087 // Returns the local mapping of the shared memory used by the watcher on the
88 // specified channel. A watcher must be created on this channel before calling
89 // this.
Brian Silvermana5450a92020-08-12 19:59:57 -070090 absl::Span<const char> GetWatcherSharedMemory(const Channel *channel);
Brian Silverman5120afb2020-01-31 17:44:35 -080091
Brian Silverman6d2b3592020-06-18 14:40:15 -070092 // Returns the local mapping of the shared memory used by the provided Sender.
Brian Silverman5120afb2020-01-31 17:44:35 -080093 template <typename T>
94 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
95 return GetShmSenderSharedMemory(GetRawSender(sender));
96 }
97
Brian Silverman6d2b3592020-06-18 14:40:15 -070098 // Returns the local mapping of the private memory used by the provided
99 // Fetcher to hold messages.
Brian Silvermana5450a92020-08-12 19:59:57 -0700100 //
101 // Note that this may be the entire shared memory region held by this fetcher,
102 // depending on its channel's read_method.
Brian Silverman6d2b3592020-06-18 14:40:15 -0700103 template <typename T>
Brian Silvermana5450a92020-08-12 19:59:57 -0700104 absl::Span<const char> GetFetcherPrivateMemory(
105 aos::Fetcher<T> *fetcher) const {
Brian Silverman6d2b3592020-06-18 14:40:15 -0700106 return GetShmFetcherPrivateMemory(GetRawFetcher(fetcher));
107 }
108
Brian Silverman4f4e0612020-08-12 19:54:41 -0700109 int NumberBuffers(const Channel *channel) override;
110
Alex Perrycb7da4b2019-08-28 19:35:56 -0700111 private:
Brian Silverman148d43d2020-06-07 18:19:22 -0500112 friend class shm_event_loop_internal::ShmWatcherState;
113 friend class shm_event_loop_internal::ShmTimerHandler;
114 friend class shm_event_loop_internal::ShmPhasedLoopHandler;
115 friend class shm_event_loop_internal::ShmSender;
Austin Schuh432784f2020-06-23 17:27:35 -0700116 friend class shm_event_loop_internal::SimpleShmFetcher;
Brian Silverman148d43d2020-06-07 18:19:22 -0500117 friend class shm_event_loop_internal::ShmFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -0800118
Austin Schuh432784f2020-06-23 17:27:35 -0700119 using EventLoop::SendTimingReport;
120
Brian Silverman6a54ff32020-04-28 16:41:39 -0700121 static cpu_set_t DefaultAffinity() {
122 cpu_set_t result;
123 for (int i = 0; i < CPU_SETSIZE; ++i) {
124 CPU_SET(i, &result);
125 }
126 return result;
127 }
128
Austin Schuh7d87b672019-12-01 20:23:49 -0800129 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130
Austin Schuh39788ff2019-12-01 18:22:57 -0800131 // Returns the TID of the event loop.
132 pid_t GetTid() override;
133
Brian Silverman6d2b3592020-06-18 14:40:15 -0700134 // Private method to access the shared memory mapping of a ShmSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800135 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
136
Brian Silverman6d2b3592020-06-18 14:40:15 -0700137 // Private method to access the private memory mapping of a ShmFetcher.
Brian Silvermana5450a92020-08-12 19:59:57 -0700138 absl::Span<const char> GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -0700139 const aos::RawFetcher *fetcher) const;
140
Austin Schuh83c7f702021-01-19 22:36:29 -0800141 const UUID boot_uuid_;
142
Austin Schuhef323c02020-09-01 14:55:28 -0700143 // Capture the --shm_base flag at construction time. This makes it much
144 // easier to make different shared memory regions for doing things like
145 // multi-node tests.
146 std::string shm_base_;
147
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 std::vector<std::function<void()>> on_run_;
149 int priority_ = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700150 cpu_set_t affinity_ = DefaultAffinity();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700151 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800152 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700153
154 internal::EPoll epoll_;
Austin Schuh5ca13112021-02-07 22:06:53 -0800155
156 // Only set during Run().
157 std::unique_ptr<ipc_lib::SignalFd> signalfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700158};
159
Alex Perrycb7da4b2019-08-28 19:35:56 -0700160} // namespace aos
161
162#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_