diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am index 4d0f81ce..11030731 100644 --- a/src/microhttpd/Makefile.am +++ b/src/microhttpd/Makefile.am @@ -67,7 +67,7 @@ libmicrohttpd_la_SOURCES = \ mhd_threads.c mhd_threads.h \ mhd_locks.h mhd_sem.c \ mhd_sockets.c mhd_sockets.h \ - mhd_itc.c mhd_itc.h \ + mhd_itc.c mhd_itc.h mhd_itc_types.h \ mhd_compat.h \ response.c response.h if HAVE_W32 diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index 10e0551d..b480dd4d 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c @@ -34,6 +34,7 @@ #include "mhd_locks.h" #include "mhd_sockets.h" #include "mhd_compat.h" +#include "mhd_itc.h" /** @@ -521,9 +522,7 @@ MHD_connection_close_ (struct MHD_Connection *connection, to resume accepting connections */ if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && (! MHD_INVALID_PIPE_ (daemon->itc)) && - (1 != MHD_pipe_write_ (daemon->itc, - "c", - 1)) ) + (! MHD_itc_activate_ (daemon->itc, "c")) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, diff --git a/src/microhttpd/daemon.c b/src/microhttpd/daemon.c index 26a87782..cad0ed53 100644 --- a/src/microhttpd/daemon.c +++ b/src/microhttpd/daemon.c @@ -2029,9 +2029,7 @@ internal_add_connection (struct MHD_Daemon *daemon, else if ( (MHD_YES == external_add) && (! MHD_INVALID_PIPE_(daemon->itc)) && - (1 != MHD_pipe_write_ (daemon->itc, - "n", - 1)) ) + (! MHD_itc_activate_ (daemon->itc, "n")) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -2212,7 +2210,7 @@ MHD_resume_connection (struct MHD_Connection *connection) connection->resuming = MHD_YES; daemon->resuming = MHD_YES; if ( (! MHD_INVALID_PIPE_(daemon->itc)) && - (1 != MHD_pipe_write_ (daemon->itc, "r", 1)) ) + (! MHD_itc_activate_ (daemon->itc, "r")) ) { #ifdef HAVE_MESSAGES MHD_DLOG (daemon, @@ -3792,9 +3790,7 @@ MHD_quiesce_daemon (struct MHD_Daemon *daemon) #endif if (! MHD_INVALID_PIPE_(daemon->worker_pool[i].itc)) { - if (1 != MHD_pipe_write_ (daemon->worker_pool[i].itc, - "q", - 1)) + if (! MHD_itc_activate_ (daemon->worker_pool[i].itc, "q")) MHD_PANIC (_("Failed to signal quiesce via pipe")); } } @@ -3815,9 +3811,7 @@ MHD_quiesce_daemon (struct MHD_Daemon *daemon) #endif if (! MHD_INVALID_PIPE_(daemon->itc)) { - if (1 != MHD_pipe_write_ (daemon->itc, - "q", - 1)) + if (! MHD_itc_activate_ (daemon->itc, "q")) MHD_PANIC (_("failed to signal quiesce via pipe")); } @@ -5186,7 +5180,7 @@ close_all_connections (struct MHD_Daemon *daemon) #if MHD_WINSOCK_SOCKETS if ( (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) && (! MHD_INVALID_PIPE_(daemon->itc)) && - (1 != MHD_pipe_write_ (daemon->itc, "e", 1)) ) + (! MHD_itc_activate_ (daemon->itc, "e")) ) MHD_PANIC (_("Failed to signal shutdown via pipe")); #endif } @@ -5289,7 +5283,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) } if (! MHD_INVALID_PIPE_(daemon->itc)) { - if (1 != MHD_pipe_write_ (daemon->itc, "e", 1)) + if (! MHD_itc_activate_ (daemon->itc, "e")) MHD_PANIC (_("Failed to signal shutdown via pipe")); } #ifdef HAVE_LISTEN_SHUTDOWN @@ -5325,9 +5319,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon) { if (! MHD_INVALID_PIPE_(daemon->worker_pool[i].itc)) { - if (1 != MHD_pipe_write_ (daemon->worker_pool[i].itc, - "e", - 1)) + if (! MHD_itc_activate_ (daemon->worker_pool[i].itc, "e")) MHD_PANIC (_("Failed to signal shutdown via pipe.")); } if (!MHD_join_thread_ (daemon->worker_pool[i].pid)) diff --git a/src/microhttpd/internal.h b/src/microhttpd/internal.h index cfaee781..0545e902 100644 --- a/src/microhttpd/internal.h +++ b/src/microhttpd/internal.h @@ -62,7 +62,7 @@ #include "mhd_threads.h" #include "mhd_locks.h" #include "mhd_sockets.h" -#include "mhd_itc.h" +#include "mhd_itc_types.h" /** diff --git a/src/microhttpd/mhd_itc.c b/src/microhttpd/mhd_itc.c index 14e474d8..e98c607a 100644 --- a/src/microhttpd/mhd_itc.c +++ b/src/microhttpd/mhd_itc.c @@ -33,24 +33,6 @@ #include "internal.h" -#ifdef _MHD_ITC_EVENTFD - -int -MHD_pipe_write_ (MHD_itc_ pip, - const void *ptr, - size_t sz) -{ - uint64_t val = 1; - if (sizeof (val) != - write (pip, - &val, - sizeof (val))) - MHD_PANIC (_("Failed to write to eventfd\n")); - return sz; -} - -#endif /* _MHD_ITC_EVENTFD */ - #if defined(_MHD_ITC_PIPE) #if !defined(_WIN32) || defined(__CYGWIN__) diff --git a/src/microhttpd/mhd_itc.h b/src/microhttpd/mhd_itc.h index 9f77dcc4..c30fc632 100644 --- a/src/microhttpd/mhd_itc.h +++ b/src/microhttpd/mhd_itc.h @@ -32,27 +32,22 @@ */ #ifndef MHD_ITC_H #define MHD_ITC_H 1 -#include "mhd_options.h" +#include "mhd_itc_types.h" -/* Force socketpair on native W32 */ -#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(_MHD_ITC_SOCKETPAIR) -#error _MHD_ITC_SOCKETPAIR is not defined on naitive W32 platform -#endif /* _WIN32 && !__CYGWIN__ && !_MHD_ITC_SOCKETPAIR */ - -#ifdef HAVE_UNISTD_H -#include -#endif /* HAVE_UNISTD_H */ #include #if defined(_MHD_ITC_EVENTFD) -#include /* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */ +#include +#include /* for uint64_t */ +#ifdef HAVE_UNISTD_H +#include /* for read(), write(), errno */ +#endif /* HAVE_UNISTD_H */ +#ifdef HAVE_STRING_H +#include /* for strerror() */ +#endif -/** - * Data type for a MHD ITC. - */ -typedef int MHD_itc_; /** * Initialise ITC by generating eventFD @@ -67,12 +62,18 @@ typedef int MHD_itc_; #define MHD_itc_last_strerror_() strerror(errno) /** - * write data to real pipe + * Internal static const helper for MHD_itc_activate_() */ -int -MHD_pipe_write_ (MHD_itc_ pip, - const void *ptr, - size_t sz); +static const uint64_t _MHD_itc_wr_data = 1; + +/** + * Activate signal on @a itc + * @param itc the itc to use + * @param str ignored + * @return non-zero if succeeded, zero otherwise + */ +#define MHD_itc_activate_(itc, str) \ + ((write((itc), (const void*)&_MHD_itc_wr_data, 8) > 0) || (EAGAIN == errno)) #define MHD_pipe_get_read_fd_(pip) (pip) @@ -122,18 +123,13 @@ MHD_pipe_write_ (MHD_itc_ pip, /* **************** Standard UNIX ITC implementation by pipe ********** */ -# ifdef HAVE_STRING_H -# include /* for strerror() */ -# endif +#ifdef HAVE_UNISTD_H +#include /* for read(), write(), errno */ +#endif /* HAVE_UNISTD_H */ +#ifdef HAVE_STRING_H +#include /* for strerror() */ +#endif -/** - * Data type for a MHD ITC. - */ -struct MHD_Itc -{ - int fd[2]; -}; -typedef struct MHD_Itc MHD_itc_; /** * Initialise ITC by generating pipe @@ -148,9 +144,13 @@ typedef struct MHD_Itc MHD_itc_; #define MHD_itc_last_strerror_() strerror(errno) /** - * write data to real pipe + * Activate signal on @a itc + * @param itc the itc to use + * @param str one-symbol string, useful only for strace debug + * @return non-zero if succeeded, zero otherwise */ -#define MHD_pipe_write_(pip, ptr, sz) write((pip).fd[1], (const void*)(ptr), (sz)) +#define MHD_itc_activate_(itc, str) \ + ((write((itc).fd[1], (const void*)(str), 1) > 0) || (EAGAIN == errno)) #define MHD_pipe_get_read_fd_(pip) ((pip).fd[0]) @@ -205,14 +205,6 @@ MHD_itc_nonblocking_ (MHD_itc_ itc); #include "mhd_sockets.h" -/** - * Data type for a MHD pipe. - */ -struct MHD_Itc -{ - MHD_socket sk[2]; -}; -typedef struct MHD_Itc MHD_itc_; /** * Initialise ITC by generating socketpair @@ -227,9 +219,14 @@ typedef struct MHD_Itc MHD_itc_; #define MHD_itc_last_strerror_() MHD_socket_last_strerr_() /** - * Write data to emulated pipe + * Activate signal on @a itc + * @param itc the itc to use + * @param str one-symbol string, useful only for strace debug + * @return non-zero if succeeded, zero otherwise */ -#define MHD_pipe_write_(pip, ptr, sz) send((pip).sk[1], (const char*)(ptr), (sz), 0) +#define MHD_itc_activate_(itc, str) \ + ((send((itc).sk[1], (const char*)(str), 1, 0) > 0) || \ + (MHD_SCKT_ERR_IS_EAGAIN_(MHD_socket_get_error_()))) #define MHD_pipe_get_read_fd_(pip) ((pip).sk[0]) diff --git a/src/microhttpd/mhd_itc_types.h b/src/microhttpd/mhd_itc_types.h new file mode 100644 index 00000000..f91e9383 --- /dev/null +++ b/src/microhttpd/mhd_itc_types.h @@ -0,0 +1,83 @@ +/* + This file is part of libmicrohttpd + Copyright (C) 2016 Karlson2k (Evgeny Grin), Christian Grothoff + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +*/ + +/** + * @file microhttpd/mhd_itc_types.h + * @brief Types for platform-independent inter-thread communication + * @author Karlson2k (Evgeny Grin) + * @author Christian Grothoff + * + * Provides basic types for inter-thread communication. + * Designed to be included by other headers. + */ +#ifndef MHD_ITC_TYPES_H +#define MHD_ITC_TYPES_H 1 +#include "mhd_options.h" + +/* Force socketpair on native W32 */ +#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(_MHD_ITC_SOCKETPAIR) +#error _MHD_ITC_SOCKETPAIR is not defined on naitive W32 platform +#endif /* _WIN32 && !__CYGWIN__ && !_MHD_ITC_SOCKETPAIR */ + +#if defined(_MHD_ITC_EVENTFD) +/* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */ + +/** + * Data type for a MHD ITC. + */ +typedef int MHD_itc_; + +#elif defined(_MHD_ITC_PIPE) +/* **************** Standard UNIX ITC implementation by pipe ********** */ + +/** + * Base data type for a MHD ITC. + */ +struct MHD_Itc +{ + int fd[2]; +}; + +/** + * Data type for a MHD ITC. + */ +typedef struct MHD_Itc MHD_itc_; + +#elif defined(_MHD_ITC_SOCKETPAIR) +/* **************** ITC implementation by socket pair ********** */ + +#include "mhd_sockets.h" + +/** + * Base data type for a MHD ITC. + */ +struct MHD_Itc +{ + MHD_socket sk[2]; +}; + +/** + * Data type for a MHD ITC. + */ +typedef struct MHD_Itc MHD_itc_; + +#endif /* _MHD_ITC_SOCKETPAIR */ + +#endif /* ! MHD_ITC_TYPES_H */ diff --git a/w32/common/libmicrohttpd-files.vcxproj b/w32/common/libmicrohttpd-files.vcxproj index 2a615196..6f1e03bc 100644 --- a/w32/common/libmicrohttpd-files.vcxproj +++ b/w32/common/libmicrohttpd-files.vcxproj @@ -42,6 +42,7 @@ + diff --git a/w32/common/libmicrohttpd-filters.vcxproj b/w32/common/libmicrohttpd-filters.vcxproj index 2bb47bfd..d0c47073 100644 --- a/w32/common/libmicrohttpd-filters.vcxproj +++ b/w32/common/libmicrohttpd-filters.vcxproj @@ -132,6 +132,9 @@ Source Files + + Source Files + Source Files