xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/taskpool.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: taskpool.c,v 1.1 2024/02/18 20:57:50 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /*! \file */
17 
18 #include <stdbool.h>
19 
20 #include <isc/mem.h>
21 #include <isc/random.h>
22 #include <isc/taskpool.h>
23 #include <isc/util.h>
24 
25 /***
26  *** Types.
27  ***/
28 
29 struct isc_taskpool {
30 	isc_mem_t *mctx;
31 	isc_taskmgr_t *tmgr;
32 	unsigned int ntasks;
33 	unsigned int quantum;
34 	isc_task_t **tasks;
35 };
36 
37 /***
38  *** Functions.
39  ***/
40 
41 static void
alloc_pool(isc_taskmgr_t * tmgr,isc_mem_t * mctx,unsigned int ntasks,unsigned int quantum,isc_taskpool_t ** poolp)42 alloc_pool(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
43 	   unsigned int quantum, isc_taskpool_t **poolp) {
44 	isc_taskpool_t *pool;
45 	unsigned int i;
46 
47 	pool = isc_mem_get(mctx, sizeof(*pool));
48 
49 	pool->mctx = NULL;
50 	isc_mem_attach(mctx, &pool->mctx);
51 	pool->ntasks = ntasks;
52 	pool->quantum = quantum;
53 	pool->tmgr = tmgr;
54 	pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
55 	for (i = 0; i < ntasks; i++) {
56 		pool->tasks[i] = NULL;
57 	}
58 
59 	*poolp = pool;
60 }
61 
62 isc_result_t
isc_taskpool_create(isc_taskmgr_t * tmgr,isc_mem_t * mctx,unsigned int ntasks,unsigned int quantum,bool priv,isc_taskpool_t ** poolp)63 isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
64 		    unsigned int quantum, bool priv, isc_taskpool_t **poolp) {
65 	unsigned int i;
66 	isc_taskpool_t *pool = NULL;
67 
68 	INSIST(ntasks > 0);
69 
70 	/* Allocate the pool structure */
71 	alloc_pool(tmgr, mctx, ntasks, quantum, &pool);
72 
73 	/* Create the tasks */
74 	for (i = 0; i < ntasks; i++) {
75 		isc_result_t result = isc_task_create_bound(tmgr, quantum,
76 							    &pool->tasks[i], i);
77 		if (result != ISC_R_SUCCESS) {
78 			isc_taskpool_destroy(&pool);
79 			return (result);
80 		}
81 		isc_task_setprivilege(pool->tasks[i], priv);
82 		isc_task_setname(pool->tasks[i], "taskpool", NULL);
83 	}
84 
85 	*poolp = pool;
86 	return (ISC_R_SUCCESS);
87 }
88 
89 void
isc_taskpool_gettask(isc_taskpool_t * pool,isc_task_t ** targetp)90 isc_taskpool_gettask(isc_taskpool_t *pool, isc_task_t **targetp) {
91 	isc_task_attach(pool->tasks[isc_random_uniform(pool->ntasks)], targetp);
92 }
93 
94 int
isc_taskpool_size(isc_taskpool_t * pool)95 isc_taskpool_size(isc_taskpool_t *pool) {
96 	REQUIRE(pool != NULL);
97 	return (pool->ntasks);
98 }
99 
100 isc_result_t
isc_taskpool_expand(isc_taskpool_t ** sourcep,unsigned int size,bool priv,isc_taskpool_t ** targetp)101 isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size, bool priv,
102 		    isc_taskpool_t **targetp) {
103 	isc_taskpool_t *pool;
104 
105 	REQUIRE(sourcep != NULL && *sourcep != NULL);
106 	REQUIRE(targetp != NULL && *targetp == NULL);
107 
108 	pool = *sourcep;
109 	*sourcep = NULL;
110 	if (size > pool->ntasks) {
111 		isc_taskpool_t *newpool = NULL;
112 		unsigned int i;
113 
114 		/* Allocate a new pool structure */
115 		alloc_pool(pool->tmgr, pool->mctx, size, pool->quantum,
116 			   &newpool);
117 
118 		/* Copy over the tasks from the old pool */
119 		for (i = 0; i < pool->ntasks; i++) {
120 			newpool->tasks[i] = pool->tasks[i];
121 			pool->tasks[i] = NULL;
122 		}
123 
124 		/* Create new tasks */
125 		for (i = pool->ntasks; i < size; i++) {
126 			isc_result_t result =
127 				isc_task_create_bound(pool->tmgr, pool->quantum,
128 						      &newpool->tasks[i], i);
129 			if (result != ISC_R_SUCCESS) {
130 				*sourcep = pool;
131 				isc_taskpool_destroy(&newpool);
132 				return (result);
133 			}
134 			isc_task_setprivilege(newpool->tasks[i], priv);
135 			isc_task_setname(newpool->tasks[i], "taskpool", NULL);
136 		}
137 
138 		isc_taskpool_destroy(&pool);
139 		pool = newpool;
140 	}
141 
142 	*targetp = pool;
143 	return (ISC_R_SUCCESS);
144 }
145 
146 void
isc_taskpool_destroy(isc_taskpool_t ** poolp)147 isc_taskpool_destroy(isc_taskpool_t **poolp) {
148 	unsigned int i;
149 	isc_taskpool_t *pool = *poolp;
150 	*poolp = NULL;
151 	for (i = 0; i < pool->ntasks; i++) {
152 		if (pool->tasks[i] != NULL) {
153 			isc_task_detach(&pool->tasks[i]);
154 		}
155 	}
156 	isc_mem_put(pool->mctx, pool->tasks,
157 		    pool->ntasks * sizeof(isc_task_t *));
158 	isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
159 }
160