From c6bc51ef28a79303500ab541684e8adc331cc662 Mon Sep 17 00:00:00 2001
From: Mark Michelson <mmichelson@digium.com>
Date: Tue, 15 Jan 2013 19:44:25 +0000
Subject: [PATCH] Make the initial size of the threadpool part of the options
 passed in.

git-svn-id: https://origsvn.digium.com/svn/asterisk/team/mmichelson/threadpool@379123 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 include/asterisk/threadpool.h | 14 ++++++++++++--
 main/threadpool.c             |  4 ++--
 tests/test_threadpool.c       | 36 +++++++++++++++++++++++------------
 3 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/include/asterisk/threadpool.h b/include/asterisk/threadpool.h
index 5cc4ea4da2..a549ae3e15 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 f37095364f..358184caad 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 bb648571cd..1143056c38 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;
 	}
-- 
GitLab