Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 1 | #ifndef AOS_VISION_EVENTS_EPOLL_EVENTS_H_ |
| 2 | #define AOS_VISION_EVENTS_EPOLL_EVENTS_H_ |
| 3 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 4 | #include <limits.h> |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 5 | #include <stdint.h> |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 6 | #include <memory> |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 7 | #include <vector> |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 8 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 9 | #include "aos/common/scoped_fd.h" |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 10 | #include "aos/common/time.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace events { |
| 14 | |
| 15 | class EpollLoop; |
| 16 | |
| 17 | // Performs an asychronous wait using an EpollLoop. |
| 18 | // |
| 19 | // Note: this does not have very high resolution (sub-millisecond). |
| 20 | class EpollWait { |
| 21 | public: |
| 22 | virtual ~EpollWait() {} |
| 23 | |
| 24 | // Called when the currently set time is reached. |
| 25 | virtual void Done() = 0; |
| 26 | |
| 27 | // Sets this wait to end at new_time. |
| 28 | // A negative new_time disables this wait. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 29 | void SetTime(const monotonic_clock::time_point new_time) { time_ = new_time; } |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 30 | |
| 31 | private: |
| 32 | // Calculates how long to wait starting at now and calls Done() if |
| 33 | // appropriate. |
| 34 | // Returns the number of milliseconds from now that this event will expire in. |
| 35 | // Returns -1 if this wait is never going to expire. |
| 36 | // Returns INT_MAX if this wait expires in longer than that. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 37 | int Recalculate(const monotonic_clock::time_point now) { |
| 38 | if (time_ < monotonic_clock::epoch()) return -1; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 39 | if (time_ <= now) { |
| 40 | Done(); |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 41 | time_ = monotonic_clock::time_point(::std::chrono::seconds(-1)); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 42 | return -1; |
| 43 | } |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 44 | if (time_ - now > ::std::chrono::milliseconds(INT_MAX)) { |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 45 | return INT_MAX; |
| 46 | } else { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 47 | return ::std::chrono::duration_cast<::std::chrono::milliseconds>(time_ - |
| 48 | now) |
| 49 | .count(); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 53 | ::aos::monotonic_clock::time_point time_ = ::aos::monotonic_clock::epoch(); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 54 | |
| 55 | friend class EpollLoop; |
| 56 | }; |
| 57 | |
| 58 | // Represents a file descriptor which signals events from an EpollLoop. |
| 59 | class EpollEvent { |
| 60 | public: |
| 61 | EpollEvent(int fd) : fd_(fd) {} |
| 62 | virtual ~EpollEvent() {} |
| 63 | |
| 64 | int fd() { return fd_; } |
| 65 | |
| 66 | // Called when fd() is readable. This must make fd() non-readable or the event |
| 67 | // loop degrades into a busy loop. |
| 68 | virtual void ReadEvent() = 0; |
| 69 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 70 | EpollLoop *loop() { return loop_; } |
| 71 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 72 | private: |
| 73 | const int fd_; |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 74 | friend class EpollLoop; |
| 75 | EpollLoop *loop_ = nullptr; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // Provides a way for code to be notified every time after events are handled by |
| 79 | // an EpollLoop. This is mainly a hack for the GTK integration and testing; |
| 80 | // think very carefully before using it anywhere else. |
| 81 | class EpollWatcher { |
| 82 | public: |
| 83 | virtual ~EpollWatcher() {} |
| 84 | |
| 85 | // Called after events have been processed each time the event loop wakes up. |
| 86 | virtual void Wake() = 0; |
| 87 | }; |
| 88 | |
| 89 | // A file descriptor based event loop implemented with epoll. |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 90 | class EpollLoop { |
| 91 | public: |
| 92 | EpollLoop(); |
| 93 | |
| 94 | // Ways to add various objects which interact with this event loop. |
| 95 | // None of these take ownership of the passed-in objects. |
| 96 | void AddWait(EpollWait *wait); |
| 97 | void Add(EpollEvent *event); |
| 98 | void AddWatcher(EpollWatcher *watcher); |
| 99 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 100 | // Delete event. Note that there are caveats here as this is |
| 101 | // not idiot proof. |
| 102 | // ie: |
| 103 | // - Do not call from other threads. |
| 104 | // - Do not free while the object could still receive events. |
| 105 | // - This is safe only because the events are set as edge. |
| 106 | // TODO(parker): The thread-safety of this should be investigated and |
| 107 | // improved as well as adding support for non-edge events if this is to |
| 108 | // be used more generally. |
| 109 | void Delete(EpollEvent *event); |
| 110 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 111 | // Loops forever, handling events. |
| 112 | void Run(); |
| 113 | |
| 114 | private: |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 115 | int epoll_fd() { return epoll_fd_.get(); } |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 116 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame^] | 117 | int CalculateTimeout(); |
| 118 | |
| 119 | ::aos::ScopedFD epoll_fd_; |
| 120 | ::std::vector<EpollWait *> waits_; |
| 121 | ::std::vector<EpollWatcher *> watchers_; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace events |
| 125 | } // namespace aos |
| 126 | |
| 127 | #endif // AOS_VISION_EVENTS_EPOLL_EVENTS_H_ |