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" |
| 7 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 8 | #include "aos/events/epoll.h" |
| 9 | #include "aos/events/event_loop.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 10 | #include "aos/events/event_loop_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | namespace internal { |
| 14 | |
| 15 | class WatcherState; |
| 16 | class TimerHandlerState; |
| 17 | class PhasedLoopHandler; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 18 | class ShmSender; |
| 19 | class ShmFetcher; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 20 | |
| 21 | } // namespace internal |
| 22 | |
| 23 | // Specialization of EventLoop that is built from queues running out of shared |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 24 | // memory. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 25 | // |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 26 | // 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 30 | // destructors are called back in one thread again) |
| 31 | class ShmEventLoop : public EventLoop { |
| 32 | public: |
| 33 | ShmEventLoop(const Configuration *configuration); |
| 34 | ~ShmEventLoop() override; |
| 35 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 36 | // 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 41 | 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 Silverman | 6b8a3c3 | 2020-03-06 11:26:14 -0800 | [diff] [blame^] | 55 | void MakeRawNoArgWatcher( |
| 56 | const Channel *channel, |
| 57 | std::function<void(const Context &context)> watcher) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 58 | |
| 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | |
| 68 | void SetRuntimeRealtimePriority(int priority) override; |
| 69 | |
James Kuszmaul | 57c2baa | 2020-01-19 14:52:52 -0800 | [diff] [blame] | 70 | void set_name(const std::string_view name) override; |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 71 | const std::string_view name() const override { return name_; } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 72 | const Node *node() const override { return node_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 73 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 74 | int priority() const override { return priority_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 75 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 76 | // Returns the epoll loop used to run the event loop. |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 77 | internal::EPoll *epoll() { return &epoll_; } |
| 78 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 79 | // Returns the local mapping of the shared memory used by the watcher on the |
| 80 | // specified channel. A watcher must be created on this channel before calling |
| 81 | // this. |
| 82 | absl::Span<char> GetWatcherSharedMemory(const Channel *channel); |
| 83 | |
| 84 | // Returns the local mapping of the shared memory used by the provided Sender |
| 85 | template <typename T> |
| 86 | absl::Span<char> GetSenderSharedMemory(aos::Sender<T> *sender) const { |
| 87 | return GetShmSenderSharedMemory(GetRawSender(sender)); |
| 88 | } |
| 89 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 90 | private: |
| 91 | friend class internal::WatcherState; |
| 92 | friend class internal::TimerHandlerState; |
| 93 | friend class internal::PhasedLoopHandler; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 94 | friend class internal::ShmSender; |
| 95 | friend class internal::ShmFetcher; |
| 96 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 97 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 99 | // Returns the TID of the event loop. |
| 100 | pid_t GetTid() override; |
| 101 | |
Brian Silverman | 5120afb | 2020-01-31 17:44:35 -0800 | [diff] [blame] | 102 | // Private method to access the shared memory mapping of a ShmSender |
| 103 | absl::Span<char> GetShmSenderSharedMemory(const aos::RawSender *sender) const; |
| 104 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 105 | std::vector<std::function<void()>> on_run_; |
| 106 | int priority_ = 0; |
| 107 | std::string name_; |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 108 | const Node *const node_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 109 | |
| 110 | internal::EPoll epoll_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 113 | } // namespace aos |
| 114 | |
| 115 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |