1999-10-21 00:32:15 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-10-21 00:32:15 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2000-04-28 04:26:08 +00:00
|
|
|
#include <isc/mem.h>
|
2011-07-06 01:36:32 +00:00
|
|
|
#include <isc/random.h>
|
1999-10-21 00:32:15 +00:00
|
|
|
#include <isc/taskpool.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
/***
|
|
|
|
*** Types.
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct isc_taskpool {
|
2000-08-01 01:33:37 +00:00
|
|
|
isc_mem_t * mctx;
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_taskmgr_t * tmgr;
|
1999-10-21 00:32:15 +00:00
|
|
|
unsigned int ntasks;
|
2011-07-06 01:36:32 +00:00
|
|
|
unsigned int quantum;
|
1999-10-21 00:32:15 +00:00
|
|
|
isc_task_t ** tasks;
|
|
|
|
};
|
2011-07-06 01:36:32 +00:00
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
/***
|
|
|
|
*** Functions.
|
|
|
|
***/
|
|
|
|
|
2020-02-08 04:37:54 -08:00
|
|
|
static void
|
2011-07-06 01:36:32 +00:00
|
|
|
alloc_pool(isc_taskmgr_t *tmgr, isc_mem_t *mctx, unsigned int ntasks,
|
|
|
|
unsigned int quantum, isc_taskpool_t **poolp)
|
1999-10-21 00:32:15 +00:00
|
|
|
{
|
|
|
|
isc_taskpool_t *pool;
|
2011-07-06 01:36:32 +00:00
|
|
|
unsigned int i;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
pool = isc_mem_get(mctx, sizeof(*pool));
|
2013-03-08 14:38:03 +11:00
|
|
|
|
|
|
|
pool->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &pool->mctx);
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->ntasks = ntasks;
|
2011-07-06 01:36:32 +00:00
|
|
|
pool->quantum = quantum;
|
|
|
|
pool->tmgr = tmgr;
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
|
2020-02-08 04:37:54 -08:00
|
|
|
for (i = 0; i < ntasks; i++) {
|
1999-10-21 00:32:15 +00:00
|
|
|
pool->tasks[i] = NULL;
|
2020-02-08 04:37:54 -08:00
|
|
|
}
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
*poolp = pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_taskpool_create(isc_taskmgr_t *tmgr, isc_mem_t *mctx,
|
|
|
|
unsigned int ntasks, unsigned int quantum,
|
|
|
|
isc_taskpool_t **poolp)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
isc_taskpool_t *pool = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
INSIST(ntasks > 0);
|
2011-07-07 23:47:50 +00:00
|
|
|
|
2011-07-06 01:36:32 +00:00
|
|
|
/* Allocate the pool structure */
|
2020-02-08 04:37:54 -08:00
|
|
|
alloc_pool(tmgr, mctx, ntasks, quantum, &pool);
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
/* Create the tasks */
|
1999-10-21 00:32:15 +00:00
|
|
|
for (i = 0; i < ntasks; i++) {
|
2000-04-12 01:41:21 +00:00
|
|
|
result = isc_task_create(tmgr, quantum, &pool->tasks[i]);
|
1999-10-21 00:32:15 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
isc_taskpool_destroy(&pool);
|
|
|
|
return (result);
|
|
|
|
}
|
2007-02-13 02:49:08 +00:00
|
|
|
isc_task_setname(pool->tasks[i], "taskpool", NULL);
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
2011-07-06 01:36:32 +00:00
|
|
|
|
1999-10-21 00:32:15 +00:00
|
|
|
*poolp = pool;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-07-06 01:36:32 +00:00
|
|
|
void
|
|
|
|
isc_taskpool_gettask(isc_taskpool_t *pool, isc_task_t **targetp) {
|
2018-05-28 15:22:23 +02:00
|
|
|
isc_task_attach(pool->tasks[isc_random_uniform(pool->ntasks)], targetp);
|
2011-07-06 01:36:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
isc_taskpool_size(isc_taskpool_t *pool) {
|
|
|
|
REQUIRE(pool != NULL);
|
|
|
|
return (pool->ntasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_taskpool_expand(isc_taskpool_t **sourcep, unsigned int size,
|
|
|
|
isc_taskpool_t **targetp)
|
1999-10-21 00:32:15 +00:00
|
|
|
{
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_taskpool_t *pool;
|
|
|
|
|
|
|
|
REQUIRE(sourcep != NULL && *sourcep != NULL);
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
|
|
|
pool = *sourcep;
|
2020-02-08 04:37:54 -08:00
|
|
|
*sourcep = NULL;
|
2011-07-06 01:36:32 +00:00
|
|
|
if (size > pool->ntasks) {
|
|
|
|
isc_taskpool_t *newpool = NULL;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* Allocate a new pool structure */
|
2020-02-08 04:37:54 -08:00
|
|
|
alloc_pool(pool->tmgr, pool->mctx, size,
|
|
|
|
pool->quantum, &newpool);
|
2011-07-06 01:36:32 +00:00
|
|
|
|
|
|
|
/* Copy over the tasks from the old pool */
|
|
|
|
for (i = 0; i < pool->ntasks; i++) {
|
|
|
|
newpool->tasks[i] = pool->tasks[i];
|
|
|
|
pool->tasks[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create new tasks */
|
|
|
|
for (i = pool->ntasks; i < size; i++) {
|
|
|
|
result = isc_task_create(pool->tmgr, pool->quantum,
|
|
|
|
&newpool->tasks[i]);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-02-08 04:37:54 -08:00
|
|
|
*sourcep = pool;
|
2011-07-06 01:36:32 +00:00
|
|
|
isc_taskpool_destroy(&newpool);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
isc_task_setname(newpool->tasks[i], "taskpool", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_taskpool_destroy(&pool);
|
|
|
|
pool = newpool;
|
|
|
|
}
|
|
|
|
|
|
|
|
*targetp = pool;
|
|
|
|
return (ISC_R_SUCCESS);
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_taskpool_destroy(isc_taskpool_t **poolp) {
|
|
|
|
unsigned int i;
|
|
|
|
isc_taskpool_t *pool = *poolp;
|
2020-02-08 04:37:54 -08:00
|
|
|
*poolp = NULL;
|
1999-10-21 00:32:15 +00:00
|
|
|
for (i = 0; i < pool->ntasks; i++) {
|
2011-09-02 21:15:39 +00:00
|
|
|
if (pool->tasks[i] != NULL)
|
1999-10-21 00:32:15 +00:00
|
|
|
isc_task_detach(&pool->tasks[i]);
|
|
|
|
}
|
|
|
|
isc_mem_put(pool->mctx, pool->tasks,
|
|
|
|
pool->ntasks * sizeof(isc_task_t *));
|
2013-03-08 14:38:03 +11:00
|
|
|
isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
|
1999-10-21 00:32:15 +00:00
|
|
|
}
|
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
void
|
2018-04-17 08:29:14 -07:00
|
|
|
isc_taskpool_setprivilege(isc_taskpool_t *pool, bool priv) {
|
2011-09-02 21:15:39 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
REQUIRE(pool != NULL);
|
1999-10-21 00:32:15 +00:00
|
|
|
|
2011-09-02 21:15:39 +00:00
|
|
|
for (i = 0; i < pool->ntasks; i++) {
|
|
|
|
if (pool->tasks[i] != NULL)
|
|
|
|
isc_task_setprivilege(pool->tasks[i], priv);
|
|
|
|
}
|
|
|
|
}
|