Taskprocessor optimization; switch Stasis to use taskprocessors

This patch optimizes taskprocessor to use a semaphore for signaling,
which the OS can do a better job at managing contention and waiting
that we can with a mutex and condition.

The taskprocessor execution was also slightly optimized to reduce the
number of locks taken.

The only observable difference in the taskprocessor implementation is
that when the final reference to the taskprocessor goes away, it will
execute all tasks to completion instead of discarding the unexecuted
tasks.

For systems where unnamed semaphores are not supported, a really
simple semaphore implementation is provided. (Which gives identical
performance as the original taskprocessor implementation).

The way we ended up implementing Stasis caused the threadpool to be a
burden instead of a boost to performance. This was switched to just
use taskprocessors directly for subscriptions.

Review: https://reviewboard.asterisk.org/r/2881/


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/12@400178 65c4cc65-6c06-0410-ace0-fbb531ad65f3
changes/78/78/1
David M. Lee 12 years ago
parent 52d73f05f4
commit e88afe2022

@ -1,8 +0,0 @@
[threadpool]
;initial_size = 0 ; Initial size of the threadpool
;idle_timeout_sec = 20 ; Number of seconds a thread should be idle before dying
; ; 0 means threads never time out
;max_size = 200 ; Maximum number of threads in the threadpool
; ; 0 means no limit to the threads in the threadpool

581
configure vendored

File diff suppressed because it is too large Load Diff

@ -808,11 +808,12 @@ AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE_NP], 1, [Define to 1 if your system defi
AC_MSG_RESULT(no)
)
AC_MSG_CHECKING(for pthread_rwlock_timedwrlock() in pthread.h)
save_LIBS="$LIBS"
save_CFLAGS="$CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"
CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
AC_MSG_CHECKING(for pthread_rwlock_timedwrlock() in pthread.h)
AC_LINK_IFELSE(
[AC_LANG_PROGRAM(
[#include <pthread.h>
@ -826,6 +827,17 @@ AC_LINK_IFELSE(
ac_cv_pthread_rwlock_timedwrlock="no"
]
)
# Some platforms define sem_init(), but only support sem_open(). joyous.
AC_MSG_CHECKING(for working unnamed semaphores)
AC_RUN_IFELSE(
[AC_LANG_PROGRAM([#include <semaphore.h>],
[sem_t sem; return sem_init(&sem, 0, 0);])],
AC_MSG_RESULT(yes)
AC_DEFINE([HAS_WORKING_SEMAPHORE], 1, [Define to 1 if anonymous semaphores work.]),
AC_MSG_RESULT(no)
)
LIBS="$save_LIBS"
CFLAGS="$save_CFLAGS"
if test "${ac_cv_pthread_rwlock_timedwrlock}" = "yes"; then

@ -29,6 +29,9 @@
/* Define to 1 if using `alloca.c'. */
#undef C_ALLOCA
/* Define to 1 if anonymous semaphores work. */
#undef HAS_WORKING_SEMAPHORE
/* Define to 1 if you have the `acos' function. */
#undef HAVE_ACOS

@ -0,0 +1,157 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* David M. Lee, II <dlee@digium.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_SEMAPHORE_H
#define ASTERISK_SEMAPHORE_H
/*!
* \file Asterisk semaphore API
*
* This API is a thin wrapper around the POSIX semaphore API (when available),
* so see the POSIX documentation for further details.
*/
#ifdef HAS_WORKING_SEMAPHORE
/* Working semaphore implementation detected */
#include <semaphore.h>
struct ast_sem {
sem_t real_sem;
};
#define AST_SEM_VALUE_MAX SEM_VALUE_MAX
/* These are thin wrappers; might as well inline them */
static force_inline int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
{
return sem_init(&sem->real_sem, pshared, value);
}
static force_inline int ast_sem_destroy(struct ast_sem *sem)
{
return sem_destroy(&sem->real_sem);
}
static force_inline int ast_sem_post(struct ast_sem *sem)
{
return sem_post(&sem->real_sem);
}
static force_inline int ast_sem_wait(struct ast_sem *sem)
{
return sem_wait(&sem->real_sem);
}
static force_inline int ast_sem_getvalue(struct ast_sem *sem, int *sval)
{
return sem_getvalue(&sem->real_sem, sval);
}
#else
/* Unnamed semaphores don't work. Rolling our own, I guess... */
#include "asterisk/lock.h"
#include <limits.h>
struct ast_sem {
/*! Current count of this semaphore */
int count;
/*! Number of threads currently waiting for this semaphore */
int waiters;
/*! Mutual exclusion */
ast_mutex_t mutex;
/*! Condition for singalling waiters */
ast_cond_t cond;
};
#define AST_SEM_VALUE_MAX INT_MAX
/*!
* \brief Initialize a semaphore.
*
* \param sem Semaphore to initialize.
* \param pshared Pass true (nonzero) to share this thread between processes.
* Not be supported on all platforms, so be wary!
* But leave the parameter, to be compatible with the POSIX ABI
* in case we need to add support in the future.
* \param value Initial value of the semaphore.
*
* \return 0 on success.
* \return -1 on error, errno set to indicate error.
*/
int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value);
/*!
* \brief Destroy a semaphore.
*
* Destroying a semaphore that other threads are currently blocked on produces
* undefined behavior.
*
* \param sem Semaphore to destroy.
*
* \return 0 on success.
* \return -1 on error, errno set to indicate error.
*/
int ast_sem_destroy(struct ast_sem *sem);
/*!
* \brief Increments the semaphore, unblocking a waiter if necessary.
*
* \param sem Semaphore to increment.
*
* \return 0 on success.
* \return -1 on error, errno set to indicate error.
*/
int ast_sem_post(struct ast_sem *sem);
/*!
* \brief Decrements the semaphore.
*
* If the semaphore's current value is zero, this function blocks until another
* thread posts (ast_sem_post()) to the semaphore (or is interrupted by a signal
* handler, which sets errno to EINTR).
*
* \param sem Semaphore to decrement.
*
* \return 0 on success.
* \return -1 on error, errno set to indicate error.
*/
int ast_sem_wait(struct ast_sem *sem);
/*!
* \brief Gets the current value of the semaphore.
*
* If threads are blocked on this semaphore, POSIX allows the return value to be
* either 0 or a negative number whose absolute value is the number of threads
* blocked. Don't assume that it will give you one or the other; Asterisk has
* been ported to just about everything.
*
* \param sem Semaphore to query.
* \param[out] sval Output value.
*
* \return 0 on success.
* \return -1 on error, errno set to indicate error.
*/
int ast_sem_getvalue(struct ast_sem *sem, int *sval);
#endif
#endif /* ASTERISK_SEMAPHORE_H */

@ -884,16 +884,6 @@ int stasis_config_init(void);
*/
int stasis_wait_init(void);
struct ast_threadpool_options;
/*!
* \internal
* \brief Retrieves the Stasis threadpool configuration.
* \param[out] threadpool_options Filled with Stasis threadpool options.
*/
void stasis_config_get_threadpool_options(
struct ast_threadpool_options *threadpool_options);
/*! @} */
/*!

@ -109,6 +109,7 @@ struct ast_taskprocessor_listener_callbacks {
* \param listener The listener
*/
void (*shutdown)(struct ast_taskprocessor_listener *listener);
void (*dtor)(struct ast_taskprocessor_listener *listener);
};
/*!

@ -0,0 +1,116 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* David M. Lee, II <dlee@digium.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.
*/
/*! \file
*
* \brief Asterisk semaphore support.
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/sem.h"
#include "asterisk/utils.h"
#ifndef HAS_WORKING_SEMAPHORE
/* DIY semaphores! */
int ast_sem_init(struct ast_sem *sem, int pshared, unsigned int value)
{
if (pshared) {
/* Don't need it... yet */
errno = ENOSYS;
return -1;
}
/* Since value is unsigned, this will also catch attempts to init with
* a negative value */
if (value > AST_SEM_VALUE_MAX) {
errno = EINVAL;
return -1;
}
sem->count = value;
sem->waiters = 0;
ast_mutex_init(&sem->mutex);
ast_cond_init(&sem->cond, NULL);
return 0;
}
int ast_sem_destroy(struct ast_sem *sem)
{
ast_mutex_destroy(&sem->mutex);
ast_cond_destroy(&sem->cond);
return 0;
}
int ast_sem_post(struct ast_sem *sem)
{
SCOPED_MUTEX(lock, &sem->mutex);
ast_assert(sem->count >= 0);
if (sem->count == AST_SEM_VALUE_MAX) {
errno = EOVERFLOW;
return -1;
}
/* Give it up! */
++sem->count;
/* Release a waiter, if needed */
if (sem->waiters) {
ast_cond_signal(&sem->cond);
}
return 0;
}
int ast_sem_wait(struct ast_sem *sem)
{
SCOPED_MUTEX(lock, &sem->mutex);
ast_assert(sem->count >= 0);
/* Wait for a non-zero count */
++sem->waiters;
while (sem->count == 0) {
ast_cond_wait(&sem->cond, &sem->mutex);
}
--sem->waiters;
/* Take it! */
--sem->count;
return 0;
}
int ast_sem_getvalue(struct ast_sem *sem, int *sval)
{
SCOPED_MUTEX(lock, &sem->mutex);
ast_assert(sem->count >= 0);
*sval = sem->count;
return 0;
}
#endif

@ -34,7 +34,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/astobj2.h"
#include "asterisk/stasis_internal.h"
#include "asterisk/stasis.h"
#include "asterisk/threadpool.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/utils.h"
#include "asterisk/uuid.h"
@ -134,9 +133,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
/*! The number of buckets to use for topic pools */
#define TOPIC_POOL_BUCKETS 57
/*! Threadpool for dispatching notifications to subscribers */
static struct ast_threadpool *pool;
STASIS_MESSAGE_TYPE_DEFN(stasis_subscription_change_type);
/*! \internal */
@ -286,7 +282,15 @@ struct stasis_subscription *internal_stasis_subscribe(
ast_uuid_generate_str(sub->uniqueid, sizeof(sub->uniqueid));
if (needs_mailbox) {
sub->mailbox = ast_threadpool_serializer(sub->uniqueid, pool);
/* With a small number of subscribers, a thread-per-sub is
* acceptable. If our usage changes so that we have larger
* numbers of subscribers, we'll probably want to consider
* a threadpool. We had that originally, but with so few
* subscribers it was actually a performance loss instead of
* a gain.
*/
sub->mailbox = ast_taskprocessor_get(sub->uniqueid,
TPS_REF_DEFAULT);
if (!sub->mailbox) {
return NULL;
}
@ -731,13 +735,6 @@ void stasis_log_bad_type_access(const char *name)
ast_log(LOG_ERROR, "Use of %s() before init/after destruction\n", name);
}
/*! \brief Shutdown function */
static void stasis_exit(void)
{
ast_threadpool_shutdown(pool);
pool = NULL;
}
/*! \brief Cleanup function for graceful shutdowns */
static void stasis_cleanup(void)
{
@ -748,36 +745,14 @@ int stasis_init(void)
{
int cache_init;
struct ast_threadpool_options opts;
/* Be sure the types are cleaned up after the message bus */
ast_register_cleanup(stasis_cleanup);
ast_register_atexit(stasis_exit);
if (stasis_config_init() != 0) {
ast_log(LOG_ERROR, "Stasis configuration failed\n");
return -1;
}
if (stasis_wait_init() != 0) {
ast_log(LOG_ERROR, "Stasis initialization failed\n");
return -1;
}
if (pool) {
ast_log(LOG_ERROR, "Stasis double-initialized\n");
return -1;
}
stasis_config_get_threadpool_options(&opts);
ast_debug(3, "Creating Stasis threadpool: initial_size = %d, max_size = %d, idle_timeout_secs = %d\n",
opts.initial_size, opts.max_size, opts.idle_timeout);
pool = ast_threadpool_create("stasis-core", NULL, &opts);
if (!pool) {
ast_log(LOG_ERROR, "Stasis threadpool allocation failed\n");
return -1;
}
cache_init = stasis_cache_init();
if (cache_init != 0) {
return -1;

@ -1,201 +0,0 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2013, Digium, Inc.
*
* David M. Lee, II <dlee@digium.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.
*/
/*! \file
*
* \brief Stasis Message Bus configuration API.
*
* \author David M. Lee, II <dlee@digium.com>
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/config_options.h"
#include "asterisk/stasis.h"
#include "asterisk/threadpool.h"
#include <limits.h>
/*** DOCUMENTATION
<configInfo name="stasis" language="en_US">
<synopsis>Stasis message bus configuration.</synopsis>
<configFile name="stasis.conf">
<configObject name="threadpool">
<synopsis>Threadpool configuration.</synopsis>
<configOption name="initial_size" default="0">
<synopsis>Initial number of threads in the message bus threadpool.</synopsis>
</configOption>
<configOption name="idle_timeout_sec" default="20">
<synopsis>Number of seconds for an idle thread to be disposed of.</synopsis>
</configOption>
<configOption name="max_size" default="200">
<synopsis>Maximum number of threads in the threadpool.</synopsis>
</configOption>
</configObject>
</configFile>
</configInfo>
***/
/*! \brief Locking container for safe configuration access. */
static AO2_GLOBAL_OBJ_STATIC(confs);
struct stasis_threadpool_conf {
int initial_size;
int idle_timeout_sec;
int max_size;
};
struct stasis_conf {
struct stasis_threadpool_conf *threadpool;
};
/*! \brief Mapping of the stasis conf struct's globals to the
* threadpool context in the config file. */
static struct aco_type threadpool_option = {
.type = ACO_GLOBAL,
.name = "threadpool",
.item_offset = offsetof(struct stasis_conf, threadpool),
.category = "^threadpool$",
.category_match = ACO_WHITELIST,
};
static struct aco_type *threadpool_options[] = ACO_TYPES(&threadpool_option);
#define CONF_FILENAME "stasis.conf"
/*! \brief The conf file that's processed for the module. */
static struct aco_file conf_file = {
/*! The config file name. */
.filename = CONF_FILENAME,
/*! The mapping object types to be processed. */
.types = ACO_TYPES(&threadpool_option),
};
static void conf_dtor(void *obj)
{
struct stasis_conf *conf = obj;
ao2_cleanup(conf->threadpool);
conf->threadpool = NULL;
}
static void *conf_alloc(void)
{
RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
conf = ao2_alloc_options(sizeof(*conf), conf_dtor,
AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!conf) {
return NULL;
}
conf->threadpool = ao2_alloc_options(sizeof(*conf->threadpool), NULL,
AO2_ALLOC_OPT_LOCK_NOLOCK);
if (!conf->threadpool) {
return NULL;
}
aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool);
ao2_ref(conf, +1);
return conf;
}
CONFIG_INFO_CORE("stasis", cfg_info, confs, conf_alloc,
.files = ACO_FILES(&conf_file));
void stasis_config_get_threadpool_options(
struct ast_threadpool_options *threadpool_options)
{
RAII_VAR(struct stasis_conf *, conf, NULL, ao2_cleanup);
conf = ao2_global_obj_ref(confs);
ast_assert(conf && conf->threadpool);
{
struct ast_threadpool_options newopts = {
.version = AST_THREADPOOL_OPTIONS_VERSION,
.initial_size = conf->threadpool->initial_size,
.auto_increment = 1,
.idle_timeout = conf->threadpool->idle_timeout_sec,
.max_size = conf->threadpool->max_size,
};
*threadpool_options = newopts;
}
}
/*! \brief Load (or reload) configuration. */
static int process_config(int reload)
{
RAII_VAR(struct stasis_conf *, conf, conf_alloc(), ao2_cleanup);
switch (aco_process_config(&cfg_info, reload)) {
case ACO_PROCESS_ERROR:
if (conf && !reload
&& !aco_set_defaults(&threadpool_option, "threadpool", conf->threadpool)) {
ast_log(AST_LOG_NOTICE, "Failed to process Stasis configuration; using defaults\n");
ao2_global_obj_replace_unref(confs, conf);
return 0;
}
return -1;
case ACO_PROCESS_OK:
case ACO_PROCESS_UNCHANGED:
break;
}
return 0;
}
static void config_exit(void)
{
aco_info_destroy(&cfg_info);
ao2_global_obj_release(confs);
}
int stasis_config_init(void)
{
if (aco_info_init(&cfg_info)) {
aco_info_destroy(&cfg_info);
return -1;
}
ast_register_atexit(config_exit);
/* threadpool section */
aco_option_register(&cfg_info, "initial_size", ACO_EXACT,
threadpool_options, "0", OPT_INT_T, PARSE_IN_RANGE,
FLDSET(struct stasis_threadpool_conf, initial_size), 0,
INT_MAX);
aco_option_register(&cfg_info, "idle_timeout_sec", ACO_EXACT,
threadpool_options, "20", OPT_INT_T, PARSE_IN_RANGE,
FLDSET(struct stasis_threadpool_conf, idle_timeout_sec), 0,
INT_MAX);
aco_option_register(&cfg_info, "max_size", ACO_EXACT,
threadpool_options, "200", OPT_INT_T, PARSE_IN_RANGE,
FLDSET(struct stasis_threadpool_conf, max_size), 0, INT_MAX);
return process_config(0);
}

@ -37,6 +37,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/astobj2.h"
#include "asterisk/cli.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/sem.h"
/*!
* \brief tps_task structure is queued to a taskprocessor
@ -113,9 +114,6 @@ static int tps_hash_cb(const void *obj, const int flags);
/*! \brief The astobj2 compare callback for taskprocessors */
static int tps_cmp_cb(void *obj, void *arg, int flags);
/*! \brief The task processing function executed by a taskprocessor */
static void *tps_processing_function(void *data);
/*! \brief Destroy the taskprocessor when its refcount reaches zero */
static void tps_taskprocessor_destroy(void *tps);
@ -138,47 +136,56 @@ static struct ast_cli_entry taskprocessor_clis[] = {
struct default_taskprocessor_listener_pvt {
pthread_t poll_thread;
ast_mutex_t lock;
ast_cond_t cond;
int wake_up;
int dead;
struct ast_sem sem;
};
static void default_tps_wake_up(struct default_taskprocessor_listener_pvt *pvt, int should_die)
static void default_listener_pvt_destroy(struct default_taskprocessor_listener_pvt *pvt)
{
SCOPED_MUTEX(lock, &pvt->lock);
pvt->wake_up = 1;
pvt->dead = should_die;
ast_cond_signal(&pvt->cond);
ast_assert(pvt->dead);
ast_sem_destroy(&pvt->sem);
ast_free(pvt);
}
static int default_tps_idle(struct default_taskprocessor_listener_pvt *pvt)
static void default_listener_pvt_dtor(struct ast_taskprocessor_listener *listener)
{
SCOPED_MUTEX(lock, &pvt->lock);
while (!pvt->wake_up) {
ast_cond_wait(&pvt->cond, lock);
}
pvt->wake_up = 0;
return pvt->dead;
struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
default_listener_pvt_destroy(pvt);
listener->user_data = NULL;
}
/*!
* \brief Function that processes tasks in the taskprocessor
* \internal
*/
static void *tps_processing_function(void *data)
static void *default_tps_processing_function(void *data)
{
struct ast_taskprocessor_listener *listener = data;
struct ast_taskprocessor *tps = listener->tps;
struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
int dead = 0;
while (!dead) {
if (!ast_taskprocessor_execute(tps)) {
dead = default_tps_idle(pvt);
int sem_value;
int res;
while (!pvt->dead) {
res = ast_sem_wait(&pvt->sem);
if (res != 0 && errno != EINTR) {
ast_log(LOG_ERROR, "ast_sem_wait(): %s\n",
strerror(errno));
/* Just give up */
break;
}
ast_taskprocessor_execute(tps);
}
/* No posting to a dead taskprocessor! */
res = ast_sem_getvalue(&pvt->sem, &sem_value);
ast_assert(res == 0 && sem_value == 0);
/* Free the shutdown reference (see default_listener_shutdown) */
ao2_t_ref(listener->tps, -1, "tps-shutdown");
return NULL;
}
@ -186,7 +193,7 @@ static int default_listener_start(struct ast_taskprocessor_listener *listener)
{
struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
if (ast_pthread_create(&pvt->poll_thread, NULL, tps_processing_function, listener)) {
if (ast_pthread_create(&pvt->poll_thread, NULL, default_tps_processing_function, listener)) {
return -1;
}
@ -197,33 +204,50 @@ static void default_task_pushed(struct ast_taskprocessor_listener *listener, int
{
struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
ast_assert(!pvt->dead);
if (was_empty) {
default_tps_wake_up(pvt, 0);
if (ast_sem_post(&pvt->sem) != 0) {
ast_log(LOG_ERROR, "Failed to notify of enqueued task: %s\n",
strerror(errno));
}
}
static void default_listener_pvt_destroy(struct default_taskprocessor_listener_pvt *pvt)
static int default_listener_die(void *data)
{
ast_mutex_destroy(&pvt->lock);
ast_cond_destroy(&pvt->cond);
ast_free(pvt);
struct default_taskprocessor_listener_pvt *pvt = data;
pvt->dead = 1;
return 0;
}
static void default_listener_shutdown(struct ast_taskprocessor_listener *listener)
{
struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
default_tps_wake_up(pvt, 1);
pthread_join(pvt->poll_thread, NULL);
int res;
/* Hold a reference during shutdown */
ao2_t_ref(listener->tps, +1, "tps-shutdown");
ast_taskprocessor_push(listener->tps, default_listener_die, pvt);
if (pthread_self() == pvt->poll_thread) {
res = pthread_detach(pvt->poll_thread);
if (res != 0) {
ast_log(LOG_ERROR, "pthread_detach(): %s\n",
strerror(errno));
}
} else {
res = pthread_join(pvt->poll_thread, NULL);
if (res != 0) {
ast_log(LOG_ERROR, "pthread_join(): %s\n",
strerror(errno));
}
}
pvt->poll_thread = AST_PTHREADT_NULL;
default_listener_pvt_destroy(pvt);
}
static const struct ast_taskprocessor_listener_callbacks default_listener_callbacks = {
.start = default_listener_start,
.task_pushed = default_task_pushed,
.shutdown = default_listener_shutdown,
.dtor = default_listener_pvt_dtor,
};
/*!
@ -268,9 +292,7 @@ static struct tps_task *tps_task_alloc(int (*task_exe)(void *datap), void *datap
/* release task resources */
static void *tps_task_free(struct tps_task *task)
{
if (task) {
ast_free(task);
}
ast_free(task);
return NULL;
}
@ -425,10 +447,8 @@ static void tps_taskprocessor_destroy(void *tps)
}
ast_debug(1, "destroying taskprocessor '%s'\n", t->name);
/* free it */
if (t->stats) {
ast_free(t->stats);
t->stats = NULL;
}
ast_free(t->stats);
t->stats = NULL;
ast_free((char *) t->name);
if (t->listener) {
ao2_ref(t->listener, -1);
@ -443,7 +463,6 @@ static void tps_taskprocessor_destroy(void *tps)
static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps)
{
struct tps_task *task;
SCOPED_AO2LOCK(lock, tps);
if ((task = AST_LIST_REMOVE_HEAD(&tps->tps_queue, list))) {
tps->tps_queue_size--;
@ -472,10 +491,21 @@ static void listener_shutdown(struct ast_taskprocessor_listener *listener)
ao2_ref(listener->tps, -1);
}
static void taskprocessor_listener_dtor(void *obj)
{
struct ast_taskprocessor_listener *listener = obj;
if (listener->callbacks->dtor) {
listener->callbacks->dtor(listener);
}
}
struct ast_taskprocessor_listener *ast_taskprocessor_listener_alloc(const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
{
RAII_VAR(struct ast_taskprocessor_listener *, listener,
ao2_alloc(sizeof(*listener), NULL), ao2_cleanup);
NULL, ao2_cleanup);
listener = ao2_alloc(sizeof(*listener), taskprocessor_listener_dtor);
if (!listener) {
return NULL;
@ -506,9 +536,12 @@ static void *default_listener_pvt_alloc(void)
if (!pvt) {
return NULL;
}
ast_cond_init(&pvt->cond, NULL);
ast_mutex_init(&pvt->lock);
pvt->poll_thread = AST_PTHREADT_NULL;
if (ast_sem_init(&pvt->sem, 0, 0) != 0) {
ast_log(LOG_ERROR, "ast_sem_init(): %s\n", strerror(errno));
ast_free(pvt);
return NULL;
}
return pvt;
}
@ -590,7 +623,6 @@ struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_o
p = __allocate_taskprocessor(name, listener);
if (!p) {
default_listener_pvt_destroy(pvt);
ao2_ref(listener, -1);
return NULL;
}
@ -662,15 +694,17 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
int size;
ao2_lock(tps);
t = tps_taskprocessor_pop(tps);
if (!t) {
ao2_unlock(tps);
return 0;
}
tps->executing = 1;
ao2_unlock(tps);
t = tps_taskprocessor_pop(tps);
if (t) {
t->execute(t->datap);
tps_task_free(t);
}
t->execute(t->datap);
tps_task_free(t);
ao2_lock(tps);
/* We need to check size in the same critical section where we reset the
@ -680,7 +714,7 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
tps->executing = 0;
size = tps_taskprocessor_depth(tps);
/* If we executed a task, bump the stats */
if (t && tps->stats) {
if (tps->stats) {
tps->stats->_tasks_processed_count++;
if (size > tps->stats->max_qsize) {
tps->stats->max_qsize = size;
@ -689,7 +723,7 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
ao2_unlock(tps);
/* If we executed a task, check for the transition to empty */
if (t && size == 0 && tps->listener->callbacks->emptied) {
if (size == 0 && tps->listener->callbacks->emptied) {
tps->listener->callbacks->emptied(tps->listener);
}
return size > 0;

Loading…
Cancel
Save