[asterisk-commits] rmudgett: branch 12 r408983 - /branches/12/tests/test_stasis.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Feb 26 17:33:12 CST 2014


Author: rmudgett
Date: Wed Feb 26 17:33:06 2014
New Revision: 408983

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=408983
Log:
test_stasis.c: Misc cleanups.

* Make the consumer ao2 object use the ao2 lock instead of a redundant
lock in the struct for ast_cond_wait().

* Fixed some curly brace placements.

* Fixed use of malloc(0).  malloc(0) has variant behavior.  It is up to
the implementation to determine if it returns NULL or a valid pointer that
can be later passed to free().

Modified:
    branches/12/tests/test_stasis.c

Modified: branches/12/tests/test_stasis.c
URL: http://svnview.digium.com/svn/asterisk/branches/12/tests/test_stasis.c?view=diff&rev=408983&r1=408982&r2=408983
==============================================================================
--- branches/12/tests/test_stasis.c (original)
+++ branches/12/tests/test_stasis.c Wed Feb 26 17:33:06 2014
@@ -140,7 +140,6 @@
 }
 
 struct consumer {
-	ast_mutex_t lock;
 	ast_cond_t out;
 	struct stasis_message **messages_rxed;
 	size_t messages_rxed_len;
@@ -148,10 +147,10 @@
 	int complete;
 };
 
-static void consumer_dtor(void *obj) {
+static void consumer_dtor(void *obj)
+{
 	struct consumer *consumer = obj;
 
-	ast_mutex_destroy(&consumer->lock);
 	ast_cond_destroy(&consumer->out);
 
 	while (consumer->messages_rxed_len > 0) {
@@ -161,25 +160,24 @@
 	consumer->messages_rxed = NULL;
 }
 
-static struct consumer *consumer_create(int ignore_subscriptions) {
-	RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
+static struct consumer *consumer_create(int ignore_subscriptions)
+{
+	struct consumer *consumer;
 
 	consumer = ao2_alloc(sizeof(*consumer), consumer_dtor);
-
 	if (!consumer) {
 		return NULL;
 	}
 
 	consumer->ignore_subscriptions = ignore_subscriptions;
-	consumer->messages_rxed = ast_malloc(0);
+	consumer->messages_rxed = ast_malloc(sizeof(*consumer->messages_rxed));
 	if (!consumer->messages_rxed) {
+		ao2_cleanup(consumer);
 		return NULL;
 	}
 
-	ast_mutex_init(&consumer->lock);
 	ast_cond_init(&consumer->out, NULL);
 
-	ao2_ref(consumer, +1);
 	return consumer;
 }
 
@@ -187,10 +185,9 @@
 {
 	struct consumer *consumer = data;
 	RAII_VAR(struct consumer *, consumer_needs_cleanup, NULL, ao2_cleanup);
-	SCOPED_MUTEX(lock, &consumer->lock);
+	SCOPED_AO2LOCK(lock, consumer);
 
 	if (!consumer->ignore_subscriptions || stasis_message_type(message) != stasis_subscription_change_type()) {
-
 		++consumer->messages_rxed_len;
 		consumer->messages_rxed = ast_realloc(consumer->messages_rxed, sizeof(*consumer->messages_rxed) * consumer->messages_rxed_len);
 		ast_assert(consumer->messages_rxed != NULL);
@@ -210,10 +207,9 @@
 {
 	struct consumer *consumer = data;
 	RAII_VAR(struct consumer *, consumer_needs_cleanup, NULL, ao2_cleanup);
-	SCOPED_MUTEX(lock, &consumer->lock);
+	SCOPED_AO2LOCK(lock, consumer);
 
 	if (!consumer->ignore_subscriptions || stasis_message_type(message) != stasis_subscription_change_type()) {
-
 		++consumer->messages_rxed_len;
 		consumer->messages_rxed = ast_realloc(consumer->messages_rxed, sizeof(*consumer->messages_rxed) * consumer->messages_rxed_len);
 		ast_assert(consumer->messages_rxed != NULL);
@@ -235,10 +231,11 @@
 		.tv_nsec = start.tv_usec * 1000
 	};
 
-	SCOPED_MUTEX(lock, &consumer->lock);
+	SCOPED_AO2LOCK(lock, consumer);
 
 	while (consumer->messages_rxed_len < expected_len) {
-		int r = ast_cond_timedwait(&consumer->out, &consumer->lock, &end);
+		int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
+
 		if (r == ETIMEDOUT) {
 			break;
 		}
@@ -255,10 +252,11 @@
 		.tv_nsec = start.tv_usec * 1000
 	};
 
-	SCOPED_MUTEX(lock, &consumer->lock);
+	SCOPED_AO2LOCK(lock, consumer);
 
 	while (!consumer->complete) {
-		int r = ast_cond_timedwait(&consumer->out, &consumer->lock, &end);
+		int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
+
 		if (r == ETIMEDOUT) {
 			break;
 		}
@@ -280,10 +278,11 @@
 		.tv_nsec = end_tv.tv_usec * 1000
 	};
 
-	SCOPED_MUTEX(lock, &consumer->lock);
+	SCOPED_AO2LOCK(lock, consumer);
 
 	while (consumer->messages_rxed_len == expected_len) {
-		int r = ast_cond_timedwait(&consumer->out, &consumer->lock, &end);
+		int r = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
+
 		if (r == ETIMEDOUT) {
 			break;
 		}
@@ -669,7 +668,8 @@
 	return stasis_message_create(type, data);
 }
 
-static const char *cache_test_data_id(struct stasis_message *message) {
+static const char *cache_test_data_id(struct stasis_message *message)
+{
 	struct cache_test_data *cachable = stasis_message_data(message);
 
 	if (0 != strcmp("Cacheable", stasis_message_type_name(stasis_message_type(message)))) {
@@ -1033,7 +1033,8 @@
 	return AST_TEST_PASS;
 }
 
-static const char *cache_simple(struct stasis_message *message) {
+static const char *cache_simple(struct stasis_message *message)
+{
 	const char *type_name =
 		stasis_message_type_name(stasis_message_type(message));
 	if (!ast_begins_with(type_name, "Cache")) {




More information about the asterisk-commits mailing list