task_api.c revision 7a54dadeb565d746ef8b3fc77dc76455d836dd30
609b8d08176469485edce25f3c2f50365bbd3819Mark Andrews/*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * purpose with or without fee is hereby granted, provided that the above
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * copyright notice and this permission notice appear in all copies.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * PERFORMANCE OF THIS SOFTWARE.
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User */
f97d56e757b9a293ffbaa915ca4d792ae84ba85aTinderbox User
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver/* $Id: task_api.c,v 1.6 2010/12/22 05:19:02 marka Exp $ */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <config.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews#include <unistd.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/app.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/magic.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/mutex.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/once.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/task.h>
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver#include <isc/util.h>
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic isc_mutex_t createlock;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic isc_once_t once = ISC_ONCE_INIT;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverstatic isc_taskmgrcreatefunc_t taskmgr_createfunc = NULL;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsstatic void
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsinitialize(void) {
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverisc_result_t
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_task_register(isc_taskmgrcreatefunc_t createfunc) {
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_result_t result = ISC_R_SUCCESS;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews LOCK(&createlock);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (taskmgr_createfunc == NULL)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver taskmgr_createfunc = createfunc;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver else
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = ISC_R_EXISTS;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver UNLOCK(&createlock);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (result);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_result_t
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_taskmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver unsigned int workers, unsigned int default_quantum,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_taskmgr_t **managerp)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver{
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_result_t result;
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver LOCK(&createlock);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(taskmgr_createfunc != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver managerp);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver UNLOCK(&createlock);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver if (result == ISC_R_SUCCESS)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_appctx_settaskmgr(actx, *managerp);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (result);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_result_t
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews unsigned int default_quantum, isc_taskmgr_t **managerp)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver{
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_result_t result;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews LOCK(&createlock);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(taskmgr_createfunc != NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews managerp);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews UNLOCK(&createlock);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (result);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews}
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsvoid
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_taskmgr_destroy(isc_taskmgr_t **managerp) {
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(managerp != NULL && ISCAPI_TASKMGR_VALID(*managerp));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver (*managerp)->methods->destroy(managerp);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver ENSURE(*managerp == NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_result_t
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryverisc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver isc_task_t **taskp)
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver{
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(ISCAPI_TASKMGR_VALID(manager));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver REQUIRE(taskp != NULL && *taskp == NULL);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver return (manager->methods->taskcreate(manager, quantum, taskp));
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryvervoid
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrewsisc_task_attach(isc_task_t *source, isc_task_t **targetp) {
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(ISCAPI_TASK_VALID(source));
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt REQUIRE(targetp != NULL && *targetp == NULL);
94315060c2b0d9deafabe72d6a0482405fd9d377Evan Hunt
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver source->methods->attach(source, targetp);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews ENSURE(*targetp == source);
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver}
9fee08f655527a5dd849b171daeeee1dbbccca76Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryvervoid
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryverisc_task_detach(isc_task_t **taskp) {
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver
afaa290bb6acc504e93a0adbf20b6dd6c64e6d63Vernon Schryver (*taskp)->methods->detach(taskp);
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews ENSURE(*taskp == NULL);
df0892aea6bfd20a01c3abf2b756625d23830390Mark Andrews}
void
isc_task_send(isc_task_t *task, isc_event_t **eventp) {
REQUIRE(ISCAPI_TASK_VALID(task));
REQUIRE(eventp != NULL && *eventp != NULL);
task->methods->send(task, eventp);
ENSURE(*eventp == NULL);
}
void
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
REQUIRE(eventp != NULL && *eventp != NULL);
(*taskp)->methods->sendanddetach(taskp, eventp);
ENSURE(*taskp == NULL && *eventp == NULL);
}
unsigned int
isc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
void *tag, isc_eventlist_t *events)
{
REQUIRE(ISCAPI_TASK_VALID(task));
return (task->methods->unsend(task, sender, type, tag, events));
}
isc_result_t
isc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, const void *arg)
{
REQUIRE(ISCAPI_TASK_VALID(task));
return (task->methods->onshutdown(task, action, arg));
}
void
isc_task_shutdown(isc_task_t *task) {
REQUIRE(ISCAPI_TASK_VALID(task));
task->methods->shutdown(task);
}
void
isc_task_setname(isc_task_t *task, const char *name, void *tag) {
REQUIRE(ISCAPI_TASK_VALID(task));
task->methods->setname(task, name, tag);
}
unsigned int
isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag)
{
REQUIRE(ISCAPI_TASK_VALID(task));
return (task->methods->purgeevents(task, sender, type, tag));
}
isc_result_t
isc_task_beginexclusive(isc_task_t *task) {
REQUIRE(ISCAPI_TASK_VALID(task));
return (task->methods->beginexclusive(task));
}
void
isc_task_endexclusive(isc_task_t *task) {
REQUIRE(ISCAPI_TASK_VALID(task));
task->methods->endexclusive(task);
}
/*%
* This is necessary for libisc's internal timer implementation. Other
* implementation might skip implementing this.
*/
unsigned int
isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first,
isc_eventtype_t last, void *tag)
{
REQUIRE(ISCAPI_TASK_VALID(task));
return (task->methods->purgerange(task, sender, first, last, tag));
}