Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_SHM_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_SHM_EVENT_LOOP_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include <vector> |
| 5 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 6 | #include "absl/types/span.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 7 | #include "aos/events/epoll.h" |
| 8 | #include "aos/events/event_loop.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 9 | #include "aos/events/event_loop_generated.h" |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 10 | #include "aos/ipc_lib/signalfd.h" |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 11 | #include "aos/stl_mutex/stl_mutex.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 12 | |
Jim Ostrowski | 2192ddb | 2020-06-24 19:07:31 -0700 | [diff] [blame] | 13 | DECLARE_string(application_name); |
| 14 | DECLARE_string(shm_base); |
| 15 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | namespace aos { |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 17 | namespace shm_event_loop_internal { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 19 | class ShmWatcherState; |
| 20 | class ShmTimerHandler; |
| 21 | class ShmPhasedLoopHandler; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 22 | class ShmSender; |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 23 | class SimpleShmFetcher; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 24 | class ShmFetcher; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 26 | } // namespace shm_event_loop_internal |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 27 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 28 | // Concrete implementation of EventLoop that is built from queues running out of |
| 29 | // shared memory. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | // |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 31 | // 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 35 | // destructors are called back in one thread again) |
| 36 | class ShmEventLoop : public EventLoop { |
| 37 | public: |
Austin Schuh | caa2a5d | 2020-11-01 22:38:20 -0800 | [diff] [blame] | 38 | ShmEventLoop(const Flatbuffer<Configuration> &configuration) |
| 39 | : ShmEventLoop(&configuration.message()) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 40 | ShmEventLoop(const Configuration *configuration); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 41 | ShmEventLoop(const ShmEventLoop &) = delete; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 42 | ~ShmEventLoop() override; |
| 43 | |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 44 | void operator=(ShmEventLoop const &) = delete; |
| 45 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 46 | // 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 | |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 51 | aos::monotonic_clock::time_point monotonic_now() const override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 52 | return aos::monotonic_clock::now(); |
| 53 | } |
Stephan Pleines | 559fa6c | 2022-01-06 17:23:51 -0800 | [diff] [blame] | 54 | aos::realtime_clock::time_point realtime_now() const override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 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 Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame] | 65 | void MakeRawNoArgWatcher( |
| 66 | const Channel *channel, |
| 67 | std::function<void(const Context &context)> watcher) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | |
| 69 | TimerHandler *AddTimer(std::function<void()> callback) override; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 70 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 74 | |
| 75 | void OnRun(std::function<void()> on_run) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | |
| 77 | void SetRuntimeRealtimePriority(int priority) override; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 78 | void SetRuntimeAffinity(const cpu_set_t &cpuset) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 79 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 80 | void set_name(const std::string_view name) override; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 81 | const std::string_view name() const override { return name_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 82 | const Node *node() const override { return node_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 83 | |
Austin Schuh | 65493d6 | 2022-08-17 15:10:37 -0700 | [diff] [blame^] | 84 | int runtime_realtime_priority() const override { return priority_; } |
| 85 | const cpu_set_t &runtime_affinity() const override { return affinity_; } |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 86 | const UUID &boot_uuid() const override { return boot_uuid_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 88 | // Returns the epoll loop used to run the event loop. |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 89 | internal::EPoll *epoll() { return &epoll_; } |
| 90 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 91 | // Returns the local mapping of the shared memory used by the watcher on the |
| 92 | // specified channel. A watcher must be created on this channel before calling |
| 93 | // this. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 94 | absl::Span<const char> GetWatcherSharedMemory(const Channel *channel); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 95 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 96 | // Returns the local mapping of the shared memory used by the provided Sender. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 97 | template <typename T> |
| 98 | absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 99 | CheckCurrentThread(); |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 100 | return GetShmSenderSharedMemory(GetRawSender(sender)); |
| 101 | } |
| 102 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 103 | // Returns the local mapping of the private memory used by the provided |
| 104 | // Fetcher to hold messages. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 105 | // |
| 106 | // Note that this may be the entire shared memory region held by this fetcher, |
| 107 | // depending on its channel's read_method. |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 108 | template <typename T> |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 109 | absl::Span<const char> GetFetcherPrivateMemory( |
| 110 | aos::Fetcher<T> *fetcher) const { |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 111 | CheckCurrentThread(); |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 112 | return GetShmFetcherPrivateMemory(GetRawFetcher(fetcher)); |
| 113 | } |
| 114 | |
Brian Silverman | 4f4e061 | 2020-08-12 19:54:41 -0700 | [diff] [blame] | 115 | int NumberBuffers(const Channel *channel) override; |
| 116 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 117 | // All public-facing APIs will verify this mutex is held when they are called. |
| 118 | // For normal use with everything in a single thread, this is unnecessary. |
| 119 | // |
| 120 | // This is helpful as a safety check when using a ShmEventLoop with external |
| 121 | // synchronization across multiple threads. It will NOT reliably catch race |
| 122 | // conditions, but if you have a race condition triggered repeatedly it'll |
| 123 | // probably catch it eventually. |
| 124 | void CheckForMutex(aos::stl_mutex *check_mutex) { |
| 125 | check_mutex_ = check_mutex; |
| 126 | } |
| 127 | |
| 128 | // All public-facing APIs will verify they are called in this thread. |
| 129 | // For normal use with the whole program in a single thread, this is |
| 130 | // unnecessary. It's helpful as a safety check for programs with multiple |
| 131 | // threads, where the EventLoop should only be interacted with from a single |
| 132 | // one. |
| 133 | void LockToThread() { check_tid_ = GetTid(); } |
| 134 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 135 | private: |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 136 | friend class shm_event_loop_internal::ShmWatcherState; |
| 137 | friend class shm_event_loop_internal::ShmTimerHandler; |
| 138 | friend class shm_event_loop_internal::ShmPhasedLoopHandler; |
| 139 | friend class shm_event_loop_internal::ShmSender; |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 140 | friend class shm_event_loop_internal::SimpleShmFetcher; |
Brian Silverman | 148d43d | 2020-06-07 18:19:22 -0500 | [diff] [blame] | 141 | friend class shm_event_loop_internal::ShmFetcher; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 142 | |
Austin Schuh | 432784f | 2020-06-23 17:27:35 -0700 | [diff] [blame] | 143 | using EventLoop::SendTimingReport; |
| 144 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 145 | void CheckCurrentThread() const; |
| 146 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 147 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 149 | // Returns the TID of the event loop. |
| 150 | pid_t GetTid() override; |
| 151 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 152 | // Private method to access the shared memory mapping of a ShmSender. |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 153 | absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const; |
| 154 | |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 155 | // Private method to access the private memory mapping of a ShmFetcher. |
Brian Silverman | a5450a9 | 2020-08-12 19:59:57 -0700 | [diff] [blame] | 156 | absl::Span<const char> GetShmFetcherPrivateMemory( |
Brian Silverman | 6d2b359 | 2020-06-18 14:40:15 -0700 | [diff] [blame] | 157 | const aos::RawFetcher *fetcher) const; |
| 158 | |
Austin Schuh | 83c7f70 | 2021-01-19 22:36:29 -0800 | [diff] [blame] | 159 | const UUID boot_uuid_; |
| 160 | |
Austin Schuh | ef323c0 | 2020-09-01 14:55:28 -0700 | [diff] [blame] | 161 | // Capture the --shm_base flag at construction time. This makes it much |
| 162 | // easier to make different shared memory regions for doing things like |
| 163 | // multi-node tests. |
| 164 | std::string shm_base_; |
| 165 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 166 | std::vector<std::function<void()>> on_run_; |
| 167 | int priority_ = 0; |
Brian Silverman | 6a54ff3 | 2020-04-28 16:41:39 -0700 | [diff] [blame] | 168 | cpu_set_t affinity_ = DefaultAffinity(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 169 | std::string name_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 170 | const Node *const node_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 171 | |
Austin Schuh | 3054f5f | 2021-07-21 15:38:01 -0700 | [diff] [blame] | 172 | aos::stl_mutex *check_mutex_ = nullptr; |
| 173 | std::optional<pid_t> check_tid_; |
| 174 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 175 | internal::EPoll epoll_; |
Austin Schuh | 5ca1311 | 2021-02-07 22:06:53 -0800 | [diff] [blame] | 176 | |
| 177 | // Only set during Run(). |
| 178 | std::unique_ptr<ipc_lib::SignalFd> signalfd_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 181 | } // namespace aos |
| 182 | |
| 183 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |