mirror of
https://github.com/curl/curl.git
synced 2026-07-22 09:37:16 +03:00
socketpair: add eventfd and use SOCK_NONBLOCK for socketpair()
Currently, we use `pipe` for `wakeup_create`, which requires ***two***
file descriptors. Furthermore, given its complexity inside, `pipe` is a
bit heavyweight for just a simple event wait/notify mechanism.
`eventfd` would be a more suitable solution for this kind of scenario,
kernel also advocates for developers to use `eventfd` instead of `pipe`
in some simple use cases:
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 change adds the new backend of `eventfd` for `wakeup_create` and
uses it where available, eliminating the overhead of `pipe`. Also, it
optimizes the `wakeup_create` to eliminate the system calls that make
file descriptors non-blocking by moving the logic of setting
non-blocking flags on file descriptors to `socketpair.c` and using
`SOCK_NONBLOCK` for `socketpair(2)`, `EFD_NONBLOCK` for `eventfd(2)`.
Ref:
https://man7.org/linux/man-pages/man7/pipe.7.html
https://man7.org/linux/man-pages/man2/eventfd.2.html
https://man7.org/linux/man-pages/man2/socketpair.2.html
https://www.gnu.org/software/gnulib/manual/html_node/eventfd.html
Closes #13874
This commit is contained in:
parent
b71916b859
commit
23fe1a52dc
10 changed files with 149 additions and 41 deletions
|
|
@ -27,14 +27,31 @@
|
|||
#include "urldata.h"
|
||||
#include "rand.h"
|
||||
|
||||
#if defined(HAVE_PIPE) && defined(HAVE_FCNTL)
|
||||
#include <fcntl.h>
|
||||
#if defined(USE_EVENTFD)
|
||||
#ifdef HAVE_SYS_EVENTFD_H
|
||||
#include <sys/eventfd.h>
|
||||
#endif
|
||||
|
||||
int Curl_pipe(curl_socket_t socks[2])
|
||||
int Curl_eventfd(curl_socket_t socks[2], bool nonblocking)
|
||||
{
|
||||
int efd = eventfd(0, nonblocking ? EFD_CLOEXEC | EFD_NONBLOCK : EFD_CLOEXEC);
|
||||
if(efd == -1) {
|
||||
socks[0] = socks[1] = CURL_SOCKET_BAD;
|
||||
return -1;
|
||||
}
|
||||
socks[0] = socks[1] = efd;
|
||||
return 0;
|
||||
}
|
||||
#elif defined(HAVE_PIPE)
|
||||
#ifdef HAVE_FCNTL
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
int Curl_pipe(curl_socket_t socks[2], bool nonblocking)
|
||||
{
|
||||
if(pipe(socks))
|
||||
return -1;
|
||||
|
||||
#ifdef HAVE_FCNTL
|
||||
if(fcntl(socks[0], F_SETFD, FD_CLOEXEC) ||
|
||||
fcntl(socks[1], F_SETFD, FD_CLOEXEC) ) {
|
||||
close(socks[0]);
|
||||
|
|
@ -42,6 +59,16 @@ int Curl_pipe(curl_socket_t socks[2])
|
|||
socks[0] = socks[1] = CURL_SOCKET_BAD;
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
if(nonblocking) {
|
||||
if(curlx_nonblock(socks[0], TRUE) < 0 ||
|
||||
curlx_nonblock(socks[1], TRUE) < 0) {
|
||||
close(socks[0]);
|
||||
close(socks[1]);
|
||||
socks[0] = socks[1] = CURL_SOCKET_BAD;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -80,7 +107,7 @@ int Curl_pipe(curl_socket_t socks[2])
|
|||
#include "memdebug.h"
|
||||
|
||||
int Curl_socketpair(int domain, int type, int protocol,
|
||||
curl_socket_t socks[2])
|
||||
curl_socket_t socks[2], bool nonblocking)
|
||||
{
|
||||
union {
|
||||
struct sockaddr_in inaddr;
|
||||
|
|
@ -198,6 +225,10 @@ int Curl_socketpair(int domain, int type, int protocol,
|
|||
} while(1);
|
||||
}
|
||||
|
||||
if(nonblocking)
|
||||
if(curlx_nonblock(socks[0], TRUE) < 0 ||
|
||||
curlx_nonblock(socks[1], TRUE) < 0)
|
||||
goto error;
|
||||
sclose(listener);
|
||||
return 0;
|
||||
|
||||
|
|
@ -207,5 +238,25 @@ error:
|
|||
sclose(socks[1]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#else
|
||||
int Curl_socketpair(int domain, int type, int protocol,
|
||||
curl_socket_t socks[2], bool nonblocking)
|
||||
{
|
||||
#ifdef SOCK_NONBLOCK
|
||||
type = nonblocking ? type | SOCK_NONBLOCK : type;
|
||||
#endif
|
||||
if(socketpair(domain, type, protocol, socks))
|
||||
return -1;
|
||||
#ifndef SOCK_NONBLOCK
|
||||
if(nonblocking) {
|
||||
if(curlx_nonblock(socks[0], TRUE) < 0 ||
|
||||
curlx_nonblock(socks[1], TRUE) < 0) {
|
||||
close(socks[0]);
|
||||
close(socks[1]);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif /* ! HAVE_SOCKETPAIR */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue