[svn-commits] russell: branch russell/event_performance r183648 - in /team/russell/event_pe...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Sun Mar 22 14:04:55 CDT 2009


Author: russell
Date: Sun Mar 22 14:04:52 2009
New Revision: 183648

URL: http://svn.digium.com/svn-view/asterisk?view=rev&rev=183648
Log:
When distributed device state is not being used, cut out unnecessary processing

Modified:
    team/russell/event_performance/include/asterisk/devicestate.h
    team/russell/event_performance/main/devicestate.c
    team/russell/event_performance/res/ais/evt.c

Modified: team/russell/event_performance/include/asterisk/devicestate.h
URL: http://svn.digium.com/svn-view/asterisk/team/russell/event_performance/include/asterisk/devicestate.h?view=diff&rev=183648&r1=183647&r2=183648
==============================================================================
--- team/russell/event_performance/include/asterisk/devicestate.h (original)
+++ team/russell/event_performance/include/asterisk/devicestate.h Sun Mar 22 14:04:52 2009
@@ -36,6 +36,8 @@
 
 #ifndef _ASTERISK_DEVICESTATE_H
 #define _ASTERISK_DEVICESTATE_H
+
+#include "asterisk/channel.h"
 
 #if defined(__cplusplus) || defined(c_plusplus)
 extern "C" {
@@ -259,6 +261,21 @@
 	unsigned int ring:1;
 };
 
+/*!
+ * \brief Enable distributed device state processing.
+ *
+ * \details
+ * By default, Asterisk assumes that device state change events will only be
+ * originating from one instance.  If a module gets loaded and configured such
+ * that multiple instances of Asterisk will be sharing device state, this
+ * function should be called to enable distributed device state processing.
+ * It is off by default to save on unnecessary processing.
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int ast_enable_distributed_devstate(void);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

Modified: team/russell/event_performance/main/devicestate.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/event_performance/main/devicestate.c?view=diff&rev=183648&r1=183647&r2=183648
==============================================================================
--- team/russell/event_performance/main/devicestate.c (original)
+++ team/russell/event_performance/main/devicestate.c Sun Mar 22 14:04:52 2009
@@ -196,8 +196,10 @@
 	ast_cond_t cond;
 	ast_mutex_t lock;
 	AST_LIST_HEAD_NOLOCK(, devstate_change) devstate_change_q;
+	unsigned int enabled:1;
 } devstate_collector = {
 	.thread = AST_PTHREADT_NULL,
+	.enabled = 0,
 };
 
 /* Forward declarations */
@@ -452,10 +454,19 @@
 static void devstate_event(const char *device, enum ast_device_state state)
 {
 	struct ast_event *event;
+	enum ast_event_type event_type;
+
+	if (devstate_collector.enabled) {
+		/* Distributed device state is enabled, so this state change is a change
+		 * for a single server, not the real state. */
+		event_type = AST_EVENT_DEVICE_STATE_CHANGE;
+	} else {
+		event_type = AST_EVENT_DEVICE_STATE;
+	}
 
 	ast_debug(3, "device '%s' state '%d'\n", device, state);
 
-	if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
+	if (!(event = ast_event_new(event_type,
 			AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, device,
 			AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, state,
 			AST_EVENT_IE_END))) {
@@ -743,21 +754,6 @@
 /*! \brief Initialize the device state engine in separate thread */
 int ast_device_state_engine_init(void)
 {
-	devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
-		devstate_change_collector_cb, NULL, AST_EVENT_IE_END);
-
-	if (!devstate_collector.event_sub) {
-		ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
-		return -1;
-	}
-
-	ast_mutex_init(&devstate_collector.lock);
-	ast_cond_init(&devstate_collector.cond, NULL);
-	if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
-		ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
-		return -1;
-	}
-
 	ast_cond_init(&change_pending, NULL);
 	if (ast_pthread_create_background(&change_thread, NULL, do_devstate_changes, NULL) < 0) {
 		ast_log(LOG_ERROR, "Unable to start device state change thread.\n");
@@ -854,3 +850,28 @@
 	return AST_DEVICE_NOT_INUSE;
 }
 
+int ast_enable_distributed_devstate(void)
+{
+	if (devstate_collector.enabled) {
+		return 0;
+	}
+
+	devstate_collector.event_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
+		devstate_change_collector_cb, NULL, AST_EVENT_IE_END);
+
+	if (!devstate_collector.event_sub) {
+		ast_log(LOG_ERROR, "Failed to create subscription for the device state change collector\n");
+		return -1;
+	}
+
+	ast_mutex_init(&devstate_collector.lock);
+	ast_cond_init(&devstate_collector.cond, NULL);
+	if (ast_pthread_create_background(&devstate_collector.thread, NULL, run_devstate_collector, NULL) < 0) {
+		ast_log(LOG_ERROR, "Unable to start device state collector thread.\n");
+		return -1;
+	}
+
+	devstate_collector.enabled = 1;
+
+	return 0;
+}

Modified: team/russell/event_performance/res/ais/evt.c
URL: http://svn.digium.com/svn-view/asterisk/team/russell/event_performance/res/ais/evt.c?view=diff&rev=183648&r1=183647&r2=183648
==============================================================================
--- team/russell/event_performance/res/ais/evt.c (original)
+++ team/russell/event_performance/res/ais/evt.c Sun Mar 22 14:04:52 2009
@@ -47,6 +47,7 @@
 #include "asterisk/event.h"
 #include "asterisk/config.h"
 #include "asterisk/linkedlists.h"
+#include "asterisk/devicestate.h"
 
 #ifndef AST_MODULE
 /* XXX HACK */
@@ -341,9 +342,17 @@
 		return;
 	}
 
-	if (!(publish_event = ast_calloc(1, sizeof(*publish_event))))
-		return;
-	
+	if (!(publish_event = ast_calloc(1, sizeof(*publish_event)))) {
+		return;
+	}
+
+	if (type == AST_EVENT_DEVICE_STATE_CHANGE) {
+		if (ast_enable_distributed_devstate()) {
+			ast_free(publish_event);
+			return;
+		}
+	}
+
 	publish_event->type = type;
 	ast_log(LOG_DEBUG, "Subscribing to event type %d\n", type);
 	publish_event->sub = ast_event_subscribe(type, ast_event_cb, event_channel,
@@ -399,9 +408,17 @@
 		return;
 	}
 
-	if (!(subscribe_event = ast_calloc(1, sizeof(*subscribe_event))))
-		return;
-	
+	if (!(subscribe_event = ast_calloc(1, sizeof(*subscribe_event)))) {
+		return;
+	}
+
+	if (type == AST_EVENT_DEVICE_STATE_CHANGE) {
+		if (ast_enable_distributed_devstate()) {
+			ast_free(subscribe_event);
+			return;
+		}
+	}
+
 	subscribe_event->type = type;
 	subscribe_event->id = ast_atomic_fetchadd_int(&unique_id, +1);
 




More information about the svn-commits mailing list