system/EpollFD: use class UniqueFileDescriptor

This commit is contained in:
Max Kellermann 2018-08-22 15:38:22 +02:00
parent 5d0a463f09
commit 98eed1f5ab
2 changed files with 9 additions and 14 deletions

View File

@ -35,7 +35,7 @@
EpollFD::EpollFD() EpollFD::EpollFD()
:fd(::epoll_create1(EPOLL_CLOEXEC)) :fd(::epoll_create1(EPOLL_CLOEXEC))
{ {
if (fd < 0) if (!fd.IsDefined())
throw MakeErrno("epoll_create1() failed"); throw MakeErrno("epoll_create1() failed");
} }

View File

@ -30,20 +30,21 @@
#ifndef EPOLL_FD_HXX #ifndef EPOLL_FD_HXX
#define EPOLL_FD_HXX #define EPOLL_FD_HXX
#include "check.h"
#include "UniqueFileDescriptor.hxx"
#include <assert.h> #include <assert.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <unistd.h> #include <unistd.h>
#include <stdint.h> #include <stdint.h>
#include "check.h"
struct epoll_event; struct epoll_event;
/** /**
* A class that wraps Linux epoll. * A class that wraps Linux epoll.
*/ */
class EpollFD { class EpollFD {
const int fd; UniqueFileDescriptor fd;
public: public:
/** /**
@ -51,21 +52,15 @@ public:
*/ */
EpollFD(); EpollFD();
~EpollFD() noexcept { EpollFD(EpollFD &&) = default;
assert(fd >= 0); EpollFD &operator=(EpollFD &&) = default;
::close(fd);
}
EpollFD(const EpollFD &other) = delete;
EpollFD &operator=(const EpollFD &other) = delete;
int Wait(epoll_event *events, int maxevents, int timeout) noexcept { int Wait(epoll_event *events, int maxevents, int timeout) noexcept {
return ::epoll_wait(fd, events, maxevents, timeout); return ::epoll_wait(fd.Get(), events, maxevents, timeout);
} }
bool Control(int op, int _fd, epoll_event *event) noexcept { bool Control(int op, int _fd, epoll_event *event) noexcept {
return ::epoll_ctl(fd, op, _fd, event) >= 0; return ::epoll_ctl(fd.Get(), op, _fd, event) >= 0;
} }
bool Add(int _fd, uint32_t events, void *ptr) noexcept { bool Add(int _fd, uint32_t events, void *ptr) noexcept {