diff options
author | Sean Bright <sean.bright@gmail.com> | 2017-04-17 20:06:10 -0400 |
---|---|---|
committer | Sean Bright <sean.bright@gmail.com> | 2017-04-24 11:50:09 -0500 |
commit | 59203c51cc6a9676ef1ab42aebe070a55f55ead2 (patch) | |
tree | 474f8136a9a10ee4720c58e4a9d9dc2b6015f03a /include/asterisk | |
parent | dc6654d969224129bdd7b4080eda6e027c6454b9 (diff) |
core: Use eventfd for alert pipes on Linux when possible
The primary win of switching to eventfd when possible is that it only
uses a single file descriptor while pipe() will use two. This means for
each bridge channel we're reducing the number of required file
descriptors by 1, and - if you're using timerfd - we also now have 1
less file descriptor per Asterisk channel.
The API is not ideal (passing int arrays), but this is the cleanest
approach I could come up with to maintain API/ABI.
I've also removed what I believe to be an erroneous code block that
checked the non-blocking flag on the pipe ends for each read. If the
file descriptor is 'losing' its non-blocking mode, it is because of a
bug somewhere else in our code.
In my testing I haven't seen any measurable difference in performance.
Change-Id: Iff0fb1573e7f7a187d5211ddc60aa8f3da3edb1d
Diffstat (limited to 'include/asterisk')
-rw-r--r-- | include/asterisk/alertpipe.h | 159 | ||||
-rw-r--r-- | include/asterisk/autoconfig.h.in | 4 | ||||
-rw-r--r-- | include/asterisk/channel.h | 7 |
3 files changed, 164 insertions, 6 deletions
diff --git a/include/asterisk/alertpipe.h b/include/asterisk/alertpipe.h new file mode 100644 index 000000000..5ff854ce8 --- /dev/null +++ b/include/asterisk/alertpipe.h @@ -0,0 +1,159 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2017, Sean Bright + * + * Sean Bright <sean.bright@gmail.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +#ifndef ASTERISK_ALERTPIPE_H +#define ASTERISK_ALERTPIPE_H + +#include "asterisk/utils.h" + +typedef enum { + AST_ALERT_READ_SUCCESS = 0, + AST_ALERT_NOT_READABLE, + AST_ALERT_READ_FAIL, + AST_ALERT_READ_FATAL, +} ast_alert_status_t; + +/*! + * \brief Initialize an alert pipe + * \since 13.16.0 + * + * \param p a two-element array to hold the alert pipe's file descriptors + * + * \return non-zero if a failure occurred, zero otherwise. + */ +int ast_alertpipe_init(int alert_pipe[2]); + +/*! + * \brief Close an alert pipe + * \since 13.16.0 + * + * \param p a two-element containing the alert pipe's file descriptors + */ +void ast_alertpipe_close(int alert_pipe[2]); + +/*! + * \brief Read an event from an alert pipe + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \retval AST_ALERT_READ_SUCCESS on success + * \retval AST_ALERT_NOT_READABLE if the alert pipe is not readable + * \retval AST_ALERT_READ_FATAL if the alert pipe's file descriptors are in + * blocking mode, or a read error occurs. + */ +ast_alert_status_t ast_alertpipe_read(int alert_pipe[2]); + +/*! + * \brief Write an event to an alert pipe + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \return see write(2) + */ +ssize_t ast_alertpipe_write(int alert_pipe[2]); + +/*! + * \brief Consume all alerts written to the alert pipe + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \retval AST_ALERT_READ_SUCCESS on success + * \retval AST_ALERT_NOT_READABLE if the alert pipe is not readable + * \retval AST_ALERT_READ_FATAL if the alert pipe's file descriptors are in + * blocking mode, or a read error occurs. + */ +ast_alert_status_t ast_alertpipe_flush(int alert_pipe[2]); + +/*! + * \brief Sets the alert pipe file descriptors to default values + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + */ +AST_INLINE_API( +void ast_alertpipe_clear(int alert_pipe[2]), +{ + alert_pipe[0] = alert_pipe[1] = -1; +} +) + +/*! + * \brief Determine if the alert pipe is readable + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \return non-zero if the alert pipe is readable, zero otherwise. + */ +AST_INLINE_API( +int attribute_pure ast_alertpipe_readable(int alert_pipe[2]), +{ + return alert_pipe[0] > -1; +} +) + +/*! + * \brief Determine if the alert pipe is writable + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \return non-zero if the alert pipe is writable, zero otherwise. + */ +AST_INLINE_API( +int attribute_pure ast_alertpipe_writable(int alert_pipe[2]), +{ + return alert_pipe[1] > -1; +} +) + +/*! + * \brief Get the alert pipe's read file descriptor + * \since 13.16.0 + * + * \param p a two-element array containing the alert pipe's file descriptors + * + * \return -1 if the file descriptor is not initialized, a non-negative value + * otherwise. + */ +AST_INLINE_API( +int attribute_pure ast_alertpipe_readfd(int alert_pipe[2]), +{ + return alert_pipe[0]; +} +) + +/*! + * \brief Swap the file descriptors from two alert pipes + * \since 13.16.0 + * + * \param p1 a two-element array containing an alert pipe's file descriptors + * \param p2 a two-element array containing an alert pipe's file descriptors + */ +AST_INLINE_API( +void ast_alertpipe_swap(int alert_pipe_1[2], int alert_pipe_2[2]), +{ + SWAP(alert_pipe_1[0], alert_pipe_2[0]); + SWAP(alert_pipe_1[1], alert_pipe_2[1]); +} +) + +#endif /* ASTERISK_ALERTPIPE_H */ diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in index 5f8a9d37c..b39386b43 100644 --- a/include/asterisk/autoconfig.h.in +++ b/include/asterisk/autoconfig.h.in @@ -225,6 +225,10 @@ /* Define to 1 if you have the `euidaccess' function. */ #undef HAVE_EUIDACCESS +/* Define to 1 if your system supports eventfd and the EFD_NONBLOCK and + EFD_SEMAPHORE flags. */ +#undef HAVE_EVENTFD + /* Define to 1 if you have the `exp' function. */ #undef HAVE_EXP diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h index 32c9c7f67..3e04b5d0b 100644 --- a/include/asterisk/channel.h +++ b/include/asterisk/channel.h @@ -123,6 +123,7 @@ References: #ifndef _ASTERISK_CHANNEL_H #define _ASTERISK_CHANNEL_H +#include "asterisk/alertpipe.h" #include "asterisk/abstract_jb.h" #include "asterisk/astobj2.h" #include "asterisk/poll-compat.h" @@ -4267,12 +4268,6 @@ struct ast_namedgroups *ast_channel_named_pickupgroups(const struct ast_channel void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value); /* Alertpipe accessors--the "internal" functions for channel.c use only */ -typedef enum { - AST_ALERT_READ_SUCCESS = 0, - AST_ALERT_NOT_READABLE, - AST_ALERT_READ_FAIL, - AST_ALERT_READ_FATAL, -} ast_alert_status_t; int ast_channel_alert_write(struct ast_channel *chan); int ast_channel_alert_writable(struct ast_channel *chan); ast_alert_status_t ast_channel_internal_alert_flush(struct ast_channel *chan); |