[Asterisk-cvs] asterisk app.c, 1.63, 1.64 channel.c, 1.200, 1.201 cli.c, 1.82, 1.83 manager.c, 1.97, 1.98 pbx.c, 1.252, 1.253

kpfleming at lists.digium.com kpfleming at lists.digium.com
Sun Jun 5 22:26:46 CDT 2005


Update of /usr/cvsroot/asterisk
In directory mongoose.digium.com:/tmp/cvs-serv4861

Modified Files:
	app.c channel.c cli.c manager.c pbx.c 
Log Message:
more efficient (and understandable) ast_channel_walk_locked, and vastly more efficient ast_channel_by_name_locked (bug #4265)


Index: app.c
===================================================================
RCS file: /usr/cvsroot/asterisk/app.c,v
retrieving revision 1.63
retrieving revision 1.64
diff -u -d -r1.63 -r1.64
--- app.c	5 Jun 2005 16:32:16 -0000	1.63
+++ app.c	6 Jun 2005 02:29:17 -0000	1.64
@@ -1013,22 +1013,20 @@
 	int count = 0;
 	char *test;
 	char cat[80] = "";
+	char *s;
 
-	if (category && !ast_strlen_zero(category)) {
-		ast_copy_string(cat, category, sizeof(cat));
-	} else {
-		ast_copy_string(cat, GROUP_CATEGORY_PREFIX, sizeof(cat));
-	}
+	if (group == NULL || ast_strlen_zero(group))
+		return 0;
 
-	if (group && !ast_strlen_zero(group)) {
-		chan = ast_channel_walk_locked(NULL);
-		while(chan) {
-			test = pbx_builtin_getvar_helper(chan, cat);
-			if (test && !strcasecmp(test, group))
-				count++;
-			ast_mutex_unlock(&chan->lock);
-			chan = ast_channel_walk_locked(chan);
-		}
+ 	s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
+	ast_copy_string(cat, s, sizeof(cat));
+
+	chan = NULL;
+	while ((chan = ast_channel_walk_locked(chan)) != NULL) {
+ 		test = pbx_builtin_getvar_helper(chan, cat);
+		if (test && !strcasecmp(test, group))
+ 			count++;
+		ast_mutex_unlock(&chan->lock);
 	}
 
 	return count;
@@ -1041,6 +1039,7 @@
 	int count = 0;
 	char *test;
 	char cat[80] = "";
+	char *s;
 
 	if (!groupmatch || ast_strlen_zero(groupmatch))
 		return 0;
@@ -1049,14 +1048,11 @@
 	if (regcomp(&regexbuf, groupmatch, REG_EXTENDED | REG_NOSUB))
 		return 0;
 
-	if (category && !ast_strlen_zero(category)) {
-		ast_copy_string(cat, category, sizeof(cat));
-	} else {
-		ast_copy_string(cat, GROUP_CATEGORY_PREFIX, sizeof(cat));
-	}
+	s = (category && !ast_strlen_zero(category)) ? category : GROUP_CATEGORY_PREFIX;
+	ast_copy_string(cat, s, sizeof(cat));
 
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
+	chan = NULL;
+	while ((chan = ast_channel_walk_locked(chan)) != NULL) {
 		test = pbx_builtin_getvar_helper(chan, cat);
 		if (test && !regexec(&regexbuf, test, 0, NULL, 0))
 			count++;

Index: channel.c
===================================================================
RCS file: /usr/cvsroot/asterisk/channel.c,v
retrieving revision 1.200
retrieving revision 1.201
diff -u -d -r1.200 -r1.201
--- channel.c	5 Jun 2005 16:32:16 -0000	1.200
+++ channel.c	6 Jun 2005 02:29:17 -0000	1.201
@@ -61,7 +61,11 @@
 #define MONITOR_DELAY	150 * 8		/* 150 ms of MONITORING DELAY */
 #endif
 
+/*
+ * Prevent new channel allocation if shutting down.
+ */
 static int shutting_down = 0;
+
 static int uniqueint = 0;
 
 unsigned long global_fin = 0, global_fout = 0;
@@ -71,12 +75,17 @@
 struct chanlist {
 	const struct ast_channel_tech *tech;
 	struct chanlist *next;
-} *backends = NULL;
-struct ast_channel *channels = NULL;
+};
 
-/* Protect the channel list (highly unlikely that two things would change
-   it at the same time, but still! */
-   
+static struct chanlist *backends = NULL;
+
+/*
+ * the list of channels we have
+ */
+static struct ast_channel *channels = NULL;
+
+/* Protect the channel list, both backends and channels.
+ */
 AST_MUTEX_DEFINE_STATIC(chlock);
 
 static int show_channeltypes(int fd, int argc, char *argv[])
@@ -514,72 +523,70 @@
 		ast_clear_flag(chan, AST_FLAG_DEFER_DTMF);
 }
 
-/*--- ast_channel_walk_locked: Browse channels in use */
-struct ast_channel *ast_channel_walk_locked(struct ast_channel *prev)
+/*
+ * Helper function to return the channel after prev, or the one matching name,
+ * with the channel's lock held. If getting the individual lock fails,
+ * unlock and retry quickly up to 10 times, then give up.
+ * 
+ * XXX Note that this code has cost O(N) because of the need to verify
+ * that the object is still on the global list.
+ *
+ * XXX also note that accessing fields (e.g. c->name in ast_log())
+ * can only be done with the lock held or someone could delete the
+ * object while we work on it. This causes some ugliness in the code.
+ * Note that removing the first ast_log() may be harmful, as it would
+ * shorten the retry period and possibly cause failures.
+ * We should definitely go for a better scheme that is deadlock-free.
+ */
+static struct ast_channel *channel_find_locked(const struct ast_channel *prev,
+					       const char *name)
 {
-	/* Returns next channel (locked) */
-	struct ast_channel *l, *ret;
-	int retries = 0;	
-retry:
-	ret=NULL;
-	ast_mutex_lock(&chlock);
-	l = channels;
-	if (!prev) {
-		if (l) {
-			if (ast_mutex_trylock(&l->lock)) {
-				if (retries < 10)
-					ast_log(LOG_DEBUG, "Avoiding initial deadlock for '%s'\n", l->name);
-				else
-					ast_log(LOG_WARNING, "Avoided initial deadlock for '%s', %d retries!\n", l->name, retries);
-				ast_mutex_unlock(&chlock);
-				if (retries < 10) {
-					usleep(1);
-					retries++;
-					goto retry;
-				} else
-					return NULL;
+	const char *msg = prev ? "initial deadlock" : "deadlock";
+	int retries, done;
+	struct ast_channel *c;
+
+	for (retries = 0; retries < 10; retries++) {
+		ast_mutex_lock(&chlock);
+		for (c = channels; c; c = c->next) {
+			if (prev == NULL) {
+				/* want either head of list or match by name */
+				if (name == NULL || !strcasecmp(name, c->name))
+					break;
+			} else if (c == prev) { /* found, return c->next */
+				c = c->next;
+				break;
 			}
 		}
+		/* exit if chan not found or mutex acquired successfully */
+		done = (c == NULL) || (ast_mutex_trylock(&c->lock) == 0);
+		/* this is slightly unsafe, as we _should_ hold the lock to access c->name */
+		if (!done && c)
+			ast_log(LOG_DEBUG, "Avoiding %s for '%s'\n", msg, c->name);
 		ast_mutex_unlock(&chlock);
-		return l;
-	}
-	while(l) {
-		if (l == prev)
-			ret = l->next;
-		l = l->next;
-	}
-	if (ret) {
-		if (ast_mutex_trylock(&ret->lock)) {
-			if (retries < 10)
-				ast_log(LOG_DEBUG, "Avoiding deadlock for '%s'\n", ret->name);
-			else
-				ast_log(LOG_WARNING, "Avoided deadlock for '%s', %d retries!\n", ret->name, retries);
-			ast_mutex_unlock(&chlock);
-			if (retries < 10) {
-				usleep(1);
-				retries++;
-				goto retry;
-			} else
-				return NULL;
-		}
+		if (done)
+			return c;
+		usleep(1);
 	}
-	ast_mutex_unlock(&chlock);
-	return ret;
-	
+	/*
+ 	 * c is surely not null, but we don't have the lock so cannot
+	 * access c->name
+	 */
+	ast_log(LOG_WARNING, "Avoided %s for '%p', %d retries!\n",
+		msg, c, retries);
+
+	return NULL;
+}
+
+/*--- ast_channel_walk_locked: Browse channels in use */
+struct ast_channel *ast_channel_walk_locked(const struct ast_channel *prev)
+{
+	return channel_find_locked(prev, NULL);
 }
 
 /*--- ast_get_channel_by_name_locked: Get channel by name and lock it */
-struct ast_channel *ast_get_channel_by_name_locked(char *channame)
+struct ast_channel *ast_get_channel_by_name_locked(const char *name)
 {
-	struct ast_channel *chan;
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
-		if (!strcasecmp(chan->name, channame))
-			return chan;
-		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
-	}
-	return NULL;
+	return channel_find_locked(NULL, name);
 }
 
 /*--- ast_safe_sleep_conditional: Wait, look for hangups and condition arg */

Index: cli.c
===================================================================
RCS file: /usr/cvsroot/asterisk/cli.c,v
retrieving revision 1.82
retrieving revision 1.83
diff -u -d -r1.82 -r1.83
--- cli.c	24 May 2005 10:23:51 -0000	1.82
+++ cli.c	6 Jun 2005 02:29:17 -0000	1.83
@@ -412,10 +412,9 @@
 		return RESULT_SHOWUSAGE;
 	
 	concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
-	c = ast_channel_walk_locked(NULL);
 	if(!concise)
 		ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
-	while(c) {
+	while ( (c = ast_channel_walk_locked(c)) != NULL) {
 		if(concise)
 			ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
 					c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
@@ -427,7 +426,6 @@
 
 		numchans++;
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
 	}
 	if(!concise) {
 		ast_cli(fd, "%d active channel(s)\n", numchans);
@@ -476,18 +474,12 @@
 	struct ast_channel *c=NULL;
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, argv[2])) {
-			ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
-			ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
-			ast_mutex_unlock(&c->lock);
-			break;
-		}
+	c = ast_get_channel_by_name_locked(argv[2]);
+	if (c) {
+		ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
+		ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
-	if (!c) 
+	} else
 		ast_cli(fd, "%s is not a known channel\n", argv[2]);
 	return RESULT_SUCCESS;
 }
@@ -601,6 +593,8 @@
 	return RESULT_SUCCESS;
 }
 
+#define	DEBUGCHAN_FLAG	0x80000000
+/* XXX todo: merge next two functions!!! */
 static int handle_debugchan(int fd, int argc, char *argv[])
 {
 	struct ast_channel *c=NULL;
@@ -610,31 +604,26 @@
 
 	is_all = !strcasecmp("all", argv[2]);
 	if (is_all) {
-		global_fin |= 0x80000000;
-		global_fout |= 0x80000000;
+		global_fin |= DEBUGCHAN_FLAG;
+		global_fout |= DEBUGCHAN_FLAG;
+		c = ast_channel_walk_locked(NULL);
+	} else {
+		c = ast_get_channel_by_name_locked(argv[2]);
+		if (c == NULL)
+			ast_cli(fd, "No such channel %s\n", argv[2]);
 	}
-	c = ast_channel_walk_locked(NULL);
 	while(c) {
-		if (is_all || !strcasecmp(c->name, argv[2])) {
-			if (!(c->fin & 0x80000000) || !(c->fout & 0x80000000)) {
-				c->fin |= 0x80000000;
-				c->fout |= 0x80000000;
-				ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
-			}
-			if (!is_all)
-				break;
+		if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
+			c->fin |= DEBUGCHAN_FLAG;
+			c->fout |= DEBUGCHAN_FLAG;
+			ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
 		}
 		ast_mutex_unlock(&c->lock);
+		if (!is_all)
+			break;
 		c = ast_channel_walk_locked(c);
 	}
-	if (!is_all) {
-		if (c)
-			ast_mutex_unlock(&c->lock);
-		else
-			ast_cli(fd, "No such channel %s\n", argv[2]);
-	}
-	else
-		ast_cli(fd, "Debugging on new channels is enabled\n");
+	ast_cli(fd, "Debugging on new channels is enabled\n");
 	return RESULT_SUCCESS;
 }
 
@@ -646,31 +635,26 @@
 		return RESULT_SHOWUSAGE;
 	is_all = !strcasecmp("all", argv[3]);
 	if (is_all) {
-		global_fin &= ~0x80000000;
-		global_fout &= ~0x80000000;
-	}
-	c = ast_channel_walk_locked(NULL);
+		global_fin &= ~DEBUGCHAN_FLAG;
+		global_fout &= ~DEBUGCHAN_FLAG;
+		c = ast_channel_walk_locked(NULL);
+	} else {
+		c = ast_get_channel_by_name_locked(argv[3]);
+		if (c == NULL)
+			ast_cli(fd, "No such channel %s\n", argv[3]);
+    }
 	while(c) {
-		if (is_all || !strcasecmp(c->name, argv[3])) {
-			if ((c->fin & 0x80000000) || (c->fout & 0x80000000)) {
-				c->fin &= 0x7fffffff;
-				c->fout &= 0x7fffffff;
-				ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
-			}
-			if (!is_all)
-				break;
+		if ((c->fin & DEBUGCHAN_FLAG) || (c->fout & DEBUGCHAN_FLAG)) {
+			c->fin &= ~DEBUGCHAN_FLAG;
+			c->fout &= ~DEBUGCHAN_FLAG;
+			ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
 		}
 		ast_mutex_unlock(&c->lock);
+		if (!is_all)
+			break;
 		c = ast_channel_walk_locked(c);
 	}
-	if (!is_all) {
-		if (c)
-			ast_mutex_unlock(&c->lock);
-		else
-			ast_cli(fd, "No such channel %s\n", argv[3]);
-	}
-	else
-		ast_cli(fd, "Debugging on new channels is disabled\n");
+	ast_cli(fd, "Debugging on new channels is disabled\n");
 	return RESULT_SUCCESS;
 }
 		
@@ -688,94 +672,86 @@
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
 	gettimeofday(&now, NULL);
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, argv[2])) {
-			if(c->cdr) {
-				elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
-				hour = elapsed_seconds / 3600;
-				min = (elapsed_seconds % 3600) / 60;
-				sec = elapsed_seconds % 60;
-				snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
-			} else
-				strncpy(cdrtime, "N/A", sizeof(cdrtime) -1);
-			ast_cli(fd, 
-				" -- General --\n"
-				"           Name: %s\n"
-				"           Type: %s\n"
-				"       UniqueID: %s\n"
-				"      Caller ID: %s\n"
-				" Caller ID Name: %s\n"
-				"    DNID Digits: %s\n"
-				"          State: %s (%d)\n"
-				"          Rings: %d\n"
-				"   NativeFormat: %d\n"
-				"    WriteFormat: %d\n"
-				"     ReadFormat: %d\n"
-				"1st File Descriptor: %d\n"
-				"      Frames in: %d%s\n"
-				"     Frames out: %d%s\n"
-				" Time to Hangup: %ld\n"
-				"   Elapsed Time: %s\n"
-				"  Direct Bridge: %s\n"
-				"Indirect Bridge: %s\n"
-				" --   PBX   --\n"
-				"        Context: %s\n"
-				"      Extension: %s\n"
-				"       Priority: %d\n"
-				"     Call Group: %d\n"
-				"   Pickup Group: %d\n"
-				"    Application: %s\n"
-				"           Data: %s\n"
-				"    Blocking in: %s\n",
-				c->name, c->type, c->uniqueid,
-				(c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
-				(c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
-				(c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
-				c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
-				c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
-				cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>", 
-				c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
-				( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
-				(ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
-			
-				if(pbx_builtin_serialize_variables(c,buf,sizeof(buf)))
-					ast_cli(fd,"      Variables:\n%s\n",buf);
-				if(c->cdr && ast_cdr_serialize_variables(c->cdr,buf, sizeof(buf), '=', '\n', 1))
-					ast_cli(fd,"  CDR Variables:\n%s\n",buf);
-
-			ast_mutex_unlock(&c->lock);
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
-	if (!c) 
+	c = ast_get_channel_by_name_locked(argv[2]);
+	if (!c) {
 		ast_cli(fd, "%s is not a known channel\n", argv[2]);
+		return RESULT_SUCCESS;
+	}
+	if(c->cdr) {
+		elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
+		hour = elapsed_seconds / 3600;
+		min = (elapsed_seconds % 3600) / 60;
+		sec = elapsed_seconds % 60;
+		snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
+	} else
+		strncpy(cdrtime, "N/A", sizeof(cdrtime) -1);
+	ast_cli(fd, 
+		" -- General --\n"
+		"           Name: %s\n"
+		"           Type: %s\n"
+		"       UniqueID: %s\n"
+		"      Caller ID: %s\n"
+		" Caller ID Name: %s\n"
+		"    DNID Digits: %s\n"
+		"          State: %s (%d)\n"
+		"          Rings: %d\n"
+		"   NativeFormat: %d\n"
+		"    WriteFormat: %d\n"
+		"     ReadFormat: %d\n"
+		"1st File Descriptor: %d\n"
+		"      Frames in: %d%s\n"
+		"     Frames out: %d%s\n"
+		" Time to Hangup: %ld\n"
+		"   Elapsed Time: %s\n"
+		"  Direct Bridge: %s\n"
+		"Indirect Bridge: %s\n"
+		" --   PBX   --\n"
+		"        Context: %s\n"
+		"      Extension: %s\n"
+		"       Priority: %d\n"
+		"     Call Group: %d\n"
+		"   Pickup Group: %d\n"
+		"    Application: %s\n"
+		"           Data: %s\n"
+		"    Blocking in: %s\n",
+		c->name, c->type, c->uniqueid,
+		(c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
+		(c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
+		(c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
+		c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
+		c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
+		cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>", 
+		c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
+		( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
+		(ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
+	
+	if(pbx_builtin_serialize_variables(c,buf,sizeof(buf)))
+		ast_cli(fd,"      Variables:\n%s\n",buf);
+	if(c->cdr && ast_cdr_serialize_variables(c->cdr,buf, sizeof(buf), '=', '\n', 1))
+		ast_cli(fd,"  CDR Variables:\n%s\n",buf);
+	
+	ast_mutex_unlock(&c->lock);
 	return RESULT_SUCCESS;
 }
 
 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
 {
-	struct ast_channel *c;
+	struct ast_channel *c = NULL;
 	int which=0;
-	char *ret;
+	char *ret = NULL;
+
 	if (pos != rpos)
 		return NULL;
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
+	while ( (c = ast_channel_walk_locked(c)) != NULL) {
 		if (!strncasecmp(word, c->name, strlen(word))) {
-			if (++which > state)
+			if (++which > state) {
+				ret = strdup(c->name);
+				ast_mutex_unlock(&c->lock);
 				break;
+			}
 		}
 		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
 	}
-	if (c) {
-		ret = strdup(c->name);
-		ast_mutex_unlock(&c->lock);
-	} else
-		ret = NULL;
 	return ret;
 }
 

Index: manager.c
===================================================================
RCS file: /usr/cvsroot/asterisk/manager.c,v
retrieving revision 1.97
retrieving revision 1.98
diff -u -d -r1.97 -r1.98
--- manager.c	17 May 2005 18:30:44 -0000	1.97
+++ manager.c	6 Jun 2005 02:29:17 -0000	1.98
@@ -574,14 +574,7 @@
 		astman_send_error(s, m, "No channel specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -615,14 +608,7 @@
 		return 0;
 	}
 
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -660,14 +646,7 @@
 		return 0;
 	}
 
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;
@@ -702,25 +681,22 @@
 	char bridge[256];
 	struct timeval now;
 	long elapsed_seconds=0;
+	int all = !name || ast_strlen_zero(name); /* set if we want all channels */
 
 	gettimeofday(&now, NULL);
 	astman_send_ack(s, m, "Channel status will follow");
-	c = ast_channel_walk_locked(NULL);
         if (id && !ast_strlen_zero(id))
                 snprintf(idText,256,"ActionID: %s\r\n",id);
-	if (name && !ast_strlen_zero(name)) {
-		while (c) {
-			if (!strcasecmp(c->name, name)) {
-				break;
-			}
-			ast_mutex_unlock(&c->lock);
-			c = ast_channel_walk_locked(c);
-		}
+	if (all)
+		c = ast_channel_walk_locked(NULL);
+	else {
+		c = ast_get_channel_by_name_locked(name);
 		if (!c) {
 			astman_send_error(s, m, "No such channel");
 			return 0;
 		}
 	}
+	/* if we look by name, we break after the first iteration */
 	while(c) {
 		if (c->_bridge)
 			snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->_bridge->name);
@@ -773,10 +749,8 @@
 			ast_state2str(c->_state), bridge, c->uniqueid, idText);
 		}
 		ast_mutex_unlock(&s->lock);
-		ast_mutex_unlock(&c->lock);
-		if (name && !ast_strlen_zero(name)) {
+		if (!all)
 			break;
-		}
 		c = ast_channel_walk_locked(c);
 	}
 	ast_mutex_lock(&s->lock);
@@ -1175,14 +1149,7 @@
 		astman_send_error(s, m, "No timeout specified");
 		return 0;
 	}
-	c = ast_channel_walk_locked(NULL);
-	while(c) {
-		if (!strcasecmp(c->name, name)) {
-			break;
-		}
-		ast_mutex_unlock(&c->lock);
-		c = ast_channel_walk_locked(c);
-	}
+	c = ast_get_channel_by_name_locked(name);
 	if (!c) {
 		astman_send_error(s, m, "No such channel");
 		return 0;

Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.252
retrieving revision 1.253
diff -u -d -r1.252 -r1.253
--- pbx.c	5 Jun 2005 16:32:16 -0000	1.252
+++ pbx.c	6 Jun 2005 02:29:17 -0000	1.253
@@ -4453,14 +4453,7 @@
 	struct ast_channel *chan;
 	int res = -1;
 
-	chan = ast_channel_walk_locked(NULL);
-	while(chan) {
-		if (!strcasecmp(channame, chan->name))
-			break;
-		ast_mutex_unlock(&chan->lock);
-		chan = ast_channel_walk_locked(chan);
-	}
-	
+	chan = ast_get_channel_by_name_locked(channame);
 	if (chan) {
 		res = ast_async_goto(chan, context, exten, priority);
 		ast_mutex_unlock(&chan->lock);
@@ -5803,7 +5796,7 @@
 	char *value;
 	char *stringp=NULL;
 	char *channel;
-	struct ast_channel *chan2=NULL;
+	struct ast_channel *chan2;
 	char tmp[4096]="";
 	char *s;
 
@@ -5817,11 +5810,7 @@
 	channel = strsep(&stringp,"|"); 
 	value = strsep(&stringp,"\0");
 	if (channel && value && name) {
-		while((chan2 = ast_channel_walk_locked(chan2))) {
-			if (!strcmp(chan2->name, channel))
-				break;
-			ast_mutex_unlock(&chan2->lock);
-		}
+		chan2 = ast_get_channel_by_name_locked(channel);
 		if (chan2) {
 			s = alloca(strlen(value) + 4);
 			if (s) {




More information about the svn-commits mailing list