task_api.c revision 9eae5f2a7a189353bd4fcbb939c2b61094b3bfe9
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
3398334b3acda24b086957286288ca9852662b12Automatic Updater * Copyright (C) 2005 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews */
1633838b8255282d10af15c5c84cee5a51466712Bob Halley
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley/* $Id: task_api.c,v 1.3 2009/09/01 03:43:27 jinmei Exp $ */
1bb3831e13a65afd87078c88e0285d23b1e0bcdfMark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <unistd.h>
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley#include <isc/app.h>
e7fb847ed570dd8c1bcdacabb3d69bd81feb79aeMark Andrews#include <isc/magic.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley#include <isc/mutex.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley#include <isc/once.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/task.h>
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence#include <isc/util.h>
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic isc_mutex_t createlock;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic isc_once_t once = ISC_ONCE_INIT;
440be4c866f6935ac069db79a414304507a664c2Michael Graffstatic isc_taskmgrcreatefunc_t taskmgr_createfunc = NULL;
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleystatic void
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyinitialize(void) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence}
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halleyisc_result_t
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_register(isc_taskmgrcreatefunc_t createfunc) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley isc_result_t result = ISC_R_SUCCESS;
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein LOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (taskmgr_createfunc == NULL)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein taskmgr_createfunc = createfunc;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein else
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = ISC_R_EXISTS;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein UNLOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return (result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_taskmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int workers, unsigned int default_quantum,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_taskmgr_t **managerp)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein{
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t result;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein LOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein REQUIRE(taskmgr_createfunc != NULL);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein managerp);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein UNLOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein if (result == ISC_R_SUCCESS)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_appctx_settaskmgr(actx, *managerp);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return (result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein unsigned int default_quantum, isc_taskmgr_t **managerp)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein{
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_result_t result;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein LOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein REQUIRE(taskmgr_createfunc != NULL);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein managerp);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein UNLOCK(&createlock);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein return (result);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein}
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinvoid
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_taskmgr_destroy(isc_taskmgr_t **managerp) {
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein REQUIRE(managerp != NULL && ISCAPI_TASKMGR_VALID(*managerp));
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein (*managerp)->methods->destroy(managerp);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein ENSURE(*managerp == NULL);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein};
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinisc_result_t
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrewsisc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews isc_task_t **taskp)
1bb3831e13a65afd87078c88e0285d23b1e0bcdfMark Andrews{
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley REQUIRE(ISCAPI_TASKMGR_VALID(manager));
fda0ab6a96a7edb1acbde0c77ead0542979b0f24Bob Halley REQUIRE(taskp != NULL && *taskp == NULL);
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley return (manager->methods->taskcreate(manager, quantum, taskp));
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_attach(isc_task_t *source, isc_task_t **targetp) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(ISCAPI_TASK_VALID(source));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(targetp != NULL && *targetp == NULL);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley source->methods->attach(source, targetp);
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ENSURE(*targetp == source);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_detach(isc_task_t **taskp) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley (*taskp)->methods->detach(taskp);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ENSURE(*taskp == NULL);
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_send(isc_task_t *task, isc_event_t **eventp) {
23fdb6a5a3d3d0e427682b68eea9553d2d326ad4Mark Andrews REQUIRE(ISCAPI_TASK_VALID(task));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(eventp != NULL && *eventp != NULL);
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley task->methods->send(task, eventp);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ENSURE(*eventp == NULL);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(eventp != NULL && *eventp != NULL);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley (*taskp)->methods->sendanddetach(taskp, eventp);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley ENSURE(*taskp == NULL && *eventp == NULL);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyunsigned int
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley void *tag, isc_eventlist_t *events)
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley{
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley return (task->methods->unsend(task, sender, type, tag, events));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrenceisc_result_t
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrenceisc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, const void *arg)
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence{
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley return (task->methods->onshutdown(task, action, arg));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
96754ed7b400ce080279de2f92111ad868105290Bob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_shutdown(isc_task_t *task) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence
3cddfdd29969046e598e9465c72936ccd30df233Bob Halley task->methods->shutdown(task);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halleyvoid
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_setname(isc_task_t *task, const char *name, void *tag) {
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley task->methods->setname(task, name, tag);
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyunsigned int
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag)
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley{
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley return (task->methods->purgeevents(task, sender, type, tag));
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley}
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley/*%
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley * This is necessary for libisc's internal timer implementation. Other
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley * implementation might skip implementing this.
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halley */
d8dcd6ad4617cc8d7df979bd62101fa9c4bac1bcBob Halleyunsigned int
e4e071ae12aee942fefc2c0a3280e402938669deBob Halleyisc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first,
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley isc_eventtype_t last, void *tag)
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley{
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley REQUIRE(ISCAPI_TASK_VALID(task));
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley
5804af97ab0875fafd233f486aa1c1899ed5c00aBob Halley return (task->methods->purgerange(task, sender, first, last, tag));
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley}
e4e071ae12aee942fefc2c0a3280e402938669deBob Halley