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 */
26 #include <isc/magic.h>
27 #include <isc/mutex.h>
32 static isc_mutex_t createlock
;
33 static isc_once_t once
= ISC_ONCE_INIT
;
34 static isc_taskmgrcreatefunc_t taskmgr_createfunc
= NULL
;
38 RUNTIME_CHECK(isc_mutex_init(&createlock
) == ISC_R_SUCCESS
);
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
);
48 if (taskmgr_createfunc
== NULL
)
49 taskmgr_createfunc
= createfunc
;
51 result
= ISC_R_EXISTS
;
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
)
66 REQUIRE(taskmgr_createfunc
!= NULL
);
67 result
= (*taskmgr_createfunc
)(mctx
, workers
, default_quantum
,
72 if (result
== ISC_R_SUCCESS
)
73 isc_appctx_settaskmgr(actx
, *managerp
);
79 isc_taskmgr_create(isc_mem_t
*mctx
, unsigned int workers
,
80 unsigned int default_quantum
, isc_taskmgr_t
**managerp
)
86 REQUIRE(taskmgr_createfunc
!= NULL
);
87 result
= (*taskmgr_createfunc
)(mctx
, workers
, default_quantum
,
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
);
105 isc_task_create(isc_taskmgr_t
*manager
, unsigned int quantum
,
108 REQUIRE(ISCAPI_TASKMGR_VALID(manager
));
109 REQUIRE(taskp
!= NULL
&& *taskp
== NULL
);
111 return (manager
->methods
->taskcreate(manager
, quantum
, taskp
));
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
);
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
);
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
);
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
);
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
));
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
));
171 isc_task_shutdown(isc_task_t
*task
) {
172 REQUIRE(ISCAPI_TASK_VALID(task
));
174 task
->methods
->shutdown(task
);
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
);
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.
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
));