blob: 8df741c9e184968970325d8f196d2c5d4da5cea1 [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#ifndef LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_EPOLL_FILE_DESCRIPTOR_H_
2#define LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_EPOLL_FILE_DESCRIPTOR_H_
3
4#include <android-base/unique_fd.h>
5#include <base/logging.h>
6#include <sys/epoll.h>
7
8namespace android {
9namespace dvr {
10
11class EpollFileDescriptor {
12 public:
13 static const int CTL_ADD = EPOLL_CTL_ADD;
14 static const int CTL_MOD = EPOLL_CTL_MOD;
15 static const int CTL_DEL = EPOLL_CTL_DEL;
16
17 EpollFileDescriptor() : fd_(-1) {}
18
19 // Constructs an EpollFileDescriptor from an integer file descriptor and
20 // takes ownership.
21 explicit EpollFileDescriptor(int fd) : fd_(fd) {}
22
23 bool IsValid() const { return fd_.get() >= 0; }
24
25 int Create() {
26 if (IsValid()) {
27 LOG(WARNING) << "epoll fd has already been created.";
28 return -EALREADY;
29 }
30
31 fd_.reset(epoll_create(64));
32
33 if (fd_.get() < 0)
34 return -errno;
35 else
36 return 0;
37 }
38
39 int Control(int op, int target_fd, epoll_event* ev) {
40 if (epoll_ctl(fd_.get(), op, target_fd, ev) < 0)
41 return -errno;
42 else
43 return 0;
44 }
45
46 int Wait(epoll_event* events, int maxevents, int timeout) {
47 int ret = epoll_wait(fd_.get(), events, maxevents, timeout);
48
49 if (ret < 0)
50 return -errno;
51 else
52 return ret;
53 }
54
55 private:
56 base::unique_fd fd_;
57};
58
59} // namespace dvr
60} // namespace android
61
62#endif // LIBS_VR_LIBDVRCOMMON_INCLUDE_PRIVATE_DVR_EPOLL_FILE_DESCRIPTOR_H_