diff options
author | Max Kellermann <max@duempel.org> | 2013-08-06 23:25:57 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-08-06 23:32:21 +0200 |
commit | 47e16dbee3d43a8fac8e2337df442814ec6d036b (patch) | |
tree | 011c45b1d151425adaef1168fcf6ba7238d6e82a /src/event | |
parent | 66f678023f6e18dde9679d0a69565d6a50d09ed4 (diff) | |
download | mpd-47e16dbee3d43a8fac8e2337df442814ec6d036b.tar.gz mpd-47e16dbee3d43a8fac8e2337df442814ec6d036b.tar.xz mpd-47e16dbee3d43a8fac8e2337df442814ec6d036b.zip |
configure.ac: add option --enable-eventfd
Remove the runtime check for eventfd(), hard-code the feature once
it's been selected at compile time. The class WakeFD is splitted into
EventFD and EventPipe, using WakeFD as a macro diversion.
Diffstat (limited to 'src/event')
-rw-r--r-- | src/event/EventFD.cxx | 72 | ||||
-rw-r--r-- | src/event/EventFD.hxx | 68 | ||||
-rw-r--r-- | src/event/EventPipe.cxx (renamed from src/event/WakeFD.cxx) | 65 | ||||
-rw-r--r-- | src/event/EventPipe.hxx | 69 | ||||
-rw-r--r-- | src/event/WakeFD.hxx | 55 |
5 files changed, 227 insertions, 102 deletions
diff --git a/src/event/EventFD.cxx b/src/event/EventFD.cxx new file mode 100644 index 000000000..f103c44b8 --- /dev/null +++ b/src/event/EventFD.cxx @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" +#ifdef USE_EVENTFD +#include "EventFD.hxx" +#include "fd_util.h" +#include "gcc.h" + +#include <unistd.h> + +#include <sys/eventfd.h> + +#ifdef WIN32 +static bool PoorSocketPair(int fd[2]); +#endif + +bool +EventFD::Create() +{ + assert(fd == -1); + + fd = eventfd_cloexec_nonblock(0, 0); + return fd >= 0; +} + +void +EventFD::Destroy() +{ + close(fd); + +#ifndef NDEBUG + fd = -1; +#endif +} + +bool +EventFD::Read() +{ + assert(fd >= 0); + + eventfd_t value; + return read(fd, &value, sizeof(value)) == (ssize_t)sizeof(value); +} + +void +EventFD::Write() +{ + assert(fd >= 0); + + static constexpr eventfd_t value = 1; + gcc_unused ssize_t nbytes = + write(fd, &value, sizeof(value)); +} + +#endif /* USE_EVENTFD */ diff --git a/src/event/EventFD.hxx b/src/event/EventFD.hxx new file mode 100644 index 000000000..803957354 --- /dev/null +++ b/src/event/EventFD.hxx @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_EVENT_FD_HXX +#define MPD_EVENT_FD_HXX + +#include "check.h" + +#include <assert.h> + +/** + * A class that wraps eventfd(). + * + * For optimization purposes, this class does not have a constructor + * or a destructor. + */ +class EventFD { + int fd; + +public: +#ifdef NDEBUG + EventFD() = default; +#else + EventFD():fd(-1) {} +#endif + + EventFD(const EventFD &other) = delete; + EventFD &operator=(const EventFD &other) = delete; + + bool Create(); + void Destroy(); + + int Get() const { + assert(fd >= 0); + + return fd; + } + + /** + * Checks if Write() was called at least once since the last + * Read() call. + */ + bool Read(); + + /** + * Wakes up the reader. Multiple calls to this function will + * be combined to one wakeup. + */ + void Write(); +}; + +#endif diff --git a/src/event/WakeFD.cxx b/src/event/EventPipe.cxx index 1a84f5645..0a37abe03 100644 --- a/src/event/WakeFD.cxx +++ b/src/event/EventPipe.cxx @@ -18,7 +18,7 @@ */ #include "config.h" -#include "WakeFD.hxx" +#include "EventPipe.hxx" #include "fd_util.h" #include "gcc.h" @@ -30,16 +30,12 @@ #include <cstring> /* for memset() */ #endif -#ifdef HAVE_EVENTFD -#include <sys/eventfd.h> -#endif - #ifdef WIN32 static bool PoorSocketPair(int fd[2]); #endif bool -WakeFD::Create() +EventPipe::Create() { assert(fds[0] == -1); assert(fds[1] == -1); @@ -47,85 +43,50 @@ WakeFD::Create() #ifdef WIN32 return PoorSocketPair(fds); #else -#ifdef HAVE_EVENTFD - fds[0] = eventfd_cloexec_nonblock(0, 0); - if (fds[0] >= 0) { - fds[1] = -2; - return true; - } -#endif return pipe_cloexec_nonblock(fds) >= 0; #endif } void -WakeFD::Destroy() +EventPipe::Destroy() { #ifdef WIN32 closesocket(fds[0]); closesocket(fds[1]); #else close(fds[0]); -#ifdef HAVE_EVENTFD - if (!IsEventFD()) -#endif - close(fds[1]); -#endif + close(fds[1]); #ifndef NDEBUG fds[0] = -1; fds[1] = -1; #endif +#endif } bool -WakeFD::Read() +EventPipe::Read() { assert(fds[0] >= 0); - -#ifdef WIN32 assert(fds[1] >= 0); + char buffer[256]; +#ifdef WIN32 return recv(fds[0], buffer, sizeof(buffer), 0) > 0; #else - -#ifdef HAVE_EVENTFD - if (IsEventFD()) { - eventfd_t value; - return read(fds[0], &value, - sizeof(value)) == (ssize_t)sizeof(value); - } -#endif - - assert(fds[1] >= 0); - - char buffer[256]; return read(fds[0], buffer, sizeof(buffer)) > 0; #endif } void -WakeFD::Write() +EventPipe::Write() { assert(fds[0] >= 0); - -#ifdef WIN32 assert(fds[1] >= 0); +#ifdef WIN32 send(fds[1], "", 1, 0); #else - -#ifdef HAVE_EVENTFD - if (IsEventFD()) { - static constexpr eventfd_t value = 1; - gcc_unused ssize_t nbytes = - write(fds[0], &value, sizeof(value)); - return; - } -#endif - - assert(fds[1] >= 0); - gcc_unused ssize_t nbytes = write(fds[1], "", 1); #endif } @@ -141,7 +102,7 @@ static void SafeCloseSocket(SOCKET s) /* Our poor man's socketpair() implementation * Due to limited protocol/address family support and primitive error handling - * it's better to keep this as a private implementation detail of WakeFD + * it's better to keep this as a private implementation detail of EventPipe * rather than wide-available API. */ static bool PoorSocketPair(int fd[2]) @@ -160,14 +121,14 @@ static bool PoorSocketPair(int fd[2]) int ret = bind(listen_socket, reinterpret_cast<sockaddr*>(&address), sizeof(address)); - + if (ret < 0) { SafeCloseSocket(listen_socket); return false; } ret = listen(listen_socket, 1); - + if (ret < 0) { SafeCloseSocket(listen_socket); return false; diff --git a/src/event/EventPipe.hxx b/src/event/EventPipe.hxx new file mode 100644 index 000000000..9c9446d8e --- /dev/null +++ b/src/event/EventPipe.hxx @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2003-2013 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_EVENT_PIPE_HXX +#define MPD_EVENT_PIPE_HXX + +#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. + */ +class EventPipe { + int fds[2]; + +public: +#ifdef NDEBUG + EventPipe() = default; +#else + EventPipe():fds{-1, -1} {}; +#endif + + 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]; + } + + /** + * Checks if Write() was called at least once since the last + * Read() call. + */ + bool Read(); + + /** + * Wakes up the reader. Multiple calls to this function will + * be combined to one wakeup. + */ + void Write(); +}; + +#endif /* MAIN_NOTIFY_H */ diff --git a/src/event/WakeFD.hxx b/src/event/WakeFD.hxx index 15b66b4cf..e4058287f 100644 --- a/src/event/WakeFD.hxx +++ b/src/event/WakeFD.hxx @@ -24,57 +24,12 @@ #include <assert.h> -/** - * This class can be used to wake up an I/O event loop. - * - * For optimization purposes, this class does not have a constructor - * or a destructor. - */ -class WakeFD { - int fds[2]; - -public: -#ifdef NDEBUG - WakeFD() = default; +#ifdef USE_EVENTFD +#include "EventFD.hxx" +#define WakeFD EventFD #else - WakeFD():fds{-1, -1} {}; -#endif - - WakeFD(const WakeFD &other) = delete; - WakeFD &operator=(const WakeFD &other) = delete; - - bool Create(); - void Destroy(); - - int Get() const { - assert(fds[0] >= 0); -#ifndef HAVE_EVENTFD - assert(fds[1] >= 0); -#endif - - return fds[0]; - } - - /** - * Checks if Write() was called at least once since the last - * Read() call. - */ - bool Read(); - - /** - * Wakes up the reader. Multiple calls to this function will - * be combined to one wakeup. - */ - void Write(); - -private: -#ifdef HAVE_EVENTFD - bool IsEventFD() { - assert(fds[0] >= 0); - - return fds[1] == -2; - } +#include "EventPipe.hxx" +#define WakeFD EventPipe #endif -}; #endif /* MAIN_NOTIFY_H */ |