[asterisk-commits] mmichelson: branch mmichelson/threadpool r377578 - in /team/mmichelson/thread...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Sun Dec 9 23:25:41 CST 2012
Author: mmichelson
Date: Sun Dec 9 23:25:38 2012
New Revision: 377578
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=377578
Log:
Improve shutdown procedure.
This helps tests to pass more often than before.
They are far less likely to queue extra processes
into the control taskprocessor since they are prevented
once the threadpool begins to shut down.
Modified:
team/mmichelson/threadpool/main/threadpool.c
team/mmichelson/threadpool/tests/test_threadpool.c
Modified: team/mmichelson/threadpool/main/threadpool.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/threadpool/main/threadpool.c?view=diff&rev=377578&r1=377577&r2=377578
==============================================================================
--- team/mmichelson/threadpool/main/threadpool.c (original)
+++ team/mmichelson/threadpool/main/threadpool.c Sun Dec 9 23:25:38 2012
@@ -214,7 +214,12 @@
static void threadpool_active_thread_idle(struct ast_threadpool *pool,
struct worker_thread *worker)
{
- struct thread_worker_pair *pair = thread_worker_pair_alloc(pool, worker);
+ struct thread_worker_pair *pair;
+ SCOPED_AO2LOCK(lock, pool);
+ if (pool->shutting_down) {
+ return;
+ }
+ pair = thread_worker_pair_alloc(pool, worker);
if (!pair) {
return;
}
@@ -249,7 +254,12 @@
static void threadpool_zombie_thread_dead(struct ast_threadpool *pool,
struct worker_thread *worker)
{
- struct thread_worker_pair *pair = thread_worker_pair_alloc(pool, worker);
+ struct thread_worker_pair *pair;
+ SCOPED_AO2LOCK(lock, pool);
+ if (pool->shutting_down) {
+ return;
+ }
+ pair = thread_worker_pair_alloc(pool, worker);
if (!pair) {
return;
}
@@ -268,9 +278,12 @@
*/
static int threadpool_execute(struct ast_threadpool *pool)
{
+ ao2_lock(pool);
if (!pool->shutting_down) {
+ ao2_unlock(pool);
return ast_taskprocessor_execute(pool->tps);
}
+ ao2_unlock(pool);
return 0;
}
@@ -422,8 +435,13 @@
int was_empty)
{
struct ast_threadpool *pool = listener->private_data;
- struct task_pushed_data *tpd = task_pushed_data_alloc(pool, was_empty);
-
+ struct task_pushed_data *tpd;
+ SCOPED_AO2LOCK(lock, pool);
+
+ if (pool->shutting_down) {
+ return;
+ }
+ tpd = task_pushed_data_alloc(pool, was_empty);
if (!tpd) {
return;
}
@@ -456,6 +474,11 @@
static void threadpool_tps_emptied(struct ast_taskprocessor_listener *listener)
{
struct ast_threadpool *pool = listener->private_data;
+ SCOPED_AO2LOCK(lock, pool);
+
+ if (pool->shutting_down) {
+ return;
+ }
ast_taskprocessor_push(pool->control_tps, handle_emptied, pool);
}
@@ -690,6 +713,10 @@
void ast_threadpool_set_size(struct ast_threadpool *pool, unsigned int size)
{
struct set_size_data *ssd;
+ SCOPED_AO2LOCK(lock, pool);
+ if (pool->shutting_down) {
+ return;
+ }
ssd = set_size_data_alloc(pool, size);
if (!ssd) {
@@ -750,6 +777,7 @@
int ast_threadpool_push(struct ast_threadpool *pool, int (*task)(void *data), void *data)
{
+ SCOPED_AO2LOCK(lock, pool);
if (!pool->shutting_down) {
return ast_taskprocessor_push(pool->tps, task, data);
}
@@ -761,7 +789,9 @@
/* Shut down the taskprocessors and everything else just
* takes care of itself via the taskprocessor callbacks
*/
- ast_atomic_fetchadd_int(&pool->shutting_down, +1);
+ ao2_lock(pool);
+ pool->shutting_down = 1;
+ ao2_unlock(pool);
ast_taskprocessor_unreference(pool->control_tps);
ast_taskprocessor_unreference(pool->tps);
}
@@ -834,6 +864,7 @@
static void worker_thread_destroy(void *obj)
{
struct worker_thread *worker = obj;
+ ast_log(LOG_NOTICE, "Worker dying\n");
worker_shutdown(worker);
ast_mutex_destroy(&worker->lock);
ast_cond_destroy(&worker->cond);
Modified: team/mmichelson/threadpool/tests/test_threadpool.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/threadpool/tests/test_threadpool.c?view=diff&rev=377578&r1=377577&r2=377578
==============================================================================
--- team/mmichelson/threadpool/tests/test_threadpool.c (original)
+++ team/mmichelson/threadpool/tests/test_threadpool.c Sun Dec 9 23:25:38 2012
@@ -69,6 +69,7 @@
SCOPED_MUTEX(lock, &tld->lock);
tld->num_active = active_threads;
tld->num_idle = idle_threads;
+ ast_log(LOG_NOTICE, "Thread state: %d active, %d idle\n", tld->num_active, tld->num_idle);
ast_cond_signal(&tld->cond);
}
More information about the asterisk-commits
mailing list