From: Mark Michelson Date: Fri, 16 Nov 2012 04:33:53 +0000 (+0000) Subject: Add a shutdown callback to taskprocessor listeners. X-Git-Tag: 13.0.0-beta1~2194^2~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12de4198b83cd30ae1f991ee3ea8e752b39898e0;p=thirdparty%2Fasterisk.git Add a shutdown callback to taskprocessor listeners. This helps account for the fact that it is unknown just how many references may exist for a given taskprocessor listener, so simply unreffing it from the taskprocessor shutdown function is not enough to convey the gravity of the situation. By putting in a shutdown callback, it now becomes clear to the listener not to try to do any further operations on the taskprocessor. git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@376381 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/include/asterisk/taskprocessor.h b/include/asterisk/taskprocessor.h index 5ea14907a1..895bc17643 100644 --- a/include/asterisk/taskprocessor.h +++ b/include/asterisk/taskprocessor.h @@ -86,6 +86,18 @@ struct ast_taskprocessor_listener_callbacks { * \param listener The listener */ void (*emptied)(struct ast_taskprocessor_listener *listener); + /*! + * \brief Indicates the taskprocessor wishes to die. + * + * All operations on the task processor must to be stopped in + * this callback. + * + * After this callback returns, it is NOT safe to operate on the + * listener's reference to the taskprocessor. + * + * \param listener The listener + */ + void (*shutdown)(struct ast_taskprocessor_listener *listener); /*! * \brief Destroy the listener's private data * diff --git a/main/taskprocessor.c b/main/taskprocessor.c index 00c9485843..49e951f3fd 100644 --- a/main/taskprocessor.c +++ b/main/taskprocessor.c @@ -136,7 +136,11 @@ static void listener_destroy(void *obj) struct ast_taskprocessor_listener *listener = obj; listener->callbacks->destroy(listener->private_data); +} +static void listener_shutdown(struct ast_taskprocessor_listener *listener) +{ + listener->callbacks->shutdown(listener); ao2_ref(listener->tps, -1); listener->tps = NULL; } @@ -184,13 +188,17 @@ static void *default_listener_alloc(struct ast_taskprocessor_listener *listener) return pvt; } -static void default_listener_destroy(void *obj) +static void default_listener_shutdown(struct ast_taskprocessor_listener *listener) { - struct default_taskprocessor_listener_pvt *pvt = obj; - + struct default_taskprocessor_listener_pvt *pvt = listener->private_data; default_tps_wake_up(pvt, 1); pthread_join(pvt->poll_thread, NULL); pvt->poll_thread = AST_PTHREADT_NULL; +} + +static void default_listener_destroy(void *obj) +{ + struct default_taskprocessor_listener_pvt *pvt = obj; ast_mutex_destroy(&pvt->lock); ast_cond_destroy(&pvt->cond); ast_free(pvt); @@ -214,6 +222,7 @@ static const struct ast_taskprocessor_listener_callbacks default_listener_callba .alloc = default_listener_alloc, .task_pushed = default_task_pushed, .emptied = default_emptied, + .shutdown = default_listener_shutdown, .destroy = default_listener_destroy, }; @@ -571,6 +580,7 @@ void *ast_taskprocessor_unreference(struct ast_taskprocessor *tps) ao2_unlink(tps_singletons, tps); listener = tps->listener; tps->listener = NULL; + listener_shutdown(listener); ao2_ref(listener, -1); return NULL; } @@ -601,7 +611,7 @@ int ast_taskprocessor_execute(struct ast_taskprocessor *tps) { struct tps_task *t; int size; - + if (!(t = tps_taskprocessor_pop(tps))) { return 0; } diff --git a/tests/test_taskprocessor.c b/tests/test_taskprocessor.c index eebf3d0b32..424449dd9b 100644 --- a/tests/test_taskprocessor.c +++ b/tests/test_taskprocessor.c @@ -103,6 +103,7 @@ struct test_listener_pvt { int num_pushed; int num_emptied; int num_was_empty; + int shutdown; }; static void *test_alloc(struct ast_taskprocessor_listener *listener) @@ -128,6 +129,12 @@ static void test_emptied(struct ast_taskprocessor_listener *listener) ++pvt->num_emptied; } +static void test_shutdown(struct ast_taskprocessor_listener *listener) +{ + struct test_listener_pvt *pvt = listener->private_data; + pvt->shutdown = 1; +} + static void test_destroy(void *private_data) { struct test_listener_pvt *pvt = private_data; @@ -138,6 +145,7 @@ static const struct ast_taskprocessor_listener_callbacks test_callbacks = { .alloc = test_alloc, .task_pushed = test_task_pushed, .emptied = test_emptied, + .shutdown = test_shutdown, .destroy = test_destroy, };