[asterisk-commits] mmichelson: branch mmichelson/threadpool r379122 - in /team/mmichelson/thread...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Tue Jan 15 13:36:36 CST 2013
Author: mmichelson
Date: Tue Jan 15 13:36:33 2013
New Revision: 379122
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=379122
Log:
Remove threadpool listener alloc and destroy callbacks.
This replaces the destroy callback with a shutdown callback
instead.
Modified:
team/mmichelson/threadpool/include/asterisk/threadpool.h
team/mmichelson/threadpool/main/threadpool.c
team/mmichelson/threadpool/tests/test_threadpool.c
Modified: team/mmichelson/threadpool/include/asterisk/threadpool.h
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/threadpool/include/asterisk/threadpool.h?view=diff&rev=379122&r1=379121&r2=379122
==============================================================================
--- team/mmichelson/threadpool/include/asterisk/threadpool.h (original)
+++ team/mmichelson/threadpool/include/asterisk/threadpool.h Tue Jan 15 13:36:33 2013
@@ -26,17 +26,9 @@
struct ast_threadpool_listener_callbacks {
/*!
- * \brief Allocate the listener's private data
- *
- * It is not necessary to assign the private data to the listener.
- * \param listener The listener the private data will belong to
- * \retval NULL Failure to allocate private data
- * \retval non-NULL The newly allocated private data
- */
- void *(*alloc)(struct ast_threadpool_listener *listener);
- /*!
* \brief Indicates that the state of threads in the pool has changed
*
+ * \param pool The pool whose state has changed
* \param listener The threadpool listener
* \param active_threads The number of active threads in the pool
* \param idle_threads The number of idle threads in the pool
@@ -48,6 +40,7 @@
/*!
* \brief Indicates that a task was pushed to the threadpool
*
+ * \param pool The pool that had a task pushed
* \param listener The threadpool listener
* \param was_empty Indicates whether there were any tasks prior to adding the new one.
*/
@@ -57,15 +50,21 @@
/*!
* \brief Indicates the threadpool's taskprocessor has become empty
*
+ * \param pool The pool that has become empty
* \param listener The threadpool's listener
*/
void (*emptied)(struct ast_threadpool *pool, struct ast_threadpool_listener *listener);
/*!
- * \brief Free the listener's private data
- * \param private_data The private data to destroy
+ * \brief The threadpool is shutting down
+ *
+ * This would be an opportune time to free the listener's user data
+ * if one wishes. However, it is acceptable to not do so if the user data
+ * should persist beyond the lifetime of the pool.
+ *
+ * \param listener The threadpool's listener
*/
- void (*destroy)(void *private_data);
+ void (*shutdown)(struct ast_threadpool_listener *listener);
};
/*!
@@ -79,7 +78,7 @@
/*! Callbacks called by the threadpool */
const struct ast_threadpool_listener_callbacks *callbacks;
/*! User data for the listener */
- void *private_data;
+ void *user_data;
};
struct ast_threadpool_options {
@@ -112,11 +111,12 @@
* listener.
*
* \param callbacks Listener callbacks to assign to the listener
+ * \param user_data User data to be stored in the threadpool listener
* \retval NULL Failed to allocate the listener
* \retval non-NULL The newly-created threadpool listener
*/
struct ast_threadpool_listener *ast_threadpool_listener_alloc(
- const struct ast_threadpool_listener_callbacks *callbacks);
+ const struct ast_threadpool_listener_callbacks *callbacks, void *user_data);
/*!
* \brief Create a new threadpool
Modified: team/mmichelson/threadpool/main/threadpool.c
URL: http://svnview.digium.com/svn/asterisk/team/mmichelson/threadpool/main/threadpool.c?view=diff&rev=379122&r1=379121&r2=379122
==============================================================================
--- team/mmichelson/threadpool/main/threadpool.c (original)
+++ team/mmichelson/threadpool/main/threadpool.c Tue Jan 15 13:36:33 2013
@@ -613,6 +613,7 @@
{
struct ast_threadpool *pool = listener->user_data;
+ pool->listener->callbacks->shutdown(pool->listener);
ao2_cleanup(pool->active_threads);
ao2_cleanup(pool->idle_threads);
ao2_cleanup(pool->zombie_threads);
@@ -808,26 +809,15 @@
ast_taskprocessor_push(pool->control_tps, queued_set_size, ssd);
}
-static void listener_destructor(void *obj)
-{
- struct ast_threadpool_listener *listener = obj;
-
- listener->callbacks->destroy(listener->private_data);
-}
-
struct ast_threadpool_listener *ast_threadpool_listener_alloc(
- const struct ast_threadpool_listener_callbacks *callbacks)
-{
- struct ast_threadpool_listener *listener = ao2_alloc(sizeof(*listener), listener_destructor);
+ const struct ast_threadpool_listener_callbacks *callbacks, void *user_data)
+{
+ struct ast_threadpool_listener *listener = ao2_alloc(sizeof(*listener), NULL);
if (!listener) {
return NULL;
}
listener->callbacks = callbacks;
- listener->private_data = listener->callbacks->alloc(listener);
- if (!listener->private_data) {
- ao2_ref(listener, -1);
- return NULL;
- }
+ listener->user_data = user_data;
return listener;
}
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=379122&r1=379121&r2=379122
==============================================================================
--- team/mmichelson/threadpool/tests/test_threadpool.c (original)
+++ team/mmichelson/threadpool/tests/test_threadpool.c Tue Jan 15 13:36:33 2013
@@ -49,7 +49,7 @@
ast_cond_t cond;
};
-static void *test_alloc(struct ast_threadpool_listener *listener)
+static struct test_listener_data *test_alloc(void)
{
struct test_listener_data *tld = ast_calloc(1, sizeof(*tld));
if (!tld) {
@@ -65,7 +65,7 @@
int active_threads,
int idle_threads)
{
- struct test_listener_data *tld = listener->private_data;
+ struct test_listener_data *tld = listener->user_data;
SCOPED_MUTEX(lock, &tld->lock);
tld->num_active = active_threads;
tld->num_idle = idle_threads;
@@ -77,7 +77,7 @@
struct ast_threadpool_listener *listener,
int was_empty)
{
- struct test_listener_data *tld = listener->private_data;
+ struct test_listener_data *tld = listener->user_data;
SCOPED_MUTEX(lock, &tld->lock);
tld->task_pushed = 1;
++tld->num_tasks;
@@ -88,26 +88,24 @@
static void test_emptied(struct ast_threadpool *pool,
struct ast_threadpool_listener *listener)
{
- struct test_listener_data *tld = listener->private_data;
+ struct test_listener_data *tld = listener->user_data;
SCOPED_MUTEX(lock, &tld->lock);
tld->empty_notice = 1;
ast_cond_signal(&tld->cond);
}
-static void test_destroy(void *private_data)
-{
- struct test_listener_data *tld = private_data;
+static void test_shutdown(struct ast_threadpool_listener *listener)
+{
+ struct test_listener_data *tld = listener->user_data;
ast_cond_destroy(&tld->cond);
ast_mutex_destroy(&tld->lock);
- ast_free(tld);
}
static const struct ast_threadpool_listener_callbacks test_callbacks = {
- .alloc = test_alloc,
.state_changed = test_state_changed,
.task_pushed = test_task_pushed,
.emptied = test_emptied,
- .destroy = test_destroy,
+ .shutdown = test_shutdown,
};
struct simple_task_data {
@@ -165,7 +163,7 @@
static void wait_for_task_pushed(struct ast_threadpool_listener *listener)
{
- struct test_listener_data *tld = listener->private_data;
+ struct test_listener_data *tld = listener->user_data;
struct timeval start = ast_tvnow();
struct timespec end = {
.tv_sec = start.tv_sec + 5,
@@ -237,7 +235,7 @@
int num_idle,
int empty_notice)
{
- struct test_listener_data *tld = listener->private_data;
+ struct test_listener_data *tld = listener->user_data;
enum ast_test_result_state res = AST_TEST_PASS;
if (tld->task_pushed != task_pushed) {
@@ -279,6 +277,7 @@
struct ast_threadpool *pool = NULL;
struct ast_threadpool_listener *listener = NULL;
struct simple_task_data *std = NULL;
+ struct test_listener_data *tld = NULL;
enum ast_test_result_state res = AST_TEST_FAIL;
struct ast_threadpool_options options = {
.version = AST_THREADPOOL_OPTIONS_VERSION,
@@ -297,10 +296,14 @@
case TEST_EXECUTE:
break;
}
-
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
+ goto end;
}
pool = ast_threadpool_create(info->name, listener, 0, &options);
@@ -325,6 +328,7 @@
}
ao2_cleanup(listener);
ast_free(std);
+ ast_free(tld);
return res;
}
@@ -353,11 +357,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 3, &options);
if (!pool) {
@@ -371,6 +379,7 @@
ast_threadpool_shutdown(pool);
}
ao2_cleanup(listener);
+ ast_free(tld);
return res;
}
@@ -399,11 +408,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -422,6 +435,7 @@
ast_threadpool_shutdown(pool);
}
ao2_cleanup(listener);
+ ast_free(tld);
return res;
}
@@ -449,11 +463,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -481,6 +499,7 @@
ast_threadpool_shutdown(pool);
}
ao2_cleanup(listener);
+ ast_free(tld);
return res;
}
@@ -508,11 +527,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -543,6 +566,7 @@
ast_threadpool_shutdown(pool);
}
ao2_cleanup(listener);
+ ast_free(tld);
return res;
}
@@ -571,11 +595,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -619,6 +647,7 @@
}
ao2_cleanup(listener);
ast_free(std);
+ ast_free(tld);
return res;
}
@@ -648,11 +677,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -697,6 +730,7 @@
}
ao2_cleanup(listener);
ast_free(std);
+ ast_free(tld);
return res;
}
@@ -727,11 +761,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -789,6 +827,7 @@
ast_free(std1);
ast_free(std2);
ast_free(std3);
+ ast_free(tld);
return res;
}
@@ -822,11 +861,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -901,6 +944,7 @@
ast_free(std2);
ast_free(std3);
ast_free(std4);
+ ast_free(tld);
return res;
}
@@ -932,11 +976,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -1000,6 +1048,7 @@
ao2_cleanup(listener);
ast_free(std1);
ast_free(std2);
+ ast_free(tld);
return res;
}
@@ -1094,11 +1143,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -1153,6 +1206,7 @@
ao2_cleanup(listener);
ast_free(ctd1);
ast_free(ctd2);
+ ast_free(tld);
return res;
}
@@ -1185,11 +1239,15 @@
break;
}
- listener = ast_threadpool_listener_alloc(&test_callbacks);
+ tld = test_alloc();
+ if (!tld) {
+ return AST_TEST_FAIL;
+ }
+
+ listener = ast_threadpool_listener_alloc(&test_callbacks, tld);
if (!listener) {
- return AST_TEST_FAIL;
- }
- tld = listener->private_data;
+ goto end;
+ }
pool = ast_threadpool_create(info->name, listener, 0, &options);
if (!pool) {
@@ -1259,6 +1317,7 @@
ao2_cleanup(listener);
ast_free(ctd1);
ast_free(ctd2);
+ ast_free(tld);
return res;
}
More information about the asterisk-commits
mailing list