Use MHD_itc_activate_ macro for ITC signaling, move ITC types to separate

header.
Separate header allow exclusion of heavy headers in internal.h, which is
included by most source files.
Fixed checking success of write() to eventFD by comparing result to 1.
Fixed error when trying to signal on fully filled ITC (ITC is already in
activated (signaled) state and those conditions are not an error).
EventFD is signaled now by macro, not a function.
This commit is contained in:
Evgeny Grin (Karlson2k)
2016-10-11 15:21:11 +00:00
parent ec820265a0
commit ee182a0b29
9 changed files with 137 additions and 80 deletions
+1 -1
View File
@@ -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
+2 -3
View File
@@ -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,
+7 -15
View File
@@ -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))
+1 -1
View File
@@ -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"
/**
-18
View File
@@ -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__)
+39 -42
View File
@@ -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 <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <fcntl.h>
#if defined(_MHD_ITC_EVENTFD)
#include <sys/eventfd.h>
/* **************** Optimized GNU/Linux ITC implementation by eventfd ********** */
#include <sys/eventfd.h>
#include <stdint.h> /* for uint64_t */
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for read(), write(), errno */
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h> /* 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 <string.h> /* for strerror() */
# endif
#ifdef HAVE_UNISTD_H
#include <unistd.h> /* for read(), write(), errno */
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h> /* 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])
+83
View File
@@ -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 */
+1
View File
@@ -42,6 +42,7 @@
<ClInclude Include="$(MhdSrc)microhttpd\mhd_locks.h" />
<ClInclude Include="$(MhdSrc)microhttpd\mhd_sockets.h" />
<ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h" />
<ClInclude Include="$(MhdSrc)microhttpd\mhd_itc_types.h" />
<ClInclude Include="$(MhdSrc)microhttpd\mhd_compat.h" />
<ClInclude Include="$(MhdW32Common)MHD_config.h" />
</ItemGroup>
+3
View File
@@ -132,6 +132,9 @@
<ClCompile Include="$(MhdSrc)microhttpd\mhd_sockets.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClInclude Include="$(MhdSrc)microhttpd\mhd_itc_types.h">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="$(MhdSrc)microhttpd\mhd_itc.h">
<Filter>Source Files</Filter>
</ClInclude>