]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/isc/taskpool.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / isc / taskpool.c
1 /*
2  * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.
8  *
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.
16  */
17
18 /* $Id: taskpool.c,v 1.12.18.3 2005/11/30 03:44:39 marka Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <isc/mem.h>
25 #include <isc/taskpool.h>
26 #include <isc/util.h>
27
28 /***
29  *** Types.
30  ***/
31
32 struct isc_taskpool {
33         isc_mem_t *                     mctx;
34         unsigned int                    ntasks;
35         isc_task_t **                   tasks;
36 };
37 /***
38  *** Functions.
39  ***/
40
41 isc_result_t
42 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
43                     unsigned int ntasks, unsigned int quantum,
44                     isc_taskpool_t **poolp)
45 {
46         unsigned int i;
47         isc_taskpool_t *pool;
48         isc_result_t result;
49
50         INSIST(ntasks > 0);
51         pool = isc_mem_get(mctx, sizeof(*pool));
52         if (pool == NULL)
53                 return (ISC_R_NOMEMORY);
54         pool->mctx = mctx;
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);
60         }
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);
67                         return (result);
68                 }
69         }
70         *poolp = pool;
71         return (ISC_R_SUCCESS);
72 }
73
74 void isc_taskpool_gettask(isc_taskpool_t *pool, unsigned int hash,
75                           isc_task_t **targetp)
76 {
77         isc_task_attach(pool->tasks[hash % pool->ntasks], targetp);
78 }
79
80 void
81 isc_taskpool_destroy(isc_taskpool_t **poolp) {
82         unsigned int i;
83         isc_taskpool_t *pool = *poolp;
84         for (i = 0; i < pool->ntasks; i++) {
85                 if (pool->tasks[i] != NULL) {
86                         isc_task_detach(&pool->tasks[i]);
87                 }
88         }
89         isc_mem_put(pool->mctx, pool->tasks,
90                     pool->ntasks * sizeof(isc_task_t *));
91         isc_mem_put(pool->mctx, pool, sizeof(*pool));
92         *poolp = NULL;
93 }
94
95