No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / task_api.c
bloba862e50310dc0d17411d5905341e9253e956e728
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: task_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp */
21 #include <config.h>
23 #include <unistd.h>
25 #include <isc/app.h>
26 #include <isc/magic.h>
27 #include <isc/mutex.h>
28 #include <isc/once.h>
29 #include <isc/task.h>
30 #include <isc/util.h>
32 static isc_mutex_t createlock;
33 static isc_once_t once = ISC_ONCE_INIT;
34 static isc_taskmgrcreatefunc_t taskmgr_createfunc = NULL;
36 static void
37 initialize(void) {
38 RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
41 isc_result_t
42 isc_task_register(isc_taskmgrcreatefunc_t createfunc) {
43 isc_result_t result = ISC_R_SUCCESS;
45 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
47 LOCK(&createlock);
48 if (taskmgr_createfunc == NULL)
49 taskmgr_createfunc = createfunc;
50 else
51 result = ISC_R_EXISTS;
52 UNLOCK(&createlock);
54 return (result);
57 isc_result_t
58 isc_taskmgr_createinctx(isc_mem_t *mctx, isc_appctx_t *actx,
59 unsigned int workers, unsigned int default_quantum,
60 isc_taskmgr_t **managerp)
62 isc_result_t result;
64 LOCK(&createlock);
66 REQUIRE(taskmgr_createfunc != NULL);
67 result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
68 managerp);
70 UNLOCK(&createlock);
72 if (result == ISC_R_SUCCESS)
73 isc_appctx_settaskmgr(actx, *managerp);
75 return (result);
78 isc_result_t
79 isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers,
80 unsigned int default_quantum, isc_taskmgr_t **managerp)
82 isc_result_t result;
84 LOCK(&createlock);
86 REQUIRE(taskmgr_createfunc != NULL);
87 result = (*taskmgr_createfunc)(mctx, workers, default_quantum,
88 managerp);
90 UNLOCK(&createlock);
92 return (result);
95 void
96 isc_taskmgr_destroy(isc_taskmgr_t **managerp) {
97 REQUIRE(managerp != NULL && ISCAPI_TASKMGR_VALID(*managerp));
99 (*managerp)->methods->destroy(managerp);
101 ENSURE(*managerp == NULL);
104 isc_result_t
105 isc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
106 isc_task_t **taskp)
108 REQUIRE(ISCAPI_TASKMGR_VALID(manager));
109 REQUIRE(taskp != NULL && *taskp == NULL);
111 return (manager->methods->taskcreate(manager, quantum, taskp));
114 void
115 isc_task_attach(isc_task_t *source, isc_task_t **targetp) {
116 REQUIRE(ISCAPI_TASK_VALID(source));
117 REQUIRE(targetp != NULL && *targetp == NULL);
119 source->methods->attach(source, targetp);
121 ENSURE(*targetp == source);
124 void
125 isc_task_detach(isc_task_t **taskp) {
126 REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
128 (*taskp)->methods->detach(taskp);
130 ENSURE(*taskp == NULL);
133 void
134 isc_task_send(isc_task_t *task, isc_event_t **eventp) {
135 REQUIRE(ISCAPI_TASK_VALID(task));
136 REQUIRE(eventp != NULL && *eventp != NULL);
138 task->methods->send(task, eventp);
140 ENSURE(*eventp == NULL);
143 void
144 isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
145 REQUIRE(taskp != NULL && ISCAPI_TASK_VALID(*taskp));
146 REQUIRE(eventp != NULL && *eventp != NULL);
148 (*taskp)->methods->sendanddetach(taskp, eventp);
150 ENSURE(*taskp == NULL && *eventp == NULL);
153 unsigned int
154 isc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
155 void *tag, isc_eventlist_t *events)
157 REQUIRE(ISCAPI_TASK_VALID(task));
159 return (task->methods->unsend(task, sender, type, tag, events));
162 isc_result_t
163 isc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, const void *arg)
165 REQUIRE(ISCAPI_TASK_VALID(task));
167 return (task->methods->onshutdown(task, action, arg));
170 void
171 isc_task_shutdown(isc_task_t *task) {
172 REQUIRE(ISCAPI_TASK_VALID(task));
174 task->methods->shutdown(task);
177 void
178 isc_task_setname(isc_task_t *task, const char *name, void *tag) {
179 REQUIRE(ISCAPI_TASK_VALID(task));
181 task->methods->setname(task, name, tag);
184 unsigned int
185 isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type, void *tag)
187 REQUIRE(ISCAPI_TASK_VALID(task));
189 return (task->methods->purgeevents(task, sender, type, tag));
193 * This is necessary for libisc's internal timer implementation. Other
194 * implementation might skip implementing this.
196 unsigned int
197 isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first,
198 isc_eventtype_t last, void *tag)
200 REQUIRE(ISCAPI_TASK_VALID(task));
202 return (task->methods->purgerange(task, sender, first, last, tag));