[asterisk-commits] kmoore: branch kmoore/stasis-mwi r383265 - in /team/kmoore/stasis-mwi: includ...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Mar 15 23:12:20 CDT 2013


Author: kmoore
Date: Fri Mar 15 23:12:17 2013
New Revision: 383265

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=383265
Log:
Refactor forwarding topic pool out of the MWI code and into stasis

Modified:
    team/kmoore/stasis-mwi/include/asterisk/stasis.h
    team/kmoore/stasis-mwi/main/app.c
    team/kmoore/stasis-mwi/main/stasis.c

Modified: team/kmoore/stasis-mwi/include/asterisk/stasis.h
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-mwi/include/asterisk/stasis.h?view=diff&rev=383265&r1=383264&r2=383265
==============================================================================
--- team/kmoore/stasis-mwi/include/asterisk/stasis.h (original)
+++ team/kmoore/stasis-mwi/include/asterisk/stasis.h Fri Mar 15 23:12:17 2013
@@ -376,6 +376,26 @@
  * \since 12
  */
 struct stasis_message_type *stasis_subscription_change(void);
+
+/*!
+ * \brief Pool for topic aggregation
+ */
+struct stasis_topic_pool;
+
+/*!
+ * \brief Create a topic pool that routes messages to the given topic
+ * \param pooled_topic Topic to which messages will be routed
+ * \retval the new stasis_topic_pool or NULL on failure
+ */
+struct stasis_topic_pool *stasis_topic_pool_create(struct stasis_topic *pooled_topic);
+
+/*!
+ * \brief Find or create a topic in the pool
+ * \param pool Pool for which to get the topic
+ * \param hash_id ID used to identify the topic
+ * \retval The stored or allocated topic or NULL if allocation failed
+ */
+struct stasis_topic *stasis_topic_pool_get_entry(struct stasis_topic_pool *pool, const char *hash_id);
 
 /*! @} */
 

Modified: team/kmoore/stasis-mwi/main/app.c
URL: http://svnview.digium.com/svn/asterisk/team/kmoore/stasis-mwi/main/app.c?view=diff&rev=383265&r1=383264&r2=383265
==============================================================================
--- team/kmoore/stasis-mwi/main/app.c (original)
+++ team/kmoore/stasis-mwi/main/app.c Fri Mar 15 23:12:17 2013
@@ -85,29 +85,7 @@
 static struct stasis_topic *mwi_topic_all;
 static struct stasis_caching_topic *mwi_topic_cached;
 static struct stasis_message_type *mwi_message_type;
-static struct ao2_container *mwi_topics;
-
-/* container for dynamically generated mailbox-specific topics */
-struct mwi_topic {
-	char *uniqueid;
-	struct stasis_subscription *forward;
-	struct stasis_topic *topic;
-};
-
-static void mwi_topic_dtor(void *obj)
-{
-	struct mwi_topic *topic = obj;
-	ast_free(topic->uniqueid);
-	topic->uniqueid = NULL;
-	topic->forward = stasis_unsubscribe(topic->forward);
-	ao2_cleanup(topic->topic);
-	topic->topic = NULL;
-}
-
-static struct mwi_topic *mwi_topic_alloc(void)
-{
-	return ao2_alloc(sizeof(struct mwi_topic), mwi_topic_dtor);
-}
+static struct stasis_topic_pool *mwi_topic_pool;
 
 static void *shaun_of_the_dead(void *data)
 {
@@ -2688,38 +2666,7 @@
 
 struct stasis_topic *stasis_mwi_topic(const char *uniqueid)
 {
-	RAII_VAR(struct mwi_topic *, mwi_topic, NULL, ao2_cleanup);
-	SCOPED_AO2LOCK(topic_container_lock, mwi_topics);
-	mwi_topic = ao2_find(mwi_topics, uniqueid, OBJ_KEY | OBJ_NOLOCK);
-
-	if (mwi_topic) {
-		return mwi_topic->topic;
-	}
-
-	mwi_topic = mwi_topic_alloc();
-
-	if (!mwi_topic) {
-		return NULL;
-	}
-
-	mwi_topic->topic = stasis_topic_create(uniqueid);
-	if (!mwi_topic->topic) {
-		return NULL;
-	}
-
-	mwi_topic->forward = stasis_forward_all(mwi_topic->topic, stasis_mwi_topic_all());
-	if (!mwi_topic->forward) {
-		return NULL;
-	}
-
-	mwi_topic->uniqueid = ast_strdup(uniqueid);
-	if (!mwi_topic->uniqueid) {
-		return NULL;
-	}
-
-	ao2_link_flags(mwi_topics, mwi_topic, OBJ_NOLOCK);
-
-	return mwi_topic->topic;
+	return stasis_topic_pool_get_entry(mwi_topic_pool, uniqueid);
 }
 
 int stasis_publish_mwi_state_full(
@@ -2780,19 +2727,6 @@
 	return NULL;
 }
 
-static int mwi_topic_hash(const void *obj, const int flags)
-{
-	const char *uniqueid = (flags & OBJ_KEY) ? obj : ((struct mwi_topic*) obj)->uniqueid;
-	return ast_str_case_hash(uniqueid);
-}
-
-static int mwi_topic_cmp(void *obj, void *arg, int flags)
-{
-	struct mwi_topic *opt1 = obj, *opt2 = arg;
-	const char *uniqueid = (flags & OBJ_KEY) ? arg : opt2->uniqueid;
-	return strcasecmp(opt1->uniqueid, uniqueid) ? 0 : CMP_MATCH | CMP_STOP;
-}
-
 static void app_exit(void)
 {
 	ao2_cleanup(mwi_topic_all);
@@ -2800,8 +2734,8 @@
 	mwi_topic_cached = stasis_caching_unsubscribe(mwi_topic_cached);
 	ao2_cleanup(mwi_message_type);
 	mwi_message_type = NULL;
-	ao2_cleanup(mwi_topics);
-	mwi_topics = NULL;
+	ao2_cleanup(mwi_topic_pool);
+	mwi_topic_pool = NULL;
 }
 
 int app_init(void)
@@ -2818,8 +2752,8 @@
 	if (!mwi_message_type) {
 		return -1;
 	}
-	mwi_topics = ao2_container_alloc(MWI_TOPIC_BUCKETS, mwi_topic_hash, mwi_topic_cmp);
-	if (!mwi_topics) {
+	mwi_topic_pool = stasis_topic_pool_create(mwi_topic_all);
+	if (!mwi_topic_pool) {
 		return -1;
 	}
 

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=383265&r1=383264&r2=383265
==============================================================================
--- team/kmoore/stasis-mwi/main/stasis.c (original)
+++ team/kmoore/stasis-mwi/main/stasis.c Fri Mar 15 23:12:17 2013
@@ -41,6 +41,9 @@
 /*! Initial size of the subscribers list. */
 #define INITIAL_SUBSCRIBERS_MAX 4
 
+/*! The number of buckets to use for topic pools */
+#define TOPIC_POOL_BUCKETS 57
+
 /*! Threadpool for dispatching notifications to subscribers */
 static struct ast_threadpool *pool;
 
@@ -470,6 +473,104 @@
 	stasis_publish(topic, msg);
 }
 
+struct topic_pool_entry {
+	char *hash_id;
+	struct stasis_subscription *forward;
+	struct stasis_topic *topic;
+};
+
+static void topic_pool_entry_dtor(void *obj)
+{
+	struct topic_pool_entry *entry = obj;
+	ast_free(entry->hash_id);
+	entry->hash_id = NULL;
+	entry->forward = stasis_unsubscribe(entry->forward);
+	ao2_cleanup(entry->topic);
+	entry->topic = NULL;
+}
+
+static struct topic_pool_entry *topic_pool_entry_alloc(void)
+{
+	return ao2_alloc(sizeof(struct topic_pool_entry), topic_pool_entry_dtor);
+}
+
+struct stasis_topic_pool {
+	struct ao2_container *pool_container;
+	struct stasis_topic *pool_topic;
+};
+
+static void topic_pool_dtor(void *obj)
+{
+	struct stasis_topic_pool *pool = obj;
+	ao2_cleanup(pool->pool_container);
+	pool->pool_container = NULL;
+	ao2_cleanup(pool->pool_topic);
+	pool->pool_topic = NULL;
+}
+
+static int topic_pool_entry_hash(const void *obj, const int flags)
+{
+	const char *hash_id = (flags & OBJ_KEY) ? obj : ((struct topic_pool_entry*) obj)->hash_id;
+	return ast_str_case_hash(hash_id);
+}
+
+static int topic_pool_entry_cmp(void *obj, void *arg, int flags)
+{
+	struct topic_pool_entry *opt1 = obj, *opt2 = arg;
+	const char *hash_id = (flags & OBJ_KEY) ? arg : opt2->hash_id;
+	return strcasecmp(opt1->hash_id, hash_id) ? 0 : CMP_MATCH | CMP_STOP;
+}
+
+struct stasis_topic_pool *stasis_topic_pool_create(struct stasis_topic *pooled_topic)
+{
+	RAII_VAR(struct stasis_topic_pool *, pool, ao2_alloc(sizeof(*pool), topic_pool_dtor), ao2_cleanup);
+	if (!pool) {
+		return NULL;
+	}
+	pool->pool_container = ao2_container_alloc(TOPIC_POOL_BUCKETS, topic_pool_entry_hash, topic_pool_entry_cmp);
+	ao2_ref(pooled_topic, +1);
+	pool->pool_topic = pooled_topic;
+
+	ao2_ref(pool, +1);
+	return pool;
+}
+
+struct stasis_topic *stasis_topic_pool_get_entry(struct stasis_topic_pool *pool, const char *hash_id)
+{
+	RAII_VAR(struct topic_pool_entry *, topic_pool_entry, NULL, ao2_cleanup);
+	SCOPED_AO2LOCK(topic_container_lock, pool->pool_container);
+	topic_pool_entry = ao2_find(pool->pool_container, hash_id, OBJ_KEY | OBJ_NOLOCK);
+
+	if (topic_pool_entry) {
+		return topic_pool_entry->topic;
+	}
+
+	topic_pool_entry = topic_pool_entry_alloc();
+
+	if (!topic_pool_entry) {
+		return NULL;
+	}
+
+	topic_pool_entry->topic = stasis_topic_create(hash_id);
+	if (!topic_pool_entry->topic) {
+		return NULL;
+	}
+
+	topic_pool_entry->forward = stasis_forward_all(topic_pool_entry->topic, pool->pool_topic);
+	if (!topic_pool_entry->forward) {
+		return NULL;
+	}
+
+	topic_pool_entry->hash_id = ast_strdup(hash_id);
+	if (!topic_pool_entry->hash_id) {
+		return NULL;
+	}
+
+	ao2_link_flags(pool->pool_container, topic_pool_entry, OBJ_NOLOCK);
+
+	return topic_pool_entry->topic;
+}
+
 /*! \brief Cleanup function */
 static void stasis_exit(void)
 {




More information about the asterisk-commits mailing list