2 * Copyright (C) 2004, 2005, 2007, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
25 #include <isc/random.h>
26 #include <isc/taskpool.h>
46 alloc_pool(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
47 unsigned int quantum, isc_taskpool_t **poolp)
52 pool = isc_mem_get(mctx, sizeof(*pool));
54 return (ISC_R_NOMEMORY);
56 pool->ntasks = ntasks;
57 pool->quantum = quantum;
59 pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
60 if (pool->tasks == NULL) {
61 isc_mem_put(mctx, pool, sizeof(*pool));
62 return (ISC_R_NOMEMORY);
64 for (i = 0; i < ntasks; i++)
65 pool->tasks[i] = NULL;
68 return (ISC_R_SUCCESS);
72 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
73 unsigned int ntasks, unsigned int quantum,
74 isc_taskpool_t **poolp)
77 isc_taskpool_t *pool = NULL;
82 /* Allocate the pool structure */
83 result = alloc_pool(tmgr, mctx, ntasks, quantum, &pool);
84 if (result != ISC_R_SUCCESS)
87 /* Create the tasks */
88 for (i = 0; i < ntasks; i++) {
89 result = isc_task_create(tmgr, quantum, &pool->tasks[i]);
90 if (result != ISC_R_SUCCESS) {
91 isc_taskpool_destroy(&pool);
94 isc_task_setname(pool->tasks[i], "taskpool", NULL);
98 return (ISC_R_SUCCESS);
102 isc_taskpool_gettask(isc_taskpool_t *pool, isc_task_t **targetp) {
105 isc_task_attach(pool->tasks[i % pool->ntasks], targetp);
109 isc_taskpool_size(isc_taskpool_t *pool) {
110 REQUIRE(pool != NULL);
111 return (pool->ntasks);
115 isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size,
116 isc_taskpool_t **targetp)
119 isc_taskpool_t *pool;
121 REQUIRE(sourcep != NULL && *sourcep != NULL);
122 REQUIRE(targetp != NULL && *targetp == NULL);
125 if (size > pool->ntasks) {
126 isc_taskpool_t *newpool = NULL;
129 /* Allocate a new pool structure */
130 result = alloc_pool(pool->tmgr, pool->mctx, size,
131 pool->quantum, &newpool);
132 if (result != ISC_R_SUCCESS)
135 /* Copy over the tasks from the old pool */
136 for (i = 0; i < pool->ntasks; i++) {
137 newpool->tasks[i] = pool->tasks[i];
138 pool->tasks[i] = NULL;
141 /* Create new tasks */
142 for (i = pool->ntasks; i < size; i++) {
143 result = isc_task_create(pool->tmgr, pool->quantum,
145 if (result != ISC_R_SUCCESS) {
146 isc_taskpool_destroy(&newpool);
149 isc_task_setname(newpool->tasks[i], "taskpool", NULL);
152 isc_taskpool_destroy(&pool);
158 return (ISC_R_SUCCESS);
162 isc_taskpool_destroy(isc_taskpool_t **poolp) {
164 isc_taskpool_t *pool = *poolp;
165 for (i = 0; i < pool->ntasks; i++) {
166 if (pool->tasks[i] != NULL)
167 isc_task_detach(&pool->tasks[i]);
169 isc_mem_put(pool->mctx, pool->tasks,
170 pool->ntasks * sizeof(isc_task_t *));
171 isc_mem_put(pool->mctx, pool, sizeof(*pool));
176 isc_taskpool_setprivilege(isc_taskpool_t *pool, isc_boolean_t priv) {
179 REQUIRE(pool != NULL);
181 for (i = 0; i < pool->ntasks; i++) {
182 if (pool->tasks[i] != NULL)
183 isc_task_setprivilege(pool->tasks[i], priv);