blob: 71dff4e45347206cdf42b0dc217a34716676e8fd [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"
Alex Perrycb7da4b2019-08-28 19:35:56 -07007#include "aos/events/epoll.h"
8#include "aos/events/event_loop.h"
Austin Schuh39788ff2019-12-01 18:22:57 -08009#include "aos/events/event_loop_generated.h"
Austin Schuh5ca13112021-02-07 22:06:53 -080010#include "aos/ipc_lib/signalfd.h"
Austin Schuh3054f5f2021-07-21 15:38:01 -070011#include "aos/stl_mutex/stl_mutex.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);
Austin Schuhad9e5eb2021-11-19 20:33:55 -080041 ShmEventLoop(const ShmEventLoop &) = delete;
Alex Perrycb7da4b2019-08-28 19:35:56 -070042 ~ShmEventLoop() override;
43
Austin Schuhad9e5eb2021-11-19 20:33:55 -080044 void operator=(ShmEventLoop const &) = delete;
45
Austin Schuh39788ff2019-12-01 18:22:57 -080046 // Runs the event loop until Exit is called, or ^C is caught.
47 void Run();
48 // Exits the event loop. Async safe.
49 void Exit();
50
Alex Perrycb7da4b2019-08-28 19:35:56 -070051 aos::monotonic_clock::time_point monotonic_now() override {
52 return aos::monotonic_clock::now();
53 }
54 aos::realtime_clock::time_point realtime_now() override {
55 return aos::realtime_clock::now();
56 }
57
58 std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override;
59 std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override;
60
61 void MakeRawWatcher(
62 const Channel *channel,
63 std::function<void(const Context &context, const void *message)> watcher)
64 override;
Brian Silverman6b8a3c32020-03-06 11:26:14 -080065 void MakeRawNoArgWatcher(
66 const Channel *channel,
67 std::function<void(const Context &context)> watcher) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070068
69 TimerHandler *AddTimer(std::function<void()> callback) override;
Brian Silverman148d43d2020-06-07 18:19:22 -050070 PhasedLoopHandler *AddPhasedLoop(std::function<void(int)> callback,
71 const monotonic_clock::duration interval,
72 const monotonic_clock::duration offset =
73 std::chrono::seconds(0)) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070074
75 void OnRun(std::function<void()> on_run) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070076
77 void SetRuntimeRealtimePriority(int priority) override;
Brian Silverman6a54ff32020-04-28 16:41:39 -070078 void SetRuntimeAffinity(const cpu_set_t &cpuset) override;
Alex Perrycb7da4b2019-08-28 19:35:56 -070079
James Kuszmaul57c2baa2020-01-19 14:52:52 -080080 void set_name(const std::string_view name) override;
James Kuszmaul3ae42262019-11-08 12:33:41 -080081 const std::string_view name() const override { return name_; }
Austin Schuh217a9782019-12-21 23:02:50 -080082 const Node *node() const override { return node_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070083
Austin Schuh39788ff2019-12-01 18:22:57 -080084 int priority() const override { return priority_; }
Austin Schuh83c7f702021-01-19 22:36:29 -080085 const UUID &boot_uuid() const override { return boot_uuid_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070086
Brian Silverman5120afb2020-01-31 17:44:35 -080087 // Returns the epoll loop used to run the event loop.
Austin Schuhe84c3ed2019-12-14 15:29:48 -080088 internal::EPoll *epoll() { return &epoll_; }
89
Brian Silverman5120afb2020-01-31 17:44:35 -080090 // Returns the local mapping of the shared memory used by the watcher on the
91 // specified channel. A watcher must be created on this channel before calling
92 // this.
Brian Silvermana5450a92020-08-12 19:59:57 -070093 absl::Span<const char> GetWatcherSharedMemory(const Channel *channel);
Brian Silverman5120afb2020-01-31 17:44:35 -080094
Brian Silverman6d2b3592020-06-18 14:40:15 -070095 // Returns the local mapping of the shared memory used by the provided Sender.
Brian Silverman5120afb2020-01-31 17:44:35 -080096 template <typename T>
97 absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -070098 CheckCurrentThread();
Brian Silverman5120afb2020-01-31 17:44:35 -080099 return GetShmSenderSharedMemory(GetRawSender(sender));
100 }
101
Brian Silverman6d2b3592020-06-18 14:40:15 -0700102 // Returns the local mapping of the private memory used by the provided
103 // Fetcher to hold messages.
Brian Silvermana5450a92020-08-12 19:59:57 -0700104 //
105 // Note that this may be the entire shared memory region held by this fetcher,
106 // depending on its channel's read_method.
Brian Silverman6d2b3592020-06-18 14:40:15 -0700107 template <typename T>
Brian Silvermana5450a92020-08-12 19:59:57 -0700108 absl::Span<const char> GetFetcherPrivateMemory(
109 aos::Fetcher<T> *fetcher) const {
Austin Schuh3054f5f2021-07-21 15:38:01 -0700110 CheckCurrentThread();
Brian Silverman6d2b3592020-06-18 14:40:15 -0700111 return GetShmFetcherPrivateMemory(GetRawFetcher(fetcher));
112 }
113
Brian Silverman4f4e0612020-08-12 19:54:41 -0700114 int NumberBuffers(const Channel *channel) override;
115
Austin Schuh3054f5f2021-07-21 15:38:01 -0700116 // All public-facing APIs will verify this mutex is held when they are called.
117 // For normal use with everything in a single thread, this is unnecessary.
118 //
119 // This is helpful as a safety check when using a ShmEventLoop with external
120 // synchronization across multiple threads. It will NOT reliably catch race
121 // conditions, but if you have a race condition triggered repeatedly it'll
122 // probably catch it eventually.
123 void CheckForMutex(aos::stl_mutex *check_mutex) {
124 check_mutex_ = check_mutex;
125 }
126
127 // All public-facing APIs will verify they are called in this thread.
128 // For normal use with the whole program in a single thread, this is
129 // unnecessary. It's helpful as a safety check for programs with multiple
130 // threads, where the EventLoop should only be interacted with from a single
131 // one.
132 void LockToThread() { check_tid_ = GetTid(); }
133
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 private:
Brian Silverman148d43d2020-06-07 18:19:22 -0500135 friend class shm_event_loop_internal::ShmWatcherState;
136 friend class shm_event_loop_internal::ShmTimerHandler;
137 friend class shm_event_loop_internal::ShmPhasedLoopHandler;
138 friend class shm_event_loop_internal::ShmSender;
Austin Schuh432784f2020-06-23 17:27:35 -0700139 friend class shm_event_loop_internal::SimpleShmFetcher;
Brian Silverman148d43d2020-06-07 18:19:22 -0500140 friend class shm_event_loop_internal::ShmFetcher;
Austin Schuh39788ff2019-12-01 18:22:57 -0800141
Austin Schuh432784f2020-06-23 17:27:35 -0700142 using EventLoop::SendTimingReport;
143
Brian Silverman6a54ff32020-04-28 16:41:39 -0700144 static cpu_set_t DefaultAffinity() {
145 cpu_set_t result;
146 for (int i = 0; i < CPU_SETSIZE; ++i) {
147 CPU_SET(i, &result);
148 }
149 return result;
150 }
151
Austin Schuh3054f5f2021-07-21 15:38:01 -0700152 void CheckCurrentThread() const;
153
Austin Schuh7d87b672019-12-01 20:23:49 -0800154 void HandleEvent();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700155
Austin Schuh39788ff2019-12-01 18:22:57 -0800156 // Returns the TID of the event loop.
157 pid_t GetTid() override;
158
Brian Silverman6d2b3592020-06-18 14:40:15 -0700159 // Private method to access the shared memory mapping of a ShmSender.
Brian Silverman5120afb2020-01-31 17:44:35 -0800160 absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const;
161
Brian Silverman6d2b3592020-06-18 14:40:15 -0700162 // Private method to access the private memory mapping of a ShmFetcher.
Brian Silvermana5450a92020-08-12 19:59:57 -0700163 absl::Span<const char> GetShmFetcherPrivateMemory(
Brian Silverman6d2b3592020-06-18 14:40:15 -0700164 const aos::RawFetcher *fetcher) const;
165
Austin Schuh83c7f702021-01-19 22:36:29 -0800166 const UUID boot_uuid_;
167
Austin Schuhef323c02020-09-01 14:55:28 -0700168 // Capture the --shm_base flag at construction time. This makes it much
169 // easier to make different shared memory regions for doing things like
170 // multi-node tests.
171 std::string shm_base_;
172
Alex Perrycb7da4b2019-08-28 19:35:56 -0700173 std::vector<std::function<void()>> on_run_;
174 int priority_ = 0;
Brian Silverman6a54ff32020-04-28 16:41:39 -0700175 cpu_set_t affinity_ = DefaultAffinity();
Alex Perrycb7da4b2019-08-28 19:35:56 -0700176 std::string name_;
Austin Schuh217a9782019-12-21 23:02:50 -0800177 const Node *const node_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178
Austin Schuh3054f5f2021-07-21 15:38:01 -0700179 aos::stl_mutex *check_mutex_ = nullptr;
180 std::optional<pid_t> check_tid_;
181
Alex Perrycb7da4b2019-08-28 19:35:56 -0700182 internal::EPoll epoll_;
Austin Schuh5ca13112021-02-07 22:06:53 -0800183
184 // Only set during Run().
185 std::unique_ptr<ipc_lib::SignalFd> signalfd_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700186};
187
Alex Perrycb7da4b2019-08-28 19:35:56 -0700188} // namespace aos
189
190#endif // AOS_EVENTS_SHM_EVENT_LOOP_H_