utils: Wrap socket() and pipe() to reduce syscalls

Some platforms provide an implementation of socket() and pipe2() that allow the
caller to specify that the resulting file descriptors should be non-blocking.

Using these allows us to potentially elide 3 calls into 1 by avoiding extraneous
calls to fcntl() to set the O_NONBLOCK flag afterwards.

In passing, change ast_alertpipe_init() to use pipe2() directly instead of the
wrapper if it is available.

Change-Id: I3ebe654fb549587537161506c6c950f4ab298bb0
This commit is contained in:
Sean Bright
2018-12-07 07:57:48 -05:00
parent 1657508ddd
commit 8a18fb81c1
6 changed files with 175 additions and 8 deletions

View File

@@ -609,6 +609,9 @@
/* Define to indicate presence of the pg_encoding_to_char API. */
#undef HAVE_PGSQL_pg_encoding_to_char
/* Define to 1 if you have the `pipe2' function. */
#undef HAVE_PIPE2
/* Define if your system has the PJPROJECT libraries. */
#undef HAVE_PJPROJECT
@@ -877,6 +880,9 @@
/* Define to 1 if you have the `socket' function. */
#undef HAVE_SOCKET
/* Define to 1 if your socket() implementation can accept SOCK_NONBLOCK. */
#undef HAVE_SOCK_NONBLOCK
/* Define to 1 if your system has soxmix application. */
#undef HAVE_SOXMIX

View File

@@ -913,6 +913,40 @@ enum ast_fd_flag_operation {
int __ast_fd_set_flags(int fd, int flags, enum ast_fd_flag_operation op,
const char *file, int lineno, const char *function);
/*!
* \brief Create a non-blocking socket
* \since 13.25
*
* Wrapper around socket(2) that sets the O_NONBLOCK flag on the resulting
* socket.
*
* \details
* For parameter and return information, see the man page for
* socket(2).
*/
#ifdef HAVE_SOCK_NONBLOCK
# define ast_socket_nonblock(domain, type, protocol) socket((domain), (type) | SOCK_NONBLOCK, (protocol))
#else
int ast_socket_nonblock(int domain, int type, int protocol);
#endif
/*!
* \brief Create a non-blocking pipe
* \since 13.25
*
* Wrapper around pipe(2) that sets the O_NONBLOCK flag on the resulting
* file descriptors.
*
* \details
* For parameter and return information, see the man page for
* pipe(2).
*/
#ifdef HAVE_PIPE2
# define ast_pipe_nonblock(filedes) pipe2((filedes), O_NONBLOCK)
#else
int ast_pipe_nonblock(int filedes[2]);
#endif
/*!
* \brief Set the current thread's user interface status.
*