Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: don't pass sockaddr to accept()
Browse files Browse the repository at this point in the history
Shaves a few nanoseconds off the accept() syscall.
  • Loading branch information
bnoordhuis committed May 24, 2012
1 parent cff2221 commit 752ac30
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
12 changes: 9 additions & 3 deletions src/unix/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,18 @@ int uv__socket(int domain, int type, int protocol) {
}


int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) {
int uv__accept(int sockfd) {
int peerfd;

assert(sockfd >= 0);

while (1) {
#if __linux__
peerfd = uv__accept4(sockfd, saddr, &slen, UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
peerfd = uv__accept4(sockfd,
NULL,
NULL,
UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);

if (peerfd != -1)
break;

Expand All @@ -463,7 +467,9 @@ int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) {
break;
#endif

if ((peerfd = accept(sockfd, saddr, &slen)) == -1) {
peerfd = accept(sockfd, NULL, NULL);

if (peerfd == -1) {
if (errno == EINTR)
continue;
else
Expand Down
2 changes: 1 addition & 1 deletion src/unix/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void uv__stream_init(uv_loop_t* loop, uv_stream_t* stream,
int uv__stream_open(uv_stream_t*, int fd, int flags);
void uv__stream_destroy(uv_stream_t* stream);
void uv__server_io(uv_loop_t* loop, uv__io_t* watcher, int events);
int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t len);
int uv__accept(int sockfd);
int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
socklen_t addrlen, uv_connect_cb cb);

Expand Down
3 changes: 1 addition & 2 deletions src/unix/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ void uv_pipe_connect(uv_connect_t* req,

/* TODO merge with uv__server_io()? */
static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events) {
struct sockaddr_un saddr;
uv_pipe_t* pipe;
int saved_errno;
int sockfd;
Expand All @@ -239,7 +238,7 @@ static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events) {

assert(pipe->type == UV_NAMED_PIPE);

sockfd = uv__accept(pipe->fd, (struct sockaddr *)&saddr, sizeof saddr);
sockfd = uv__accept(pipe->fd);
if (sockfd == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
uv__set_sys_error(pipe->loop, errno);
Expand Down
3 changes: 1 addition & 2 deletions src/unix/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ void uv__stream_destroy(uv_stream_t* stream) {

void uv__server_io(uv_loop_t* loop, uv__io_t* w, int events) {
int fd;
struct sockaddr_storage addr;
uv_stream_t* stream = container_of(w, uv_stream_t, read_watcher);

assert(events == UV__IO_READ);
Expand All @@ -181,7 +180,7 @@ void uv__server_io(uv_loop_t* loop, uv__io_t* w, int events) {
*/
while (stream->fd != -1) {
assert(stream->accepted_fd < 0);
fd = uv__accept(stream->fd, (struct sockaddr*)&addr, sizeof addr);
fd = uv__accept(stream->fd);

if (fd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
Expand Down

0 comments on commit 752ac30

Please sign in to comment.