4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: taskpool.c,v 1.18 2007/06/18 23:47:44 tbox Exp */
27 #include <isc/taskpool.h>
44 isc_taskpool_create(isc_taskmgr_t
*tmgr
, isc_mem_t
*mctx
,
45 unsigned int ntasks
, unsigned int quantum
,
46 isc_taskpool_t
**poolp
)
53 pool
= isc_mem_get(mctx
, sizeof(*pool
));
55 return (ISC_R_NOMEMORY
);
57 pool
->ntasks
= ntasks
;
58 pool
->tasks
= isc_mem_get(mctx
, ntasks
* sizeof(isc_task_t
*));
59 if (pool
->tasks
== NULL
) {
60 isc_mem_put(mctx
, pool
, sizeof(*pool
));
61 return (ISC_R_NOMEMORY
);
63 for (i
= 0; i
< ntasks
; i
++)
64 pool
->tasks
[i
] = NULL
;
65 for (i
= 0; i
< ntasks
; i
++) {
66 result
= isc_task_create(tmgr
, quantum
, &pool
->tasks
[i
]);
67 if (result
!= ISC_R_SUCCESS
) {
68 isc_taskpool_destroy(&pool
);
71 isc_task_setname(pool
->tasks
[i
], "taskpool", NULL
);
74 return (ISC_R_SUCCESS
);
77 void isc_taskpool_gettask(isc_taskpool_t
*pool
, unsigned int hash
,
80 isc_task_attach(pool
->tasks
[hash
% pool
->ntasks
], targetp
);
84 isc_taskpool_destroy(isc_taskpool_t
**poolp
) {
86 isc_taskpool_t
*pool
= *poolp
;
87 for (i
= 0; i
< pool
->ntasks
; i
++) {
88 if (pool
->tasks
[i
] != NULL
) {
89 isc_task_detach(&pool
->tasks
[i
]);
92 isc_mem_put(pool
->mctx
, pool
->tasks
,
93 pool
->ntasks
* sizeof(isc_task_t
*));
94 isc_mem_put(pool
->mctx
, pool
, sizeof(*pool
));