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 |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame^] | 20 | // |
| 21 | // This object must be interacted with from one thread, but the Senders and |
| 22 | // Fetchers may be used from multiple threads afterwords (as long as their |
| 23 | // destructors are called back in one thread again) |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 24 | class ShmEventLoop : public EventLoop { |
| 25 | public: |
| 26 | ShmEventLoop(); |
| 27 | ~ShmEventLoop() override; |
| 28 | |
| 29 | ::aos::monotonic_clock::time_point monotonic_now() override { |
| 30 | return ::aos::monotonic_clock::now(); |
| 31 | } |
| 32 | |
| 33 | std::unique_ptr<RawSender> MakeRawSender(const std::string &path, |
| 34 | const QueueTypeInfo &type) override; |
| 35 | std::unique_ptr<RawFetcher> MakeRawFetcher( |
| 36 | const std::string &path, const QueueTypeInfo &type) override; |
| 37 | |
| 38 | void MakeRawWatcher( |
| 39 | const std::string &path, const QueueTypeInfo &type, |
| 40 | std::function<void(const aos::Message *message)> watcher) override; |
| 41 | |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 42 | TimerHandler *AddTimer(::std::function<void()> callback) override; |
| 43 | |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 44 | void OnRun(std::function<void()> on_run) override; |
| 45 | void Run() override; |
| 46 | void Exit() override; |
| 47 | |
| 48 | private: |
| 49 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 50 | friend class internal::TimerHandlerState; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 51 | // This ThreadState ensures that two watchers in the same loop cannot be |
| 52 | // triggered concurrently. Because watchers block threads indefinitely, this |
| 53 | // has to be shared_ptr in case the EventLoop is destroyed before the thread |
| 54 | // receives any new events. |
| 55 | class ThreadState { |
| 56 | public: |
| 57 | void WaitForStart(); |
| 58 | |
| 59 | bool is_running() { return loop_running_; } |
| 60 | |
| 61 | void Run(); |
| 62 | |
| 63 | void Exit(); |
| 64 | |
| 65 | private: |
| 66 | friend class internal::WatcherThreadState; |
Neil Balch | 229001a | 2018-01-07 18:22:52 -0800 | [diff] [blame] | 67 | friend class internal::TimerHandlerState; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 68 | friend class ShmEventLoop; |
| 69 | |
| 70 | // This mutex ensures that only one watch event happens at a time. |
| 71 | aos::Mutex mutex_; |
| 72 | // Block on this until the loop starts. |
| 73 | aos::Condition loop_running_cond_{&mutex_}; |
| 74 | // Used to notify watchers that the loop is done. |
| 75 | std::atomic<bool> loop_running_{false}; |
| 76 | bool loop_finished_ = false; |
| 77 | }; |
| 78 | |
| 79 | // Exclude multiple of the same type for path. |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 80 | |
| 81 | std::vector<std::function<void()>> on_run_; |
| 82 | std::shared_ptr<ThreadState> thread_state_; |
| 83 | |
Austin Schuh | 81fc9cc | 2019-02-02 23:25:47 -0800 | [diff] [blame^] | 84 | void Take(const std::string &path); |
| 85 | |
| 86 | std::vector<::std::string> taken_; |
Parker Schuh | e4a70d6 | 2017-12-27 20:10:20 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace aos |
Austin Schuh | 520f33d | 2019-01-27 22:38:01 -0800 | [diff] [blame] | 90 | |
| 91 | #endif // AOS_EVENTS_SHM_EVENT_LOOP_H_ |