2 * Copyright (C) 2004, 2005, 2007 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.
18 /* $Id: taskpool.c,v 1.18 2007/06/18 23:47:44 tbox Exp $ */
25 #include <isc/taskpool.h>
42 isc_taskpool_create(isc_taskmgr_t
*tmgr
, isc_mem_t
*mctx
,
43 unsigned int ntasks
, unsigned int quantum
,
44 isc_taskpool_t
**poolp
)
51 pool
= isc_mem_get(mctx
, sizeof(*pool
));
53 return (ISC_R_NOMEMORY
);
55 pool
->ntasks
= ntasks
;
56 pool
->tasks
= isc_mem_get(mctx
, ntasks
* sizeof(isc_task_t
*));
57 if (pool
->tasks
== NULL
) {
58 isc_mem_put(mctx
, pool
, sizeof(*pool
));
59 return (ISC_R_NOMEMORY
);
61 for (i
= 0; i
< ntasks
; i
++)
62 pool
->tasks
[i
] = NULL
;
63 for (i
= 0; i
< ntasks
; i
++) {
64 result
= isc_task_create(tmgr
, quantum
, &pool
->tasks
[i
]);
65 if (result
!= ISC_R_SUCCESS
) {
66 isc_taskpool_destroy(&pool
);
69 isc_task_setname(pool
->tasks
[i
], "taskpool", NULL
);
72 return (ISC_R_SUCCESS
);
75 void isc_taskpool_gettask(isc_taskpool_t
*pool
, unsigned int hash
,
78 isc_task_attach(pool
->tasks
[hash
% pool
->ntasks
], targetp
);
82 isc_taskpool_destroy(isc_taskpool_t
**poolp
) {
84 isc_taskpool_t
*pool
= *poolp
;
85 for (i
= 0; i
< pool
->ntasks
; i
++) {
86 if (pool
->tasks
[i
] != NULL
) {
87 isc_task_detach(&pool
->tasks
[i
]);
90 isc_mem_put(pool
->mctx
, pool
->tasks
,
91 pool
->ntasks
* sizeof(isc_task_t
*));
92 isc_mem_put(pool
->mctx
, pool
, sizeof(*pool
));