blob: 4bcedf14a6f72760252b7eb80a53ec71e9d350d4 [file] [log] [blame]
Austin Schuh6b6dfa52019-06-12 20:16:20 -07001#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 Perrycb7da4b2019-08-28 19:35:56 -07009#include <functional>
Austin Schuh6b6dfa52019-06-12 20:16:20 -070010#include <vector>
11
Austin Schuh6b6dfa52019-06-12 20:16:20 -070012#include "aos/time/time.h"
13
14namespace aos {
15namespace internal {
16
17// Class wrapping up timerfd.
18class TimerFd {
19 public:
20 TimerFd();
Austin Schuhf257f3c2019-10-27 21:00:43 -070021 ~TimerFd();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070022
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 Schuhde8a8ff2019-11-30 15:25:36 -080039 // Reads the event. Returns the number of elapsed cycles.
40 uint64_t Read();
Austin Schuh6b6dfa52019-06-12 20:16:20 -070041
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.
50class 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 Kuszmaulc8e657e2020-12-14 23:49:45 -080062 // 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 Schuh6b6dfa52019-06-12 20:16:20 -070068 // Quits. Async safe.
69 void Quit();
70
Austin Schuh219b7782020-12-21 12:01:40 -080071 // Called before waiting on the epoll file descriptor.
72 void BeforeWait(std::function<void()> function);
73
Austin Schuh6b6dfa52019-06-12 20:16:20 -070074 // Registers a function to be called if the fd becomes readable.
Brian Silverman441591b2020-01-31 17:44:32 -080075 // Only one function may be registered for readability on each fd.
Austin Schuh6b6dfa52019-06-12 20:16:20 -070076 void OnReadable(int fd, ::std::function<void()> function);
77
Austin Schuh603419f2021-04-24 18:15:20 -070078 // Registers a function to be called if the fd reports an error.
79 // Only one function may be registered for errors on each fd.
80 void OnError(int fd, ::std::function<void()> function);
81
Brian Silverman441591b2020-01-31 17:44:32 -080082 // Registers a function to be called if the fd becomes writeable.
83 // Only one function may be registered for writability on each fd.
84 void OnWriteable(int fd, ::std::function<void()> function);
85
Austin Schuh6b6dfa52019-06-12 20:16:20 -070086 // Removes fd from the event loop.
87 // All Fds must be cleaned up before this class is destroyed.
88 void DeleteFd(int fd);
89
Austin Schuh219b7782020-12-21 12:01:40 -080090 // Removes a closed fd. When fds are closed, they are automatically
91 // unregistered by the kernel. But we need to clean up any state here.
92 // All Fds must be cleaned up before this class is destroyed.
93 void ForgetClosedFd(int fd);
94
Brian Silverman441591b2020-01-31 17:44:32 -080095 // Enables calling the existing function registered for fd when it becomes
96 // writeable.
97 void EnableWriteable(int fd) { EnableEvents(fd, kOutEvents); }
98
99 // Disables calling the existing function registered for fd when it becomes
100 // writeable.
101 void DisableWriteable(int fd) { DisableEvents(fd, kOutEvents); }
102
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700103 private:
Brian Silverman441591b2020-01-31 17:44:32 -0800104 // Structure whose pointer should be returned by epoll. Makes looking up the
105 // function fast and easy.
106 struct EventData {
107 EventData(int fd_in) : fd(fd_in) {}
108 // We use pointers to these objects as persistent identifiers, so they can't
109 // be moved.
110 EventData(const EventData &) = delete;
111 EventData &operator=(const EventData &) = delete;
112
113 const int fd;
114 uint32_t events = 0;
Austin Schuh603419f2021-04-24 18:15:20 -0700115 std::function<void()> in_fn, out_fn, err_fn;
Brian Silverman441591b2020-01-31 17:44:32 -0800116 };
117
118 void EnableEvents(int fd, uint32_t events);
119 void DisableEvents(int fd, uint32_t events);
120
121 EventData *GetEventData(int fd);
122
123 void DoEpollCtl(EventData *event_data, uint32_t new_events);
124
125 // TODO(Brian): Figure out a nicer way to handle EPOLLPRI than lumping it in
126 // with input.
127 static constexpr uint32_t kInEvents = EPOLLIN | EPOLLPRI;
128 static constexpr uint32_t kOutEvents = EPOLLOUT;
Austin Schuh603419f2021-04-24 18:15:20 -0700129 static constexpr uint32_t kErrorEvents = EPOLLERR;
Brian Silverman441591b2020-01-31 17:44:32 -0800130
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700131 ::std::atomic<bool> run_{true};
132
133 // Main epoll fd.
134 int epoll_fd_;
135
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700136 ::std::vector<::std::unique_ptr<EventData>> fns_;
137
138 // Pipe pair for handling quit.
139 int quit_signal_fd_;
140 int quit_epoll_fd_;
Austin Schuh219b7782020-12-21 12:01:40 -0800141
142 std::vector<std::function<void()>> before_epoll_wait_functions_;
Austin Schuh6b6dfa52019-06-12 20:16:20 -0700143};
144
145} // namespace internal
146} // namespace aos
147
148#endif // AOS_EVENTS_EPOLL_H_