[svn-commits] trunk - r7967 in /trunk: include/asterisk/translate.h translate.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Tue Jan 10 23:00:47 CST 2006


Author: russell
Date: Tue Jan 10 23:00:45 2006
New Revision: 7967

URL: http://svn.digium.com/view/asterisk?rev=7967&view=rev
Log:
lock list of translators *before* recalculating translation matrix.
Also, store translators using linked list macros.

Modified:
    trunk/include/asterisk/translate.h
    trunk/translate.c

Modified: trunk/include/asterisk/translate.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/translate.h?rev=7967&r1=7966&r2=7967&view=diff
==============================================================================
--- trunk/include/asterisk/translate.h (original)
+++ trunk/include/asterisk/translate.h Tue Jan 10 23:00:45 2006
@@ -31,6 +31,7 @@
 
 #include "asterisk/frame.h"
 #include "asterisk/plc.h"
+#include "asterisk/linkedlists.h"
 
 /* Declared by individual translators */
 struct ast_translator_pvt;
@@ -57,7 +58,7 @@
 	/*! Cost in milliseconds for encoding/decoding 1 second of sound */
 	int cost;
 	/*! For linking, not to be modified by the translator */
-	struct ast_translator *next;
+	AST_LIST_ENTRY(ast_translator) list;
 };
 
 struct ast_trans_pvt;

Modified: trunk/translate.c
URL: http://svn.digium.com/view/asterisk/trunk/translate.c?rev=7967&r1=7966&r2=7967&view=diff
==============================================================================
--- trunk/translate.c (original)
+++ trunk/translate.c Tue Jan 10 23:00:45 2006
@@ -51,8 +51,7 @@
    This could all be done more efficiently *IF* we chained packets together
    by default, but it would also complicate virtually every application. */
    
-AST_MUTEX_DEFINE_STATIC(list_lock);
-static struct ast_translator *list = NULL;
+static AST_LIST_HEAD_STATIC(translators, ast_translator);
 
 struct ast_translator_dir {
 	struct ast_translator *step;	/*!< Next step translator */
@@ -267,7 +266,11 @@
 		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)
 {
 	struct ast_translator *t;
@@ -277,8 +280,7 @@
 	if (option_debug)
 		ast_log(LOG_DEBUG, "Resetting translation matrix\n");
 	bzero(tr_matrix, sizeof(tr_matrix));
-	t = list;
-	while(t) {
+	AST_LIST_TRAVERSE(&translators, t, list) {
 		if(samples)
 			calc_cost(t, samples);
 	  
@@ -287,7 +289,6 @@
 			tr_matrix[t->srcfmt][t->dstfmt].step = t;
 			tr_matrix[t->srcfmt][t->dstfmt].cost = t->cost;
 		}
-		t = t->next;
 	}
 	do {
 		changed = 0;
@@ -332,6 +333,8 @@
 	if (argc > 4) 
 		return RESULT_SHOWUSAGE;
 
+	AST_LIST_LOCK(&translators);	
+	
 	if (argv[2] && !strcasecmp(argv[2],"recalc")) {
 		z = argv[3] ? atoi(argv[3]) : 1;
 
@@ -350,7 +353,6 @@
 
 	ast_cli(fd, "         Translation times between formats (in milliseconds)\n");
 	ast_cli(fd, "          Source Format (Rows) Destination Format(Columns)\n\n");
-	ast_mutex_lock(&list_lock);
 	for (x = -1; x < SHOW_TRANS; x++) {
 		/* next 2 lines run faster than using strcpy() */
 		line[0] = ' ';
@@ -371,7 +373,7 @@
 		snprintf(line + strlen(line), sizeof(line) - strlen(line), "\n");
 		ast_cli(fd, line);			
 	}
-	ast_mutex_unlock(&list_lock);
+	AST_LIST_UNLOCK(&translators);
 	return RESULT_SUCCESS;
 }
 
@@ -403,15 +405,14 @@
 	calc_cost(t,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_mutex_lock(&list_lock);
+	AST_LIST_LOCK(&translators);
 	if (!added_cli) {
 		ast_cli_register(&show_trans);
 		added_cli++;
 	}
-	t->next = list;
-	list = t;
+	AST_LIST_INSERT_HEAD(&translators, t, list);
 	rebuild_matrix(0);
-	ast_mutex_unlock(&list_lock);
+	AST_LIST_UNLOCK(&translators);
 	return 0;
 }
 
@@ -419,24 +420,19 @@
 int ast_unregister_translator(struct ast_translator *t)
 {
 	char tmp[80];
-	struct ast_translator *u, *ul = NULL;
-	ast_mutex_lock(&list_lock);
-	u = list;
-	while(u) {
+	struct ast_translator *u;
+	AST_LIST_LOCK(&translators);
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
 		if (u == t) {
-			if (ul)
-				ul->next = u->next;
-			else
-				list = u->next;
+			AST_LIST_REMOVE_CURRENT(&translators, list);
 			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));
 			break;
 		}
-		ul = u;
-		u = u->next;
-	}
+	}
+	AST_LIST_TRAVERSE_SAFE_END
 	rebuild_matrix(0);
-	ast_mutex_unlock(&list_lock);
+	AST_LIST_UNLOCK(&translators);
 	return (u ? 0 : -1);
 }
 
@@ -463,7 +459,7 @@
 		}
 	} else {
 		/* We will need to translate */
-		ast_mutex_lock(&list_lock);
+		AST_LIST_LOCK(&translators);
 		for (y=0; y < MAX_FORMAT; y++) {
 			if (cur & *dst)
 				for (x=0; x < MAX_FORMAT; x++) {
@@ -477,7 +473,7 @@
 				}
 			cur = cur << 1;
 		}
-		ast_mutex_unlock(&list_lock);
+		AST_LIST_UNLOCK(&translators);
 	}
 	if (best > -1) {
 		*srcs = best;



More information about the svn-commits mailing list