Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.3 / lib / isc / taskpool.c
1 /*
2  * Copyright (C) 2004, 2006  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.10.12.5 2006/01/04 23:50:21 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/mem.h>
23 #include <isc/taskpool.h>
24 #include <isc/util.h>
25
26 /***
27  *** Types.
28  ***/
29
30 struct isc_taskpool {
31         isc_mem_t *                     mctx;
32         unsigned int                    ntasks;
33         isc_task_t **                   tasks;
34 };
35 /***
36  *** Functions.
37  ***/
38
39 isc_result_t
40 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
41                     unsigned int ntasks, unsigned int quantum,
42                     isc_taskpool_t **poolp)
43 {
44         unsigned int i;
45         isc_taskpool_t *pool;
46         isc_result_t result;
47
48         INSIST(ntasks > 0);
49         pool = isc_mem_get(mctx, sizeof(*pool));
50         if (pool == NULL)
51                 return (ISC_R_NOMEMORY);
52         pool->mctx = mctx;
53         pool->ntasks = ntasks;
54         pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
55         if (pool->tasks == NULL) {
56                 isc_mem_put(mctx, pool, sizeof(*pool));
57                 return (ISC_R_NOMEMORY);
58         }
59         for (i = 0; i < ntasks; i++)
60                 pool->tasks[i] = NULL;
61         for (i = 0; i < ntasks; i++) {
62                 result = isc_task_create(tmgr, quantum, &pool->tasks[i]);
63                 if (result != ISC_R_SUCCESS) {
64                         isc_taskpool_destroy(&pool);
65                         return (result);
66                 }
67         }
68         *poolp = pool;
69         return (ISC_R_SUCCESS);
70 }
71
72 void isc_taskpool_gettask(isc_taskpool_t *pool, unsigned int hash,
73                           isc_task_t **targetp)
74 {
75         isc_task_attach(pool->tasks[hash % pool->ntasks], targetp);
76 }
77
78 void
79 isc_taskpool_destroy(isc_taskpool_t **poolp) {
80         unsigned int i;
81         isc_taskpool_t *pool = *poolp;
82         for (i = 0; i < pool->ntasks; i++) {
83                 if (pool->tasks[i] != NULL) {
84                         isc_task_detach(&pool->tasks[i]);
85                 }
86         }
87         isc_mem_put(pool->mctx, pool->tasks,
88                     pool->ntasks * sizeof(isc_task_t *));
89         isc_mem_put(pool->mctx, pool, sizeof(*pool));
90         *poolp = NULL;
91 }
92
93