mirror of
https://github.com/warmcat/libwebsockets.git
synced 2025-03-09 00:00:04 +01:00
unix plat: use eventfd in place of pipe where possible
From eventfd man page: Applications can use an eventfd file descriptor instead of a pipe (see pipe(2)) in all cases where a pipe is used simply to signal events. The kernel overhead of an eventfd file descriptor is much lower than that of a pipe, and only one file descriptor is required (versus the two required for a pipe).
This commit is contained in:
parent
cbc8f0d9c0
commit
a404f5e95a
5 changed files with 20 additions and 3 deletions
|
@ -852,6 +852,7 @@ CHECK_FUNCTION_EXISTS(atoll LWS_HAVE_ATOLL)
|
|||
CHECK_FUNCTION_EXISTS(_atoi64 LWS_HAVE__ATOI64)
|
||||
CHECK_FUNCTION_EXISTS(_stat32i64 LWS_HAVE__STAT32I64)
|
||||
CHECK_FUNCTION_EXISTS(clock_gettime LWS_HAVE_CLOCK_GETTIME)
|
||||
CHECK_FUNCTION_EXISTS(eventfd LWS_HAVE_EVENTFD)
|
||||
|
||||
if (NOT LWS_HAVE_GETIFADDRS)
|
||||
if (LWS_WITHOUT_BUILTIN_GETIFADDRS)
|
||||
|
|
|
@ -55,6 +55,7 @@
|
|||
#cmakedefine LWS_HAVE_NEW_UV_VERSION_H
|
||||
#cmakedefine LWS_HAVE_OPENSSL_ECDH_H
|
||||
#cmakedefine LWS_HAVE_PIPE2
|
||||
#cmakedefine LWS_HAVE_EVENTFD
|
||||
#cmakedefine LWS_HAVE_PTHREAD_H
|
||||
#cmakedefine LWS_HAVE_RSA_SET0_KEY
|
||||
#cmakedefine LWS_HAVE_RSA_verify_pss_mgf1
|
||||
|
|
|
@ -46,6 +46,9 @@
|
|||
#include <sys/time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/un.h>
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
#include <sys/eventfd.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <machine/endian.h>
|
||||
|
|
|
@ -32,8 +32,11 @@ int
|
|||
lws_plat_pipe_create(struct lws *wsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
||||
|
||||
#if defined(LWS_HAVE_PIPE2)
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
pt->dummy_pipe_fds[0] = eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK);
|
||||
pt->dummy_pipe_fds[1] = -1;
|
||||
return pt->dummy_pipe_fds[0]<0?-1:0;
|
||||
#elif defined(LWS_HAVE_PIPE2)
|
||||
return pipe2(pt->dummy_pipe_fds, O_NONBLOCK);
|
||||
#else
|
||||
return pipe(pt->dummy_pipe_fds);
|
||||
|
@ -44,12 +47,17 @@ int
|
|||
lws_plat_pipe_signal(struct lws *wsi)
|
||||
{
|
||||
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
eventfd_t value = 1;
|
||||
return eventfd_write(pt->dummy_pipe_fds[0], value);
|
||||
#else
|
||||
char buf = 0;
|
||||
int n;
|
||||
|
||||
n = write(pt->dummy_pipe_fds[1], &buf, 1);
|
||||
|
||||
return n != 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
|
|
|
@ -28,7 +28,11 @@ static int
|
|||
rops_handle_POLLIN_pipe(struct lws_context_per_thread *pt, struct lws *wsi,
|
||||
struct lws_pollfd *pollfd)
|
||||
{
|
||||
#if !defined(WIN32) && !defined(_WIN32)
|
||||
#if defined(LWS_HAVE_EVENTFD)
|
||||
eventfd_t value;
|
||||
if (eventfd_read(wsi->desc.sockfd, &value) < 0)
|
||||
return LWS_HPI_RET_PLEASE_CLOSE_ME;
|
||||
#elif !defined(WIN32) && !defined(_WIN32)
|
||||
char s[100];
|
||||
int n;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue