Skip to content

Commit ea8f22b

Browse files
committed
erts: Accept sockets non-blocking with accept4
Passing SOCK_NONBLOCK to accept4() makes the SET_NONBLOCKING call on every accepted socket redundant, saving a syscall per accept on platforms that have accept4().
1 parent 6b6e6da commit ea8f22b

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

erts/config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@
462462
/* Define to 1 if _Float16 can be converted from/to double. */
463463
#undef FLOAT16_IS_CONVERTIBLE
464464

465+
/* Define to 1 if you have the 'accept4' function. */
466+
#undef HAVE_ACCEPT4
467+
465468
/* Define to 1 if you have the <arpa/nameser.h> header file. */
466469
#undef HAVE_ARPA_NAMESER_H
467470

erts/configure

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21341,6 +21341,14 @@ fi
2134121341
;;
2134221342
esac
2134321343

21344+
ac_fn_c_check_func "$LINENO" "accept4" "ac_cv_func_accept4"
21345+
if test "x$ac_cv_func_accept4" = xyes
21346+
then :
21347+
printf "%s\n" "#define HAVE_ACCEPT4 1" >>confdefs.h
21348+
21349+
fi
21350+
21351+
2134421352

2134521353

2134621354
saved_cppflags=$CPPFLAGS

erts/configure.ac

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,6 +2243,10 @@ AS_CASE([$host_os],
22432243
[[#include <sys/socket.h>]])
22442244
])
22452245

2246+
dnl Check for accept4, which accepts a socket and sets it non-blocking
2247+
dnl and close-on-exec in one call
2248+
AC_CHECK_FUNCS([accept4])
2249+
22462250

22472251
dnl ----------------------------------------------------------------------
22482252
dnl Checks for library functions.

erts/emulator/nifs/unix/unix_socket_syncio.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@
195195
* ======================================================================== *
196196
*/
197197

198-
#ifdef HAS_ACCEPT4
199-
// We have to figure out what the flags are...
198+
#if defined(HAVE_ACCEPT4) && defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK)
200199
#define sock_accept(s, addr, len) \
201-
accept4((s), (addr), (len), (SOCK_CLOEXEC))
200+
accept4((s), (addr), (len), (SOCK_CLOEXEC | SOCK_NONBLOCK))
201+
/* The accepted socket is already non-blocking */
202+
#define ESSIO_ACCEPTED_NONBLOCK 1
202203
#else
203204
#define sock_accept(s, addr, len) accept((s), (addr), (len))
204205
#endif
@@ -2863,7 +2864,9 @@ BOOLEAN_T essio_accept_accepted(ErlNifEnv* env,
28632864
&accDescP->ctrlPid,
28642865
&accDescP->ctrlMon) == 0 );
28652866

2867+
#ifndef ESSIO_ACCEPTED_NONBLOCK
28662868
SET_NONBLOCKING(accDescP->sock);
2869+
#endif
28672870

28682871
accDescP->writeState |= ESOCK_STATE_CONNECTED;
28692872

0 commit comments

Comments
 (0)