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). |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 20 | // TODO(parker): This is mostly broken. |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 21 | class EpollWait { |
| 22 | public: |
| 23 | virtual ~EpollWait() {} |
| 24 | |
| 25 | // Called when the currently set time is reached. |
| 26 | virtual void Done() = 0; |
| 27 | |
| 28 | // Sets this wait to end at new_time. |
| 29 | // A negative new_time disables this wait. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 30 | void SetTime(const monotonic_clock::time_point new_time) { time_ = new_time; } |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 31 | |
| 32 | private: |
| 33 | // Calculates how long to wait starting at now and calls Done() if |
| 34 | // appropriate. |
| 35 | // Returns the number of milliseconds from now that this event will expire in. |
| 36 | // Returns -1 if this wait is never going to expire. |
| 37 | // Returns INT_MAX if this wait expires in longer than that. |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 38 | int Recalculate(const monotonic_clock::time_point now) { |
| 39 | if (time_ < monotonic_clock::epoch()) return -1; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 40 | if (time_ <= now) { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 41 | time_ = monotonic_clock::time_point(::std::chrono::seconds(-1)); |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 42 | Done(); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 43 | } |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 44 | // Duplicate above to allow Done to change itself. |
| 45 | if (time_ < monotonic_clock::epoch()) return -1; |
| 46 | if (time_ <= now) { |
Parker Schuh | 309dd72 | 2017-02-25 11:31:18 -0800 | [diff] [blame^] | 47 | return -1; |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 50 | if (time_ - now > ::std::chrono::milliseconds(INT_MAX)) { |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 51 | return INT_MAX; |
| 52 | } else { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 53 | return ::std::chrono::duration_cast<::std::chrono::milliseconds>(time_ - |
| 54 | now) |
| 55 | .count(); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 59 | ::aos::monotonic_clock::time_point time_ = ::aos::monotonic_clock::epoch(); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 60 | |
| 61 | friend class EpollLoop; |
| 62 | }; |
| 63 | |
| 64 | // Represents a file descriptor which signals events from an EpollLoop. |
| 65 | class EpollEvent { |
| 66 | public: |
| 67 | EpollEvent(int fd) : fd_(fd) {} |
| 68 | virtual ~EpollEvent() {} |
| 69 | |
| 70 | int fd() { return fd_; } |
| 71 | |
| 72 | // Called when fd() is readable. This must make fd() non-readable or the event |
| 73 | // loop degrades into a busy loop. |
| 74 | virtual void ReadEvent() = 0; |
| 75 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame] | 76 | EpollLoop *loop() { return loop_; } |
| 77 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 78 | private: |
| 79 | const int fd_; |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame] | 80 | friend class EpollLoop; |
| 81 | EpollLoop *loop_ = nullptr; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 84 | // A file descriptor based event loop implemented with epoll. |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 85 | class EpollLoop { |
| 86 | public: |
| 87 | EpollLoop(); |
| 88 | |
| 89 | // Ways to add various objects which interact with this event loop. |
| 90 | // None of these take ownership of the passed-in objects. |
| 91 | void AddWait(EpollWait *wait); |
| 92 | void Add(EpollEvent *event); |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 93 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame] | 94 | // Delete event. Note that there are caveats here as this is |
| 95 | // not idiot proof. |
| 96 | // ie: |
| 97 | // - Do not call from other threads. |
| 98 | // - Do not free while the object could still receive events. |
| 99 | // - This is safe only because the events are set as edge. |
| 100 | // TODO(parker): The thread-safety of this should be investigated and |
| 101 | // improved as well as adding support for non-edge events if this is to |
| 102 | // be used more generally. |
| 103 | void Delete(EpollEvent *event); |
| 104 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 105 | // Loops forever, handling events. |
| 106 | void Run(); |
| 107 | |
Parker Schuh | d7db83d | 2017-02-08 20:49:15 -0800 | [diff] [blame] | 108 | // Fuses with gtk_main(). |
| 109 | // Note that the dep for this is separate: //aos/vision/events:gtk_event |
| 110 | void RunWithGtkMain(); |
| 111 | |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 112 | private: |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame] | 113 | int epoll_fd() { return epoll_fd_.get(); } |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 114 | |
Parker Schuh | b59bf5e | 2016-12-28 21:09:36 -0800 | [diff] [blame] | 115 | int CalculateTimeout(); |
| 116 | |
| 117 | ::aos::ScopedFD epoll_fd_; |
| 118 | ::std::vector<EpollWait *> waits_; |
Brian Silverman | 801d49c | 2016-03-20 15:50:22 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // namespace events |
| 122 | } // namespace aos |
| 123 | |
| 124 | #endif // AOS_VISION_EVENTS_EPOLL_EVENTS_H_ |