From db447440ff8973961e1936a6a64ca4971eeeea57 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 7 Aug 2013 10:53:22 +0200 Subject: event/Event{Pipe,FD}: auto-create in constructor Errors are fatal now. This makes the class a lot easier to use. --- src/event/EventFD.cxx | 25 +++++++++---------------- src/event/EventFD.hxx | 17 +++-------------- src/event/EventPipe.cxx | 22 ++++++++-------------- src/event/EventPipe.hxx | 18 +++--------------- 4 files changed, 23 insertions(+), 59 deletions(-) diff --git a/src/event/EventFD.cxx b/src/event/EventFD.cxx index dba80d9be..716ff80ae 100644 --- a/src/event/EventFD.cxx +++ b/src/event/EventFD.cxx @@ -21,33 +21,26 @@ #ifdef USE_EVENTFD #include "EventFD.hxx" #include "system/fd_util.h" +#include "system/FatalError.hxx" #include "gcc.h" +#include #include #include -#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 diff --git a/src/event/EventFD.hxx b/src/event/EventFD.hxx index 803957354..67a0258ab 100644 --- a/src/event/EventFD.hxx +++ b/src/event/EventFD.hxx @@ -22,33 +22,22 @@ #include "check.h" -#include - /** * 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; } diff --git a/src/event/EventPipe.cxx b/src/event/EventPipe.cxx index 3b52118e4..e1a38c503 100644 --- a/src/event/EventPipe.cxx +++ b/src/event/EventPipe.cxx @@ -20,8 +20,10 @@ #include "config.h" #include "EventPipe.hxx" #include "system/fd_util.h" +#include "system/FatalError.hxx" #include "gcc.h" +#include #include #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 } diff --git a/src/event/EventPipe.hxx b/src/event/EventPipe.hxx index 9c9446d8e..86a10b0bb 100644 --- a/src/event/EventPipe.hxx +++ b/src/event/EventPipe.hxx @@ -22,34 +22,22 @@ #include "check.h" -#include - /** * 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]; } -- cgit v1.2.3