task_api.c revision 280849
167754Smsmith/* 267754Smsmith * Copyright (C) 2009-2012 Internet Systems Consortium, Inc. ("ISC") 367754Smsmith * 467754Smsmith * Permission to use, copy, modify, and/or distribute this software for any 5167802Sjkim * purpose with or without fee is hereby granted, provided that the above 667754Smsmith * copyright notice and this permission notice appear in all copies. 767754Smsmith * 867754Smsmith * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 967754Smsmith * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 1067754Smsmith * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 1167754Smsmith * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 1267754Smsmith * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13167802Sjkim * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 1470243Smsmith * PERFORMANCE OF THIS SOFTWARE. 1567754Smsmith */ 1667754Smsmith 1767754Smsmith/* $Id$ */ 1867754Smsmith 1967754Smsmith#include <config.h> 2067754Smsmith 2167754Smsmith#include <unistd.h> 2267754Smsmith 2367754Smsmith#include <isc/app.h> 2467754Smsmith#include <isc/magic.h> 2567754Smsmith#include <isc/mutex.h> 2667754Smsmith#include <isc/once.h> 2767754Smsmith#include <isc/task.h> 2867754Smsmith#include <isc/util.h> 2967754Smsmith 3067754Smsmithstatic isc_mutex_t createlock; 3167754Smsmithstatic isc_once_t once = ISC_ONCE_INIT; 3267754Smsmithstatic isc_taskmgrcreatefunc_t taskmgr_createfunc = NULL; 3367754Smsmith 3467754Smsmithstatic void 3567754Smsmithinitialize(void) { 3667754Smsmith RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS); 3767754Smsmith} 3867754Smsmith 3967754Smsmithisc_result_t 4067754Smsmithisc_task_register(isc_taskmgrcreatefunc_t createfunc) { 4167754Smsmith isc_result_t result = ISC_R_SUCCESS; 4267754Smsmith 4367754Smsmith RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS); 4467754Smsmith 4567754Smsmith LOCK(&createlock); 4667754Smsmith if (taskmgr_createfunc == NULL) 4767754Smsmith taskmgr_createfunc = createfunc; 4867754Smsmith else 4967754Smsmith result = ISC_R_EXISTS; 5067754Smsmith UNLOCK(&createlock); 5167754Smsmith 5267754Smsmith return (result); 5367754Smsmith} 5467754Smsmith 5567754Smsmithisc_result_t 5667754Smsmithisc_taskmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx, 5767754Smsmith unsigned int workers, unsigned int default_quantum, 5867754Smsmith isc_taskmgr_t **managerp) 5967754Smsmith{ 6067754Smsmith isc_result_t result; 6167754Smsmith 6267754Smsmith LOCK(&createlock); 6367754Smsmith 6467754Smsmith REQUIRE(taskmgr_createfunc != NULL); 6567754Smsmith result = (*taskmgr_createfunc)(mctx, workers, default_quantum, 6667754Smsmith managerp); 6767754Smsmith 6867754Smsmith UNLOCK(&createlock); 6967754Smsmith 7067754Smsmith if (result == ISC_R_SUCCESS) 7167754Smsmith isc_appctx_settaskmgr(actx, *managerp); 7267754Smsmith 7367754Smsmith return (result); 7467754Smsmith} 7567754Smsmith 7667754Smsmithisc_result_t 7767754Smsmithisc_taskmgr_create(isc_mem_t *mctx, unsigned int workers, 7867754Smsmith unsigned int default_quantum, isc_taskmgr_t **managerp) 7967754Smsmith{ 8067754Smsmith isc_result_t result; 8167754Smsmith 8267754Smsmith LOCK(&createlock); 8367754Smsmith 8467754Smsmith REQUIRE(taskmgr_createfunc != NULL); 8567754Smsmith result = (*taskmgr_createfunc)(mctx, workers, default_quantum, 8667754Smsmith managerp); 8767754Smsmith 8867754Smsmith UNLOCK(&createlock); 8967754Smsmith 9067754Smsmith return (result); 9167754Smsmith} 9267754Smsmith 9367754Smsmithvoid 9467754Smsmithisc_taskmgr_destroy(isc_taskmgr_t **managerp) { 9567754Smsmith REQUIRE(managerp != NULL && ISCAPI_TASKMGR_VALID(*managerp)); 9667754Smsmith 9767754Smsmith (*managerp)->methods->destroy(managerp); 9867754Smsmith 9967754Smsmith ENSURE(*managerp == NULL); 10067754Smsmith} 10167754Smsmith 10267754Smsmithvoid 10367754Smsmithisc_taskmgr_setmode(isc_taskmgr_t *manager, isc_taskmgrmode_t mode) { 10467754Smsmith REQUIRE(ISCAPI_TASKMGR_VALID(manager)); 10567754Smsmith 10667754Smsmith manager->methods->setmode(manager, mode); 10767754Smsmith} 10867754Smsmith 10967754Smsmithisc_taskmgrmode_t 11067754Smsmithisc_taskmgr_mode(isc_taskmgr_t *manager) { 11167754Smsmith REQUIRE(ISCAPI_TASKMGR_VALID(manager)); 11267754Smsmith 11367754Smsmith return (manager->methods->mode(manager)); 11467754Smsmith} 11567754Smsmith 11667754Smsmithisc_result_t 11767754Smsmithisc_task_create(isc_taskmgr_t *manager, unsigned int quantum, 11867754Smsmith isc_task_t **taskp) 11967754Smsmith{ 12067754Smsmith REQUIRE(ISCAPI_TASKMGR_VALID(manager)); 121151600Sobrien REQUIRE(taskp != NULL && *taskp == NULL); 122151600Sobrien 12367754Smsmith return (manager->methods->taskcreate(manager, quantum, taskp)); 12467754Smsmith} 12577424Smsmith 12691116Smsmithvoid 12767754Smsmithisc_task_attach(isc_task_t *source, isc_task_t **targetp) { 12867754Smsmith REQUIRE(ISCAPI_TASK_VALID(source)); 12967754Smsmith REQUIRE(targetp != NULL && *targetp == NULL); 13067754Smsmith 13167754Smsmith source->methods->attach(source, targetp); 13267754Smsmith 133151937Sjkim ENSURE(*targetp == source); 13467754Smsmith} 13567754Smsmith 13667754Smsmithvoid 13767754Smsmithisc_task_detach(isc_task_t **taskp) { 13867754Smsmith REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp)); 13967754Smsmith 14067754Smsmith (*taskp)->methods->detach(taskp); 14167754Smsmith 14267754Smsmith ENSURE(*taskp == NULL); 14367754Smsmith} 14467754Smsmith 14567754Smsmithvoid 14667754Smsmithisc_task_send(isc_task_t *task, isc_event_t **eventp) { 14791116Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 14867754Smsmith REQUIRE(eventp != NULL && *eventp != NULL); 14967754Smsmith 15067754Smsmith task->methods->send(task, eventp); 15167754Smsmith 15267754Smsmith ENSURE(*eventp == NULL); 15367754Smsmith} 15467754Smsmith 15567754Smsmithvoid 15667754Smsmithisc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) { 15767754Smsmith REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp)); 15867754Smsmith REQUIRE(eventp != NULL && *eventp != NULL); 15967754Smsmith 16067754Smsmith (*taskp)->methods->sendanddetach(taskp, eventp); 16167754Smsmith 16267754Smsmith ENSURE(*taskp == NULL && *eventp == NULL); 16367754Smsmith} 16467754Smsmith 16567754Smsmithunsigned int 16667754Smsmithisc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type, 16791116Smsmith void *tag, isc_eventlist_t *events) 16891116Smsmith{ 16991116Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 17091116Smsmith 17191116Smsmith return (task->methods->unsend(task, sender, type, tag, events)); 17267754Smsmith} 17367754Smsmith 17467754Smsmithisc_result_t 17585756Smsmithisc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, const void *arg) 17667754Smsmith{ 17767754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 17891116Smsmith 17967754Smsmith return (task->methods->onshutdown(task, action, arg)); 18067754Smsmith} 18167754Smsmith 18267754Smsmithvoid 18367754Smsmithisc_task_shutdown(isc_task_t *task) { 18467754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 18591116Smsmith 18691116Smsmith task->methods->shutdown(task); 18767754Smsmith} 18867754Smsmith 189167802Sjkimvoid 19067754Smsmithisc_task_setname(isc_task_t *task, const char *name, void *tag) { 191167802Sjkim REQUIRE(ISCAPI_TASK_VALID(task)); 19267754Smsmith 19367754Smsmith task->methods->setname(task, name, tag); 19467754Smsmith} 19567754Smsmith 19667754Smsmithunsigned int 19767754Smsmithisc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag) 19867754Smsmith{ 19967754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 20067754Smsmith 20167754Smsmith return (task->methods->purgeevents(task, sender, type, tag)); 20267754Smsmith} 20367754Smsmith 20467754Smsmithisc_result_t 20567754Smsmithisc_task_beginexclusive(isc_task_t *task) { 20667754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 20767754Smsmith 20867754Smsmith return (task->methods->beginexclusive(task)); 20967754Smsmith} 21067754Smsmith 21167754Smsmithvoid 21291116Smsmithisc_task_endexclusive(isc_task_t *task) { 21367754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 21467754Smsmith 21567754Smsmith task->methods->endexclusive(task); 21667754Smsmith} 21767754Smsmith 21867754Smsmithvoid 21967754Smsmithisc_task_setprivilege(isc_task_t *task, isc_boolean_t priv) { 22067754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 22167754Smsmith 22267754Smsmith task->methods->setprivilege(task, priv); 22367754Smsmith} 22467754Smsmith 22567754Smsmithisc_boolean_t 22667754Smsmithisc_task_privilege(isc_task_t *task) { 22791116Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 22891116Smsmith 22991116Smsmith return (task->methods->privilege(task)); 23091116Smsmith} 23191116Smsmith 23267754Smsmith 23367754Smsmith/*% 23467754Smsmith * This is necessary for libisc's internal timer implementation. Other 23585756Smsmith * implementation might skip implementing this. 23667754Smsmith */ 23767754Smsmithunsigned int 23867754Smsmithisc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first, 23967754Smsmith isc_eventtype_t last, void *tag) 24067754Smsmith{ 24167754Smsmith REQUIRE(ISCAPI_TASK_VALID(task)); 24267754Smsmith 24367754Smsmith return (task->methods->purgerange(task, sender, first, last, tag)); 24467754Smsmith} 24591116Smsmith