Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EPOLL_H_ |
| 2 | #define AOS_EVENTS_EPOLL_H_ |
| 3 | |
| 4 | #include <fcntl.h> |
| 5 | #include <sys/epoll.h> |
| 6 | #include <sys/timerfd.h> |
| 7 | #include <unistd.h> |
| 8 | #include <atomic> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 9 | #include <functional> |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 12 | #include "aos/time/time.h" |
| 13 | |
| 14 | namespace aos { |
| 15 | namespace internal { |
| 16 | |
| 17 | // Class wrapping up timerfd. |
| 18 | class TimerFd { |
| 19 | public: |
| 20 | TimerFd(); |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 21 | ~TimerFd(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 22 | |
| 23 | TimerFd(const TimerFd &) = delete; |
| 24 | TimerFd &operator=(const TimerFd &) = delete; |
| 25 | TimerFd(TimerFd &&) = delete; |
| 26 | TimerFd &operator=(TimerFd &&) = delete; |
| 27 | |
| 28 | // Sets the trigger time and repeat for the timerfd. |
| 29 | // An interval of 0 results in a single expiration. |
| 30 | void SetTime(monotonic_clock::time_point start, |
| 31 | monotonic_clock::duration interval); |
| 32 | |
| 33 | // Disarms the timer. |
| 34 | void Disable() { |
| 35 | // Disarm the timer by feeding zero values |
| 36 | SetTime(::aos::monotonic_clock::epoch(), ::aos::monotonic_clock::zero()); |
| 37 | } |
| 38 | |
Austin Schuh | de8a8ff | 2019-11-30 15:25:36 -0800 | [diff] [blame] | 39 | // Reads the event. Returns the number of elapsed cycles. |
| 40 | uint64_t Read(); |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 41 | |
| 42 | // Returns the file descriptor associated with the timerfd. |
| 43 | int fd() { return fd_; } |
| 44 | |
| 45 | private: |
| 46 | int fd_ = -1; |
| 47 | }; |
| 48 | |
| 49 | // Class to wrap epoll and call a callback when an event happens. |
| 50 | class EPoll { |
| 51 | public: |
| 52 | EPoll(); |
| 53 | ~EPoll(); |
| 54 | EPoll(const EPoll &) = delete; |
| 55 | EPoll &operator=(const EPoll &) = delete; |
| 56 | EPoll(EPoll &&) = delete; |
| 57 | EPoll &operator=(EPoll &&) = delete; |
| 58 | |
| 59 | // Runs until Quit() is called. |
| 60 | void Run(); |
| 61 | |
James Kuszmaul | c8e657e | 2020-12-14 23:49:45 -0800 | [diff] [blame] | 62 | // Consumes a single epoll event. Blocks indefinitely if block is true, or |
| 63 | // does not block at all. Returns true if an event was consumed, and false on |
| 64 | // any retryable error or if no events are available. Dies fatally on |
| 65 | // non-retryable errors. |
| 66 | bool Poll(bool block); |
| 67 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 68 | // Quits. Async safe. |
| 69 | void Quit(); |
| 70 | |
| 71 | // Registers a function to be called if the fd becomes readable. |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 72 | // Only one function may be registered for readability on each fd. |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 73 | void OnReadable(int fd, ::std::function<void()> function); |
| 74 | |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 75 | // Registers a function to be called if the fd becomes writeable. |
| 76 | // Only one function may be registered for writability on each fd. |
| 77 | void OnWriteable(int fd, ::std::function<void()> function); |
| 78 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 79 | // Removes fd from the event loop. |
| 80 | // All Fds must be cleaned up before this class is destroyed. |
| 81 | void DeleteFd(int fd); |
| 82 | |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 83 | // Enables calling the existing function registered for fd when it becomes |
| 84 | // writeable. |
| 85 | void EnableWriteable(int fd) { EnableEvents(fd, kOutEvents); } |
| 86 | |
| 87 | // Disables calling the existing function registered for fd when it becomes |
| 88 | // writeable. |
| 89 | void DisableWriteable(int fd) { DisableEvents(fd, kOutEvents); } |
| 90 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 91 | private: |
Brian Silverman | 441591b | 2020-01-31 17:44:32 -0800 | [diff] [blame] | 92 | // Structure whose pointer should be returned by epoll. Makes looking up the |
| 93 | // function fast and easy. |
| 94 | struct EventData { |
| 95 | EventData(int fd_in) : fd(fd_in) {} |
| 96 | // We use pointers to these objects as persistent identifiers, so they can't |
| 97 | // be moved. |
| 98 | EventData(const EventData &) = delete; |
| 99 | EventData &operator=(const EventData &) = delete; |
| 100 | |
| 101 | const int fd; |
| 102 | uint32_t events = 0; |
| 103 | ::std::function<void()> in_fn, out_fn; |
| 104 | }; |
| 105 | |
| 106 | void EnableEvents(int fd, uint32_t events); |
| 107 | void DisableEvents(int fd, uint32_t events); |
| 108 | |
| 109 | EventData *GetEventData(int fd); |
| 110 | |
| 111 | void DoEpollCtl(EventData *event_data, uint32_t new_events); |
| 112 | |
| 113 | // TODO(Brian): Figure out a nicer way to handle EPOLLPRI than lumping it in |
| 114 | // with input. |
| 115 | static constexpr uint32_t kInEvents = EPOLLIN | EPOLLPRI; |
| 116 | static constexpr uint32_t kOutEvents = EPOLLOUT; |
| 117 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 118 | ::std::atomic<bool> run_{true}; |
| 119 | |
| 120 | // Main epoll fd. |
| 121 | int epoll_fd_; |
| 122 | |
Austin Schuh | 6b6dfa5 | 2019-06-12 20:16:20 -0700 | [diff] [blame] | 123 | ::std::vector<::std::unique_ptr<EventData>> fns_; |
| 124 | |
| 125 | // Pipe pair for handling quit. |
| 126 | int quit_signal_fd_; |
| 127 | int quit_epoll_fd_; |
| 128 | }; |
| 129 | |
| 130 | } // namespace internal |
| 131 | } // namespace aos |
| 132 | |
| 133 | #endif // AOS_EVENTS_EPOLL_H_ |