1 /* $NetBSD: taskpool.c,v 1.6 2014/12/10 04:37:59 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2011-2013 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.
27 #include <isc/random.h>
28 #include <isc/taskpool.h>
48 alloc_pool(isc_taskmgr_t
*tmgr
, isc_mem_t
*mctx
, unsigned int ntasks
,
49 unsigned int quantum
, isc_taskpool_t
**poolp
)
54 pool
= isc_mem_get(mctx
, sizeof(*pool
));
56 return (ISC_R_NOMEMORY
);
59 isc_mem_attach(mctx
, &pool
->mctx
);
60 pool
->ntasks
= ntasks
;
61 pool
->quantum
= quantum
;
63 pool
->tasks
= isc_mem_get(mctx
, ntasks
* sizeof(isc_task_t
*));
64 if (pool
->tasks
== NULL
) {
65 isc_mem_putanddetach(&pool
->mctx
, pool
, sizeof(*pool
));
66 return (ISC_R_NOMEMORY
);
68 for (i
= 0; i
< ntasks
; i
++)
69 pool
->tasks
[i
] = NULL
;
72 return (ISC_R_SUCCESS
);
76 isc_taskpool_create(isc_taskmgr_t
*tmgr
, isc_mem_t
*mctx
,
77 unsigned int ntasks
, unsigned int quantum
,
78 isc_taskpool_t
**poolp
)
81 isc_taskpool_t
*pool
= NULL
;
86 /* Allocate the pool structure */
87 result
= alloc_pool(tmgr
, mctx
, ntasks
, quantum
, &pool
);
88 if (result
!= ISC_R_SUCCESS
)
91 /* Create the tasks */
92 for (i
= 0; i
< ntasks
; i
++) {
93 result
= isc_task_create(tmgr
, quantum
, &pool
->tasks
[i
]);
94 if (result
!= ISC_R_SUCCESS
) {
95 isc_taskpool_destroy(&pool
);
98 isc_task_setname(pool
->tasks
[i
], "taskpool", NULL
);
102 return (ISC_R_SUCCESS
);
106 isc_taskpool_gettask(isc_taskpool_t
*pool
, isc_task_t
**targetp
) {
109 isc_task_attach(pool
->tasks
[i
% pool
->ntasks
], targetp
);
113 isc_taskpool_size(isc_taskpool_t
*pool
) {
114 REQUIRE(pool
!= NULL
);
115 return (pool
->ntasks
);
119 isc_taskpool_expand(isc_taskpool_t
**sourcep
, unsigned int size
,
120 isc_taskpool_t
**targetp
)
123 isc_taskpool_t
*pool
;
125 REQUIRE(sourcep
!= NULL
&& *sourcep
!= NULL
);
126 REQUIRE(targetp
!= NULL
&& *targetp
== NULL
);
129 if (size
> pool
->ntasks
) {
130 isc_taskpool_t
*newpool
= NULL
;
133 /* Allocate a new pool structure */
134 result
= alloc_pool(pool
->tmgr
, pool
->mctx
, size
,
135 pool
->quantum
, &newpool
);
136 if (result
!= ISC_R_SUCCESS
)
139 /* Copy over the tasks from the old pool */
140 for (i
= 0; i
< pool
->ntasks
; i
++) {
141 newpool
->tasks
[i
] = pool
->tasks
[i
];
142 pool
->tasks
[i
] = NULL
;
145 /* Create new tasks */
146 for (i
= pool
->ntasks
; i
< size
; i
++) {
147 result
= isc_task_create(pool
->tmgr
, pool
->quantum
,
149 if (result
!= ISC_R_SUCCESS
) {
150 isc_taskpool_destroy(&newpool
);
153 isc_task_setname(newpool
->tasks
[i
], "taskpool", NULL
);
156 isc_taskpool_destroy(&pool
);
162 return (ISC_R_SUCCESS
);
166 isc_taskpool_destroy(isc_taskpool_t
**poolp
) {
168 isc_taskpool_t
*pool
= *poolp
;
169 for (i
= 0; i
< pool
->ntasks
; i
++) {
170 if (pool
->tasks
[i
] != NULL
)
171 isc_task_detach(&pool
->tasks
[i
]);
173 isc_mem_put(pool
->mctx
, pool
->tasks
,
174 pool
->ntasks
* sizeof(isc_task_t
*));
175 isc_mem_putanddetach(&pool
->mctx
, pool
, sizeof(*pool
));
180 isc_taskpool_setprivilege(isc_taskpool_t
*pool
, isc_boolean_t priv
) {
183 REQUIRE(pool
!= NULL
);
185 for (i
= 0; i
< pool
->ntasks
; i
++) {
186 if (pool
->tasks
[i
] != NULL
)
187 isc_task_setprivilege(pool
->tasks
[i
], priv
);