[asterisk-commits] trunk r25443 - in /trunk: include/asterisk/pbx.h pbx.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon May 8 04:20:20 MST 2006


Author: russell
Date: Mon May  8 06:20:19 2006
New Revision: 25443

URL: http://svn.digium.com/view/asterisk?rev=25443&view=rev
Log:
 - convert the lists of switches to use the linked list macros
 - remove some checks of the result of ast_mutex_lock, since it is not necessary
   (this would be a good project to add to the janitor projects list).

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

Modified: trunk/include/asterisk/pbx.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/pbx.h?rev=25443&r1=25442&r2=25443&view=diff
==============================================================================
--- trunk/include/asterisk/pbx.h (original)
+++ trunk/include/asterisk/pbx.h Mon May  8 06:20:19 2006
@@ -25,6 +25,7 @@
 
 #include "asterisk/sched.h"
 #include "asterisk/channel.h"
+#include "asterisk/linkedlists.h"
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
@@ -80,7 +81,7 @@
 
 /*!< Data structure associated with an Asterisk switch */
 struct ast_switch {
-	struct ast_switch *next;	
+	AST_LIST_ENTRY(ast_switch) list;
 	const char *name;			/*!< Name of the switch */
 	const char *description;		/*!< Description of the switch */
 	

Modified: trunk/pbx.c
URL: http://svn.digium.com/view/asterisk/trunk/pbx.c?rev=25443&r1=25442&r2=25443&view=diff
==============================================================================
--- trunk/pbx.c (original)
+++ trunk/pbx.c Mon May  8 06:20:19 2006
@@ -144,7 +144,7 @@
 	const char *registrar;			/*!< Registrar */
 	char *data;				/*!< Data load */
 	int eval;
-	struct ast_sw *next;			/*!< Link them together */
+	AST_LIST_ENTRY(ast_sw) list;
 	char *tmpdata;
 	char stuff[0];
 };
@@ -164,7 +164,7 @@
 	struct ast_include *includes;		/*!< Include other contexts */
 	struct ast_ignorepat *ignorepats;	/*!< Patterns for which to continue playing dialtone */
 	const char *registrar;			/*!< Registrar */
-	struct ast_sw *alts;			/*!< Alternative switches */
+	AST_LIST_HEAD_NOLOCK(, ast_sw) alts;	/*!< Alternative switches */
 	char name[0];				/*!< Name of the context */
 };
 
@@ -466,8 +466,7 @@
 
 static AST_LIST_HEAD_STATIC(apps, ast_app);
 
-struct ast_switch *switches = NULL;
-AST_MUTEX_DEFINE_STATIC(switchlock);		/*!< Lock for switches */
+static AST_LIST_HEAD_STATIC(switches, ast_switch);
 
 static int stateid = 1;
 /* WARNING:
@@ -544,15 +543,13 @@
 {
 	struct ast_switch *asw;
 
-	if (ast_mutex_lock(&switchlock)) {
-		ast_log(LOG_WARNING, "Unable to obtain application lock\n");
-		return NULL;
-	}
-	for (asw = switches; asw; asw = asw->next) {
+	AST_LIST_LOCK(&switches);
+	AST_LIST_TRAVERSE(&switches, asw, list) {
 		if (!strcasecmp(asw->name, sw))
 			break;
 	}
-	ast_mutex_unlock(&switchlock);
+	AST_LIST_UNLOCK(&switches);
+
 	return asw;
 }
 
@@ -797,7 +794,7 @@
 				return NULL;
 			}
 			/* Check alternative switches */
-			for (sw = tmp->alts; sw; sw = sw->next) {
+			AST_LIST_TRAVERSE(&tmp->alts, sw, list) {
 				if ((asw = pbx_findswitch(sw->name))) {
 					/* Substitute variables now */
 					if (sw->eval) 
@@ -1466,13 +1463,8 @@
 	char atmp[80];
 	char atmp2[EXT_DATA_SIZE+100];
 
-	if (ast_mutex_lock(&conlock)) {
-		ast_log(LOG_WARNING, "Unable to obtain lock\n");
-		if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH) || (action == HELPER_MATCHMORE))
-			return 0;
-		else
-			return -1;
-	}
+	ast_mutex_lock(&conlock);
+
 	e = pbx_find_extension(c, con, context, exten, priority, label, callerid, action, incstack, &stacklen, &status, &sw, &data, &foundcontext);
 	if (e) {
 		switch(action) {
@@ -1599,12 +1591,10 @@
 	char *incstack[AST_PBX_MAX_STACK];
 	int stacklen = 0;
 
-	if (ast_mutex_lock(&conlock)) {
-		ast_log(LOG_WARNING, "Unable to obtain lock\n");
-		return NULL;
-	}
+	ast_mutex_lock(&conlock);
 	e = pbx_find_extension(c, NULL, context, exten, PRIORITY_HINT, NULL, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data, &foundcontext);
-	ast_mutex_unlock(&conlock);	
+	ast_mutex_unlock(&conlock);
+
 	return e;
 }
 
@@ -2357,16 +2347,14 @@
 static struct ast_context *find_context_locked(const char *context)
 {
 	struct ast_context *c = NULL;
-	if (ast_lock_contexts()) {
-		errno = EBUSY;
-		return NULL;
-	}
+
+	ast_lock_contexts();
 	while ( (c = ast_walk_contexts(c)) ) {
 		if (!strcmp(ast_get_context_name(c), context))
 			return c;
 	}
 	ast_unlock_contexts();
-	errno = ENOENT;
+
 	return NULL;
 }
 
@@ -2453,27 +2441,26 @@
  */
 int ast_context_remove_switch2(struct ast_context *con, const char *sw, const char *data, const char *registrar)
 {
-	struct ast_sw *i, *pi = NULL;
+	struct ast_sw *i;
 	int ret = -1;
 
-	if (ast_mutex_lock(&con->lock))
-		return -1;
-
-	/* walk switchs */
-	for (i = con->alts; i; pi = i, i = i->next) {
+	ast_mutex_lock(&con->lock);
+
+	/* walk switches */
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&con->alts, i, list) {
 		if (!strcmp(i->name, sw) && !strcmp(i->data, data) && 
 			(!registrar || !strcmp(i->registrar, registrar))) {
 			/* found, remove from list */
-			if (pi)
-				pi->next = i->next;
-			else
-				con->alts = i->next;
+			AST_LIST_REMOVE_CURRENT(&con->alts, list);
 			free(i); /* free switch and return */
 			ret = 0;
 			break;
 		}
 	}
+	AST_LIST_TRAVERSE_SAFE_END
+
 	ast_mutex_unlock(&con->lock);
+
 	return ret;
 }
 
@@ -2508,8 +2495,7 @@
 {
 	struct ast_exten *exten, *prev_exten = NULL;
 
-	if (ast_mutex_lock(&con->lock))
-		return -1;
+	ast_mutex_lock(&con->lock);
 
 	/* go through all extensions in context and search the right one ... */
 	exten = con->root;
@@ -2662,47 +2648,27 @@
  */
 int ast_register_switch(struct ast_switch *sw)
 {
-	struct ast_switch *tmp, *prev=NULL;
-	if (ast_mutex_lock(&switchlock)) {
-		ast_log(LOG_ERROR, "Unable to lock switch lock\n");
-		return -1;
-	}
-	for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) {
-		if (!strcasecmp(tmp->name, sw->name))
-			break;
-	}
-	if (tmp) {	
-		ast_mutex_unlock(&switchlock);
-		ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
-		return -1;
-	}
-	sw->next = NULL;
-	if (prev) 
-		prev->next = sw;
-	else
-		switches = sw;
-	ast_mutex_unlock(&switchlock);
+	struct ast_switch *tmp;
+
+	AST_LIST_LOCK(&switches);
+	AST_LIST_TRAVERSE(&switches, tmp, list) {
+		if (!strcasecmp(tmp->name, sw->name)) {
+			AST_LIST_UNLOCK(&switches);
+			ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
+			return -1;
+		}
+	}
+	AST_LIST_INSERT_TAIL(&switches, sw, list);
+	AST_LIST_UNLOCK(&switches);
+	
 	return 0;
 }
 
 void ast_unregister_switch(struct ast_switch *sw)
 {
-	struct ast_switch *tmp, *prev=NULL;
-	if (ast_mutex_lock(&switchlock)) {
-		ast_log(LOG_ERROR, "Unable to lock switch lock\n");
-		return;
-	}
-	for (tmp = switches; tmp; prev = tmp, tmp = tmp->next) {
-		if (tmp == sw) {
-			if (prev)
-				prev->next = tmp->next;
-			else
-				switches = tmp->next;
-			tmp->next = NULL;
-			break;			
-		}
-	}
-	ast_mutex_unlock(&switchlock);
+	AST_LIST_LOCK(&switches);
+	AST_LIST_REMOVE(&switches, sw, list);
+	AST_LIST_UNLOCK(&switches);
 }
 
 /*
@@ -2863,10 +2829,7 @@
 	}
 	/* ... we have hints ... */
 	ast_cli(fd, "\n    -= Registered Asterisk Dial Plan Hints =-\n");
-	if (AST_LIST_LOCK(&hints)) {
-		ast_log(LOG_ERROR, "Unable to lock hints\n");
-		return -1;
-	}
+	AST_LIST_LOCK(&hints);
 	AST_LIST_TRAVERSE(&hints, hint, list) {
 		watchers = 0;
 		for (watcher = hint->callbacks; watcher; watcher = watcher->next)
@@ -2888,19 +2851,21 @@
 static int handle_show_switches(int fd, int argc, char *argv[])
 {
 	struct ast_switch *sw;
-	if (!switches) {
+
+	AST_LIST_LOCK(&switches);
+	
+	if (AST_LIST_EMPTY(&switches)) {
+		AST_LIST_UNLOCK(&switches);
 		ast_cli(fd, "There are no registered alternative switches\n");
 		return RESULT_SUCCESS;
 	}
-	/* ... we have applications ... */
+
 	ast_cli(fd, "\n    -= Registered Asterisk Alternative Switches =-\n");
-	if (ast_mutex_lock(&switchlock)) {
-		ast_log(LOG_ERROR, "Unable to lock switches\n");
-		return -1;
-	}
-	for (sw = switches; sw; sw = sw->next)
+	AST_LIST_TRAVERSE(&switches, sw, list)
 		ast_cli(fd, "%s: %s\n", sw->name, sw->description);
-	ast_mutex_unlock(&switchlock);
+
+	AST_LIST_UNLOCK(&switches);
+
 	return RESULT_SUCCESS;
 }
 
@@ -2998,11 +2963,7 @@
 	if (pos != 2)
 		return NULL;
 
-	/* try to lock contexts list ... */
-	if (ast_lock_contexts()) {
-		ast_log(LOG_ERROR, "Unable to lock context list\n");
-		return NULL;
-	}
+	ast_lock_contexts();
 
 	wordlen = strlen(word);
 
@@ -3014,8 +2975,8 @@
 		}
 	}
 
-	/* ... unlock and return */
 	ast_unlock_contexts();
+
 	return ret;
 }
 
@@ -3032,11 +2993,7 @@
 	struct ast_context *c = NULL;
 	int res = 0, old_total_exten = dpc->total_exten;
 
-	/* try to lock contexts */
-	if (ast_lock_contexts()) {
-		ast_log(LOG_WARNING, "Failed to lock contexts list\n");
-		return -1;
-	}
+	ast_lock_contexts();
 
 	/* walk all contexts ... */
 	while ( (c = ast_walk_contexts(c)) ) {
@@ -3045,8 +3002,8 @@
 			!strcmp(ast_get_context_name(c), context)) {
 			dpc->context_existence = 1;
 
-			/* try to lock context before walking in ... */
-			if (!ast_lock_context(c)) {
+			ast_lock_context(c);
+			/* XXX re-indent this block */
 				struct ast_exten *e;
 				struct ast_include *i;
 				struct ast_ignorepat *ip;
@@ -3201,7 +3158,6 @@
 
 				/* if we print something in context, make an empty line */
 				if (context_info_printed) ast_cli(fd, "\r\n");
-			}
 		}
 	}
 	ast_unlock_contexts();
@@ -3800,12 +3756,7 @@
 	new_include->next      = NULL;
 	new_include->registrar = registrar;
 
-	/* ... try to lock this context ... */
-	if (ast_mutex_lock(&con->lock)) {
-		free(new_include);
-		errno = EBUSY;
-		return -1;
-	}
+	ast_mutex_lock(&con->lock);
 
 	/* ... go to last include and check if context is already included too... */
 	for (i = con->includes; i; i = i->next) {
@@ -3858,7 +3809,7 @@
 	const char *data, int eval, const char *registrar)
 {
 	struct ast_sw *new_sw;
-	struct ast_sw *i, *il = NULL; /* sw, sw_last */
+	struct ast_sw *i;
 	int length;
 	char *p;
 	
@@ -3892,35 +3843,28 @@
 	}
 	if (eval) 
 		new_sw->tmpdata = p;
-	new_sw->next      = NULL;
 	new_sw->eval	  = eval;
 	new_sw->registrar = registrar;
 
 	/* ... try to lock this context ... */
-	if (ast_mutex_lock(&con->lock)) {
-		free(new_sw);
-		errno = EBUSY;
-		return -1;
-	}
+	ast_mutex_lock(&con->lock);
 
 	/* ... go to last sw and check if context is already swd too... */
-	for (i = con->alts; i; i = i->next) {
+	AST_LIST_TRAVERSE(&con->alts, i, list) {
 		if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
 			free(new_sw);
 			ast_mutex_unlock(&con->lock);
 			errno = EEXIST;
 			return -1;
 		}
-		il = i;
 	}
 
 	/* ... sw new context into context list, unlock, return */
-	if (il)
-		il->next = new_sw;
-	else
-		con->alts = new_sw;
+	AST_LIST_INSERT_TAIL(&con->alts, new_sw, list);
+	
 	if (option_verbose > 2)
-		ast_verbose(VERBOSE_PREFIX_3 "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con)); 
+		ast_verbose(VERBOSE_PREFIX_3 "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con));
+
 	ast_mutex_unlock(&con->lock);
 
 	return 0;
@@ -3946,10 +3890,7 @@
 {
 	struct ast_ignorepat *ip, *ipl = NULL;
 
-	if (ast_mutex_lock(&con->lock)) {
-		errno = EBUSY;
-		return -1;
-	}
+	ast_mutex_lock(&con->lock);
 
 	for (ip = con->ignorepats; ip; ip = ip->next) {
 		if (!strcmp(ip->pattern, ignorepat) &&
@@ -4252,14 +4193,7 @@
 	tmp->datad = datad;
 	tmp->registrar = registrar;
 	
-	if (ast_mutex_lock(&con->lock)) {
-		free(tmp);
-		/* And properly destroy the data */
-		datad(data);
-		ast_log(LOG_WARNING, "Failed to lock context '%s'\n", con->name);
-		errno = EBUSY;
-		return -1;
-	}
+	ast_mutex_lock(&con->lock);
 	for (e = con->root; e; e = e->next) {
 		/* Make sure patterns are always last! */
 		if ((e->exten[0] != '_') && (extension[0] == '_'))
@@ -4819,7 +4753,7 @@
 {
 	struct ast_context *tmp, *tmpl=NULL;
 	struct ast_include *tmpi, *tmpil= NULL;
-	struct ast_sw *sw, *swl= NULL;
+	struct ast_sw *sw;
 	struct ast_exten *e, *el, *en;
 	struct ast_ignorepat *ipi, *ipl = NULL;
 
@@ -4830,10 +4764,7 @@
 		    (!registrar || !strcasecmp(registrar, tmp->registrar))) {
 			/* Okay, let's lock the structure to be sure nobody else
 			   is searching through it. */
-			if (ast_mutex_lock(&tmp->lock)) {
-				ast_log(LOG_WARNING, "Unable to lock context lock\n");
-				break;
-			}
+			ast_mutex_lock(&tmp->lock);
 			if (tmpl)
 				tmpl->next = tmp->next;
 			else
@@ -4853,13 +4784,8 @@
 				ipi = ipi->next;
 				free(ipl);
 			}
-			for (sw = tmp->alts; sw; ) {
-				/* Free switches */
-				swl = sw;
-				sw = sw->next;
-				free(swl);
-				swl = sw;
-			}
+			while ((sw = AST_LIST_REMOVE_HEAD(&tmp->alts, list)))
+				free(sw);
 			for (e = tmp->root; e;) {
 				for (en = e->peer; en;) {
 					el = en;
@@ -5734,9 +5660,9 @@
 	struct ast_sw *sw)
 {
 	if (!sw)
-		return con ? con->alts : NULL;
+		return con ? AST_LIST_FIRST(&con->alts) : NULL;
 	else
-		return sw->next;
+		return AST_LIST_NEXT(sw, list);
 }
 
 struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,



More information about the asterisk-commits mailing list