[asterisk-commits] kmoore: branch kmoore/stasis-mwi r382461 - in /team/kmoore/stasis-mwi: ./ mai...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 5 09:52:44 CST 2013


Author: kmoore
Date: Tue Mar  5 09:52:40 2013
New Revision: 382461

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=382461
Log:
Reversed the strong/weak pointer relationship between topic and subscriber.

Now subscriptions require an explicit unsubscribe, which causes an
implicit unref. You can still ref/unref the subscription if you need to
pass it around.
........

Merged revisions 382458 from http://svn.asterisk.org/svn/asterisk/team/dlee/stasis-core

Modified:
    team/kmoore/stasis-mwi/   (props changed)
    team/kmoore/stasis-mwi/main/stasis.c
    team/kmoore/stasis-mwi/tests/test_stasis.c

Propchange: team/kmoore/stasis-mwi/
------------------------------------------------------------------------------
--- svnmerge-integrated (original)
+++ svnmerge-integrated Tue Mar  5 09:52:40 2013
@@ -1,1 +1,1 @@
-/team/dlee/stasis-core:1-382320
+/team/dlee/stasis-core:1-382460

Modified: team/kmoore/stasis-mwi/main/stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-mwi/main/stasis.c?view=diff&rev=382461&r1=382460&r2=382461
==============================================================================
--- team/kmoore/stasis-mwi/main/stasis.c (original)
+++ team/kmoore/stasis-mwi/main/stasis.c Tue Mar  5 09:52:40 2013
@@ -49,7 +49,7 @@
 /*! \private */
 struct stasis_topic {
 	char *name;
-	/*! Variable length array of the subscribers */
+	/*! Variable length array of the subscribers (raw pointer to avoid cyclic references) */
 	struct stasis_subscription **subscribers;
 	/*! Allocated length of the subscribers array */
 	size_t num_subscribers_max;
@@ -118,6 +118,7 @@
 {
 	struct stasis_subscription *sub = obj;
 	ao2_cleanup(sub->topic);
+	sub->topic = NULL;
 	ao2_cleanup(sub->mailbox);
 	sub->mailbox = NULL;
 }
@@ -171,26 +172,23 @@
 void stasis_unsubscribe(struct stasis_subscription *sub)
 {
 	if (sub) {
-		RAII_VAR(struct stasis_subscription *, cleanup_after_unlock, NULL, ao2_cleanup);
-		SCOPED_AO2LOCK(lock, sub);
+		size_t i;
+		struct stasis_topic *topic = sub->topic;
+		SCOPED_AO2LOCK(lock_topic, topic);
 		cleanup_after_unlock = sub;
 
-		if (sub->topic) {
-			size_t i;
-			struct stasis_topic *topic = sub->topic;
-			SCOPED_AO2LOCK(lock, topic);
-
-			for (i = 0; i < topic->num_subscribers_current; ++i) {
-				if (topic->subscribers[i] == sub) {
-					send_subscription_change_message(topic, sub->uniqueid, "Unsubscribe", 1);
-					/* swap [i] with last entry; remove last entry */
-					topic->subscribers[i] = topic->subscribers[--topic->num_subscribers_current];
-					return;
-				}
+		for (i = 0; i < topic->num_subscribers_current; ++i) {
+			if (topic->subscribers[i] == sub) {
+				send_subscription_change_message(topic, sub->uniqueid, "Unsubscribe", 1);
+				/* swap [i] with last entry; remove last entry */
+				topic->subscribers[i] = topic->subscribers[--topic->num_subscribers_current];
+				/* Unsubscribing unrefs the subscription */
+				ao2_cleanup(sub);
+				return;
 			}
-
-			ast_log(LOG_ERROR, "Internal error: subscription has invalid topic\n");
-		}
+		}
+
+		ast_log(LOG_ERROR, "Internal error: subscription has invalid topic\n");
 	}
 }
 
@@ -283,18 +281,10 @@
 	RAII_VAR(struct dispatch *, dispatch, data, ao2_cleanup);
 	RAII_VAR(struct stasis_topic *, sub_topic, NULL, ao2_cleanup);
 
-	/* If the subscription's topic is NULL, then we've been unsubscribed,
-	 * and don't dispatch. Check this while locking the subscription, but
-	 * don't hold the lock while dispatching.
-	 */
-	{
-		SCOPED_AO2LOCK(lock, dispatch->sub);
-		if (!dispatch->sub->topic) {
-			return 0;
-		}
-		sub_topic = dispatch->sub->topic;
-		ao2_ref(sub_topic, +1);
-	}
+	/* Since sub->topic doesn't change, no need to lock sub */
+	ast_assert(dispatch->sub->topic != NULL);
+	ao2_ref(dispatch->sub->topic, +1);
+	sub_topic = dispatch->sub->topic;
 
 	dispatch->sub->callback(dispatch->sub->data,
 				sub_topic,
@@ -305,7 +295,7 @@
 
 static void __forward_message_full(struct stasis_topic *topic, struct stasis_topic *publisher_topic, struct stasis_message *message, int topic_locked)
 {
-	struct stasis_subscription **subscribers;
+	struct stasis_subscription **subscribers = NULL;
 	size_t num_subscribers, i;
 
 	if (topic_locked) {
@@ -318,9 +308,10 @@
 		num_subscribers = topic->num_subscribers_current;
 		subscribers = ast_malloc(num_subscribers * sizeof(*subscribers));
 		if (subscribers) {
-			memcpy(subscribers,
-			       topic->subscribers,
-			       num_subscribers * sizeof(*subscribers));
+			for (i = 0; i < num_subscribers; ++i) {
+				ao2_ref(topic->subscribers[i], +1);
+				subscribers[i] = topic->subscribers[i];
+			}
 		}
 	}
 
@@ -353,6 +344,9 @@
 	}
 
 	if (!topic_locked) {
+		for (i = 0; i < num_subscribers; ++i) {
+			ao2_cleanup(subscribers[i]);
+		}
 		ast_free(subscribers);
 	}
 }

Modified: team/kmoore/stasis-mwi/tests/test_stasis.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-mwi/tests/test_stasis.c?view=diff&rev=382461&r1=382460&r2=382461
==============================================================================
--- team/kmoore/stasis-mwi/tests/test_stasis.c (original)
+++ team/kmoore/stasis-mwi/tests/test_stasis.c Tue Mar  5 09:52:40 2013
@@ -334,6 +334,7 @@
 	ast_test_validate(test, 1 == consumer->subs_rxed);
 
 	stasis_unsubscribe(uut);
+	uut = NULL;
 
 	actual_len = consumer_wait_for(consumer, 1, UNSUBSCRIPTION);
 	ast_test_validate(test, 1 == consumer->unsubs_rxed);




More information about the asterisk-commits mailing list