[svn-commits] file: trunk r46972 - /trunk/main/translate.c

svn-commits at lists.digium.com svn-commits at lists.digium.com
Thu Nov 2 11:01:11 MST 2006


Author: file
Date: Thu Nov  2 12:01:10 2006
New Revision: 46972

URL: http://svn.digium.com/view/asterisk?rev=46972&view=rev
Log:
Convert translation core linked list over to read/write based one, since it spends most of it's time only reading.

Modified:
    trunk/main/translate.c

Modified: trunk/main/translate.c
URL: http://svn.digium.com/view/asterisk/trunk/main/translate.c?rev=46972&r1=46971&r2=46972&view=diff
==============================================================================
--- trunk/main/translate.c (original)
+++ trunk/main/translate.c Thu Nov  2 12:01:10 2006
@@ -49,7 +49,7 @@
 #define MAX_RECALC 200 /* max sample recalc */
 
 /*! \brief the list of translators */
-static AST_LIST_HEAD_STATIC(translators, ast_translator);
+static AST_RWLIST_HEAD_STATIC(translators, ast_translator);
 
 struct translator_path {
 	struct ast_translator *step;	/*!< Next step translator */
@@ -256,7 +256,7 @@
 	source = powerof(source);
 	dest = powerof(dest);
 	
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_RDLOCK(&translators);
 
 	while (source != dest) {
 		struct ast_trans_pvt *cur;
@@ -264,14 +264,14 @@
 		if (!t) {
 			ast_log(LOG_WARNING, "No translator path from %s to %s\n", 
 				ast_getformatname(source), ast_getformatname(dest));
-			AST_LIST_UNLOCK(&translators);
+			AST_RWLIST_UNLOCK(&translators);
 			return NULL;
 		}
 		if (!(cur = newpvt(t))) {
 			ast_log(LOG_WARNING, "Failed to build translator step from %d to %d\n", source, dest);
 			if (head)
 				ast_translator_free_path(head);	
-			AST_LIST_UNLOCK(&translators);
+			AST_RWLIST_UNLOCK(&translators);
 			return NULL;
 		}
 		if (!head)
@@ -284,7 +284,7 @@
 		source = cur->t->dstfmt;
 	}
 
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 	return head;
 }
 
@@ -426,7 +426,7 @@
 	bzero(tr_matrix, sizeof(tr_matrix));
 
 	/* first, compute all direct costs */
-	AST_LIST_TRAVERSE(&translators, t, list) {
+	AST_RWLIST_TRAVERSE(&translators, t, list) {
 		if (!t->active)
 			continue;
 
@@ -494,8 +494,6 @@
 
 	if (argc > 5)
 		return RESULT_SHOWUSAGE;
-
-	AST_LIST_LOCK(&translators);	
 	
 	if (argv[3] && !strcasecmp(argv[3], "recalc")) {
 		z = argv[4] ? atoi(argv[4]) : 1;
@@ -510,8 +508,12 @@
 			z = MAX_RECALC;
 		}
 		ast_cli(fd, "         Recalculating Codec Translation (number of sample seconds: %d)\n\n", z);
+		AST_RWLIST_WRLOCK(&translators);
 		rebuild_matrix(z);
-	}
+		AST_RWLIST_UNLOCK(&translators);
+	}
+
+	AST_RWLIST_RDLOCK(&translators);
 
 	ast_cli(fd, "         Translation times between formats (in milliseconds) for one second of data\n");
 	ast_cli(fd, "          Source Format (Rows) Destination Format (Columns)\n\n");
@@ -551,7 +553,7 @@
 		ast_build_string(&buf, &left, "\n");
 		ast_cli(fd, line);			
 	}
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 	return RESULT_SUCCESS;
 }
 
@@ -639,28 +641,28 @@
 		added_cli++;
 	}
 
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_WRLOCK(&translators);
 
 	/* find any existing translators that provide this same srcfmt/dstfmt,
 	   and put this one in order based on cost */
-	AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
 		if ((u->srcfmt == t->srcfmt) &&
 		    (u->dstfmt == t->dstfmt) &&
 		    (u->cost > t->cost)) {
-			AST_LIST_INSERT_BEFORE_CURRENT(&translators, t, list);
+			AST_RWLIST_INSERT_BEFORE_CURRENT(&translators, t, list);
 			t = NULL;
 		}
 	}
-	AST_LIST_TRAVERSE_SAFE_END;
+	AST_RWLIST_TRAVERSE_SAFE_END;
 
 	/* if no existing translator was found for this format combination,
 	   add it to the beginning of the list */
 	if (t)
-		AST_LIST_INSERT_HEAD(&translators, t, list);
+		AST_RWLIST_INSERT_HEAD(&translators, t, list);
 
 	rebuild_matrix(0);
 
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 
 	return 0;
 }
@@ -672,40 +674,40 @@
 	struct ast_translator *u;
 	int found = 0;
 
-	AST_LIST_LOCK(&translators);
-	AST_LIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
+	AST_RWLIST_WRLOCK(&translators);
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&translators, u, list) {
 		if (u == t) {
-			AST_LIST_REMOVE_CURRENT(&translators, list);
+			AST_RWLIST_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));
 			found = 1;
 			break;
 		}
 	}
-	AST_LIST_TRAVERSE_SAFE_END;
+	AST_RWLIST_TRAVERSE_SAFE_END;
 
 	if (found)
 		rebuild_matrix(0);
 
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 
 	return (u ? 0 : -1);
 }
 
 void ast_translator_activate(struct ast_translator *t)
 {
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_WRLOCK(&translators);
 	t->active = 1;
 	rebuild_matrix(0);
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 }
 
 void ast_translator_deactivate(struct ast_translator *t)
 {
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_WRLOCK(&translators);
 	t->active = 0;
 	rebuild_matrix(0);
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 }
 
 /*! \brief Calculate our best translator source format, given costs, and a desired destination */
@@ -728,7 +730,7 @@
 		*srcs = *dst = cur;
 		return 0;
 	} else {	/* No, we will need to translate */
-		AST_LIST_LOCK(&translators);
+		AST_RWLIST_RDLOCK(&translators);
 		for (cur = 1, y = 0; y < MAX_FORMAT; cur <<= 1, y++) {
 			if (! (cur & *dst))
 				continue;
@@ -746,7 +748,7 @@
 				}
 			}
 		}
-		AST_LIST_UNLOCK(&translators);
+		AST_RWLIST_UNLOCK(&translators);
 		if (best > -1) {
 			*srcs = best;
 			*dst = bestdst;
@@ -764,12 +766,12 @@
 	src = powerof(src);
 	dest = powerof(dest);
 
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_RDLOCK(&translators);
 
 	if (tr_matrix[src][dest].step)
 		res = tr_matrix[src][dest].multistep + 1;
 
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 
 	return res;
 }
@@ -794,7 +796,7 @@
 	if (src_video)
 		src_video = powerof(src_video);
 
-	AST_LIST_LOCK(&translators);
+	AST_RWLIST_RDLOCK(&translators);
 
 	/* For a given source audio format, traverse the list of
 	   known audio formats to determine whether there exists
@@ -848,7 +850,7 @@
 			res &= ~x;
 	}
 
-	AST_LIST_UNLOCK(&translators);
+	AST_RWLIST_UNLOCK(&translators);
 
 	return res;
 }



More information about the svn-commits mailing list