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