diff --git a/include/asterisk/threadpool.h b/include/asterisk/threadpool.h index 5cc4ea4da2fd6fbeb9362ccf0dd7d2ebdd8f46ca..a549ae3e155a8f150f1b6a7fa35a2af7d9082cb1 100644 --- a/include/asterisk/threadpool.h +++ b/include/asterisk/threadpool.h @@ -102,6 +102,16 @@ struct ast_threadpool_options { * to control threadpool growth yourself via your listener. */ int auto_increment; + /*! + * \brief Number of threads the pool will start with + * + * When the threadpool is allocated, it will immediately size + * itself to have this number of threads in it. + * + * Zero is a valid value if the threadpool should start + * without any threads allocated. + */ + int initial_size; }; /*! @@ -126,13 +136,13 @@ struct ast_threadpool_listener *ast_threadpool_listener_alloc( * * \param name The name for the threadpool * \param listener The listener the threadpool will notify of changes - * \param initial_size The number of threads for the pool to start with + * \param options The behavioral options for this threadpool * \retval NULL Failed to create the threadpool * \retval non-NULL The newly-created threadpool */ struct ast_threadpool *ast_threadpool_create(const char *name, struct ast_threadpool_listener *listener, - int initial_size, const struct ast_threadpool_options *options); + const struct ast_threadpool_options *options); /*! * \brief Set the number of threads for the thread pool diff --git a/main/threadpool.c b/main/threadpool.c index f37095364f2a5bc58992a9c637343cb9633d05be..358184caad355d7712b6de7a3f3d4e7d043d5180 100644 --- a/main/threadpool.c +++ b/main/threadpool.c @@ -828,7 +828,7 @@ struct pool_options_pair { struct ast_threadpool *ast_threadpool_create(const char *name, struct ast_threadpool_listener *listener, - int initial_size, const struct ast_threadpool_options *options) + const struct ast_threadpool_options *options) { struct ast_taskprocessor *tps; RAII_VAR(struct ast_taskprocessor_listener *, tps_listener, NULL, ao2_cleanup); @@ -858,7 +858,7 @@ struct ast_threadpool *ast_threadpool_create(const char *name, ao2_ref(listener, +1); pool->listener = listener; } - ast_threadpool_set_size(pool, initial_size); + ast_threadpool_set_size(pool, pool->options.initial_size); ao2_ref(pool, +1); return pool; } diff --git a/tests/test_threadpool.c b/tests/test_threadpool.c index bb648571cd9fe857c5dc438da33da3c3f1872e98..1143056c38dc1566b9b0d5f6cff86a97e92d7f14 100644 --- a/tests/test_threadpool.c +++ b/tests/test_threadpool.c @@ -283,6 +283,7 @@ AST_TEST_DEFINE(threadpool_push) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -306,7 +307,7 @@ AST_TEST_DEFINE(threadpool_push) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -342,6 +343,7 @@ AST_TEST_DEFINE(threadpool_initial_threads) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 3, }; switch (cmd) { @@ -367,7 +369,7 @@ AST_TEST_DEFINE(threadpool_initial_threads) goto end; } - pool = ast_threadpool_create(info->name, listener, 3, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -394,6 +396,7 @@ AST_TEST_DEFINE(threadpool_thread_creation) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -418,7 +421,7 @@ AST_TEST_DEFINE(threadpool_thread_creation) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -449,6 +452,7 @@ AST_TEST_DEFINE(threadpool_thread_destruction) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -473,7 +477,7 @@ AST_TEST_DEFINE(threadpool_thread_destruction) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -513,6 +517,7 @@ AST_TEST_DEFINE(threadpool_thread_timeout) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 2, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -537,7 +542,7 @@ AST_TEST_DEFINE(threadpool_thread_timeout) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -581,6 +586,7 @@ AST_TEST_DEFINE(threadpool_one_task_one_thread) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -605,7 +611,7 @@ AST_TEST_DEFINE(threadpool_one_task_one_thread) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -663,6 +669,7 @@ AST_TEST_DEFINE(threadpool_one_thread_one_task) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -687,7 +694,7 @@ AST_TEST_DEFINE(threadpool_one_thread_one_task) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -747,6 +754,7 @@ AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -771,7 +779,7 @@ AST_TEST_DEFINE(threadpool_one_thread_multiple_tasks) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -845,6 +853,7 @@ AST_TEST_DEFINE(threadpool_auto_increment) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 3, + .initial_size = 0, }; switch (cmd) { @@ -871,7 +880,7 @@ AST_TEST_DEFINE(threadpool_auto_increment) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -960,6 +969,7 @@ AST_TEST_DEFINE(threadpool_reactivation) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -986,7 +996,7 @@ AST_TEST_DEFINE(threadpool_reactivation) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -1128,6 +1138,7 @@ AST_TEST_DEFINE(threadpool_task_distribution) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -1153,7 +1164,7 @@ AST_TEST_DEFINE(threadpool_task_distribution) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; } @@ -1222,6 +1233,7 @@ AST_TEST_DEFINE(threadpool_more_destruction) .version = AST_THREADPOOL_OPTIONS_VERSION, .idle_timeout = 0, .auto_increment = 0, + .initial_size = 0, }; switch (cmd) { @@ -1249,7 +1261,7 @@ AST_TEST_DEFINE(threadpool_more_destruction) goto end; } - pool = ast_threadpool_create(info->name, listener, 0, &options); + pool = ast_threadpool_create(info->name, listener, &options); if (!pool) { goto end; }