[asterisk-commits] branch group/sip-threading r26455 - /team/group/sip-threading/channels/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed May 10 06:43:30 MST 2006


Author: file
Date: Wed May 10 08:43:30 2006
New Revision: 26455

URL: http://svn.digium.com/view/asterisk?rev=26455&view=rev
Log:
Convert storage of private structures to use linkedlists.h

Modified:
    team/group/sip-threading/channels/chan_sip.c

Modified: team/group/sip-threading/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/team/group/sip-threading/channels/chan_sip.c?rev=26455&r1=26454&r2=26455&view=diff
==============================================================================
--- team/group/sip-threading/channels/chan_sip.c (original)
+++ team/group/sip-threading/channels/chan_sip.c Wed May 10 08:43:30 2006
@@ -487,9 +487,6 @@
 
 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
 
-/*! \brief Protect the SIP dialog list (of sip_pvt's) */
-AST_MUTEX_DEFINE_STATIC(iflock);
-
 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
    when it's doing something critical. */
 AST_MUTEX_DEFINE_STATIC(netlock);
@@ -735,7 +732,7 @@
 };
 
 /*! \brief sip_pvt: PVT structures are used for each SIP dialog, ie. a call, a registration, a subscribe  */
-static struct sip_pvt {
+struct sip_pvt {
 	ast_mutex_t lock;			/*!< Dialog private lock */
 	int method;				/*!< SIP method that opened this dialog */
 	AST_DECLARE_STRING_FIELDS(
@@ -843,9 +840,11 @@
 	struct sip_pkt *packets;		/*!< Packets scheduled for re-transmission */
 	struct sip_history_head *history;	/*!< History of this SIP dialog */
 	struct ast_variable *chanvars;		/*!< Channel variables to set for inbound call */
-	struct sip_pvt *next;			/*!< Next dialog in chain */
+	AST_LIST_ENTRY(sip_pvt) entry;			/*!< Next dialog in chain */
 	struct sip_invite_param *options;	/*!< Options for INVITE */
-} *iflist = NULL;
+};
+
+static AST_LIST_HEAD_STATIC(iflist, sip_pvt);
 
 #define FLAG_RESPONSE (1 << 0)
 #define FLAG_FATAL (1 << 1)
@@ -2333,8 +2332,7 @@
 /*! \brief Execute destrucion of SIP dialog structure, release memory */
 static void __sip_destroy(struct sip_pvt *p, int lockowner)
 {
-	struct sip_pvt *cur, *prev = NULL;
-	struct sip_pkt *cp;
+	struct sip_pkt *cp = NULL;
 
 	if (sip_debug_test_pvt(p) || option_debug > 2)
 		ast_verbose("Really destroying SIP dialog '%s' Method: %s\n", p->callid, sip_methods[p->method].text);
@@ -2391,19 +2389,10 @@
 		p->history = NULL;
 	}
 
-	for (prev = NULL, cur = iflist; cur; prev = cur, cur = cur->next) {
-		if (cur == p) {
-			if (prev)
-				prev->next = cur->next;
-			else
-				iflist = cur->next;
-			break;
-		}
-	}
-	if (!cur) {
-		ast_log(LOG_WARNING, "Trying to destroy \"%s\", not found in dialog list?!?! \n", p->callid);
-		return;
-	} 
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_REMOVE(&iflist, p, entry);
+	AST_LIST_UNLOCK(&iflist);
+
 	if (p->initid > -1)
 		ast_sched_del(sched, p->initid);
 
@@ -2521,11 +2510,9 @@
 /*! \brief Destroy SIP call structure */
 static void sip_destroy(struct sip_pvt *p)
 {
-	ast_mutex_lock(&iflock);
 	if (option_debug > 2)
 		ast_log(LOG_DEBUG, "Destroying SIP dialog %s\n", p->callid);
 	__sip_destroy(p, 1);
-	ast_mutex_unlock(&iflock);
 }
 
 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
@@ -3455,10 +3442,9 @@
 	ast_string_field_set(p, context, default_context);
 
 	/* Add to active dialog list */
-	ast_mutex_lock(&iflock);
-	p->next = iflist;
-	iflist = p;
-	ast_mutex_unlock(&iflock);
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_INSERT_HEAD(&iflist, p, entry);
+	AST_LIST_UNLOCK(&iflist);
 	if (option_debug)
 		ast_log(LOG_DEBUG, "Allocating new SIP dialog for %s - %s (%s)\n", callid ? callid : "(No Call-ID)", sip_methods[intended_method].text, p->rtp ? "With RTP" : "No RTP");
 	return p;
@@ -3497,8 +3483,8 @@
 			ast_log(LOG_DEBUG, "= Looking for  Call ID: %s (Checking %s) --From tag %s --To-tag %s  \n", callid, req->method==SIP_RESPONSE ? "To" : "From", fromtag, totag);
 	}
 
-	ast_mutex_lock(&iflock);
-	for (p = iflist; p; p = p->next) {
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, p, entry) {
 		/* In pedantic, we do not want packets with bad syntax to be connected to a PVT */
 		int found = FALSE;
 		if (req->method == SIP_REGISTER)
@@ -3528,11 +3514,11 @@
 		if (found) {
 			/* Found the call */
 			ast_mutex_lock(&p->lock);
-			ast_mutex_unlock(&iflock);
+			AST_LIST_UNLOCK(&iflist);
 			return p;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
 	/* Allocate new call */
 	if ((p = sip_alloc(callid, sin, 1, intended_method)))
 		ast_mutex_lock(&p->lock);
@@ -6939,13 +6925,11 @@
 {
 	struct sip_pvt *sip_pvt_ptr = NULL;
 	
-	/* Search interfaces and find the match */
-	ast_mutex_lock(&iflock);
-
 	if (option_debug > 3 && totag)
 		ast_log(LOG_DEBUG, "Looking for callid %s (fromtag %s totag %s)\n", callid, fromtag ? fromtag : "<no fromtag>", totag ? totag : "<no totag>");
 
-	for (sip_pvt_ptr = iflist; sip_pvt_ptr; sip_pvt_ptr = sip_pvt_ptr->next) {
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, sip_pvt_ptr, entry) {
 		if (!strcmp(sip_pvt_ptr->callid, callid)) {
 			int match = 1;
 			char *ourtag = sip_pvt_ptr->tag;
@@ -6980,7 +6964,8 @@
 			break;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
+
 	if (option_debug > 3 && !sip_pvt_ptr)
 		ast_log(LOG_DEBUG, "Found no match for callid %s to-tag %s from-tag %s\n", callid, totag, fromtag);
 	return sip_pvt_ptr;
@@ -8717,20 +8702,21 @@
 #define FORMAT3 "%-15.15s  %-10.10s  %-11.11s  %-15.15s  %-13.13s  %-15.15s %-10.10s\n"
 #define FORMAT2 "%-15.15s  %-10.10s  %-11.11s  %-11.11s  %-4.4s  %-7.7s  %-15.15s\n"
 #define FORMAT  "%-15.15s  %-10.10s  %-11.11s  %5.5d/%5.5d  %-4.4s  %-3.3s %-3.3s  %-15.15s %-10.10s\n"
-	struct sip_pvt *cur;
+	struct sip_pvt *cur = NULL;
 	char iabuf[INET_ADDRSTRLEN];
 	int numchans = 0;
 	char *referstatus = NULL;
 
 	if (argc != 3)
 		return RESULT_SHOWUSAGE;
-	ast_mutex_lock(&iflock);
-	cur = iflist;
+
 	if (!subscriptions)
 		ast_cli(fd, FORMAT2, "Peer", "User/ANR", "Call ID", "Seq (Tx/Rx)", "Format", "Hold", "Last Message");
 	else 
 		ast_cli(fd, FORMAT3, "Peer", "User", "Call ID", "Extension", "Last state", "Type", "Mailbox");
-	for (; cur; cur = cur->next) {
+
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, cur, entry) {
 		referstatus = "";
 		if (cur->refer) { /* SIP transfer in progress */
 			referstatus = referstatus2str(cur->refer->status);
@@ -8761,7 +8747,7 @@
 			numchans++;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
 	if (!subscriptions)
 		ast_cli(fd, "%d active SIP channel%s\n", numchans, (numchans != 1) ? "s" : "");
 	else
@@ -8775,19 +8761,20 @@
 /*! \brief  complete_sipch: Support routine for 'sip show channel' CLI */
 static char *complete_sipch(const char *line, const char *word, int pos, int state)
 {
-	int which=0;
-	struct sip_pvt *cur;
+	int which = 0;
+	struct sip_pvt *cur = NULL;
 	char *c = NULL;
 	int wordlen = strlen(word);
 
-	ast_mutex_lock(&iflock);
-	for (cur = iflist; cur; cur = cur->next) {
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, cur, entry) {
 		if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
 			c = ast_strdup(cur->callid);
 			break;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
+
 	return c;
 }
 
@@ -8905,7 +8892,7 @@
 /*! \brief  sip_show_channel: Show details of one call */
 static int sip_show_channel(int fd, int argc, char *argv[])
 {
-	struct sip_pvt *cur;
+	struct sip_pvt *cur = NULL;
 	char iabuf[INET_ADDRSTRLEN];
 	size_t len;
 	int found = 0;
@@ -8913,8 +8900,9 @@
 	if (argc != 4)
 		return RESULT_SHOWUSAGE;
 	len = strlen(argv[3]);
-	ast_mutex_lock(&iflock);
-	for (cur = iflist; cur; cur = cur->next) {
+
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, cur, entry) {
 		if (!strncasecmp(cur->callid, argv[3], len)) {
 			ast_cli(fd,"\n");
 			if (cur->subscribed != NONE)
@@ -8962,7 +8950,8 @@
 			found++;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
+
 	if (!found) 
 		ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
 	return RESULT_SUCCESS;
@@ -8971,7 +8960,7 @@
 /*! \brief  sip_show_history: Show history details of one call */
 static int sip_show_history(int fd, int argc, char *argv[])
 {
-	struct sip_pvt *cur;
+	struct sip_pvt *cur = NULL;
 	size_t len;
 	int found = 0;
 
@@ -8980,8 +8969,9 @@
 	if (!recordhistory)
 		ast_cli(fd, "\n***Note: History recording is currently DISABLED.  Use 'sip history' to ENABLE.\n");
 	len = strlen(argv[3]);
-	ast_mutex_lock(&iflock);
-	for (cur = iflist; cur; cur = cur->next) {
+
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, cur, entry) {
 		if (!strncasecmp(cur->callid, argv[3], len)) {
 			struct sip_history *hist;
 			int x = 0;
@@ -8999,9 +8989,11 @@
 			found++;
 		}
 	}
-	ast_mutex_unlock(&iflock);
+	AST_LIST_UNLOCK(&iflist);
+
 	if (!found) 
 		ast_cli(fd, "No such SIP Call ID starting with '%s'\n", argv[3]);
+
 	return RESULT_SUCCESS;
 }
 
@@ -11637,8 +11629,8 @@
 			   	for it to expire and send NOTIFY messages to the peer only to have them
 			   	ignored (or generate errors)
 				*/
-				ast_mutex_lock(&iflock);
-				for (p_old = iflist; p_old; p_old = p_old->next) {
+				AST_LIST_LOCK(&iflist);
+				AST_LIST_TRAVERSE(&iflist, p_old, entry) {
 					if (p_old == p)
 						continue;
 					if (p_old->initreq.method != SIP_SUBSCRIBE)
@@ -11656,7 +11648,7 @@
 					}
 					ast_mutex_unlock(&p_old->lock);
 				}
-				ast_mutex_unlock(&iflock);
+				AST_LIST_UNLOCK(&iflist);
 			}
 		}
 		if (!p->expiry)
@@ -12124,7 +12116,7 @@
 static void *do_monitor(void *data)
 {
 	int res;
-	struct sip_pvt *sip;
+	struct sip_pvt *sip = NULL;
 	struct sip_peer *peer = NULL;
 	time_t t;
 	int fastrestart = FALSE;
@@ -12149,10 +12141,10 @@
 			sip_do_reload(sip_reloadreason);
 		}
 		/* Check for interfaces needing to be killed */
-		ast_mutex_lock(&iflock);
+		AST_LIST_LOCK(&iflist);
 restartsearch:		
 		time(&t);
-		for (sip = iflist; sip; sip = sip->next) {
+		AST_LIST_TRAVERSE(&iflist, sip, entry) {
 			ast_mutex_lock(&sip->lock);
 			/* Check RTP timeouts and kill calls if we have a timeout set and do not get RTP */
 			if (sip->rtp && sip->owner && (sip->owner->_state == AST_STATE_UP) && !sip->redirip.sin_addr.s_addr) {
@@ -12193,7 +12185,7 @@
 			}
 			ast_mutex_unlock(&sip->lock);
 		}
-		ast_mutex_unlock(&iflock);
+		AST_LIST_UNLOCK(&iflist);
 
 		pthread_testcancel();
 		/* Wait for sched or io */
@@ -14105,7 +14097,7 @@
 static int unload_module(void *mod)
 {
 	struct sip_thread *thread = NULL;
-	struct sip_pvt *p, *pl;
+	struct sip_pvt *p = NULL;
 	
 	/* First, take us out of the channel type list */
 	ast_channel_unregister(&sip_tech);
@@ -14154,17 +14146,12 @@
 	}
 	AST_LIST_UNLOCK(&active_list);
 
-	if (!ast_mutex_lock(&iflock)) {
-		/* Hangup all interfaces if they have an owner */
-		for (p = iflist; p ; p = p->next) {
-			if (p->owner)
-				ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
-		}
-		ast_mutex_unlock(&iflock);
-	} else {
-		ast_log(LOG_WARNING, "Unable to lock the interface list\n");
-		return -1;
-	}
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, p, entry) {
+		if (p->owner)
+			ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
+	}
+	AST_LIST_UNLOCK(&iflist);
 
 	if (!ast_mutex_lock(&monlock)) {
 		if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
@@ -14179,26 +14166,14 @@
 		return -1;
 	}
 
-	if (!ast_mutex_lock(&iflock)) {
-		/* Destroy all the interfaces and free their memory */
-		p = iflist;
-		while (p) {
-			pl = p;
-			p = p->next;
-			/* Free associated memory */
-			ast_mutex_destroy(&pl->lock);
-			if (pl->chanvars) {
-				ast_variables_destroy(pl->chanvars);
-				pl->chanvars = NULL;
-			}
-			free(pl);
-		}
-		iflist = NULL;
-		ast_mutex_unlock(&iflock);
-	} else {
-		ast_log(LOG_WARNING, "Unable to lock the interface list\n");
-		return -1;
-	}
+	AST_LIST_LOCK(&iflist);
+	AST_LIST_TRAVERSE(&iflist, p, entry) {
+		ast_mutex_destroy(&p->lock);
+		if (p->chanvars)
+			ast_variables_destroy(p->chanvars);
+		free(p);
+	}
+	AST_LIST_UNLOCK(&iflist);
 
 	/* Free memory for local network address mask */
 	ast_free_ha(localaddr);



More information about the asterisk-commits mailing list