[asterisk-commits] file: branch file/bridging r90308 - /team/file/bridging/main/bridging.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Nov 30 09:52:44 CST 2007


Author: file
Date: Fri Nov 30 09:52:44 2007
New Revision: 90308

URL: http://svn.digium.com/view/asterisk?view=rev&rev=90308
Log:
Move where we increment/decrement channel count of the bridge. It would have been possible for the bridge to be destroyed while bridged channels were still hanging up.

Modified:
    team/file/bridging/main/bridging.c

Modified: team/file/bridging/main/bridging.c
URL: http://svn.digium.com/view/asterisk/team/file/bridging/main/bridging.c?view=diff&rev=90308&r1=90307&r2=90308
==============================================================================
--- team/file/bridging/main/bridging.c (original)
+++ team/file/bridging/main/bridging.c Fri Nov 30 09:52:44 2007
@@ -483,9 +483,6 @@
 	/* Before we actually become part of this bridge make sure we are in the signalling wait state */
 	bridge_channel->state = AST_BRIDGE_CHANNEL_STATE_WAIT;
 
-	/* Increment bridged channel count */
-	ast_atomic_fetchadd_int(&bridge->num, +1);
-
 	/* Make the bridged channel part of the bridge */
 	AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, list);
 
@@ -526,9 +523,6 @@
 	/* Remove ourselves from the bridge */
 	AST_LIST_REMOVE(&bridge->channels, bridge_channel, list);
 
-	/* Decrement bridged channel count */
-	ast_atomic_fetchadd_int(&bridge->num, -1);
-
 	/* Restore original formats if need be */
 	if (bridge_channel->chan->readformat != formats[0]) {
 		ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(formats[0]), formats[0]);
@@ -574,6 +568,9 @@
 
 	ast_mutex_lock(&bridge->lock);
 
+	/* Increment channel count since we are joining */
+	ast_atomic_fetchadd_int(&bridge->num, +1);
+
 	/* Boom, record the bridge this channel is part of */
 	chan->bridge = bridge;
 
@@ -585,6 +582,10 @@
 
 	/* All done... we are out of here! */
 	ast_mutex_unlock(&bridge->lock);
+
+	/* Decrement channel count, boom */
+	ast_atomic_fetchadd_int(&bridge->num, -1);
+
 	ast_cond_destroy(&bridge_channel.cond);
 
 	return state;
@@ -606,6 +607,8 @@
 	bridge_channel->chan->bridge = NULL;
 
 	ast_mutex_unlock(&bridge->lock);
+
+	ast_atomic_fetchadd_int(&bridge->num, -1);
 
 	/* Self destruct the bridge channel structure */
 	ast_cond_destroy(&bridge_channel->cond);
@@ -649,8 +652,12 @@
 	/* Setup synchronization for our thread */
 	ast_cond_init(&bridge_channel->cond, NULL);
 
+	/* Before we actually hand over this channel to the other thread increment the bridge channel number so the bridge can't go away */
+	ast_atomic_fetchadd_int(&bridge->num, +1);
+
 	/* Now we can create the thread to handle this channel and be done with things */
 	if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
+		ast_atomic_fetchadd_int(&bridge->num, -1);
 		ast_cond_destroy(&bridge_channel->cond);
 		free(bridge_channel);
 		return -1;




More information about the asterisk-commits mailing list