2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007-2008, Digium, Inc.
6 * Dwayne M. Hubbard <dhubbard@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Maintain a container of uniquely-named taskprocessor threads that can be shared across modules.
23 * \author Dwayne Hubbard <dhubbard@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
35 #include "asterisk/module.h"
36 #include "asterisk/time.h"
37 #include "asterisk/astobj2.h"
38 #include "asterisk/cli.h"
39 #include "asterisk/taskprocessor.h"
43 * \brief tps_task structure is queued to a taskprocessor
45 * tps_tasks are processed in FIFO order and freed by the taskprocessing
46 * thread after the task handler returns. The callback function that is assigned
47 * to the execute() function pointer is responsible for releasing datap resources if necessary.
50 /*! \brief The execute() task callback function pointer */
51 int (*execute)(void *datap);
52 /*! \brief The data pointer for the task execute() function */
54 /*! \brief AST_LIST_ENTRY overhead */
55 AST_LIST_ENTRY(tps_task) list;
58 /*! \brief tps_taskprocessor_stats maintain statistics for a taskprocessor. */
59 struct tps_taskprocessor_stats {
60 /*! \brief This is the maximum number of tasks queued at any one time */
61 unsigned long max_qsize;
62 /*! \brief This is the current number of tasks processed */
63 unsigned long _tasks_processed_count;
66 /*! \brief A ast_taskprocessor structure is a singleton by name */
67 struct ast_taskprocessor {
68 /*! \brief Friendly name of the taskprocessor */
70 /*! \brief Thread poll condition */
72 /*! \brief Taskprocessor thread */
73 pthread_t poll_thread;
74 /*! \brief Taskprocessor lock */
75 ast_mutex_t taskprocessor_lock;
76 /*! \brief Taskprocesor thread run flag */
77 unsigned char poll_thread_run;
78 /*! \brief Taskprocessor statistics */
79 struct tps_taskprocessor_stats *stats;
80 /*! \brief Taskprocessor current queue size */
82 /*! \brief Taskprocessor queue */
83 AST_LIST_HEAD_NOLOCK(tps_queue, tps_task) tps_queue;
84 /*! \brief Taskprocessor singleton list entry */
85 AST_LIST_ENTRY(ast_taskprocessor) list;
87 #define TPS_MAX_BUCKETS 7
88 /*! \brief tps_singletons is the astobj2 container for taskprocessor singletons */
89 static struct ao2_container *tps_singletons;
91 /*! \brief CLI <example>taskprocessor ping <blah></example> operation requires a ping condition */
92 static ast_cond_t cli_ping_cond;
94 /*! \brief CLI <example>taskprocessor ping <blah></example> operation requires a ping condition lock */
95 AST_MUTEX_DEFINE_STATIC(cli_ping_cond_lock);
97 /*! \brief The astobj2 hash callback for taskprocessors */
98 static int tps_hash_cb(const void *obj, const int flags);
99 /*! \brief The astobj2 compare callback for taskprocessors */
100 static int tps_cmp_cb(void *obj, void *arg, int flags);
102 /*! \brief The task processing function executed by a taskprocessor */
103 static void *tps_processing_function(void *data);
105 /*! \brief Destroy the taskprocessor when its refcount reaches zero */
106 static void tps_taskprocessor_destroy(void *tps);
108 /*! \brief CLI <example>taskprocessor ping <blah></example> handler function */
109 static int tps_ping_handler(void *datap);
111 /*! \brief Remove the front task off the taskprocessor queue */
112 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps);
114 /*! \brief Return the size of the taskprocessor queue */
115 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps);
117 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
118 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
120 static struct ast_cli_entry taskprocessor_clis[] = {
121 AST_CLI_DEFINE(cli_tps_ping, "Ping a named task processor"),
122 AST_CLI_DEFINE(cli_tps_report, "List instantiated task processors and statistics"),
127 * \brief Clean up resources on Asterisk shutdown
129 static void tps_shutdown(void)
131 ast_cli_unregister_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
132 ao2_t_ref(tps_singletons, -1, "Unref tps_singletons in shutdown");
133 tps_singletons = NULL;
136 /* initialize the taskprocessor container and register CLI operations */
137 int ast_tps_init(void)
139 if (!(tps_singletons = ao2_container_alloc(TPS_MAX_BUCKETS, tps_hash_cb, tps_cmp_cb))) {
140 ast_log(LOG_ERROR, "taskprocessor container failed to initialize!\n");
144 ast_cond_init(&cli_ping_cond, NULL);
146 ast_cli_register_multiple(taskprocessor_clis, ARRAY_LEN(taskprocessor_clis));
148 ast_register_atexit(tps_shutdown);
153 /* allocate resources for the task */
154 static struct tps_task *tps_task_alloc(int (*task_exe)(void *datap), void *datap)
157 if ((t = ast_calloc(1, sizeof(*t)))) {
158 t->execute = task_exe;
164 /* release task resources */
165 static void *tps_task_free(struct tps_task *task)
173 /* taskprocessor tab completion */
174 static char *tps_taskprocessor_tab_complete(struct ast_taskprocessor *p, struct ast_cli_args *a)
179 struct ao2_iterator i;
184 tklen = strlen(a->word);
185 i = ao2_iterator_init(tps_singletons, 0);
186 while ((p = ao2_iterator_next(&i))) {
187 if (!strncasecmp(a->word, p->name, tklen) && ++wordnum > a->n) {
188 name = ast_strdup(p->name);
194 ao2_iterator_destroy(&i);
198 /* ping task handling function */
199 static int tps_ping_handler(void *datap)
201 ast_mutex_lock(&cli_ping_cond_lock);
202 ast_cond_signal(&cli_ping_cond);
203 ast_mutex_unlock(&cli_ping_cond_lock);
207 /* ping the specified taskprocessor and display the ping time on the CLI */
208 static char *cli_tps_ping(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
210 struct timeval begin, end, delta;
214 struct ast_taskprocessor *tps = NULL;
218 e->command = "core ping taskprocessor";
220 "Usage: core ping taskprocessor <taskprocessor>\n"
221 " Displays the time required for a task to be processed\n";
224 return tps_taskprocessor_tab_complete(tps, a);
228 return CLI_SHOWUSAGE;
231 if (!(tps = ast_taskprocessor_get(name, TPS_REF_IF_EXISTS))) {
232 ast_cli(a->fd, "\nping failed: %s not found\n\n", name);
235 ast_cli(a->fd, "\npinging %s ...", name);
236 when = ast_tvadd((begin = ast_tvnow()), ast_samp2tv(1000, 1000));
237 ts.tv_sec = when.tv_sec;
238 ts.tv_nsec = when.tv_usec * 1000;
239 ast_mutex_lock(&cli_ping_cond_lock);
240 if (ast_taskprocessor_push(tps, tps_ping_handler, 0) < 0) {
241 ast_cli(a->fd, "\nping failed: could not push task to %s\n\n", name);
245 ast_cond_timedwait(&cli_ping_cond, &cli_ping_cond_lock, &ts);
246 ast_mutex_unlock(&cli_ping_cond_lock);
248 delta = ast_tvsub(end, begin);
249 ast_cli(a->fd, "\n\t%24s ping time: %.1ld.%.6ld sec\n\n", name, (long)delta.tv_sec, (long int)delta.tv_usec);
254 static char *cli_tps_report(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
259 unsigned long maxqsize;
260 unsigned long processed;
261 struct ast_taskprocessor *p;
262 struct ao2_iterator i;
266 e->command = "core show taskprocessors";
268 "Usage: core show taskprocessors\n"
269 " Shows a list of instantiated task processors and their statistics\n";
275 if (a->argc != e->args)
276 return CLI_SHOWUSAGE;
278 ast_cli(a->fd, "\n\t+----- Processor -----+--- Processed ---+- In Queue -+- Max Depth -+");
279 i = ao2_iterator_init(tps_singletons, 0);
280 while ((p = ao2_iterator_next(&i))) {
281 ast_copy_string(name, p->name, sizeof(name));
282 qsize = p->tps_queue_size;
283 maxqsize = p->stats->max_qsize;
284 processed = p->stats->_tasks_processed_count;
285 ast_cli(a->fd, "\n%24s %17ld %12ld %12ld", name, processed, qsize, maxqsize);
288 ao2_iterator_destroy(&i);
289 tcount = ao2_container_count(tps_singletons);
290 ast_cli(a->fd, "\n\t+---------------------+-----------------+------------+-------------+\n\t%d taskprocessors\n\n", tcount);
294 /* this is the task processing worker function */
295 static void *tps_processing_function(void *data)
297 struct ast_taskprocessor *i = data;
302 ast_log(LOG_ERROR, "cannot start thread_function loop without a ast_taskprocessor structure.\n");
306 while (i->poll_thread_run) {
307 ast_mutex_lock(&i->taskprocessor_lock);
308 if (!i->poll_thread_run) {
309 ast_mutex_unlock(&i->taskprocessor_lock);
312 if (!(size = tps_taskprocessor_depth(i))) {
313 ast_cond_wait(&i->poll_cond, &i->taskprocessor_lock);
314 if (!i->poll_thread_run) {
315 ast_mutex_unlock(&i->taskprocessor_lock);
319 ast_mutex_unlock(&i->taskprocessor_lock);
320 /* stuff is in the queue */
321 if (!(t = tps_taskprocessor_pop(i))) {
322 ast_log(LOG_ERROR, "Wtf?? %d tasks in the queue, but we're popping blanks!\n", size);
326 ast_log(LOG_WARNING, "Task is missing a function to execute!\n");
330 t->execute(t->datap);
332 ast_mutex_lock(&i->taskprocessor_lock);
334 i->stats->_tasks_processed_count++;
335 if (size > i->stats->max_qsize) {
336 i->stats->max_qsize = size;
339 ast_mutex_unlock(&i->taskprocessor_lock);
343 while ((t = tps_taskprocessor_pop(i))) {
349 /* hash callback for astobj2 */
350 static int tps_hash_cb(const void *obj, const int flags)
352 const struct ast_taskprocessor *tps = obj;
354 return ast_str_case_hash(tps->name);
357 /* compare callback for astobj2 */
358 static int tps_cmp_cb(void *obj, void *arg, int flags)
360 struct ast_taskprocessor *lhs = obj, *rhs = arg;
362 return !strcasecmp(lhs->name, rhs->name) ? CMP_MATCH | CMP_STOP : 0;
365 /* destroy the taskprocessor */
366 static void tps_taskprocessor_destroy(void *tps)
368 struct ast_taskprocessor *t = tps;
371 ast_log(LOG_ERROR, "missing taskprocessor\n");
374 ast_debug(1, "destroying taskprocessor '%s'\n", t->name);
376 ast_mutex_lock(&t->taskprocessor_lock);
377 t->poll_thread_run = 0;
378 ast_cond_signal(&t->poll_cond);
379 ast_mutex_unlock(&t->taskprocessor_lock);
380 pthread_join(t->poll_thread, NULL);
381 t->poll_thread = AST_PTHREADT_NULL;
382 ast_mutex_destroy(&t->taskprocessor_lock);
383 ast_cond_destroy(&t->poll_cond);
389 ast_free((char *) t->name);
392 /* pop the front task and return it */
393 static struct tps_task *tps_taskprocessor_pop(struct ast_taskprocessor *tps)
395 struct tps_task *task;
398 ast_log(LOG_ERROR, "missing taskprocessor\n");
401 ast_mutex_lock(&tps->taskprocessor_lock);
402 if ((task = AST_LIST_REMOVE_HEAD(&tps->tps_queue, list))) {
403 tps->tps_queue_size--;
405 ast_mutex_unlock(&tps->taskprocessor_lock);
409 static int tps_taskprocessor_depth(struct ast_taskprocessor *tps)
411 return (tps) ? tps->tps_queue_size : -1;
414 /* taskprocessor name accessor */
415 const char *ast_taskprocessor_name(struct ast_taskprocessor *tps)
418 ast_log(LOG_ERROR, "no taskprocessor specified!\n");
424 /* Provide a reference to a taskprocessor. Create the taskprocessor if necessary, but don't
425 * create the taskprocessor if we were told via ast_tps_options to return a reference only
426 * if it already exists */
427 struct ast_taskprocessor *ast_taskprocessor_get(const char *name, enum ast_tps_options create)
429 struct ast_taskprocessor *p, tmp_tps = {
433 if (ast_strlen_zero(name)) {
434 ast_log(LOG_ERROR, "requesting a nameless taskprocessor!!!\n");
437 ao2_lock(tps_singletons);
438 p = ao2_find(tps_singletons, &tmp_tps, OBJ_POINTER);
440 ao2_unlock(tps_singletons);
443 if (create & TPS_REF_IF_EXISTS) {
444 /* calling function does not want a new taskprocessor to be created if it doesn't already exist */
445 ao2_unlock(tps_singletons);
448 /* create a new taskprocessor */
449 if (!(p = ao2_alloc(sizeof(*p), tps_taskprocessor_destroy))) {
450 ao2_unlock(tps_singletons);
451 ast_log(LOG_WARNING, "failed to create taskprocessor '%s'\n", name);
455 ast_cond_init(&p->poll_cond, NULL);
456 ast_mutex_init(&p->taskprocessor_lock);
458 if (!(p->stats = ast_calloc(1, sizeof(*p->stats)))) {
459 ao2_unlock(tps_singletons);
460 ast_log(LOG_WARNING, "failed to create taskprocessor stats for '%s'\n", name);
464 if (!(p->name = ast_strdup(name))) {
465 ao2_unlock(tps_singletons);
469 p->poll_thread_run = 1;
470 p->poll_thread = AST_PTHREADT_NULL;
471 if (ast_pthread_create(&p->poll_thread, NULL, tps_processing_function, p) < 0) {
472 ao2_unlock(tps_singletons);
473 ast_log(LOG_ERROR, "Taskprocessor '%s' failed to create the processing thread.\n", p->name);
477 if (!(ao2_link(tps_singletons, p))) {
478 ao2_unlock(tps_singletons);
479 ast_log(LOG_ERROR, "Failed to add taskprocessor '%s' to container\n", p->name);
483 ao2_unlock(tps_singletons);
487 /* decrement the taskprocessor reference count and unlink from the container if necessary */
488 void *ast_taskprocessor_unreference(struct ast_taskprocessor *tps)
491 ao2_lock(tps_singletons);
492 ao2_unlink(tps_singletons, tps);
493 if (ao2_ref(tps, -1) > 1) {
494 ao2_link(tps_singletons, tps);
496 ao2_unlock(tps_singletons);
501 /* push the task into the taskprocessor queue */
502 int ast_taskprocessor_push(struct ast_taskprocessor *tps, int (*task_exe)(void *datap), void *datap)
506 if (!tps || !task_exe) {
507 ast_log(LOG_ERROR, "%s is missing!!\n", (tps) ? "task callback" : "taskprocessor");
510 if (!(t = tps_task_alloc(task_exe, datap))) {
511 ast_log(LOG_ERROR, "failed to allocate task! Can't push to '%s'\n", tps->name);
514 ast_mutex_lock(&tps->taskprocessor_lock);
515 AST_LIST_INSERT_TAIL(&tps->tps_queue, t, list);
516 tps->tps_queue_size++;
517 ast_cond_signal(&tps->poll_cond);
518 ast_mutex_unlock(&tps->taskprocessor_lock);