Skip to content

Commit

Permalink
use accept4 for tcp_accept when available (twitter#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevyang committed Jul 26, 2018
1 parent 21ba10e commit e710712
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -96,6 +96,7 @@ check_symbol_exists(sys_signame signal.h HAVE_SIGNAME)

include(CheckFunctionExists)
check_function_exists(backtrace HAVE_BACKTRACE)
check_function_exists(accept4 HAVE_ACCEPT4)

include(TestBigEndian)
test_big_endian(HAVE_BIG_ENDIAN)
Expand Down
2 changes: 2 additions & 0 deletions config.h.in
Expand Up @@ -12,6 +12,8 @@

#cmakedefine HAVE_BACKTRACE

#cmakedefine HAVE_ACCEPT4

#cmakedefine HAVE_BIG_ENDIAN

#cmakedefine HAVE_LOGGING
Expand Down
4 changes: 4 additions & 0 deletions include/cc_define.h
Expand Up @@ -54,6 +54,10 @@ extern "C" {
# define CC_BACKTRACE 1
#endif

#ifdef HAVE_ACCEPT4
# define CC_ACCEPT4 1
#endif

#ifdef HAVE_DEBUG_MM
#define CC_DEBUG_MM 1
#endif
Expand Down
7 changes: 7 additions & 0 deletions src/channel/cc_tcp.c
Expand Up @@ -18,6 +18,7 @@
#include <channel/cc_tcp.h>

#include <cc_debug.h>
#include <cc_define.h>
#include <cc_mm.h>
#include <cc_pool.h>
#include <cc_util.h>
Expand Down Expand Up @@ -321,7 +322,11 @@ _tcp_accept(struct tcp_conn *sc)
ASSERT(sc->sd > 0);

for (;;) { /* we accept at most one tcp_conn with the 'break' at the end */
#ifdef CC_ACCEPT4
sd = accept4(sc->sd, NULL, NULL, SOCK_NONBLOCK);
#else
sd = accept(sc->sd, NULL, NULL);
#endif /* CC_ACCEPT4 */
if (sd < 0) {
if (errno == EINTR) {
log_debug("accept on sd %d not ready: eintr", sc->sd);
Expand Down Expand Up @@ -360,11 +365,13 @@ tcp_accept(struct tcp_conn *sc, struct tcp_conn *c)
c->level = CHANNEL_BASE;
c->state = CHANNEL_ESTABLISHED;

#ifndef CC_ACCEPT4 /* if we have accept4, nonblock will already have been set */
ret = tcp_set_nonblocking(sd);
if (ret < 0) {
log_warn("set nonblock on sd %d failed, ignored: %s", sd,
strerror(errno));
}
#endif

ret = tcp_set_tcpnodelay(sd);
if (ret < 0) {
Expand Down

0 comments on commit e710712

Please sign in to comment.