[asterisk-commits] mmichelson: trunk r157427 - /trunk/channels/chan_sip.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Nov 18 14:23:59 CST 2008


Author: mmichelson
Date: Tue Nov 18 14:23:58 2008
New Revision: 157427

URL: http://svn.digium.com/view/asterisk?view=rev&rev=157427
Log:
* Add a lock to be used in the update_call_counter function.
* Revert logic to mirror 1.4's in the sense that it will not allow
  the call counter to dip below 0.

These two measures prevent potential races that could cause a SIP peer
to appear to be busy forever.

(closes issue #13668)
Reported by: mjc
Patches:
      hintfix_trunk_rev152649.patch uploaded by wolfelectronic (license 586)


Modified:
    trunk/channels/chan_sip.c

Modified: trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_sip.c?view=diff&rev=157427&r1=157426&r2=157427
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Tue Nov 18 14:23:58 2008
@@ -2118,6 +2118,9 @@
 	SDP_T38_ACCEPT,   /*!< Remote side accepted our T38 request */
 };
 
+/*! \brief Protect the callcounters inuse,inringing and the corresponding flags */
+AST_MUTEX_DEFINE_STATIC(callctrlock);
+
 /*---------------------------- Forward declarations of functions in chan_sip.c */
 /* Note: This is added to help splitting up chan_sip.c into several files
 	in coming releases. */
@@ -5080,12 +5083,12 @@
 	ast_copy_string(name, fup->username, sizeof(name));
 
 	/* Check the list of devices */
-	if ( (p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FINDALLDEVICES, FALSE) ) ) { /* Try to find peer */
+	if ((p = find_peer(ast_strlen_zero(fup->peername) ? name : fup->peername, NULL, TRUE, FINDALLDEVICES, FALSE))) { 
 		inuse = &p->inUse;
 		call_limit = &p->call_limit;
 		inringing = &p->inRinging;
 		ast_copy_string(name, fup->peername, sizeof(name));
-	} 
+	}
 	if (!p) {
 		ast_debug(2, "%s is not a local device, no call limit\n", name);
 		return 0;
@@ -5095,20 +5098,37 @@
 	/* incoming and outgoing affects the inUse counter */
 	case DEC_CALL_LIMIT:
 		/* Decrement inuse count if applicable */
-		if (inuse && *inuse > 0 && ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
-			ast_atomic_fetchadd_int(inuse, -1);
-			ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
-		} else
-			*inuse = 0;
+		if (inuse) {
+			ast_mutex_lock(&callctrlock);
+			if ((*inuse > 0) && ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
+				(*inuse)--;
+				ast_clear_flag(&fup->flags[0], SIP_INC_COUNT);
+			} else {
+				*inuse = 0;
+			}
+			ast_mutex_unlock(&callctrlock);
+		}
+
 		/* Decrement ringing count if applicable */
-		if (inringing && *inringing > 0 && ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-			ast_atomic_fetchadd_int(inringing, -1);
-			ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
-		}
+		if (inringing) {
+			ast_mutex_lock(&callctrlock);
+			if ((*inringing > 0)&& ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
+				(*inringing)--;
+				ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
+			} else {
+			   *inringing = 0;
+			}
+			ast_mutex_unlock(&callctrlock);
+		}
+
 		/* Decrement onhold count if applicable */
+		ast_mutex_lock(&callctrlock);
 		if (ast_test_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD) && global_notifyhold) {
 			ast_clear_flag(&fup->flags[1], SIP_PAGE2_CALL_ONHOLD);
+			ast_mutex_unlock(&callctrlock);
 			sip_peer_hold(fup, FALSE);
+		} else {
+			ast_mutex_unlock(&callctrlock);
 		}
 		if (sipdebug)
 			ast_debug(2, "Call %s %s '%s' removed from call limit %d\n", outgoing ? "to" : "from", "peer", name, *call_limit);
@@ -5125,29 +5145,41 @@
 			}
 		}
 		if (inringing && (event == INC_CALL_RINGING)) {
+			ast_mutex_lock(&callctrlock);
 			if (!ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-				ast_atomic_fetchadd_int(inringing, +1);
+				(*inringing)++;
 				ast_set_flag(&fup->flags[0], SIP_INC_RINGING);
 			}
-		}
-		/* Continue */
-		ast_atomic_fetchadd_int(inuse, +1);
-		ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
+			ast_mutex_unlock(&callctrlock);
+		}
+		if (inuse) {
+			ast_mutex_lock(&callctrlock);
+			if (!ast_test_flag(&fup->flags[0], SIP_INC_COUNT)) {
+				(*inuse)++;
+				ast_set_flag(&fup->flags[0], SIP_INC_COUNT);
+			}
+			ast_mutex_unlock(&callctrlock);
+		}
 		if (sipdebug) {
 			ast_debug(2, "Call %s %s '%s' is %d out of %d\n", outgoing ? "to" : "from", "peer", name, *inuse, *call_limit);
 		}
 		break;
 
 	case DEC_CALL_RINGING:
-		if (inringing && *inringing > 0 && ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
-			ast_atomic_fetchadd_int(inringing, -1);
-			ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
+		if (inringing) {
+			ast_mutex_lock(&callctrlock);
+			if (ast_test_flag(&fup->flags[0], SIP_INC_RINGING)) {
+				(*inringing)--;
+				ast_clear_flag(&fup->flags[0], SIP_INC_RINGING);
+			}
+			ast_mutex_unlock(&callctrlock);
 		}
 		break;
 
 	default:
 		ast_log(LOG_ERROR, "update_call_counter(%s, %d) called with no event!\n", name, event);
 	}
+
 	if (p) {
 		ast_devstate_changed(AST_DEVICE_UNKNOWN, "SIP/%s", p->name);
 		unref_peer(p, "update_call_counter: unref_peer from call counter");




More information about the asterisk-commits mailing list