2 * Copyright (C) 2004, 2005, 2007, 2011-2013 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);
57 isc_mem_attach(mctx, &pool->mctx);
58 pool->ntasks = ntasks;
59 pool->quantum = quantum;
61 pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
62 if (pool->tasks == NULL) {
63 isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
64 return (ISC_R_NOMEMORY);
66 for (i = 0; i < ntasks; i++)
67 pool->tasks[i] = NULL;
70 return (ISC_R_SUCCESS);
74 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
75 unsigned int ntasks, unsigned int quantum,
76 isc_taskpool_t **poolp)
79 isc_taskpool_t *pool = NULL;
84 /* Allocate the pool structure */
85 result = alloc_pool(tmgr, mctx, ntasks, quantum, &pool);
86 if (result != ISC_R_SUCCESS)
89 /* Create the tasks */
90 for (i = 0; i < ntasks; i++) {
91 result = isc_task_create(tmgr, quantum, &pool->tasks[i]);
92 if (result != ISC_R_SUCCESS) {
93 isc_taskpool_destroy(&pool);
96 isc_task_setname(pool->tasks[i], "taskpool", NULL);
100 return (ISC_R_SUCCESS);
104 isc_taskpool_gettask(isc_taskpool_t *pool, isc_task_t **targetp) {
107 isc_task_attach(pool->tasks[i % pool->ntasks], targetp);
111 isc_taskpool_size(isc_taskpool_t *pool) {
112 REQUIRE(pool != NULL);
113 return (pool->ntasks);
117 isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size,
118 isc_taskpool_t **targetp)
121 isc_taskpool_t *pool;
123 REQUIRE(sourcep != NULL && *sourcep != NULL);
124 REQUIRE(targetp != NULL && *targetp == NULL);
127 if (size > pool->ntasks) {
128 isc_taskpool_t *newpool = NULL;
131 /* Allocate a new pool structure */
132 result = alloc_pool(pool->tmgr, pool->mctx, size,
133 pool->quantum, &newpool);
134 if (result != ISC_R_SUCCESS)
137 /* Copy over the tasks from the old pool */
138 for (i = 0; i < pool->ntasks; i++) {
139 newpool->tasks[i] = pool->tasks[i];
140 pool->tasks[i] = NULL;
143 /* Create new tasks */
144 for (i = pool->ntasks; i < size; i++) {
145 result = isc_task_create(pool->tmgr, pool->quantum,
147 if (result != ISC_R_SUCCESS) {
148 isc_taskpool_destroy(&newpool);
151 isc_task_setname(newpool->tasks[i], "taskpool", NULL);
154 isc_taskpool_destroy(&pool);
160 return (ISC_R_SUCCESS);
164 isc_taskpool_destroy(isc_taskpool_t **poolp) {
166 isc_taskpool_t *pool = *poolp;
167 for (i = 0; i < pool->ntasks; i++) {
168 if (pool->tasks[i] != NULL)
169 isc_task_detach(&pool->tasks[i]);
171 isc_mem_put(pool->mctx, pool->tasks,
172 pool->ntasks * sizeof(isc_task_t *));
173 isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
178 isc_taskpool_setprivilege(isc_taskpool_t *pool, isc_boolean_t priv) {
181 REQUIRE(pool != NULL);
183 for (i = 0; i < pool->ntasks; i++) {
184 if (pool->tasks[i] != NULL)
185 isc_task_setprivilege(pool->tasks[i], priv);