Austin Schuh | 520f33d | 2019-01-27 22:38:01 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_SHM_EVENT_LOOP_H_ |
| 2 | #define AOS_EVENTS_SHM_EVENT_LOOP_H_ |
| 3 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 4 | #include <unordered_set> |
| 5 | #include <vector> |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 6 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 7 | #include "aos/condition.h" |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 8 | #include "aos/events/epoll.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 9 | #include "aos/events/event-loop.h" |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 10 | #include "aos/mutex/mutex.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | namespace internal { |
| 14 | |
| 15 | class WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 16 | class TimerHandlerState; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 17 | class PhasedLoopHandler; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 18 | |
| 19 | } // namespace internal |
| 20 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 21 | // Specialization of EventLoop that is built from queues running out of shared |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 22 | // memory. See more details at aos/queue.h |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame] | 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) |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 27 | class ShmEventLoop : public EventLoop { |
| 28 | public: |
| 29 | ShmEventLoop(); |
| 30 | ~ShmEventLoop() override; |
| 31 | |
| 32 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 33 | return ::aos::monotonic_clock::now(); |
| 34 | } |
| 35 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 36 | ::std::unique_ptr<RawSender> MakeRawSender( |
| 37 | const ::std::string &path, const QueueTypeInfo &type) override; |
| 38 | ::std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 39 | const ::std::string &path, const QueueTypeInfo &type) override; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 40 | |
| 41 | void MakeRawWatcher( |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 42 | const ::std::string &path, const QueueTypeInfo &type, |
| 43 | ::std::function<void(const aos::Message *message)> watcher) override; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 44 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 45 | TimerHandler *AddTimer(::std::function<void()> callback) override; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 46 | ::aos::PhasedLoopHandler *AddPhasedLoop( |
| 47 | ::std::function<void(int)> callback, |
| 48 | const monotonic_clock::duration interval, |
| 49 | const monotonic_clock::duration offset = |
| 50 | ::std::chrono::seconds(0)) override; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 52 | void OnRun(::std::function<void()> on_run) override; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 53 | void Run() override; |
| 54 | void Exit() override; |
| 55 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 56 | // TODO(austin): Add a function to register control-C call. |
| 57 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 58 | void SetRuntimeRealtimePriority(int priority) override { |
| 59 | if (is_running()) { |
| 60 | ::aos::Die("Cannot set realtime priority while running."); |
| 61 | } |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 62 | thread_state_.priority_ = priority; |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 65 | private: |
| 66 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 67 | friend class internal::TimerHandlerState; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 68 | friend class internal::PhasedLoopHandler; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 69 | // This ThreadState ensures that two watchers in the same loop cannot be |
| 70 | // triggered concurrently. Because watchers block threads indefinitely, this |
| 71 | // has to be shared_ptr in case the EventLoop is destroyed before the thread |
| 72 | // receives any new events. |
| 73 | class ThreadState { |
| 74 | public: |
| 75 | void WaitForStart(); |
| 76 | |
| 77 | bool is_running() { return loop_running_; } |
| 78 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 79 | void Start(); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 80 | |
| 81 | void Exit(); |
| 82 | |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 83 | void MaybeSetCurrentThreadRealtimePriority(); |
| 84 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 85 | private: |
| 86 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 87 | friend class internal::TimerHandlerState; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 88 | friend class internal::PhasedLoopHandler; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 89 | friend class ShmEventLoop; |
| 90 | |
| 91 | // This mutex ensures that only one watch event happens at a time. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 92 | ::aos::Mutex mutex_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 93 | // Block on this until the loop starts. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 94 | ::aos::Condition loop_running_cond_{&mutex_}; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 95 | // Used to notify watchers that the loop is done. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 96 | ::std::atomic<bool> loop_running_{false}; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 97 | bool loop_finished_ = false; |
Austin Schuh | 3115a20 | 2019-05-27 21:02:14 -0700 | [diff] [blame] | 98 | int priority_ = -1; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 101 | // Tracks that we can't have multiple watchers or a sender and a watcher (or |
| 102 | // multiple senders) on a single queue (path). |
| 103 | void Take(const ::std::string &path); |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 105 | ::std::vector<::std::function<void()>> on_run_; |
| 106 | ThreadState thread_state_; |
| 107 | ::std::vector<::std::string> taken_; |
| 108 | internal::EPoll epoll_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 109 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 110 | ::std::vector<::std::unique_ptr<internal::TimerHandlerState>> timers_; |
Austin Schuh | 52d325c | 2019-06-23 18:59:06 -0700 | [diff] [blame^] | 111 | ::std::vector<::std::unique_ptr<internal::PhasedLoopHandler>> phased_loops_; |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 112 | ::std::vector<::std::unique_ptr<internal::WatcherThreadState>> watchers_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace aos |
Austin Schuh | 520f33d | 2019-01-27 22:38:01 -0800 | [diff] [blame] | 116 | |
| 117 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |