[asterisk-commits] dlee: branch dlee/taskprocessor-optimization r399654 - in /team/dlee/taskproc...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Sep 24 00:22:18 CDT 2013


Author: dlee
Date: Tue Sep 24 00:22:16 2013
New Revision: 399654

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=399654
Log:
Fix taskprocessor life cycle for self destruction.

Modified:
    team/dlee/taskprocessor-optimization/include/asterisk/taskprocessor.h
    team/dlee/taskprocessor-optimization/main/taskprocessor.c

Modified: team/dlee/taskprocessor-optimization/include/asterisk/taskprocessor.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/taskprocessor-optimization/include/asterisk/taskprocessor.h?view=diff&rev=399654&r1=399653&r2=399654
==============================================================================
--- team/dlee/taskprocessor-optimization/include/asterisk/taskprocessor.h (original)
+++ team/dlee/taskprocessor-optimization/include/asterisk/taskprocessor.h Tue Sep 24 00:22:16 2013
@@ -109,6 +109,7 @@
 	 * \param listener The listener
 	 */
 	void (*shutdown)(struct ast_taskprocessor_listener *listener);
+	void (*dtor)(struct ast_taskprocessor_listener *listener);
 };
 
 /*!

Modified: team/dlee/taskprocessor-optimization/main/taskprocessor.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/taskprocessor-optimization/main/taskprocessor.c?view=diff&rev=399654&r1=399653&r2=399654
==============================================================================
--- team/dlee/taskprocessor-optimization/main/taskprocessor.c (original)
+++ team/dlee/taskprocessor-optimization/main/taskprocessor.c Tue Sep 24 00:22:16 2013
@@ -147,6 +147,15 @@
 	ast_free(pvt);
 }
 
+static void default_listener_pvt_dtor(struct ast_taskprocessor_listener *listener)
+{
+	struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
+
+	default_listener_pvt_destroy(pvt);
+
+	listener->user_data = NULL;
+}
+
 /*!
  * \brief Function that processes tasks in the taskprocessor
  * \internal
@@ -161,7 +170,7 @@
 
 	while (!pvt->dead) {
 		res = ast_sem_wait(&pvt->sem);
-		if (res != 0 && errno != EINTR) { 
+		if (res != 0 && errno != EINTR) {
 			ast_log(LOG_ERROR, "ast_sem_wait(): %s\n",
 				strerror(errno));
 			/* Just give up */
@@ -174,6 +183,9 @@
 	res = ast_sem_getvalue(&pvt->sem, &sem_value);
 	ast_assert(res == 0 && sem_value == 0);
 
+	/* Free the shutdown reference */
+	ao2_ref(listener->tps, -1);
+
 	return NULL;
 }
 
@@ -208,18 +220,34 @@
 static void default_listener_shutdown(struct ast_taskprocessor_listener *listener)
 {
 	struct default_taskprocessor_listener_pvt *pvt = listener->user_data;
+	int res;
+
+	/* Hold a reference during shutdown */
+	ao2_ref(listener->tps, +1);
 
 	ast_taskprocessor_push(listener->tps, default_listener_die, pvt);
 
-	pthread_join(pvt->poll_thread, NULL);
+	if (pthread_self() == pvt->poll_thread) {
+		res = pthread_detach(pvt->poll_thread);
+		if (res != 0) {
+			ast_log(LOG_ERROR, "pthread_detach(): %s\n",
+				strerror(errno));
+		}
+	} else {
+		res = pthread_join(pvt->poll_thread, NULL);
+		if (res != 0) {
+			ast_log(LOG_ERROR, "pthread_join(): %s\n",
+				strerror(errno));
+		}
+	}
 	pvt->poll_thread = AST_PTHREADT_NULL;
-	default_listener_pvt_destroy(pvt);
 }
 
 static const struct ast_taskprocessor_listener_callbacks default_listener_callbacks = {
 	.start = default_listener_start,
 	.task_pushed = default_task_pushed,
 	.shutdown = default_listener_shutdown,
+	.dtor = default_listener_pvt_dtor,
 };
 
 /*!
@@ -264,9 +292,7 @@
 /* release task resources */
 static void *tps_task_free(struct tps_task *task)
 {
-	if (task) {
-		ast_free(task);
-	}
+	ast_free(task);
 	return NULL;
 }
 
@@ -421,10 +447,8 @@
 	}
 	ast_debug(1, "destroying taskprocessor '%s'\n", t->name);
 	/* free it */
-	if (t->stats) {
-		ast_free(t->stats);
-		t->stats = NULL;
-	}
+	ast_free(t->stats);
+	t->stats = NULL;
 	ast_free((char *) t->name);
 	if (t->listener) {
 		ao2_ref(t->listener, -1);
@@ -467,10 +491,21 @@
 	ao2_ref(listener->tps, -1);
 }
 
+static void taskprocessor_listener_dtor(void *obj)
+{
+	struct ast_taskprocessor_listener *listener = obj;
+
+	if (listener->callbacks->dtor) {
+		listener->callbacks->dtor(listener);
+	}
+}
+
 struct ast_taskprocessor_listener *ast_taskprocessor_listener_alloc(const struct ast_taskprocessor_listener_callbacks *callbacks, void *user_data)
 {
 	RAII_VAR(struct ast_taskprocessor_listener *, listener,
-			ao2_alloc(sizeof(*listener), NULL), ao2_cleanup);
+			NULL, ao2_cleanup);
+
+	listener = ao2_alloc(sizeof(*listener), taskprocessor_listener_dtor);
 
 	if (!listener) {
 		return NULL;
@@ -588,7 +623,6 @@
 
 	p = __allocate_taskprocessor(name, listener);
 	if (!p) {
-		default_listener_pvt_destroy(pvt);
 		ao2_ref(listener, -1);
 		return NULL;
 	}




More information about the asterisk-commits mailing list