event/Event{Pipe,FD}: auto-create in constructor
Errors are fatal now. This makes the class a lot easier to use.
This commit is contained in:
parent
b70d38dc10
commit
db447440ff
@ -21,33 +21,26 @@
|
||||
#ifdef USE_EVENTFD
|
||||
#include "EventFD.hxx"
|
||||
#include "system/fd_util.h"
|
||||
#include "system/FatalError.hxx"
|
||||
#include "gcc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/eventfd.h>
|
||||
|
||||
#ifdef WIN32
|
||||
static bool PoorSocketPair(int fd[2]);
|
||||
#endif
|
||||
|
||||
bool
|
||||
EventFD::Create()
|
||||
EventFD::EventFD()
|
||||
:fd(eventfd_cloexec_nonblock(0, 0))
|
||||
{
|
||||
assert(fd == -1);
|
||||
|
||||
fd = eventfd_cloexec_nonblock(0, 0);
|
||||
return fd >= 0;
|
||||
if (fd < 0)
|
||||
FatalSystemError("eventfd() failed");
|
||||
}
|
||||
|
||||
void
|
||||
EventFD::Destroy()
|
||||
EventFD::~EventFD()
|
||||
{
|
||||
close(fd);
|
||||
assert(fd >= 0);
|
||||
|
||||
#ifndef NDEBUG
|
||||
fd = -1;
|
||||
#endif
|
||||
close(fd);
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -22,33 +22,22 @@
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* A class that wraps eventfd().
|
||||
*
|
||||
* For optimization purposes, this class does not have a constructor
|
||||
* or a destructor.
|
||||
* Errors in the constructor are fatal.
|
||||
*/
|
||||
class EventFD {
|
||||
int fd;
|
||||
|
||||
public:
|
||||
#ifdef NDEBUG
|
||||
EventFD() = default;
|
||||
#else
|
||||
EventFD():fd(-1) {}
|
||||
#endif
|
||||
EventFD();
|
||||
~EventFD();
|
||||
|
||||
EventFD(const EventFD &other) = delete;
|
||||
EventFD &operator=(const EventFD &other) = delete;
|
||||
|
||||
bool Create();
|
||||
void Destroy();
|
||||
|
||||
int Get() const {
|
||||
assert(fd >= 0);
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
@ -20,8 +20,10 @@
|
||||
#include "config.h"
|
||||
#include "EventPipe.hxx"
|
||||
#include "system/fd_util.h"
|
||||
#include "system/FatalError.hxx"
|
||||
#include "gcc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#ifdef WIN32
|
||||
@ -34,21 +36,18 @@
|
||||
static bool PoorSocketPair(int fd[2]);
|
||||
#endif
|
||||
|
||||
bool
|
||||
EventPipe::Create()
|
||||
EventPipe::EventPipe()
|
||||
{
|
||||
assert(fds[0] == -1);
|
||||
assert(fds[1] == -1);
|
||||
|
||||
#ifdef WIN32
|
||||
return PoorSocketPair(fds);
|
||||
bool success = PoorSocketPair(fds);
|
||||
#else
|
||||
return pipe_cloexec_nonblock(fds) >= 0;
|
||||
bool success = pipe_cloexec_nonblock(fds) >= 0;
|
||||
#endif
|
||||
if (!success)
|
||||
FatalSystemError("pipe() has failed");
|
||||
}
|
||||
|
||||
void
|
||||
EventPipe::Destroy()
|
||||
EventPipe::~EventPipe()
|
||||
{
|
||||
#ifdef WIN32
|
||||
closesocket(fds[0]);
|
||||
@ -56,11 +55,6 @@ EventPipe::Destroy()
|
||||
#else
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
|
||||
#ifndef NDEBUG
|
||||
fds[0] = -1;
|
||||
fds[1] = -1;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -22,34 +22,22 @@
|
||||
|
||||
#include "check.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* A pipe that can be used to trigger an event to the read side.
|
||||
*
|
||||
* For optimization purposes, this class does not have a constructor
|
||||
* or a destructor.
|
||||
* Errors in the constructor are fatal.
|
||||
*/
|
||||
class EventPipe {
|
||||
int fds[2];
|
||||
|
||||
public:
|
||||
#ifdef NDEBUG
|
||||
EventPipe() = default;
|
||||
#else
|
||||
EventPipe():fds{-1, -1} {};
|
||||
#endif
|
||||
EventPipe();
|
||||
~EventPipe();
|
||||
|
||||
EventPipe(const EventPipe &other) = delete;
|
||||
EventPipe &operator=(const EventPipe &other) = delete;
|
||||
|
||||
bool Create();
|
||||
void Destroy();
|
||||
|
||||
int Get() const {
|
||||
assert(fds[0] >= 0);
|
||||
assert(fds[1] >= 0);
|
||||
|
||||
return fds[0];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user