From 8760e32ae380f65420799c290f7b92d40cd7926c Mon Sep 17 00:00:00 2001 From: Mark Michelson Date: Tue, 11 Dec 2012 16:34:00 +0000 Subject: Add auto-increment option and accompanying test. This allows for the threadpool to automatically grow if tasks are pushed to it and no idle threads are currently available. git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@377803 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- main/threadpool.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'main/threadpool.c') diff --git a/main/threadpool.c b/main/threadpool.c index 57520cf11..b3195fc4c 100644 --- a/main/threadpool.c +++ b/main/threadpool.c @@ -416,7 +416,7 @@ static struct task_pushed_data *task_pushed_data_alloc(struct ast_threadpool *po /*! * \brief Activate idle threads * - * This function always returns CMP_MATCH because all threads that this + * This function always returns CMP_MATCH because all workers that this * function acts on need to be seen as matches so they are unlinked from the * list of idle threads. * @@ -425,7 +425,7 @@ static struct task_pushed_data *task_pushed_data_alloc(struct ast_threadpool *po * \param arg The pool where the worker belongs * \retval CMP_MATCH */ -static int activate_threads(void *obj, void *arg, int flags) +static int activate_thread(void *obj, void *arg, int flags) { struct worker_thread *worker = obj; struct ast_threadpool *pool = arg; @@ -435,6 +435,8 @@ static int activate_threads(void *obj, void *arg, int flags) return CMP_MATCH; } +static void grow(struct ast_threadpool *pool, int delta); + /*! * \brief Queued task called when tasks are pushed into the threadpool * @@ -451,8 +453,13 @@ static int queued_task_pushed(void *data) int was_empty = tpd->was_empty; pool->listener->callbacks->task_pushed(pool, pool->listener, was_empty); - ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA | OBJ_MULTIPLE, - activate_threads, pool); + if (ao2_container_count(pool->idle_threads) == 0 && pool->options.auto_increment > 0) { + grow(pool, pool->options.auto_increment); + } else { + ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NOLOCK | OBJ_NODATA, + activate_thread, pool); + } + threadpool_send_state_changed(pool); ao2_ref(tpd, -1); return 0; } -- cgit v1.2.3