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 | |
| 6 | #include "aos/events/epoll.h" |
| 7 | #include "aos/events/event_loop.h" |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 8 | #include "aos/events/event_loop_generated.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | namespace internal { |
| 12 | |
| 13 | class WatcherState; |
| 14 | class TimerHandlerState; |
| 15 | class PhasedLoopHandler; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 16 | class ShmSender; |
| 17 | class ShmFetcher; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 18 | |
| 19 | } // namespace internal |
| 20 | |
| 21 | // Specialization of EventLoop that is built from queues running out of shared |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 22 | // memory. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 23 | // |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 24 | // TODO(austin): Timing reports break multiple threads. Need to add back in a |
| 25 | // mutex. |
| 26 | // This object must be interacted with from one thread, but the Senders |
| 27 | // 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] | 28 | // destructors are called back in one thread again) |
| 29 | class ShmEventLoop : public EventLoop { |
| 30 | public: |
| 31 | ShmEventLoop(const Configuration *configuration); |
| 32 | ~ShmEventLoop() override; |
| 33 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 34 | // Runs the event loop until Exit is called, or ^C is caught. |
| 35 | void Run(); |
| 36 | // Exits the event loop. Async safe. |
| 37 | void Exit(); |
| 38 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 39 | aos::monotonic_clock::time_point monotonic_now() override { |
| 40 | return aos::monotonic_clock::now(); |
| 41 | } |
| 42 | aos::realtime_clock::time_point realtime_now() override { |
| 43 | return aos::realtime_clock::now(); |
| 44 | } |
| 45 | |
| 46 | std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 47 | std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 48 | |
| 49 | void MakeRawWatcher( |
| 50 | const Channel *channel, |
| 51 | std::function<void(const Context &context, const void *message)> watcher) |
| 52 | override; |
| 53 | |
| 54 | TimerHandler *AddTimer(std::function<void()> callback) override; |
| 55 | aos::PhasedLoopHandler *AddPhasedLoop( |
| 56 | std::function<void(int)> callback, |
| 57 | const monotonic_clock::duration interval, |
| 58 | const monotonic_clock::duration offset = |
| 59 | std::chrono::seconds(0)) override; |
| 60 | |
| 61 | void OnRun(std::function<void()> on_run) override; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 62 | |
| 63 | void SetRuntimeRealtimePriority(int priority) override; |
| 64 | |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 65 | void set_name(const std::string_view name) override { |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | name_ = std::string(name); |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 67 | UpdateTimingReport(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | } |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 69 | const std::string_view name() const override { return name_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 70 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 71 | int priority() const override { return priority_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | friend class internal::WatcherState; |
| 75 | friend class internal::TimerHandlerState; |
| 76 | friend class internal::PhasedLoopHandler; |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 77 | friend class internal::ShmSender; |
| 78 | friend class internal::ShmFetcher; |
| 79 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame^] | 80 | void HandleEvent(); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 81 | |
| 82 | // Tracks that we can't have multiple watchers or a sender and a watcher (or |
| 83 | // multiple senders) on a single queue (path). |
| 84 | void Take(const Channel *channel); |
| 85 | |
Austin Schuh | 39788ff | 2019-12-01 18:22:57 -0800 | [diff] [blame] | 86 | // Returns the TID of the event loop. |
| 87 | pid_t GetTid() override; |
| 88 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 89 | std::vector<std::function<void()>> on_run_; |
| 90 | int priority_ = 0; |
| 91 | std::string name_; |
| 92 | std::vector<std::string> taken_; |
| 93 | |
| 94 | internal::EPoll epoll_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame^] | 97 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | } // namespace aos |
| 99 | |
| 100 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |