lock list of translators *before* recalculating translation matrix.

Also, store translators using linked list macros.


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7967 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant
2006-01-11 05:00:45 +00:00
parent 1d83e4a2ab
commit e0d4fcfb55
2 changed files with 23 additions and 26 deletions

View File

@@ -31,6 +31,7 @@ extern "C" {
#include "asterisk/frame.h" #include "asterisk/frame.h"
#include "asterisk/plc.h" #include "asterisk/plc.h"
#include "asterisk/linkedlists.h"
/* Declared by individual translators */ /* Declared by individual translators */
struct ast_translator_pvt; struct ast_translator_pvt;
@@ -57,7 +58,7 @@ struct ast_translator {
/*! Cost in milliseconds for encoding/decoding 1 second of sound */ /*! Cost in milliseconds for encoding/decoding 1 second of sound */
int cost; int cost;
/*! For linking, not to be modified by the translator */ /*! For linking, not to be modified by the translator */
struct ast_translator *next; AST_LIST_ENTRY(ast_translator) list;
}; };
struct ast_trans_pvt; struct ast_trans_pvt;

View File

@@ -51,8 +51,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
This could all be done more efficiently *IF* we chained packets together This could all be done more efficiently *IF* we chained packets together
by default, but it would also complicate virtually every application. */ by default, but it would also complicate virtually every application. */
AST_MUTEX_DEFINE_STATIC(list_lock); static AST_LIST_HEAD_STATIC(translators, ast_translator);
static struct ast_translator *list = NULL;
struct ast_translator_dir { struct ast_translator_dir {
struct ast_translator *step; /*!< Next step translator */ struct ast_translator *step; /*!< Next step translator */
@@ -267,7 +266,11 @@ static void calc_cost(struct ast_translator *t, int samples)
t->cost = 1; t->cost = 1;
} }
/*! \brief Use the list of translators to build a translation matrix */ /*!
\brief Use the list of translators to build a translation matrix
\note This function expects the list of translators to be locked
*/
static void rebuild_matrix(int samples) static void rebuild_matrix(int samples)
{ {
struct ast_translator *t; struct ast_translator *t;
@@ -277,8 +280,7 @@ static void rebuild_matrix(int samples)
if (option_debug) if (option_debug)
ast_log(LOG_DEBUG, "Resetting translation matrix\n"); ast_log(LOG_DEBUG, "Resetting translation matrix\n");
bzero(tr_matrix, sizeof(tr_matrix)); bzero(tr_matrix, sizeof(tr_matrix));
t = list; AST_LIST_TRAVERSE(&translators, t, list) {
while(t) {
if(samples) if(samples)
calc_cost(t, samples); calc_cost(t, samples);
@@ -287,7 +289,6 @@ static void rebuild_matrix(int samples)
tr_matrix[t->srcfmt][t->dstfmt].step = t; tr_matrix[t->srcfmt][t->dstfmt].step = t;
tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost; tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
} }
t = t->next;
} }
do { do {
changed = 0; changed = 0;
@@ -332,6 +333,8 @@ static int show_translation(int fd, int argc, char *argv[])
if (argc > 4) if (argc > 4)
return RESULT_SHOWUSAGE; return RESULT_SHOWUSAGE;
AST_LIST_LOCK(&translators);
if (argv[2] && !strcasecmp(argv[2],"recalc")) { if (argv[2] && !strcasecmp(argv[2],"recalc")) {
z = argv[3] ? atoi(argv[3]) : 1; z = argv[3] ? atoi(argv[3]) : 1;
@@ -350,7 +353,6 @@ static int show_translation(int fd, int argc, char *argv[])
ast_cli(fd, " Translation times between formats (in milliseconds)\n"); ast_cli(fd, " Translation times between formats (in milliseconds)\n");
ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n"); ast_cli(fd, " Source Format (Rows) Destination Format(Columns)\n\n");
ast_mutex_lock(&list_lock);
for (x = -1; x < SHOW_TRANS; x++) { for (x = -1; x < SHOW_TRANS; x++) {
/* next 2 lines run faster than using strcpy() */ /* next 2 lines run faster than using strcpy() */
line[0] = ' '; line[0] = ' ';
@@ -371,7 +373,7 @@ static int show_translation(int fd, int argc, char *argv[])
snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n"); snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
ast_cli(fd, line); ast_cli(fd, line);
} }
ast_mutex_unlock(&list_lock); AST_LIST_UNLOCK(&translators);
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
@@ -403,15 +405,14 @@ int ast_register_translator(struct ast_translator *t)
calc_cost(t,1); calc_cost(t,1);
if (option_verbose > 1) if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost); ast_verbose(VERBOSE_PREFIX_2 "Registered translator '%s' from format %s to %s, cost %d\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt), t->cost);
ast_mutex_lock(&list_lock); AST_LIST_LOCK(&translators);
if (!added_cli) { if (!added_cli) {
ast_cli_register(&show_trans); ast_cli_register(&show_trans);
added_cli++; added_cli++;
} }
t->next = list; AST_LIST_INSERT_HEAD(&translators, t, list);
list = t;
rebuild_matrix(0); rebuild_matrix(0);
ast_mutex_unlock(&list_lock); AST_LIST_UNLOCK(&translators);
return 0; return 0;
} }
@@ -419,24 +420,19 @@ int ast_register_translator(struct ast_translator *t)
int ast_unregister_translator(struct ast_translator *t) int ast_unregister_translator(struct ast_translator *t)
{ {
char tmp[80]; char tmp[80];
struct ast_translator *u, *ul = NULL; struct ast_translator *u;
ast_mutex_lock(&list_lock); AST_LIST_LOCK(&translators);
u = list; AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
while(u) {
if (u == t) { if (u == t) {
if (ul) AST_LIST_REMOVE_CURRENT(&translators, list);
ul->next = u->next;
else
list = u->next;
if (option_verbose > 1) if (option_verbose > 1)
ast_verbose(VERBOSE_PREFIX_2 "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt)); ast_verbose(VERBOSE_PREFIX_2 "Unregistered translator '%s' from format %s to %s\n", term_color(tmp, t->name, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp)), ast_getformatname(1 << t->srcfmt), ast_getformatname(1 << t->dstfmt));
break; break;
} }
ul = u;
u = u->next;
} }
AST_LIST_TRAVERSE_SAFE_END
rebuild_matrix(0); rebuild_matrix(0);
ast_mutex_unlock(&list_lock); AST_LIST_UNLOCK(&translators);
return (u ? 0 : -1); return (u ? 0 : -1);
} }
@@ -463,7 +459,7 @@ int ast_translator_best_choice(int *dst, int *srcs)
} }
} else { } else {
/* We will need to translate */ /* We will need to translate */
ast_mutex_lock(&list_lock); AST_LIST_LOCK(&translators);
for (y=0; y < MAX_FORMAT; y++) { for (y=0; y < MAX_FORMAT; y++) {
if (cur & *dst) if (cur & *dst)
for (x=0; x < MAX_FORMAT; x++) { for (x=0; x < MAX_FORMAT; x++) {
@@ -477,7 +473,7 @@ int ast_translator_best_choice(int *dst, int *srcs)
} }
cur = cur << 1; cur = cur << 1;
} }
ast_mutex_unlock(&list_lock); AST_LIST_UNLOCK(&translators);
} }
if (best > -1) { if (best > -1) {
*srcs = best; *srcs = best;