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 | |
| 4 | #include <unordered_set> |
| 5 | #include <vector> |
| 6 | |
| 7 | #include "aos/events/epoll.h" |
| 8 | #include "aos/events/event_loop.h" |
| 9 | #include "aos/ipc_lib/signalfd.h" |
| 10 | #include "aos/mutex/mutex.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace internal { |
| 14 | |
| 15 | class WatcherState; |
| 16 | class TimerHandlerState; |
| 17 | class PhasedLoopHandler; |
| 18 | |
| 19 | } // namespace internal |
| 20 | |
| 21 | // Specialization of EventLoop that is built from queues running out of shared |
| 22 | // memory. See more details at aos/queue.h |
| 23 | // |
| 24 | // This object must be interacted with from one thread, but the Senders and |
| 25 | // Fetchers may be used from multiple threads afterwords (as long as their |
| 26 | // destructors are called back in one thread again) |
| 27 | class ShmEventLoop : public EventLoop { |
| 28 | public: |
| 29 | ShmEventLoop(const Configuration *configuration); |
| 30 | ~ShmEventLoop() override; |
| 31 | |
| 32 | aos::monotonic_clock::time_point monotonic_now() override { |
| 33 | return aos::monotonic_clock::now(); |
| 34 | } |
| 35 | aos::realtime_clock::time_point realtime_now() override { |
| 36 | return aos::realtime_clock::now(); |
| 37 | } |
| 38 | |
| 39 | std::unique_ptr<RawSender> MakeRawSender(const Channel *channel) override; |
| 40 | std::unique_ptr<RawFetcher> MakeRawFetcher(const Channel *channel) override; |
| 41 | |
| 42 | void MakeRawWatcher( |
| 43 | const Channel *channel, |
| 44 | std::function<void(const Context &context, const void *message)> watcher) |
| 45 | override; |
| 46 | |
| 47 | TimerHandler *AddTimer(std::function<void()> callback) override; |
| 48 | aos::PhasedLoopHandler *AddPhasedLoop( |
| 49 | std::function<void(int)> callback, |
| 50 | const monotonic_clock::duration interval, |
| 51 | const monotonic_clock::duration offset = |
| 52 | std::chrono::seconds(0)) override; |
| 53 | |
| 54 | void OnRun(std::function<void()> on_run) override; |
| 55 | void Run(); |
| 56 | void Exit(); |
| 57 | |
| 58 | // TODO(austin): Add a function to register control-C call. |
| 59 | |
| 60 | void SetRuntimeRealtimePriority(int priority) override; |
| 61 | |
| 62 | void set_name(const absl::string_view name) override { |
| 63 | name_ = std::string(name); |
| 64 | } |
| 65 | const absl::string_view name() const override { return name_; } |
| 66 | |
| 67 | int priority() const { return priority_; } |
| 68 | |
| 69 | private: |
| 70 | friend class internal::WatcherState; |
| 71 | friend class internal::TimerHandlerState; |
| 72 | friend class internal::PhasedLoopHandler; |
| 73 | |
| 74 | // Tracks that we can't have multiple watchers or a sender and a watcher (or |
| 75 | // multiple senders) on a single queue (path). |
| 76 | void Take(const Channel *channel); |
| 77 | |
| 78 | std::vector<std::function<void()>> on_run_; |
| 79 | int priority_ = 0; |
| 80 | std::string name_; |
| 81 | std::vector<std::string> taken_; |
| 82 | |
| 83 | internal::EPoll epoll_; |
| 84 | |
| 85 | std::vector<std::unique_ptr<internal::TimerHandlerState>> timers_; |
| 86 | std::vector<std::unique_ptr<internal::PhasedLoopHandler>> phased_loops_; |
| 87 | std::vector<std::unique_ptr<internal::WatcherState>> watchers_; |
| 88 | }; |
| 89 | |
| 90 | } // namespace aos |
| 91 | |
| 92 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |