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> |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/condition.h" |
| 7 | #include "aos/mutex/mutex.h" |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 8 | #include "aos/events/event-loop.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | namespace internal { |
| 12 | |
| 13 | class WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 14 | class TimerHandlerState; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 15 | |
| 16 | } // namespace internal |
| 17 | |
Neil Balch | c8f41ed | 2018-01-20 22:06:53 -0800 | [diff] [blame] | 18 | // Specialization of EventLoop that is built from queues running out of shared |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 19 | // memory. See more details at aos/queue.h |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 20 | class ShmEventLoop : public EventLoop { |
| 21 | public: |
| 22 | ShmEventLoop(); |
| 23 | ~ShmEventLoop() override; |
| 24 | |
| 25 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 26 | return ::aos::monotonic_clock::now(); |
| 27 | } |
| 28 | |
| 29 | std::unique_ptr<RawSender> MakeRawSender(const std::string &path, |
| 30 | const QueueTypeInfo &type) override; |
| 31 | std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 32 | const std::string &path, const QueueTypeInfo &type) override; |
| 33 | |
| 34 | void MakeRawWatcher( |
| 35 | const std::string &path, const QueueTypeInfo &type, |
| 36 | std::function<void(const aos::Message *message)> watcher) override; |
| 37 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 38 | TimerHandler *AddTimer(::std::function<void()> callback) override; |
| 39 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 40 | void OnRun(std::function<void()> on_run) override; |
| 41 | void Run() override; |
| 42 | void Exit() override; |
| 43 | |
| 44 | private: |
| 45 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 46 | friend class internal::TimerHandlerState; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 47 | // This ThreadState ensures that two watchers in the same loop cannot be |
| 48 | // triggered concurrently. Because watchers block threads indefinitely, this |
| 49 | // has to be shared_ptr in case the EventLoop is destroyed before the thread |
| 50 | // receives any new events. |
| 51 | class ThreadState { |
| 52 | public: |
| 53 | void WaitForStart(); |
| 54 | |
| 55 | bool is_running() { return loop_running_; } |
| 56 | |
| 57 | void Run(); |
| 58 | |
| 59 | void Exit(); |
| 60 | |
| 61 | private: |
| 62 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 63 | friend class internal::TimerHandlerState; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 64 | friend class ShmEventLoop; |
| 65 | |
| 66 | // This mutex ensures that only one watch event happens at a time. |
| 67 | aos::Mutex mutex_; |
| 68 | // Block on this until the loop starts. |
| 69 | aos::Condition loop_running_cond_{&mutex_}; |
| 70 | // Used to notify watchers that the loop is done. |
| 71 | std::atomic<bool> loop_running_{false}; |
| 72 | bool loop_finished_ = false; |
| 73 | }; |
| 74 | |
| 75 | // Exclude multiple of the same type for path. |
| 76 | void Take(const std::string &path); |
| 77 | |
| 78 | std::vector<std::function<void()>> on_run_; |
| 79 | std::shared_ptr<ThreadState> thread_state_; |
| 80 | |
| 81 | std::unordered_set<std::string> taken_; |
| 82 | }; |
| 83 | |
| 84 | } // namespace aos |
Austin Schuh | 520f33d | 2019-01-27 22:38:01 -0800 | [diff] [blame^] | 85 | |
| 86 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |