Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / ntp / dist / lib / isc / taskpool.c
blobf05598439920bb479f0657b1b5131d6f1dc076fd
1 /* $NetBSD$ */
3 /*
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 */
22 /*! \file */
24 #include <config.h>
26 #include <isc/mem.h>
27 #include <isc/taskpool.h>
28 #include <isc/util.h>
30 /***
31 *** Types.
32 ***/
34 struct isc_taskpool {
35 isc_mem_t * mctx;
36 unsigned int ntasks;
37 isc_task_t ** tasks;
39 /***
40 *** Functions.
41 ***/
43 isc_result_t
44 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
45 unsigned int ntasks, unsigned int quantum,
46 isc_taskpool_t **poolp)
48 unsigned int i;
49 isc_taskpool_t *pool;
50 isc_result_t result;
52 INSIST(ntasks > 0);
53 pool = isc_mem_get(mctx, sizeof(*pool));
54 if (pool == NULL)
55 return (ISC_R_NOMEMORY);
56 pool->mctx = mctx;
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);
69 return (result);
71 isc_task_setname(pool->tasks[i], "taskpool", NULL);
73 *poolp = pool;
74 return (ISC_R_SUCCESS);
77 void isc_taskpool_gettask(isc_taskpool_t *pool, unsigned int hash,
78 isc_task_t **targetp)
80 isc_task_attach(pool->tasks[hash % pool->ntasks], targetp);
83 void
84 isc_taskpool_destroy(isc_taskpool_t **poolp) {
85 unsigned int i;
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));
95 *poolp = NULL;