ARI: Add ability to raise arbitrary User Events
[asterisk/asterisk.git] / main / taskprocessor.c
index 8b065e4..0582f76 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Asterisk -- An open source telephony toolkit.
  *
- * Copyright (C) 2007-2008, Dwayne M. Hubbard 
+ * Copyright (C) 2007-2013, Digium, Inc.
  *
  * Dwayne M. Hubbard <dhubbard@digium.com>
  *
  * the GNU General Public License Version 2. See the LICENSE file
  * at the top of the source tree.
  */
-/*! \file
- *
+
+/*!
+ * \file
  * \brief Maintain a container of uniquely-named taskprocessor threads that can be shared across modules.
  *
  * \author Dwayne Hubbard <dhubbard@digium.com>
  */
 
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
 #include "asterisk.h"
+
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
 #include "asterisk/_private.h"
@@ -31,22 +37,26 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/astobj2.h"
 #include "asterisk/cli.h"
 #include "asterisk/taskprocessor.h"
-#include "signal.h"
-#include "sys/time.h"
+#include "asterisk/sem.h"
 
-
-/*! \brief tps_task structure is queued to a taskprocessor
+/*!
+ * \brief tps_task structure is queued to a taskprocessor
  *
  * tps_tasks are processed in FIFO order and freed by the taskprocessing
  * thread after the task handler returns.  The callback function that is assigned
- * to the execute() function pointer is responsible for releasing datap resources if necessary. */
+ * to the execute() function pointer is responsible for releasing datap resources if necessary.
+ */
 struct tps_task {
        /*! \brief The execute() task callback function pointer */
-       int (*execute)(void *datap);
+       union {
+               int (*execute)(void *datap);
+               int (*execute_local)(struct ast_taskprocessor_local *local);
+       } callback;
        /*! \brief The data pointer for the task execute() function */
        void *datap;
        /*! \brief AST_LIST_ENTRY overhead */
        AST_LIST_ENTRY(tps_task) list;
+       unsigned int wants_local:1;
 };
 
 /*! \brief tps_taskprocessor_stats maintain statistics for a taskprocessor. */
@@ -60,45 +70,59 @@ struct tps_taskprocessor_stats {
 /*! \brief A ast_taskprocessor structure is a singleton by name */
 struct ast_taskprocessor {
        /*! \brief Friendly name of the taskprocessor */
-       char *name;
-       /*! \brief Thread poll condition */
-       ast_cond_t poll_cond;
-       /*! \brief Taskprocessor thread */
-       pthread_t poll_thread;
-       /*! \brief Taskprocessor lock */
-       ast_mutex_t taskprocessor_lock;
-       /*! \brief Taskprocesor thread run flag */
-       unsigned char poll_thread_run;
+       const char *name;
        /*! \brief Taskprocessor statistics */
        struct tps_taskprocessor_stats *stats;
+       void *local_data;
        /*! \brief Taskprocessor current queue size */
-       long queue_size;
+       long tps_queue_size;
        /*! \brief Taskprocessor queue */
-       AST_LIST_HEAD_NOLOCK(queue, tps_task) queue;
+       AST_LIST_HEAD_NOLOCK(tps_queue, tps_task) tps_queue;
        /*! \brief Taskprocessor singleton list entry */
        AST_LIST_ENTRY(ast_taskprocessor) list;
+       struct ast_taskprocessor_listener *listener;
+       /*! Indicates if the taskprocessor is currently executing a task */
+       unsigned int executing:1;
 };
+
+/*!
+ * \brief A listener for taskprocessors
+ *
+ * \since 12.0.0
+ *
+ * When a taskprocessor's state changes, the listener
+ * is notified of the change. This allows for tasks
+ * to be addressed in whatever way is appropriate for
+ * the module using the taskprocessor.
+ */
+struct ast_taskprocessor_listener {
+       /*! The callbacks the taskprocessor calls into to notify of state changes */
+       const struct ast_taskprocessor_listener_callbacks *callbacks;
+       /*! The taskprocessor that the listener is listening to */
+       struct ast_taskprocessor *tps;
+       /*! Data private to the listener */
+       void *user_data;
+};
+
 #define TPS_MAX_BUCKETS 7
 /*! \brief tps_singletons is the astobj2 container for taskprocessor singletons */
 static struct ao2_container *tps_singletons;
 
-/*! \brief CLI 'taskprocessor ping <blah>' operation requires a ping condition */
+/*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> operation requires a ping condition */
 static ast_cond_t cli_ping_cond;
-/*! \brief CLI 'taskprocessor ping <blah>' operation requires a ping condition lock */
-static ast_mutex_t cli_ping_cond_lock;
+
+/*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> operation requires a ping condition lock */
+AST_MUTEX_DEFINE_STATIC(cli_ping_cond_lock);
 
 /*! \brief The astobj2 hash callback for taskprocessors */
 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);
 
-/*! \brief CLI 'taskprocessor ping <blah>' handler function */
+/*! \brief CLI <example>taskprocessor ping &lt;blah&gt;</example> handler function */
 static int tps_ping_handler(void *datap);
 
 /*! \brief Remove the front task off the taskprocessor queue */
@@ -111,10 +135,137 @@ static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args
 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
 
 static struct ast_cli_entry taskprocessor_clis[] = {
-       AST_CLI_DEFINE(cli_tps_ping, "Ping a named task processors"),
+       AST_CLI_DEFINE(cli_tps_ping, "Ping a named task processor"),
        AST_CLI_DEFINE(cli_tps_report, "List instantiated task processors and statistics"),
 };
 
+struct default_taskprocessor_listener_pvt {
+       pthread_t poll_thread;
+       int dead;
+       struct ast_sem sem;
+};
+
+static void default_listener_pvt_destroy(struct default_taskprocessor_listener_pvt *pvt)
+{
+       ast_assert(pvt->dead);
+       ast_sem_destroy(&pvt->sem);
+       ast_free(pvt);
+}
+
+static void default_listener_pvt_dtor(struct ast_taskprocessor_listener *listener)
+{
+       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 *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 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;
+}
+
+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, default_tps_processing_function, listener)) {
+               return -1;
+       }
+
+       return 0;
+}
+
+static void default_task_pushed(struct ast_taskprocessor_listener *listener, int was_empty)
+{
+       struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
+
+       if (ast_sem_post(&pvt->sem) != 0) {
+               ast_log(LOG_ERROR, "Failed to notify of enqueued task: %s\n",
+                       strerror(errno));
+       }
+}
+
+static int default_listener_die(void *data)
+{
+       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;
+       int res;
+
+       /* Hold a reference during shutdown */
+       ao2_t_ref(listener->tps, +1, "tps-shutdown");
+
+       ast_taskprocessor_push(listener->tps, default_listener_die, pvt);
+
+       ast_assert(pvt->poll_thread != AST_PTHREADT_NULL);
+
+       if (pthread_equal(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;
+}
+
+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,
+};
+
+/*!
+ * \internal
+ * \brief Clean up resources on Asterisk shutdown
+ */
+static void tps_shutdown(void)
+{
+       ast_cli_unregister_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
+       ao2_t_ref(tps_singletons, -1, "Unref tps_singletons in shutdown");
+       tps_singletons = NULL;
+}
+
 /* initialize the taskprocessor container and register CLI operations */
 int ast_tps_init(void)
 {
@@ -122,7 +273,13 @@ int ast_tps_init(void)
                ast_log(LOG_ERROR, "taskprocessor container failed to initialize!\n");
                return -1;
        }
+
+       ast_cond_init(&cli_ping_cond, NULL);
+
        ast_cli_register_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
+
+       ast_register_atexit(tps_shutdown);
+
        return 0;
 }
 
@@ -130,31 +287,60 @@ int ast_tps_init(void)
 static struct tps_task *tps_task_alloc(int (*task_exe)(void *datap), void *datap)
 {
        struct tps_task *t;
-       if ((t = ast_calloc(1, sizeof(*t)))) {
-               t->execute = task_exe;
-               t->datap = datap;
+       if (!task_exe) {
+               ast_log(LOG_ERROR, "task_exe is NULL!\n");
+               return NULL;
        }
+
+       t = ast_calloc(1, sizeof(*t));
+       if (!t) {
+               ast_log(LOG_ERROR, "failed to allocate task!\n");
+               return NULL;
+       }
+
+       t->callback.execute = task_exe;
+       t->datap = datap;
+
        return t;
 }
 
-/* release task resources */   
-static void *tps_task_free(struct tps_task *task)
+static struct tps_task *tps_task_alloc_local(int (*task_exe)(struct ast_taskprocessor_local *local), void *datap)
 {
-       if (task) {
-               ast_free(task);
+       struct tps_task *t;
+       if (!task_exe) {
+               ast_log(LOG_ERROR, "task_exe is NULL!\n");
+               return NULL;
        }
+
+       t = ast_calloc(1, sizeof(*t));
+       if (!t) {
+               ast_log(LOG_ERROR, "failed to allocate task!\n");
+               return NULL;
+       }
+
+       t->callback.execute_local = task_exe;
+       t->datap = datap;
+       t->wants_local = 1;
+
+       return t;
+}
+
+/* release task resources */
+static void *tps_task_free(struct tps_task *task)
+{
+       ast_free(task);
        return NULL;
 }
 
 /* taskprocessor tab completion */
-static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct ast_cli_args *a) 
+static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct ast_cli_args *a)
 {
        int tklen;
        int wordnum = 0;
        char *name = NULL;
        struct ao2_iterator i;
 
-       if (a->pos != 2)
+       if (a->pos != 3)
                return NULL;
 
        tklen = strlen(a->word);
@@ -167,6 +353,7 @@ static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct
                }
                ao2_ref(p, -1);
        }
+       ao2_iterator_destroy(&i);
        return name;
 }
 
@@ -183,34 +370,34 @@ static int tps_ping_handler(void *datap)
 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct timeval begin, end, delta;
-       char *name;
-       struct timeval tv;
+       const char *name;
+       struct timeval when;
        struct timespec ts;
        struct ast_taskprocessor *tps = NULL;
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "taskprocessor ping";
-               e->usage = 
-                       "Usage: taskprocessor ping <taskprocessor>\n"
-                       "       Displays the time required for a processor to deliver a task\n";
+               e->command = "core ping taskprocessor";
+               e->usage =
+                       "Usage: core ping taskprocessor <taskprocessor>\n"
+                       "       Displays the time required for a task to be processed\n";
                return NULL;
        case CLI_GENERATE:
                return tps_taskprocessor_tab_complete(tps, a);
        }
 
-       if (a->argc != 3)
+       if (a->argc != 4)
                return CLI_SHOWUSAGE;
 
-       name = a->argv[2];
+       name = a->argv[3];
        if (!(tps = ast_taskprocessor_get(name, TPS_REF_IF_EXISTS))) {
                ast_cli(a->fd, "\nping failed: %s not found\n\n", name);
                return CLI_SUCCESS;
        }
        ast_cli(a->fd, "\npinging %s ...", name);
-       tv = ast_tvadd((begin = ast_tvnow()), ast_samp2tv(1000, 1000));
-       ts.tv_sec = tv.tv_sec;
-       ts.tv_nsec = tv.tv_usec * 1000;
+       when = ast_tvadd((begin = ast_tvnow()), ast_samp2tv(1000, 1000));
+       ts.tv_sec = when.tv_sec;
+       ts.tv_nsec = when.tv_usec * 1000;
        ast_mutex_lock(&cli_ping_cond_lock);
        if (ast_taskprocessor_push(tps, tps_ping_handler, 0) < 0) {
                ast_cli(a->fd, "\nping failed: could not push task to %s\n\n", name);
@@ -223,10 +410,9 @@ static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args
        delta = ast_tvsub(end, begin);
        ast_cli(a->fd, "\n\t%24s ping time: %.1ld.%.6ld sec\n\n", name, (long)delta.tv_sec, (long int)delta.tv_usec);
        ao2_ref(tps, -1);
-       return CLI_SUCCESS;     
+       return CLI_SUCCESS;
 }
 
-/* TPS reports are cool */
 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        char name[256];
@@ -239,13 +425,13 @@ static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "taskprocessor show stats";
-               e->usage = 
-                       "Usage: taskprocessor show stats\n"
+               e->command = "core show taskprocessors";
+               e->usage =
+                       "Usage: core show taskprocessors\n"
                        "       Shows a list of instantiated task processors and their statistics\n";
                return NULL;
        case CLI_GENERATE:
-               return NULL;    
+               return NULL;
        }
 
        if (a->argc != e->args)
@@ -255,113 +441,58 @@ static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
        i = ao2_iterator_init(tps_singletons, 0);
        while ((p = ao2_iterator_next(&i))) {
                ast_copy_string(name, p->name, sizeof(name));
-               qsize = p->queue_size;
+               qsize = p->tps_queue_size;
                maxqsize = p->stats->max_qsize;
                processed = p->stats->_tasks_processed_count;
-               ast_cli(a->fd, "\n%24s   %17ld %12ld %12ld", name, processed, qsize, maxqsize);
+               ast_cli(a->fd, "\n%24s   %17lu %12lu %12lu", name, processed, qsize, maxqsize);
                ao2_ref(p, -1);
        }
-       tcount = ao2_container_count(tps_singletons); 
+       ao2_iterator_destroy(&i);
+       tcount = ao2_container_count(tps_singletons);
        ast_cli(a->fd, "\n\t+---------------------+-----------------+------------+-------------+\n\t%d taskprocessors\n\n", tcount);
-       return CLI_SUCCESS;     
-}
-
-/* this is the task processing worker function */
-static void *tps_processing_function(void *data)
-{
-       struct ast_taskprocessor *i = data;
-       struct tps_task *t;
-       int size;
-
-       if (!i) {
-               ast_log(LOG_ERROR, "cannot start thread_function loop without a ast_taskprocessor structure.\n");
-               return NULL;
-       }
-
-       while (i->poll_thread_run) {
-               ast_mutex_lock(&i->taskprocessor_lock);
-               if (!i->poll_thread_run) {
-                       ast_mutex_unlock(&i->taskprocessor_lock);
-                       break;
-               }
-               if (!(size = tps_taskprocessor_depth(i))) {
-                       ast_cond_wait(&i->poll_cond, &i->taskprocessor_lock);
-                       if (!i->poll_thread_run) {
-                               ast_mutex_unlock(&i->taskprocessor_lock);
-                               break;
-                       }
-               }
-               ast_mutex_unlock(&i->taskprocessor_lock);
-               /* stuff is in the queue */
-               if (!(t = tps_taskprocessor_pop(i))) {
-                       ast_log(LOG_ERROR, "Wtf?? %d tasks in the queue, but we're popping blanks!\n", size);
-                       continue;
-               }
-               if (!t->execute) {
-                       ast_log(LOG_WARNING, "Task is missing a function to execute!\n");
-                       tps_task_free(t);
-                       continue;
-               }
-               t->execute(t->datap);
-               ast_mutex_lock(&i->taskprocessor_lock);
-               if (i->stats) {
-                       i->stats->_tasks_processed_count++;
-                       if (size > i->stats->max_qsize) {
-                               i->stats->max_qsize = size;
-                       }
-               }
-               ast_mutex_unlock(&i->taskprocessor_lock);
-               tps_task_free(t);
-       }
-       while ((t = tps_taskprocessor_pop(i))) {
-               tps_task_free(t);
-       }
-       return NULL;
+       return CLI_SUCCESS;
 }
 
 /* hash callback for astobj2 */
 static int tps_hash_cb(const void *obj, const int flags)
 {
        const struct ast_taskprocessor *tps = obj;
+       const char *name = flags & OBJ_KEY ? obj : tps->name;
 
-       return ast_str_hash(tps->name);
+       return ast_str_case_hash(name);
 }
 
 /* compare callback for astobj2 */
 static int tps_cmp_cb(void *obj, void *arg, int flags)
 {
        struct ast_taskprocessor *lhs = obj, *rhs = arg;
+       const char *rhsname = flags & OBJ_KEY ? arg : rhs->name;
 
-       return !strcasecmp(lhs->name, rhs->name) ? CMP_MATCH : 0;
+       return !strcasecmp(lhs->name, rhsname) ? CMP_MATCH | CMP_STOP : 0;
 }
 
 /* destroy the taskprocessor */
 static void tps_taskprocessor_destroy(void *tps)
 {
        struct ast_taskprocessor *t = tps;
-       
+       struct tps_task *task;
+
        if (!tps) {
                ast_log(LOG_ERROR, "missing taskprocessor\n");
                return;
        }
-       ast_log(LOG_DEBUG, "destroying taskprocessor '%s'\n", t->name);
-       /* kill it */   
-       ast_mutex_lock(&t->taskprocessor_lock);
-       t->poll_thread_run = 0;
-       ast_cond_signal(&t->poll_cond);
-       ast_mutex_unlock(&t->taskprocessor_lock);
-       pthread_join(t->poll_thread, NULL);
-       t->poll_thread = AST_PTHREADT_NULL;
-       ast_mutex_destroy(&t->taskprocessor_lock);
-       ast_cond_destroy(&t->poll_cond);
+       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);
+               t->listener = NULL;
+       }
+       while ((task = AST_LIST_REMOVE_HEAD(&t->tps_queue, list))) {
+               tps_task_free(task);
        }
-       ast_free(t->name);
 }
 
 /* pop the front task and return it */
@@ -369,21 +500,15 @@ static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps)
 {
        struct tps_task *task;
 
-       if (!tps) {
-               ast_log(LOG_ERROR, "missing taskprocessor\n");
-               return NULL;
-       }
-       ast_mutex_lock(&tps->taskprocessor_lock);
-       if ((task = AST_LIST_REMOVE_HEAD(&tps->queue, list))) {
-               tps->queue_size--;
+       if ((task = AST_LIST_REMOVE_HEAD(&tps->tps_queue, list))) {
+               tps->tps_queue_size--;
        }
-       ast_mutex_unlock(&tps->taskprocessor_lock);
        return task;
 }
 
 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps)
 {
-       return (tps) ? tps->queue_size : -1;
+       return (tps) ? tps->tps_queue_size : -1;
 }
 
 /* taskprocessor name accessor */
@@ -396,101 +521,271 @@ const char *ast_taskprocessor_name(struct ast_taskprocessor *tps)
        return tps->name;
 }
 
-/* Provide a reference to a taskprocessor.  Create the taskprocessor if necessary, but don't
- * create the taskprocessor if we were told via ast_tps_options to return a reference only 
- * if it already exists */
-struct ast_taskprocessor *ast_taskprocessor_get(char *name, enum ast_tps_options create)
+static void listener_shutdown(struct ast_taskprocessor_listener *listener)
 {
-       struct ast_taskprocessor *p, tmp_tps = {
-               .name = name,
-       };
-               
-       if (ast_strlen_zero(name)) {
-               ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
+       listener->callbacks->shutdown(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)
+{
+       struct ast_taskprocessor_listener *listener;
+
+       listener = ao2_alloc(sizeof(*listener), taskprocessor_listener_dtor);
+       if (!listener) {
                return NULL;
        }
-       ao2_lock(tps_singletons);
-       p = ao2_find(tps_singletons, &tmp_tps, OBJ_POINTER);
-       if (p) {
-               ao2_unlock(tps_singletons);
-               return p;
+       listener->callbacks = callbacks;
+       listener->user_data = user_data;
+
+       return listener;
+}
+
+struct ast_taskprocessor *ast_taskprocessor_listener_get_tps(const struct ast_taskprocessor_listener *listener)
+{
+       ao2_ref(listener->tps, +1);
+       return listener->tps;
+}
+
+void *ast_taskprocessor_listener_get_user_data(const struct ast_taskprocessor_listener *listener)
+{
+       return listener->user_data;
+}
+
+static void *default_listener_pvt_alloc(void)
+{
+       struct default_taskprocessor_listener_pvt *pvt;
+
+       pvt = ast_calloc(1, sizeof(*pvt));
+       if (!pvt) {
+               return NULL;
        }
-       if (create & TPS_REF_IF_EXISTS) {
-               /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
-               ao2_unlock(tps_singletons);
+       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;
        }
-       /* create a new taskprocessor */
-       if (!(p = ao2_alloc(sizeof(*p), tps_taskprocessor_destroy))) {
-               ao2_unlock(tps_singletons);
+       return pvt;
+}
+
+static struct ast_taskprocessor *__allocate_taskprocessor(const char *name, struct ast_taskprocessor_listener *listener)
+{
+       RAII_VAR(struct ast_taskprocessor *, p,
+                       ao2_alloc(sizeof(*p), tps_taskprocessor_destroy), ao2_cleanup);
+
+       if (!p) {
                ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
                return NULL;
        }
 
-       ast_cond_init(&p->poll_cond, NULL);
-       ast_mutex_init(&p->taskprocessor_lock);
-
        if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
-               ao2_unlock(tps_singletons);
                ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
-               ao2_ref(p, -1);
                return NULL;
        }
        if (!(p->name = ast_strdup(name))) {
-               ao2_unlock(tps_singletons);
-               ao2_ref(p, -1);
-               return NULL;
-       }
-       p->poll_thread_run = 1;
-       p->poll_thread = AST_PTHREADT_NULL;
-       if (ast_pthread_create(&p->poll_thread, NULL, tps_processing_function, p) < 0) {
-               ao2_unlock(tps_singletons);
-               ast_log(LOG_ERROR, "Taskprocessor '%s' failed to create the processing thread.\n", p->name);
                ao2_ref(p, -1);
                return NULL;
        }
+
+       ao2_ref(listener, +1);
+       p->listener = listener;
+
+       ao2_ref(p, +1);
+       listener->tps = p;
+
        if (!(ao2_link(tps_singletons, p))) {
-               ao2_unlock(tps_singletons);
                ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
-               ao2_ref(p, -1);
                return NULL;
        }
-       ao2_unlock(tps_singletons);
+
+       if (p->listener->callbacks->start(p->listener)) {
+               ast_log(LOG_ERROR, "Unable to start taskprocessor listener for taskprocessor %s\n", p->name);
+               ast_taskprocessor_unreference(p);
+               return NULL;
+       }
+
+       /* RAII_VAR will decrement the refcount at the end of the function.
+        * Since we want to pass back a reference to p, we bump the refcount
+        */
+       ao2_ref(p, +1);
+       return p;
+
+}
+
+/* Provide a reference to a taskprocessor.  Create the taskprocessor if necessary, but don't
+ * create the taskprocessor if we were told via ast_tps_options to return a reference only
+ * if it already exists */
+struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_options create)
+{
+       struct ast_taskprocessor *p;
+       struct ast_taskprocessor_listener *listener;
+       struct default_taskprocessor_listener_pvt *pvt;
+
+       if (ast_strlen_zero(name)) {
+               ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
+               return NULL;
+       }
+       p = ao2_find(tps_singletons, name, OBJ_KEY);
+       if (p) {
+               return p;
+       }
+       if (create & TPS_REF_IF_EXISTS) {
+               /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
+               return NULL;
+       }
+       /* Create a new taskprocessor. Start by creating a default listener */
+       pvt = default_listener_pvt_alloc();
+       if (!pvt) {
+               return NULL;
+       }
+       listener = ast_taskprocessor_listener_alloc(&default_listener_callbacks, pvt);
+       if (!listener) {
+               default_listener_pvt_destroy(pvt);
+               return NULL;
+       }
+
+       p = __allocate_taskprocessor(name, listener);
+       if (!p) {
+               ao2_ref(listener, -1);
+               return NULL;
+       }
+
+       /* Unref listener here since the taskprocessor has gained a reference to the listener */
+       ao2_ref(listener, -1);
        return p;
 }
 
+struct ast_taskprocessor *ast_taskprocessor_create_with_listener(const char *name, struct ast_taskprocessor_listener *listener)
+{
+       struct ast_taskprocessor *p = ao2_find(tps_singletons, name, OBJ_KEY);
+
+       if (p) {
+               ast_taskprocessor_unreference(p);
+               return NULL;
+       }
+       return __allocate_taskprocessor(name, listener);
+}
+
+void ast_taskprocessor_set_local(struct ast_taskprocessor *tps,
+       void *local_data)
+{
+       SCOPED_AO2LOCK(lock, tps);
+       tps->local_data = local_data;
+}
+
 /* decrement the taskprocessor reference count and unlink from the container if necessary */
 void *ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
 {
-       if (tps) {
-               ao2_lock(tps_singletons);
-               ao2_unlink(tps_singletons, tps);
-               if (ao2_ref(tps, -1) > 1) {
-                       ao2_link(tps_singletons, tps);
-               }
-               ao2_unlock(tps_singletons);
+       if (!tps) {
+               return NULL;
        }
+
+       if (ao2_ref(tps, -1) > 3) {
+               return NULL;
+       }
+       /* If we're down to 3 references, then those must be:
+        * 1. The reference we just got rid of
+        * 2. The container
+        * 3. The listener
+        */
+       ao2_unlink(tps_singletons, tps);
+       listener_shutdown(tps->listener);
        return NULL;
 }
 
-/* push the task into the taskprocessor queue */       
-int ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap)
+/* push the task into the taskprocessor queue */
+static int taskprocessor_push(struct ast_taskprocessor *tps, struct tps_task *t)
 {
-       struct tps_task *t;
+       int previous_size;
+       int was_empty;
 
-       if (!tps || !task_exe) {
-               ast_log(LOG_ERROR, "%s is missing!!\n", (tps) ? "task callback" : "taskprocessor");
+       if (!tps) {
+               ast_log(LOG_ERROR, "tps is NULL!\n");
                return -1;
        }
-       if (!(t = tps_task_alloc(task_exe, datap))) {
-               ast_log(LOG_ERROR, "failed to allocate task!  Can't push to '%s'\n", tps->name);
+
+       if (!t) {
+               ast_log(LOG_ERROR, "t is NULL!\n");
                return -1;
        }
-       ast_mutex_lock(&tps->taskprocessor_lock);
-       AST_LIST_INSERT_TAIL(&tps->queue, t, list);
-       tps->queue_size++;
-       ast_cond_signal(&tps->poll_cond);
-       ast_mutex_unlock(&tps->taskprocessor_lock);
+
+       ao2_lock(tps);
+       AST_LIST_INSERT_TAIL(&tps->tps_queue, t, list);
+       previous_size = tps->tps_queue_size++;
+       /* The currently executing task counts as still in queue */
+       was_empty = tps->executing ? 0 : previous_size == 0;
+       ao2_unlock(tps);
+       tps->listener->callbacks->task_pushed(tps->listener, was_empty);
        return 0;
 }
 
+int ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap)
+{
+       return taskprocessor_push(tps, tps_task_alloc(task_exe, datap));
+}
+
+int ast_taskprocessor_push_local(struct ast_taskprocessor *tps, int (*task_exe)(struct ast_taskprocessor_local *datap), void *datap)
+{
+       return taskprocessor_push(tps, tps_task_alloc_local(task_exe, datap));
+}
+
+int ast_taskprocessor_execute(struct ast_taskprocessor *tps)
+{
+       struct ast_taskprocessor_local local;
+       struct tps_task *t;
+       int size;
+
+       ao2_lock(tps);
+       t = tps_taskprocessor_pop(tps);
+       if (!t) {
+               ao2_unlock(tps);
+               return 0;
+       }
+
+       tps->executing = 1;
+
+       if (t->wants_local) {
+               local.local_data = tps->local_data;
+               local.data = t->datap;
+       }
+       ao2_unlock(tps);
+
+       if (t->wants_local) {
+               t->callback.execute_local(&local);
+       } else {
+               t->callback.execute(t->datap);
+       }
+       tps_task_free(t);
+
+       ao2_lock(tps);
+       /* We need to check size in the same critical section where we reset the
+        * executing bit. Avoids a race condition where a task is pushed right
+        * after we pop an empty stack.
+        */
+       tps->executing = 0;
+       size = tps_taskprocessor_depth(tps);
+       /* If we executed a task, bump the stats */
+       if (tps->stats) {
+               tps->stats->_tasks_processed_count++;
+               if (size > tps->stats->max_qsize) {
+                       tps->stats->max_qsize = size;
+               }
+       }
+       ao2_unlock(tps);
+
+       /* If we executed a task, check for the transition to empty */
+       if (size == 0 && tps->listener->callbacks->emptied) {
+               tps->listener->callbacks->emptied(tps->listener);
+       }
+       return size > 0;
+}