[asterisk-commits] dhubbard: branch dhubbard/named_processors r109760 - /team/dhubbard/named_pro...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Mar 18 17:28:10 CDT 2008


Author: dhubbard
Date: Tue Mar 18 17:28:10 2008
New Revision: 109760

URL: http://svn.digium.com/view/asterisk?view=rev&rev=109760
Log:
integrated taskprocessor into pbx.c with bkruse

Modified:
    team/dhubbard/named_processors/main/pbx.c

Modified: team/dhubbard/named_processors/main/pbx.c
URL: http://svn.digium.com/view/asterisk/team/dhubbard/named_processors/main/pbx.c?view=diff&rev=109760&r1=109759&r2=109760
==============================================================================
--- team/dhubbard/named_processors/main/pbx.c (original)
+++ team/dhubbard/named_processors/main/pbx.c Tue Mar 18 17:28:10 2008
@@ -64,6 +64,7 @@
 #include "asterisk/hashtab.h"
 #include "asterisk/module.h"
 #include "asterisk/indications.h"
+#include "asterisk/taskprocessor.h"
 
 /*!
  * \note I M P O R T A N T :
@@ -122,6 +123,9 @@
 
 struct ast_context;
 struct ast_app;
+
+struct taskprocessor_singleton_info *taskprocessor;
+struct taskproducer *taskproducer;
 
 AST_THREADSTORAGE(switch_data);
 
@@ -266,24 +270,6 @@
 struct statechange {
 	AST_LIST_ENTRY(statechange) entry;
 	char dev[0];
-};
-
-/*!
- * \brief Data used by the device state thread
- */
-static struct {
-	/*! Set to 1 to stop the thread */
-	unsigned int stop:1;
-	/*! The device state monitoring thread */
-	pthread_t thread;
-	/*! Lock for the state change queue */
-	ast_mutex_t lock;
-	/*! Condition for the state change queue */
-	ast_cond_t cond;
-	/*! Queue of state changes */
-	AST_LIST_HEAD_NOLOCK(, statechange) state_change_q;
-} device_state = {
-	.thread = AST_PTHREADT_NULL,
 };
 
 struct pbx_exception {
@@ -2929,10 +2915,10 @@
 	return ast_extension_state2(e);    		/* Check all devices in the hint */
 }
 
-static void handle_statechange(const char *device)
+static int handle_statechange(struct a_task *task)
 {
 	struct ast_hint *hint;
-
+	struct statechange *sc = task->_datap;
 	AST_RWLIST_RDLOCK(&hints);
 
 	AST_RWLIST_TRAVERSE(&hints, hint, list) {
@@ -2944,7 +2930,7 @@
 
 		ast_copy_string(buf, ast_get_extension_app(hint->exten), sizeof(buf));
 		while ( (cur = strsep(&parse, "&")) ) {
-			if (!strcasecmp(cur, device))
+			if (!strcasecmp(cur, sc->dev))
 				break;
 		}
 		if (!cur)
@@ -2968,49 +2954,10 @@
 
 		hint->laststate = state;	/* record we saw the change */
 	}
-
 	AST_RWLIST_UNLOCK(&hints);
-}
-
-static int statechange_queue(const char *dev)
-{
-	struct statechange *sc;
-
-	if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(dev) + 1)))
-		return 0;
-
-	strcpy(sc->dev, dev);
-
-	ast_mutex_lock(&device_state.lock);
-	AST_LIST_INSERT_TAIL(&device_state.state_change_q, sc, entry);
-	ast_cond_signal(&device_state.cond);
-	ast_mutex_unlock(&device_state.lock);
-
+	ast_free(sc);
+	task->_datap = NULL;
 	return 0;
-}
-
-static void *device_state_thread(void *data)
-{
-	struct statechange *sc;
-
-	while (!device_state.stop) {
-		ast_mutex_lock(&device_state.lock);
-		while (!(sc = AST_LIST_REMOVE_HEAD(&device_state.state_change_q, entry))) {
-			ast_cond_wait(&device_state.cond, &device_state.lock);
-			/* Check to see if we were woken up to see the request to stop */
-			if (device_state.stop) {
-				ast_mutex_unlock(&device_state.lock);
-				return NULL;
-			}
-		}
-		ast_mutex_unlock(&device_state.lock);
-
-		handle_statechange(sc->dev);
-
-		ast_free(sc);
-	}
-
-	return NULL;
 }
 
 /*! \brief  Add watcher for extension states */
@@ -7800,6 +7747,8 @@
 static void device_state_cb(const struct ast_event *event, void *unused)
 {
 	const char *device;
+	struct statechange *sc;
+	struct a_task *t;
 
 	device = ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE);
 	if (ast_strlen_zero(device)) {
@@ -7807,7 +7756,14 @@
 		return;
 	}
 
-	statechange_queue(device);
+	if (!(sc = ast_calloc(1, sizeof(*sc) + strlen(device) + 1)))
+		return;
+	strcpy(sc->dev, device);
+	t = ast_task_alloc(handle_statechange, sc, "pbx-device_state_cb");
+	if (taskproducer->queue_task(taskproducer, t) < 0) {
+		ast_log(LOG_WARNING, "queue_task failed!\n");
+		ast_task_free(t);
+	}
 }
 
 int load_pbx(void)
@@ -7833,9 +7789,8 @@
 	/* Register manager application */
 	ast_manager_register2("ShowDialPlan", EVENT_FLAG_CONFIG | EVENT_FLAG_REPORTING, manager_show_dialplan, "List dialplan", mandescr_show_dialplan);
 
-	ast_mutex_init(&device_state.lock);
-	ast_cond_init(&device_state.cond, NULL);
-	ast_pthread_create(&device_state.thread, NULL, device_state_thread, NULL);
+	taskprocessor = ast_taskprocessor_alloc("pbx", 0);
+	taskproducer = ast_taskproducer_alloc(taskprocessor);
 
 	if (!(device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE, device_state_cb, NULL,
 			AST_EVENT_IE_END))) {




More information about the asterisk-commits mailing list