From 47e16dbee3d43a8fac8e2337df442814ec6d036b Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 6 Aug 2013 23:25:57 +0200 Subject: 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. --- Makefile.am | 4 +- configure.ac | 3 +- m4/mpd_func.m4 | 12 +++ src/event/EventFD.cxx | 72 ++++++++++++++++ src/event/EventFD.hxx | 68 +++++++++++++++ src/event/EventPipe.cxx | 186 +++++++++++++++++++++++++++++++++++++++ src/event/EventPipe.hxx | 69 +++++++++++++++ src/event/WakeFD.cxx | 225 ------------------------------------------------ src/event/WakeFD.hxx | 55 ++---------- src/fd_util.c | 4 +- src/fd_util.h | 2 +- 11 files changed, 420 insertions(+), 280 deletions(-) create mode 100644 m4/mpd_func.m4 create mode 100644 src/event/EventFD.cxx create mode 100644 src/event/EventFD.hxx create mode 100644 src/event/EventPipe.cxx create mode 100644 src/event/EventPipe.hxx delete mode 100644 src/event/WakeFD.cxx diff --git a/Makefile.am b/Makefile.am index 4df940cb6..57d553587 100644 --- a/Makefile.am +++ b/Makefile.am @@ -288,7 +288,9 @@ libutil_a_SOURCES = \ # Event loop library libevent_a_SOURCES = \ - src/event/WakeFD.cxx src/event/WakeFD.hxx \ + src/event/EventPipe.cxx src/event/EventPipe.hxx \ + src/event/EventFD.cxx src/event/EventFD.hxx \ + src/event/WakeFD.hxx \ src/event/TimeoutMonitor.hxx src/event/TimeoutMonitor.cxx \ src/event/SocketMonitor.cxx src/event/SocketMonitor.hxx \ src/event/BufferedSocket.cxx src/event/BufferedSocket.hxx \ diff --git a/configure.ac b/configure.ac index edf29d215..556c50024 100644 --- a/configure.ac +++ b/configure.ac @@ -139,7 +139,8 @@ AC_SEARCH_LIBS([syslog], [bsd socket inet], AC_SEARCH_LIBS([socket], [socket]) AC_SEARCH_LIBS([gethostbyname], [nsl]) -AC_CHECK_FUNCS(pipe2 accept4 eventfd) +AC_CHECK_FUNCS(pipe2 accept4) +MPD_OPTIONAL_FUNC(eventfd, USE_EVENTFD) AC_SEARCH_LIBS([exp], [m],, [AC_MSG_ERROR([exp() not found])]) diff --git a/m4/mpd_func.m4 b/m4/mpd_func.m4 new file mode 100644 index 000000000..c4650336b --- /dev/null +++ b/m4/mpd_func.m4 @@ -0,0 +1,12 @@ +dnl MPD_OPTIONAL_FUNC(func, macro) +dnl +dnl Allow the user to enable or disable the use of a function. If the +dnl option is not specified, the function is auto-detected. +AC_DEFUN([MPD_OPTIONAL_FUNC], [ + AC_ARG_ENABLE([$1], + AS_HELP_STRING([--enable-$1], + [use the function "$1()" (default: auto)]), + [test xenable_$1 = xyes && AC_DEFINE([$2], 1, [Define to use $1()])], + [AC_CHECK_FUNC([$1], + [AC_DEFINE([$2], 1, [Define to use $1()])],)]) +]) 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 + +#include + +#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 + +/** + * 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/EventPipe.cxx b/src/event/EventPipe.cxx new file mode 100644 index 000000000..0a37abe03 --- /dev/null +++ b/src/event/EventPipe.cxx @@ -0,0 +1,186 @@ +/* + * 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" +#include "EventPipe.hxx" +#include "fd_util.h" +#include "gcc.h" + +#include + +#ifdef WIN32 +#include +#include +#include /* for memset() */ +#endif + +#ifdef WIN32 +static bool PoorSocketPair(int fd[2]); +#endif + +bool +EventPipe::Create() +{ + assert(fds[0] == -1); + assert(fds[1] == -1); + +#ifdef WIN32 + return PoorSocketPair(fds); +#else + return pipe_cloexec_nonblock(fds) >= 0; +#endif +} + +void +EventPipe::Destroy() +{ +#ifdef WIN32 + closesocket(fds[0]); + closesocket(fds[1]); +#else + close(fds[0]); + close(fds[1]); + +#ifndef NDEBUG + fds[0] = -1; + fds[1] = -1; +#endif +#endif +} + +bool +EventPipe::Read() +{ + assert(fds[0] >= 0); + assert(fds[1] >= 0); + + char buffer[256]; +#ifdef WIN32 + return recv(fds[0], buffer, sizeof(buffer), 0) > 0; +#else + return read(fds[0], buffer, sizeof(buffer)) > 0; +#endif +} + +void +EventPipe::Write() +{ + assert(fds[0] >= 0); + assert(fds[1] >= 0); + +#ifdef WIN32 + send(fds[1], "", 1, 0); +#else + gcc_unused ssize_t nbytes = write(fds[1], "", 1); +#endif +} + +#ifdef WIN32 + +static void SafeCloseSocket(SOCKET s) +{ + int error = WSAGetLastError(); + closesocket(s); + WSASetLastError(error); +} + +/* 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 EventPipe + * rather than wide-available API. + */ +static bool PoorSocketPair(int fd[2]) +{ + assert (fd != nullptr); + + SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listen_socket == INVALID_SOCKET) + return false; + + sockaddr_in address; + std::memset(&address, 0, sizeof(address)); + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + + int ret = bind(listen_socket, + reinterpret_cast(&address), + sizeof(address)); + + if (ret < 0) { + SafeCloseSocket(listen_socket); + return false; + } + + ret = listen(listen_socket, 1); + + if (ret < 0) { + SafeCloseSocket(listen_socket); + return false; + } + + int address_len = sizeof(address); + ret = getsockname(listen_socket, + reinterpret_cast(&address), + &address_len); + + if (ret < 0) { + SafeCloseSocket(listen_socket); + return false; + } + + SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (socket0 == INVALID_SOCKET) { + SafeCloseSocket(listen_socket); + return false; + } + + ret = connect(socket0, + reinterpret_cast(&address), + sizeof(address)); + + if (ret < 0) { + SafeCloseSocket(listen_socket); + SafeCloseSocket(socket0); + return false; + } + + SOCKET socket1 = accept(listen_socket, nullptr, nullptr); + if (socket1 == INVALID_SOCKET) { + SafeCloseSocket(listen_socket); + SafeCloseSocket(socket0); + return false; + } + + SafeCloseSocket(listen_socket); + + u_long non_block = 1; + if (ioctlsocket(socket0, FIONBIO, &non_block) < 0 + || ioctlsocket(socket1, FIONBIO, &non_block) < 0) { + SafeCloseSocket(socket0); + SafeCloseSocket(socket1); + return false; + } + + fd[0] = static_cast(socket0); + fd[1] = static_cast(socket1); + + return true; +} + +#endif 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 + +/** + * 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.cxx b/src/event/WakeFD.cxx deleted file mode 100644 index 1a84f5645..000000000 --- a/src/event/WakeFD.cxx +++ /dev/null @@ -1,225 +0,0 @@ -/* - * 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" -#include "WakeFD.hxx" -#include "fd_util.h" -#include "gcc.h" - -#include - -#ifdef WIN32 -#include -#include -#include /* for memset() */ -#endif - -#ifdef HAVE_EVENTFD -#include -#endif - -#ifdef WIN32 -static bool PoorSocketPair(int fd[2]); -#endif - -bool -WakeFD::Create() -{ - assert(fds[0] == -1); - assert(fds[1] == -1); - -#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() -{ -#ifdef WIN32 - closesocket(fds[0]); - closesocket(fds[1]); -#else - close(fds[0]); -#ifdef HAVE_EVENTFD - if (!IsEventFD()) -#endif - close(fds[1]); -#endif - -#ifndef NDEBUG - fds[0] = -1; - fds[1] = -1; -#endif -} - -bool -WakeFD::Read() -{ - assert(fds[0] >= 0); - -#ifdef WIN32 - assert(fds[1] >= 0); - char buffer[256]; - 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() -{ - assert(fds[0] >= 0); - -#ifdef WIN32 - assert(fds[1] >= 0); - - 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 -} - -#ifdef WIN32 - -static void SafeCloseSocket(SOCKET s) -{ - int error = WSAGetLastError(); - closesocket(s); - WSASetLastError(error); -} - -/* 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 - * rather than wide-available API. - */ -static bool PoorSocketPair(int fd[2]) -{ - assert (fd != nullptr); - - SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (listen_socket == INVALID_SOCKET) - return false; - - sockaddr_in address; - std::memset(&address, 0, sizeof(address)); - address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - - int ret = bind(listen_socket, - reinterpret_cast(&address), - sizeof(address)); - - if (ret < 0) { - SafeCloseSocket(listen_socket); - return false; - } - - ret = listen(listen_socket, 1); - - if (ret < 0) { - SafeCloseSocket(listen_socket); - return false; - } - - int address_len = sizeof(address); - ret = getsockname(listen_socket, - reinterpret_cast(&address), - &address_len); - - if (ret < 0) { - SafeCloseSocket(listen_socket); - return false; - } - - SOCKET socket0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (socket0 == INVALID_SOCKET) { - SafeCloseSocket(listen_socket); - return false; - } - - ret = connect(socket0, - reinterpret_cast(&address), - sizeof(address)); - - if (ret < 0) { - SafeCloseSocket(listen_socket); - SafeCloseSocket(socket0); - return false; - } - - SOCKET socket1 = accept(listen_socket, nullptr, nullptr); - if (socket1 == INVALID_SOCKET) { - SafeCloseSocket(listen_socket); - SafeCloseSocket(socket0); - return false; - } - - SafeCloseSocket(listen_socket); - - u_long non_block = 1; - if (ioctlsocket(socket0, FIONBIO, &non_block) < 0 - || ioctlsocket(socket1, FIONBIO, &non_block) < 0) { - SafeCloseSocket(socket0); - SafeCloseSocket(socket1); - return false; - } - - fd[0] = static_cast(socket0); - fd[1] = static_cast(socket1); - - return true; -} - -#endif 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 -/** - * 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 */ diff --git a/src/fd_util.c b/src/fd_util.c index ea29d6eaa..17976a5ee 100644 --- a/src/fd_util.c +++ b/src/fd_util.c @@ -49,7 +49,7 @@ #include #endif -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD #include #endif @@ -332,7 +332,7 @@ inotify_init_cloexec(void) #endif -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD int eventfd_cloexec_nonblock(unsigned initval, int flags) diff --git a/src/fd_util.h b/src/fd_util.h index e65c6a69b..9003b1616 100644 --- a/src/fd_util.h +++ b/src/fd_util.h @@ -144,7 +144,7 @@ inotify_init_cloexec(void); #endif -#ifdef HAVE_EVENTFD +#ifdef USE_EVENTFD /** * Wrapper for eventfd() which sets the flags CLOEXEC and NONBLOCK -- cgit v1.2.3