Commit db447440 authored by Max Kellermann's avatar Max Kellermann

event/Event{Pipe,FD}: auto-create in constructor

Errors are fatal now. This makes the class a lot easier to use.
parent b70d38dc
...@@ -21,33 +21,26 @@ ...@@ -21,33 +21,26 @@
#ifdef USE_EVENTFD #ifdef USE_EVENTFD
#include "EventFD.hxx" #include "EventFD.hxx"
#include "system/fd_util.h" #include "system/fd_util.h"
#include "system/FatalError.hxx"
#include "gcc.h" #include "gcc.h"
#include <assert.h>
#include <unistd.h> #include <unistd.h>
#include <sys/eventfd.h> #include <sys/eventfd.h>
#ifdef WIN32 EventFD::EventFD()
static bool PoorSocketPair(int fd[2]); :fd(eventfd_cloexec_nonblock(0, 0))
#endif
bool
EventFD::Create()
{ {
assert(fd == -1); if (fd < 0)
FatalSystemError("eventfd() failed");
fd = eventfd_cloexec_nonblock(0, 0);
return fd >= 0;
} }
void EventFD::~EventFD()
EventFD::Destroy()
{ {
close(fd); assert(fd >= 0);
#ifndef NDEBUG close(fd);
fd = -1;
#endif
} }
bool bool
......
...@@ -22,33 +22,22 @@ ...@@ -22,33 +22,22 @@
#include "check.h" #include "check.h"
#include <assert.h>
/** /**
* A class that wraps eventfd(). * A class that wraps eventfd().
* *
* For optimization purposes, this class does not have a constructor * Errors in the constructor are fatal.
* or a destructor.
*/ */
class EventFD { class EventFD {
int fd; int fd;
public: public:
#ifdef NDEBUG EventFD();
EventFD() = default; ~EventFD();
#else
EventFD():fd(-1) {}
#endif
EventFD(const EventFD &other) = delete; EventFD(const EventFD &other) = delete;
EventFD &operator=(const EventFD &other) = delete; EventFD &operator=(const EventFD &other) = delete;
bool Create();
void Destroy();
int Get() const { int Get() const {
assert(fd >= 0);
return fd; return fd;
} }
......
...@@ -20,8 +20,10 @@ ...@@ -20,8 +20,10 @@
#include "config.h" #include "config.h"
#include "EventPipe.hxx" #include "EventPipe.hxx"
#include "system/fd_util.h" #include "system/fd_util.h"
#include "system/FatalError.hxx"
#include "gcc.h" #include "gcc.h"
#include <assert.h>
#include <unistd.h> #include <unistd.h>
#ifdef WIN32 #ifdef WIN32
...@@ -34,21 +36,18 @@ ...@@ -34,21 +36,18 @@
static bool PoorSocketPair(int fd[2]); static bool PoorSocketPair(int fd[2]);
#endif #endif
bool EventPipe::EventPipe()
EventPipe::Create()
{ {
assert(fds[0] == -1);
assert(fds[1] == -1);
#ifdef WIN32 #ifdef WIN32
return PoorSocketPair(fds); bool success = PoorSocketPair(fds);
#else #else
return pipe_cloexec_nonblock(fds) >= 0; bool success = pipe_cloexec_nonblock(fds) >= 0;
#endif #endif
if (!success)
FatalSystemError("pipe() has failed");
} }
void EventPipe::~EventPipe()
EventPipe::Destroy()
{ {
#ifdef WIN32 #ifdef WIN32
closesocket(fds[0]); closesocket(fds[0]);
...@@ -56,11 +55,6 @@ EventPipe::Destroy() ...@@ -56,11 +55,6 @@ EventPipe::Destroy()
#else #else
close(fds[0]); close(fds[0]);
close(fds[1]); close(fds[1]);
#ifndef NDEBUG
fds[0] = -1;
fds[1] = -1;
#endif
#endif #endif
} }
......
...@@ -22,34 +22,22 @@ ...@@ -22,34 +22,22 @@
#include "check.h" #include "check.h"
#include <assert.h>
/** /**
* A pipe that can be used to trigger an event to the read side. * A pipe that can be used to trigger an event to the read side.
* *
* For optimization purposes, this class does not have a constructor * Errors in the constructor are fatal.
* or a destructor.
*/ */
class EventPipe { class EventPipe {
int fds[2]; int fds[2];
public: public:
#ifdef NDEBUG EventPipe();
EventPipe() = default; ~EventPipe();
#else
EventPipe():fds{-1, -1} {};
#endif
EventPipe(const EventPipe &other) = delete; EventPipe(const EventPipe &other) = delete;
EventPipe &operator=(const EventPipe &other) = delete; EventPipe &operator=(const EventPipe &other) = delete;
bool Create();
void Destroy();
int Get() const { int Get() const {
assert(fds[0] >= 0);
assert(fds[1] >= 0);
return fds[0]; return fds[0];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment