system/FileDescriptor: new wrapper class for a file descriptor

This commit is contained in:
Max Kellermann
2015-03-03 17:03:21 +01:00
parent 818d729d8b
commit 40a587bbaf
14 changed files with 443 additions and 124 deletions

View File

@@ -20,46 +20,35 @@
#include "config.h"
#ifdef USE_EVENTFD
#include "EventFD.hxx"
#include "system/fd_util.h"
#include "system/FatalError.hxx"
#include "Compiler.h"
#include <assert.h>
#include <unistd.h>
#include <sys/eventfd.h>
EventFD::EventFD()
:fd(eventfd_cloexec_nonblock(0, 0))
{
if (fd < 0)
if (!fd.CreateEventFD(0))
FatalSystemError("eventfd() failed");
}
EventFD::~EventFD()
{
assert(fd >= 0);
close(fd);
}
bool
EventFD::Read()
{
assert(fd >= 0);
assert(fd.IsDefined());
eventfd_t value;
return read(fd, &value, sizeof(value)) == (ssize_t)sizeof(value);
return fd.Read(&value, sizeof(value)) == (ssize_t)sizeof(value);
}
void
EventFD::Write()
{
assert(fd >= 0);
assert(fd.IsDefined());
static constexpr eventfd_t value = 1;
gcc_unused ssize_t nbytes =
write(fd, &value, sizeof(value));
fd.Write(&value, sizeof(value));
}
#endif /* USE_EVENTFD */