-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce the number of system calls for accept (#769)
Use accept4 to avoid 2 fcntl system calls. accept4 accepts flags to tell to create the file descriptor in non blocking mode. This way we don't need to set it later with Unix.set_nonblock. Also, since we know that all the calls to accept will be on non blocking sockets we can avoid releasing the runtime lock.
- Loading branch information
Showing
4 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* This file is part of Lwt, released under the MIT license. See LICENSE.md for | ||
details, or visit https://github.com/ocsigen/lwt/blob/master/LICENSE.md. */ | ||
|
||
|
||
|
||
#include "lwt_config.h" | ||
|
||
#ifdef HAVE_ACCEPT4 | ||
|
||
#define _GNU_SOURCE | ||
#include <caml/alloc.h> | ||
#include <caml/memory.h> | ||
#include <caml/unixsupport.h> | ||
#include <caml/socketaddr.h> | ||
|
||
CAMLprim value lwt_unix_accept4(value vcloexec, value vnonblock, value vsock) | ||
{ | ||
CAMLparam3(vcloexec, vnonblock, vsock); | ||
CAMLlocal2(vaddr, res); | ||
|
||
union sock_addr_union addr; | ||
socklen_param_type addr_len; | ||
int cloexec = Int_val(vcloexec) ? SOCK_CLOEXEC : 0; | ||
int nonblock = Int_val(vnonblock) ? SOCK_NONBLOCK : 0; | ||
addr_len = sizeof(addr); | ||
|
||
int fd = | ||
accept4(Int_val(vsock), &addr.s_gen, &addr_len, cloexec | nonblock); | ||
if (fd == -1) | ||
uerror("accept", Nothing); | ||
|
||
vaddr = alloc_sockaddr(&addr, addr_len, fd); | ||
res = caml_alloc_small(2, 0); | ||
Field(res, 0) = Val_int(fd); | ||
Field(res, 1) = vaddr; | ||
|
||
CAMLreturn(res); | ||
} | ||
|
||
#else | ||
|
||
#include "lwt_unix.h" | ||
LWT_NOT_AVAILABLE3(unix_accept4) | ||
|
||
#endif |