xref: /freebsd-src/contrib/ntp/libntp/lib/isc/task.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004-2012  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1998-2003  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id$ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file
21*a466cc55SCy Schubert  * \author Principal Author: Bob Halley
22*a466cc55SCy Schubert  */
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert /*
25*a466cc55SCy Schubert  * XXXRTH  Need to document the states a task can be in, and the rules
26*a466cc55SCy Schubert  * for changing states.
27*a466cc55SCy Schubert  */
28*a466cc55SCy Schubert 
29*a466cc55SCy Schubert #include <config.h>
30*a466cc55SCy Schubert 
31*a466cc55SCy Schubert #include <isc/condition.h>
32*a466cc55SCy Schubert #include <isc/event.h>
33*a466cc55SCy Schubert #include <isc/magic.h>
34*a466cc55SCy Schubert #include <isc/mem.h>
35*a466cc55SCy Schubert #include <isc/msgs.h>
36*a466cc55SCy Schubert #include <isc/platform.h>
37*a466cc55SCy Schubert #include <isc/string.h>
38*a466cc55SCy Schubert #include <isc/task.h>
39*a466cc55SCy Schubert #include <isc/thread.h>
40*a466cc55SCy Schubert #include <isc/util.h>
41*a466cc55SCy Schubert #include <isc/xml.h>
42*a466cc55SCy Schubert 
43*a466cc55SCy Schubert #ifdef OPENSSL_LEAKS
44*a466cc55SCy Schubert #include <openssl/err.h>
45*a466cc55SCy Schubert #endif
46*a466cc55SCy Schubert 
47*a466cc55SCy Schubert /*%
48*a466cc55SCy Schubert  * For BIND9 internal applications:
49*a466cc55SCy Schubert  * when built with threads we use multiple worker threads shared by the whole
50*a466cc55SCy Schubert  * application.
51*a466cc55SCy Schubert  * when built without threads we share a single global task manager and use
52*a466cc55SCy Schubert  * an integrated event loop for socket, timer, and other generic task events.
53*a466cc55SCy Schubert  * For generic library:
54*a466cc55SCy Schubert  * we don't use either of them: an application can have multiple task managers
55*a466cc55SCy Schubert  * whether or not it's threaded, and if the application is threaded each thread
56*a466cc55SCy Schubert  * is expected to have a separate manager; no "worker threads" are shared by
57*a466cc55SCy Schubert  * the application threads.
58*a466cc55SCy Schubert  */
59*a466cc55SCy Schubert #ifdef BIND9
60*a466cc55SCy Schubert #ifdef ISC_PLATFORM_USETHREADS
61*a466cc55SCy Schubert #define USE_WORKER_THREADS
62*a466cc55SCy Schubert #else
63*a466cc55SCy Schubert #define USE_SHARED_MANAGER
64*a466cc55SCy Schubert #endif	/* ISC_PLATFORM_USETHREADS */
65*a466cc55SCy Schubert #endif	/* BIND9 */
66*a466cc55SCy Schubert 
67*a466cc55SCy Schubert #include "task_p.h"
68*a466cc55SCy Schubert 
69*a466cc55SCy Schubert #ifdef ISC_TASK_TRACE
70*a466cc55SCy Schubert #define XTRACE(m)		fprintf(stderr, "task %p thread %lu: %s\n", \
71*a466cc55SCy Schubert 				       task, isc_thread_self(), (m))
72*a466cc55SCy Schubert #define XTTRACE(t, m)		fprintf(stderr, "task %p thread %lu: %s\n", \
73*a466cc55SCy Schubert 				       (t), isc_thread_self(), (m))
74*a466cc55SCy Schubert #define XTHREADTRACE(m)		fprintf(stderr, "thread %lu: %s\n", \
75*a466cc55SCy Schubert 				       isc_thread_self(), (m))
76*a466cc55SCy Schubert #else
77*a466cc55SCy Schubert #define XTRACE(m)
78*a466cc55SCy Schubert #define XTTRACE(t, m)
79*a466cc55SCy Schubert #define XTHREADTRACE(m)
80*a466cc55SCy Schubert #endif
81*a466cc55SCy Schubert 
82*a466cc55SCy Schubert /***
83*a466cc55SCy Schubert  *** Types.
84*a466cc55SCy Schubert  ***/
85*a466cc55SCy Schubert 
86*a466cc55SCy Schubert typedef enum {
87*a466cc55SCy Schubert 	task_state_idle, task_state_ready, task_state_running,
88*a466cc55SCy Schubert 	task_state_done
89*a466cc55SCy Schubert } task_state_t;
90*a466cc55SCy Schubert 
91*a466cc55SCy Schubert #if defined(HAVE_LIBXML2) && defined(BIND9)
92*a466cc55SCy Schubert static const char *statenames[] = {
93*a466cc55SCy Schubert 	"idle", "ready", "running", "done",
94*a466cc55SCy Schubert };
95*a466cc55SCy Schubert #endif
96*a466cc55SCy Schubert 
97*a466cc55SCy Schubert #define TASK_MAGIC			ISC_MAGIC('T', 'A', 'S', 'K')
98*a466cc55SCy Schubert #define VALID_TASK(t)			ISC_MAGIC_VALID(t, TASK_MAGIC)
99*a466cc55SCy Schubert 
100*a466cc55SCy Schubert typedef struct isc__task isc__task_t;
101*a466cc55SCy Schubert typedef struct isc__taskmgr isc__taskmgr_t;
102*a466cc55SCy Schubert 
103*a466cc55SCy Schubert struct isc__task {
104*a466cc55SCy Schubert 	/* Not locked. */
105*a466cc55SCy Schubert 	isc_task_t			common;
106*a466cc55SCy Schubert 	isc__taskmgr_t *		manager;
107*a466cc55SCy Schubert 	isc_mutex_t			lock;
108*a466cc55SCy Schubert 	/* Locked by task lock. */
109*a466cc55SCy Schubert 	task_state_t			state;
110*a466cc55SCy Schubert 	unsigned int			references;
111*a466cc55SCy Schubert 	isc_eventlist_t			events;
112*a466cc55SCy Schubert 	isc_eventlist_t			on_shutdown;
113*a466cc55SCy Schubert 	unsigned int			quantum;
114*a466cc55SCy Schubert 	unsigned int			flags;
115*a466cc55SCy Schubert 	isc_stdtime_t			now;
116*a466cc55SCy Schubert 	char				name[16];
117*a466cc55SCy Schubert 	void *				tag;
118*a466cc55SCy Schubert 	/* Locked by task manager lock. */
119*a466cc55SCy Schubert 	LINK(isc__task_t)		link;
120*a466cc55SCy Schubert 	LINK(isc__task_t)		ready_link;
121*a466cc55SCy Schubert 	LINK(isc__task_t)		ready_priority_link;
122*a466cc55SCy Schubert };
123*a466cc55SCy Schubert 
124*a466cc55SCy Schubert #define TASK_F_SHUTTINGDOWN		0x01
125*a466cc55SCy Schubert #define TASK_F_PRIVILEGED		0x02
126*a466cc55SCy Schubert 
127*a466cc55SCy Schubert #define TASK_SHUTTINGDOWN(t)		(((t)->flags & TASK_F_SHUTTINGDOWN) \
128*a466cc55SCy Schubert 					 != 0)
129*a466cc55SCy Schubert 
130*a466cc55SCy Schubert #define TASK_MANAGER_MAGIC		ISC_MAGIC('T', 'S', 'K', 'M')
131*a466cc55SCy Schubert #define VALID_MANAGER(m)		ISC_MAGIC_VALID(m, TASK_MANAGER_MAGIC)
132*a466cc55SCy Schubert 
133*a466cc55SCy Schubert typedef ISC_LIST(isc__task_t)	isc__tasklist_t;
134*a466cc55SCy Schubert 
135*a466cc55SCy Schubert struct isc__taskmgr {
136*a466cc55SCy Schubert 	/* Not locked. */
137*a466cc55SCy Schubert 	isc_taskmgr_t			common;
138*a466cc55SCy Schubert 	isc_mem_t *			mctx;
139*a466cc55SCy Schubert 	isc_mutex_t			lock;
140*a466cc55SCy Schubert #ifdef ISC_PLATFORM_USETHREADS
141*a466cc55SCy Schubert 	unsigned int			workers;
142*a466cc55SCy Schubert 	isc_thread_t *			threads;
143*a466cc55SCy Schubert #endif /* ISC_PLATFORM_USETHREADS */
144*a466cc55SCy Schubert 	/* Locked by task manager lock. */
145*a466cc55SCy Schubert 	unsigned int			default_quantum;
146*a466cc55SCy Schubert 	LIST(isc__task_t)		tasks;
147*a466cc55SCy Schubert 	isc__tasklist_t			ready_tasks;
148*a466cc55SCy Schubert 	isc__tasklist_t			ready_priority_tasks;
149*a466cc55SCy Schubert 	isc_taskmgrmode_t		mode;
150*a466cc55SCy Schubert #ifdef ISC_PLATFORM_USETHREADS
151*a466cc55SCy Schubert 	isc_condition_t			work_available;
152*a466cc55SCy Schubert 	isc_condition_t			exclusive_granted;
153*a466cc55SCy Schubert 	isc_condition_t			paused;
154*a466cc55SCy Schubert #endif /* ISC_PLATFORM_USETHREADS */
155*a466cc55SCy Schubert 	unsigned int			tasks_running;
156*a466cc55SCy Schubert 	isc_boolean_t			pause_requested;
157*a466cc55SCy Schubert 	isc_boolean_t			exclusive_requested;
158*a466cc55SCy Schubert 	isc_boolean_t			exiting;
159*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
160*a466cc55SCy Schubert 	unsigned int			refs;
161*a466cc55SCy Schubert #endif /* ISC_PLATFORM_USETHREADS */
162*a466cc55SCy Schubert };
163*a466cc55SCy Schubert 
164*a466cc55SCy Schubert #define DEFAULT_TASKMGR_QUANTUM		10
165*a466cc55SCy Schubert #define DEFAULT_DEFAULT_QUANTUM		5
166*a466cc55SCy Schubert #define FINISHED(m)			((m)->exiting && EMPTY((m)->tasks))
167*a466cc55SCy Schubert 
168*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
169*a466cc55SCy Schubert static isc__taskmgr_t *taskmgr = NULL;
170*a466cc55SCy Schubert #endif /* USE_SHARED_MANAGER */
171*a466cc55SCy Schubert 
172*a466cc55SCy Schubert /*%
173*a466cc55SCy Schubert  * The following can be either static or public, depending on build environment.
174*a466cc55SCy Schubert  */
175*a466cc55SCy Schubert 
176*a466cc55SCy Schubert #ifdef BIND9
177*a466cc55SCy Schubert #define ISC_TASKFUNC_SCOPE
178*a466cc55SCy Schubert #else
179*a466cc55SCy Schubert #define ISC_TASKFUNC_SCOPE static
180*a466cc55SCy Schubert #endif
181*a466cc55SCy Schubert 
182*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
183*a466cc55SCy Schubert isc__task_create(isc_taskmgr_t *manager0, unsigned int quantum,
184*a466cc55SCy Schubert 		 isc_task_t **taskp);
185*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
186*a466cc55SCy Schubert isc__task_attach(isc_task_t *source0, isc_task_t **targetp);
187*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
188*a466cc55SCy Schubert isc__task_detach(isc_task_t **taskp);
189*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
190*a466cc55SCy Schubert isc__task_send(isc_task_t *task0, isc_event_t **eventp);
191*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
192*a466cc55SCy Schubert isc__task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp);
193*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
194*a466cc55SCy Schubert isc__task_purgerange(isc_task_t *task0, void *sender, isc_eventtype_t first,
195*a466cc55SCy Schubert 		     isc_eventtype_t last, void *tag);
196*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
197*a466cc55SCy Schubert isc__task_purge(isc_task_t *task, void *sender, isc_eventtype_t type,
198*a466cc55SCy Schubert 		void *tag);
199*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_boolean_t
200*a466cc55SCy Schubert isc__task_purgeevent(isc_task_t *task0, isc_event_t *event);
201*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
202*a466cc55SCy Schubert isc__task_unsendrange(isc_task_t *task, void *sender, isc_eventtype_t first,
203*a466cc55SCy Schubert 		      isc_eventtype_t last, void *tag,
204*a466cc55SCy Schubert 		      isc_eventlist_t *events);
205*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
206*a466cc55SCy Schubert isc__task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
207*a466cc55SCy Schubert 		 void *tag, isc_eventlist_t *events);
208*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
209*a466cc55SCy Schubert isc__task_onshutdown(isc_task_t *task0, isc_taskaction_t action,
210*a466cc55SCy Schubert 		     const void *arg);
211*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
212*a466cc55SCy Schubert isc__task_shutdown(isc_task_t *task0);
213*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
214*a466cc55SCy Schubert isc__task_destroy(isc_task_t **taskp);
215*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
216*a466cc55SCy Schubert isc__task_setname(isc_task_t *task0, const char *name, void *tag);
217*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE const char *
218*a466cc55SCy Schubert isc__task_getname(isc_task_t *task0);
219*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void *
220*a466cc55SCy Schubert isc__task_gettag(isc_task_t *task0);
221*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
222*a466cc55SCy Schubert isc__task_getcurrenttime(isc_task_t *task0, isc_stdtime_t *t);
223*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
224*a466cc55SCy Schubert isc__taskmgr_create(isc_mem_t *mctx, unsigned int workers,
225*a466cc55SCy Schubert 		    unsigned int default_quantum, isc_taskmgr_t **managerp);
226*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
227*a466cc55SCy Schubert isc__taskmgr_destroy(isc_taskmgr_t **managerp);
228*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
229*a466cc55SCy Schubert isc__task_beginexclusive(isc_task_t *task);
230*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
231*a466cc55SCy Schubert isc__task_endexclusive(isc_task_t *task0);
232*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
233*a466cc55SCy Schubert isc__task_setprivilege(isc_task_t *task0, isc_boolean_t priv);
234*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_boolean_t
235*a466cc55SCy Schubert isc__task_privilege(isc_task_t *task0);
236*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
237*a466cc55SCy Schubert isc__taskmgr_setmode(isc_taskmgr_t *manager0, isc_taskmgrmode_t mode);
238*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_taskmgrmode_t
239*a466cc55SCy Schubert isc__taskmgr_mode(isc_taskmgr_t *manager0);
240*a466cc55SCy Schubert 
241*a466cc55SCy Schubert static inline isc_boolean_t
242*a466cc55SCy Schubert empty_readyq(isc__taskmgr_t *manager);
243*a466cc55SCy Schubert 
244*a466cc55SCy Schubert static inline isc__task_t *
245*a466cc55SCy Schubert pop_readyq(isc__taskmgr_t *manager);
246*a466cc55SCy Schubert 
247*a466cc55SCy Schubert static inline void
248*a466cc55SCy Schubert push_readyq(isc__taskmgr_t *manager, isc__task_t *task);
249*a466cc55SCy Schubert 
250*a466cc55SCy Schubert static struct isc__taskmethods {
251*a466cc55SCy Schubert 	isc_taskmethods_t methods;
252*a466cc55SCy Schubert 
253*a466cc55SCy Schubert 	/*%
254*a466cc55SCy Schubert 	 * The following are defined just for avoiding unused static functions.
255*a466cc55SCy Schubert 	 */
256*a466cc55SCy Schubert #ifndef BIND9
257*a466cc55SCy Schubert 	void *purgeevent, *unsendrange, *getname, *gettag, *getcurrenttime;
258*a466cc55SCy Schubert #endif
259*a466cc55SCy Schubert } taskmethods = {
260*a466cc55SCy Schubert 	{
261*a466cc55SCy Schubert 		isc__task_attach,
262*a466cc55SCy Schubert 		isc__task_detach,
263*a466cc55SCy Schubert 		isc__task_destroy,
264*a466cc55SCy Schubert 		isc__task_send,
265*a466cc55SCy Schubert 		isc__task_sendanddetach,
266*a466cc55SCy Schubert 		isc__task_unsend,
267*a466cc55SCy Schubert 		isc__task_onshutdown,
268*a466cc55SCy Schubert 		isc__task_shutdown,
269*a466cc55SCy Schubert 		isc__task_setname,
270*a466cc55SCy Schubert 		isc__task_purge,
271*a466cc55SCy Schubert 		isc__task_purgerange,
272*a466cc55SCy Schubert 		isc__task_beginexclusive,
273*a466cc55SCy Schubert 		isc__task_endexclusive,
274*a466cc55SCy Schubert 		isc__task_setprivilege,
275*a466cc55SCy Schubert 		isc__task_privilege
276*a466cc55SCy Schubert 	}
277*a466cc55SCy Schubert #ifndef BIND9
278*a466cc55SCy Schubert 	,
279*a466cc55SCy Schubert 	(void *)isc__task_purgeevent, (void *)isc__task_unsendrange,
280*a466cc55SCy Schubert 	(void *)isc__task_getname, (void *)isc__task_gettag,
281*a466cc55SCy Schubert 	(void *)isc__task_getcurrenttime
282*a466cc55SCy Schubert #endif
283*a466cc55SCy Schubert };
284*a466cc55SCy Schubert 
285*a466cc55SCy Schubert static isc_taskmgrmethods_t taskmgrmethods = {
286*a466cc55SCy Schubert 	isc__taskmgr_destroy,
287*a466cc55SCy Schubert 	isc__taskmgr_setmode,
288*a466cc55SCy Schubert 	isc__taskmgr_mode,
289*a466cc55SCy Schubert 	isc__task_create
290*a466cc55SCy Schubert };
291*a466cc55SCy Schubert 
292*a466cc55SCy Schubert /***
293*a466cc55SCy Schubert  *** Tasks.
294*a466cc55SCy Schubert  ***/
295*a466cc55SCy Schubert 
296*a466cc55SCy Schubert static void
task_finished(isc__task_t * task)297*a466cc55SCy Schubert task_finished(isc__task_t *task) {
298*a466cc55SCy Schubert 	isc__taskmgr_t *manager = task->manager;
299*a466cc55SCy Schubert 
300*a466cc55SCy Schubert 	REQUIRE(EMPTY(task->events));
301*a466cc55SCy Schubert 	REQUIRE(EMPTY(task->on_shutdown));
302*a466cc55SCy Schubert 	REQUIRE(task->references == 0);
303*a466cc55SCy Schubert 	REQUIRE(task->state == task_state_done);
304*a466cc55SCy Schubert 
305*a466cc55SCy Schubert 	XTRACE("task_finished");
306*a466cc55SCy Schubert 
307*a466cc55SCy Schubert 	LOCK(&manager->lock);
308*a466cc55SCy Schubert 	UNLINK(manager->tasks, task, link);
309*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
310*a466cc55SCy Schubert 	if (FINISHED(manager)) {
311*a466cc55SCy Schubert 		/*
312*a466cc55SCy Schubert 		 * All tasks have completed and the
313*a466cc55SCy Schubert 		 * task manager is exiting.  Wake up
314*a466cc55SCy Schubert 		 * any idle worker threads so they
315*a466cc55SCy Schubert 		 * can exit.
316*a466cc55SCy Schubert 		 */
317*a466cc55SCy Schubert 		BROADCAST(&manager->work_available);
318*a466cc55SCy Schubert 	}
319*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
320*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
321*a466cc55SCy Schubert 
322*a466cc55SCy Schubert 	DESTROYLOCK(&task->lock);
323*a466cc55SCy Schubert 	task->common.impmagic = 0;
324*a466cc55SCy Schubert 	task->common.magic = 0;
325*a466cc55SCy Schubert 	isc_mem_put(manager->mctx, task, sizeof(*task));
326*a466cc55SCy Schubert }
327*a466cc55SCy Schubert 
328*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
isc__task_create(isc_taskmgr_t * manager0,unsigned int quantum,isc_task_t ** taskp)329*a466cc55SCy Schubert isc__task_create(isc_taskmgr_t *manager0, unsigned int quantum,
330*a466cc55SCy Schubert 		 isc_task_t **taskp)
331*a466cc55SCy Schubert {
332*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
333*a466cc55SCy Schubert 	isc__task_t *task;
334*a466cc55SCy Schubert 	isc_boolean_t exiting;
335*a466cc55SCy Schubert 	isc_result_t result;
336*a466cc55SCy Schubert 
337*a466cc55SCy Schubert 	REQUIRE(VALID_MANAGER(manager));
338*a466cc55SCy Schubert 	REQUIRE(taskp != NULL && *taskp == NULL);
339*a466cc55SCy Schubert 
340*a466cc55SCy Schubert 	task = isc_mem_get(manager->mctx, sizeof(*task));
341*a466cc55SCy Schubert 	if (task == NULL)
342*a466cc55SCy Schubert 		return (ISC_R_NOMEMORY);
343*a466cc55SCy Schubert 	XTRACE("isc_task_create");
344*a466cc55SCy Schubert 	result = isc_mutex_init(&task->lock);
345*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS) {
346*a466cc55SCy Schubert 		isc_mem_put(manager->mctx, task, sizeof(*task));
347*a466cc55SCy Schubert 		return (result);
348*a466cc55SCy Schubert 	}
349*a466cc55SCy Schubert 	LOCK(&manager->lock);
350*a466cc55SCy Schubert 	LOCK(&task->lock);	/* helps coverity analysis noise ratio */
351*a466cc55SCy Schubert 	task->manager = manager;
352*a466cc55SCy Schubert 	task->state = task_state_idle;
353*a466cc55SCy Schubert 	task->references = 1;
354*a466cc55SCy Schubert 	INIT_LIST(task->events);
355*a466cc55SCy Schubert 	INIT_LIST(task->on_shutdown);
356*a466cc55SCy Schubert 	task->quantum = quantum;
357*a466cc55SCy Schubert 	task->flags = 0;
358*a466cc55SCy Schubert 	task->now = 0;
359*a466cc55SCy Schubert 	memset(task->name, 0, sizeof(task->name));
360*a466cc55SCy Schubert 	task->tag = NULL;
361*a466cc55SCy Schubert 	INIT_LINK(task, link);
362*a466cc55SCy Schubert 	INIT_LINK(task, ready_link);
363*a466cc55SCy Schubert 	INIT_LINK(task, ready_priority_link);
364*a466cc55SCy Schubert 	UNLOCK(&task->lock);
365*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
366*a466cc55SCy Schubert 
367*a466cc55SCy Schubert 	exiting = ISC_FALSE;
368*a466cc55SCy Schubert 	LOCK(&manager->lock);
369*a466cc55SCy Schubert 	if (!manager->exiting) {
370*a466cc55SCy Schubert 		if (task->quantum == 0)
371*a466cc55SCy Schubert 			task->quantum = manager->default_quantum;
372*a466cc55SCy Schubert 		APPEND(manager->tasks, task, link);
373*a466cc55SCy Schubert 	} else
374*a466cc55SCy Schubert 		exiting = ISC_TRUE;
375*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
376*a466cc55SCy Schubert 
377*a466cc55SCy Schubert 	if (exiting) {
378*a466cc55SCy Schubert 		DESTROYLOCK(&task->lock);
379*a466cc55SCy Schubert 		isc_mem_put(manager->mctx, task, sizeof(*task));
380*a466cc55SCy Schubert 		return (ISC_R_SHUTTINGDOWN);
381*a466cc55SCy Schubert 	}
382*a466cc55SCy Schubert 
383*a466cc55SCy Schubert 	task->common.methods = (isc_taskmethods_t *)&taskmethods;
384*a466cc55SCy Schubert 	task->common.magic = ISCAPI_TASK_MAGIC;
385*a466cc55SCy Schubert 	task->common.impmagic = TASK_MAGIC;
386*a466cc55SCy Schubert 	*taskp = (isc_task_t *)task;
387*a466cc55SCy Schubert 
388*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
389*a466cc55SCy Schubert }
390*a466cc55SCy Schubert 
391*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_attach(isc_task_t * source0,isc_task_t ** targetp)392*a466cc55SCy Schubert isc__task_attach(isc_task_t *source0, isc_task_t **targetp) {
393*a466cc55SCy Schubert 	isc__task_t *source = (isc__task_t *)source0;
394*a466cc55SCy Schubert 
395*a466cc55SCy Schubert 	/*
396*a466cc55SCy Schubert 	 * Attach *targetp to source.
397*a466cc55SCy Schubert 	 */
398*a466cc55SCy Schubert 
399*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(source));
400*a466cc55SCy Schubert 	REQUIRE(targetp != NULL && *targetp == NULL);
401*a466cc55SCy Schubert 
402*a466cc55SCy Schubert 	XTTRACE(source, "isc_task_attach");
403*a466cc55SCy Schubert 
404*a466cc55SCy Schubert 	LOCK(&source->lock);
405*a466cc55SCy Schubert 	source->references++;
406*a466cc55SCy Schubert 	UNLOCK(&source->lock);
407*a466cc55SCy Schubert 
408*a466cc55SCy Schubert 	*targetp = (isc_task_t *)source;
409*a466cc55SCy Schubert }
410*a466cc55SCy Schubert 
411*a466cc55SCy Schubert static inline isc_boolean_t
task_shutdown(isc__task_t * task)412*a466cc55SCy Schubert task_shutdown(isc__task_t *task) {
413*a466cc55SCy Schubert 	isc_boolean_t was_idle = ISC_FALSE;
414*a466cc55SCy Schubert 	isc_event_t *event, *prev;
415*a466cc55SCy Schubert 
416*a466cc55SCy Schubert 	/*
417*a466cc55SCy Schubert 	 * Caller must be holding the task's lock.
418*a466cc55SCy Schubert 	 */
419*a466cc55SCy Schubert 
420*a466cc55SCy Schubert 	XTRACE("task_shutdown");
421*a466cc55SCy Schubert 
422*a466cc55SCy Schubert 	if (! TASK_SHUTTINGDOWN(task)) {
423*a466cc55SCy Schubert 		XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
424*a466cc55SCy Schubert 				      ISC_MSG_SHUTTINGDOWN, "shutting down"));
425*a466cc55SCy Schubert 		task->flags |= TASK_F_SHUTTINGDOWN;
426*a466cc55SCy Schubert 		if (task->state == task_state_idle) {
427*a466cc55SCy Schubert 			INSIST(EMPTY(task->events));
428*a466cc55SCy Schubert 			task->state = task_state_ready;
429*a466cc55SCy Schubert 			was_idle = ISC_TRUE;
430*a466cc55SCy Schubert 		}
431*a466cc55SCy Schubert 		INSIST(task->state == task_state_ready ||
432*a466cc55SCy Schubert 		       task->state == task_state_running);
433*a466cc55SCy Schubert 
434*a466cc55SCy Schubert 		/*
435*a466cc55SCy Schubert 		 * Note that we post shutdown events LIFO.
436*a466cc55SCy Schubert 		 */
437*a466cc55SCy Schubert 		for (event = TAIL(task->on_shutdown);
438*a466cc55SCy Schubert 		     event != NULL;
439*a466cc55SCy Schubert 		     event = prev) {
440*a466cc55SCy Schubert 			prev = PREV(event, ev_link);
441*a466cc55SCy Schubert 			DEQUEUE(task->on_shutdown, event, ev_link);
442*a466cc55SCy Schubert 			ENQUEUE(task->events, event, ev_link);
443*a466cc55SCy Schubert 		}
444*a466cc55SCy Schubert 	}
445*a466cc55SCy Schubert 
446*a466cc55SCy Schubert 	return (was_idle);
447*a466cc55SCy Schubert }
448*a466cc55SCy Schubert 
449*a466cc55SCy Schubert /*
450*a466cc55SCy Schubert  * Moves a task onto the appropriate run queue.
451*a466cc55SCy Schubert  *
452*a466cc55SCy Schubert  * Caller must NOT hold manager lock.
453*a466cc55SCy Schubert  */
454*a466cc55SCy Schubert static inline void
task_ready(isc__task_t * task)455*a466cc55SCy Schubert task_ready(isc__task_t *task) {
456*a466cc55SCy Schubert 	isc__taskmgr_t *manager = task->manager;
457*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
458*a466cc55SCy Schubert 	isc_boolean_t has_privilege = isc__task_privilege((isc_task_t *) task);
459*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
460*a466cc55SCy Schubert 
461*a466cc55SCy Schubert 	REQUIRE(VALID_MANAGER(manager));
462*a466cc55SCy Schubert 	REQUIRE(task->state == task_state_ready);
463*a466cc55SCy Schubert 
464*a466cc55SCy Schubert 	XTRACE("task_ready");
465*a466cc55SCy Schubert 
466*a466cc55SCy Schubert 	LOCK(&manager->lock);
467*a466cc55SCy Schubert 	push_readyq(manager, task);
468*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
469*a466cc55SCy Schubert 	if (manager->mode == isc_taskmgrmode_normal || has_privilege)
470*a466cc55SCy Schubert 		SIGNAL(&manager->work_available);
471*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
472*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
473*a466cc55SCy Schubert }
474*a466cc55SCy Schubert 
475*a466cc55SCy Schubert static inline isc_boolean_t
task_detach(isc__task_t * task)476*a466cc55SCy Schubert task_detach(isc__task_t *task) {
477*a466cc55SCy Schubert 
478*a466cc55SCy Schubert 	/*
479*a466cc55SCy Schubert 	 * Caller must be holding the task lock.
480*a466cc55SCy Schubert 	 */
481*a466cc55SCy Schubert 
482*a466cc55SCy Schubert 	REQUIRE(task->references > 0);
483*a466cc55SCy Schubert 
484*a466cc55SCy Schubert 	XTRACE("detach");
485*a466cc55SCy Schubert 
486*a466cc55SCy Schubert 	task->references--;
487*a466cc55SCy Schubert 	if (task->references == 0 && task->state == task_state_idle) {
488*a466cc55SCy Schubert 		INSIST(EMPTY(task->events));
489*a466cc55SCy Schubert 		/*
490*a466cc55SCy Schubert 		 * There are no references to this task, and no
491*a466cc55SCy Schubert 		 * pending events.  We could try to optimize and
492*a466cc55SCy Schubert 		 * either initiate shutdown or clean up the task,
493*a466cc55SCy Schubert 		 * depending on its state, but it's easier to just
494*a466cc55SCy Schubert 		 * make the task ready and allow run() or the event
495*a466cc55SCy Schubert 		 * loop to deal with shutting down and termination.
496*a466cc55SCy Schubert 		 */
497*a466cc55SCy Schubert 		task->state = task_state_ready;
498*a466cc55SCy Schubert 		return (ISC_TRUE);
499*a466cc55SCy Schubert 	}
500*a466cc55SCy Schubert 
501*a466cc55SCy Schubert 	return (ISC_FALSE);
502*a466cc55SCy Schubert }
503*a466cc55SCy Schubert 
504*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_detach(isc_task_t ** taskp)505*a466cc55SCy Schubert isc__task_detach(isc_task_t **taskp) {
506*a466cc55SCy Schubert 	isc__task_t *task;
507*a466cc55SCy Schubert 	isc_boolean_t was_idle;
508*a466cc55SCy Schubert 
509*a466cc55SCy Schubert 	/*
510*a466cc55SCy Schubert 	 * Detach *taskp from its task.
511*a466cc55SCy Schubert 	 */
512*a466cc55SCy Schubert 
513*a466cc55SCy Schubert 	REQUIRE(taskp != NULL);
514*a466cc55SCy Schubert 	task = (isc__task_t *)*taskp;
515*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
516*a466cc55SCy Schubert 
517*a466cc55SCy Schubert 	XTRACE("isc_task_detach");
518*a466cc55SCy Schubert 
519*a466cc55SCy Schubert 	LOCK(&task->lock);
520*a466cc55SCy Schubert 	was_idle = task_detach(task);
521*a466cc55SCy Schubert 	UNLOCK(&task->lock);
522*a466cc55SCy Schubert 
523*a466cc55SCy Schubert 	if (was_idle)
524*a466cc55SCy Schubert 		task_ready(task);
525*a466cc55SCy Schubert 
526*a466cc55SCy Schubert 	*taskp = NULL;
527*a466cc55SCy Schubert }
528*a466cc55SCy Schubert 
529*a466cc55SCy Schubert static inline isc_boolean_t
task_send(isc__task_t * task,isc_event_t ** eventp)530*a466cc55SCy Schubert task_send(isc__task_t *task, isc_event_t **eventp) {
531*a466cc55SCy Schubert 	isc_boolean_t was_idle = ISC_FALSE;
532*a466cc55SCy Schubert 	isc_event_t *event;
533*a466cc55SCy Schubert 
534*a466cc55SCy Schubert 	/*
535*a466cc55SCy Schubert 	 * Caller must be holding the task lock.
536*a466cc55SCy Schubert 	 */
537*a466cc55SCy Schubert 
538*a466cc55SCy Schubert 	REQUIRE(eventp != NULL);
539*a466cc55SCy Schubert 	event = *eventp;
540*a466cc55SCy Schubert 	REQUIRE(event != NULL);
541*a466cc55SCy Schubert 	REQUIRE(event->ev_type > 0);
542*a466cc55SCy Schubert 	REQUIRE(task->state != task_state_done);
543*a466cc55SCy Schubert 
544*a466cc55SCy Schubert 	XTRACE("task_send");
545*a466cc55SCy Schubert 
546*a466cc55SCy Schubert 	if (task->state == task_state_idle) {
547*a466cc55SCy Schubert 		was_idle = ISC_TRUE;
548*a466cc55SCy Schubert 		INSIST(EMPTY(task->events));
549*a466cc55SCy Schubert 		task->state = task_state_ready;
550*a466cc55SCy Schubert 	}
551*a466cc55SCy Schubert 	INSIST(task->state == task_state_ready ||
552*a466cc55SCy Schubert 	       task->state == task_state_running);
553*a466cc55SCy Schubert 	ENQUEUE(task->events, event, ev_link);
554*a466cc55SCy Schubert 	*eventp = NULL;
555*a466cc55SCy Schubert 
556*a466cc55SCy Schubert 	return (was_idle);
557*a466cc55SCy Schubert }
558*a466cc55SCy Schubert 
559*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_send(isc_task_t * task0,isc_event_t ** eventp)560*a466cc55SCy Schubert isc__task_send(isc_task_t *task0, isc_event_t **eventp) {
561*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
562*a466cc55SCy Schubert 	isc_boolean_t was_idle;
563*a466cc55SCy Schubert 
564*a466cc55SCy Schubert 	/*
565*a466cc55SCy Schubert 	 * Send '*event' to 'task'.
566*a466cc55SCy Schubert 	 */
567*a466cc55SCy Schubert 
568*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
569*a466cc55SCy Schubert 
570*a466cc55SCy Schubert 	XTRACE("isc_task_send");
571*a466cc55SCy Schubert 
572*a466cc55SCy Schubert 	/*
573*a466cc55SCy Schubert 	 * We're trying hard to hold locks for as short a time as possible.
574*a466cc55SCy Schubert 	 * We're also trying to hold as few locks as possible.  This is why
575*a466cc55SCy Schubert 	 * some processing is deferred until after the lock is released.
576*a466cc55SCy Schubert 	 */
577*a466cc55SCy Schubert 	LOCK(&task->lock);
578*a466cc55SCy Schubert 	was_idle = task_send(task, eventp);
579*a466cc55SCy Schubert 	UNLOCK(&task->lock);
580*a466cc55SCy Schubert 
581*a466cc55SCy Schubert 	if (was_idle) {
582*a466cc55SCy Schubert 		/*
583*a466cc55SCy Schubert 		 * We need to add this task to the ready queue.
584*a466cc55SCy Schubert 		 *
585*a466cc55SCy Schubert 		 * We've waited until now to do it because making a task
586*a466cc55SCy Schubert 		 * ready requires locking the manager.  If we tried to do
587*a466cc55SCy Schubert 		 * this while holding the task lock, we could deadlock.
588*a466cc55SCy Schubert 		 *
589*a466cc55SCy Schubert 		 * We've changed the state to ready, so no one else will
590*a466cc55SCy Schubert 		 * be trying to add this task to the ready queue.  The
591*a466cc55SCy Schubert 		 * only way to leave the ready state is by executing the
592*a466cc55SCy Schubert 		 * task.  It thus doesn't matter if events are added,
593*a466cc55SCy Schubert 		 * removed, or a shutdown is started in the interval
594*a466cc55SCy Schubert 		 * between the time we released the task lock, and the time
595*a466cc55SCy Schubert 		 * we add the task to the ready queue.
596*a466cc55SCy Schubert 		 */
597*a466cc55SCy Schubert 		task_ready(task);
598*a466cc55SCy Schubert 	}
599*a466cc55SCy Schubert }
600*a466cc55SCy Schubert 
601*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_sendanddetach(isc_task_t ** taskp,isc_event_t ** eventp)602*a466cc55SCy Schubert isc__task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
603*a466cc55SCy Schubert 	isc_boolean_t idle1, idle2;
604*a466cc55SCy Schubert 	isc__task_t *task;
605*a466cc55SCy Schubert 
606*a466cc55SCy Schubert 	/*
607*a466cc55SCy Schubert 	 * Send '*event' to '*taskp' and then detach '*taskp' from its
608*a466cc55SCy Schubert 	 * task.
609*a466cc55SCy Schubert 	 */
610*a466cc55SCy Schubert 
611*a466cc55SCy Schubert 	REQUIRE(taskp != NULL);
612*a466cc55SCy Schubert 	task = (isc__task_t *)*taskp;
613*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
614*a466cc55SCy Schubert 
615*a466cc55SCy Schubert 	XTRACE("isc_task_sendanddetach");
616*a466cc55SCy Schubert 
617*a466cc55SCy Schubert 	LOCK(&task->lock);
618*a466cc55SCy Schubert 	idle1 = task_send(task, eventp);
619*a466cc55SCy Schubert 	idle2 = task_detach(task);
620*a466cc55SCy Schubert 	UNLOCK(&task->lock);
621*a466cc55SCy Schubert 
622*a466cc55SCy Schubert 	/*
623*a466cc55SCy Schubert 	 * If idle1, then idle2 shouldn't be true as well since we're holding
624*a466cc55SCy Schubert 	 * the task lock, and thus the task cannot switch from ready back to
625*a466cc55SCy Schubert 	 * idle.
626*a466cc55SCy Schubert 	 */
627*a466cc55SCy Schubert 	INSIST(!(idle1 && idle2));
628*a466cc55SCy Schubert 
629*a466cc55SCy Schubert 	if (idle1 || idle2)
630*a466cc55SCy Schubert 		task_ready(task);
631*a466cc55SCy Schubert 
632*a466cc55SCy Schubert 	*taskp = NULL;
633*a466cc55SCy Schubert }
634*a466cc55SCy Schubert 
635*a466cc55SCy Schubert #define PURGE_OK(event)	(((event)->ev_attributes & ISC_EVENTATTR_NOPURGE) == 0)
636*a466cc55SCy Schubert 
637*a466cc55SCy Schubert static unsigned int
dequeue_events(isc__task_t * task,void * sender,isc_eventtype_t first,isc_eventtype_t last,void * tag,isc_eventlist_t * events,isc_boolean_t purging)638*a466cc55SCy Schubert dequeue_events(isc__task_t *task, void *sender, isc_eventtype_t first,
639*a466cc55SCy Schubert 	       isc_eventtype_t last, void *tag,
640*a466cc55SCy Schubert 	       isc_eventlist_t *events, isc_boolean_t purging)
641*a466cc55SCy Schubert {
642*a466cc55SCy Schubert 	isc_event_t *event, *next_event;
643*a466cc55SCy Schubert 	unsigned int count = 0;
644*a466cc55SCy Schubert 
645*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
646*a466cc55SCy Schubert 	REQUIRE(last >= first);
647*a466cc55SCy Schubert 
648*a466cc55SCy Schubert 	XTRACE("dequeue_events");
649*a466cc55SCy Schubert 
650*a466cc55SCy Schubert 	/*
651*a466cc55SCy Schubert 	 * Events matching 'sender', whose type is >= first and <= last, and
652*a466cc55SCy Schubert 	 * whose tag is 'tag' will be dequeued.  If 'purging', matching events
653*a466cc55SCy Schubert 	 * which are marked as unpurgable will not be dequeued.
654*a466cc55SCy Schubert 	 *
655*a466cc55SCy Schubert 	 * sender == NULL means "any sender", and tag == NULL means "any tag".
656*a466cc55SCy Schubert 	 */
657*a466cc55SCy Schubert 
658*a466cc55SCy Schubert 	LOCK(&task->lock);
659*a466cc55SCy Schubert 
660*a466cc55SCy Schubert 	for (event = HEAD(task->events); event != NULL; event = next_event) {
661*a466cc55SCy Schubert 		next_event = NEXT(event, ev_link);
662*a466cc55SCy Schubert 		if (event->ev_type >= first && event->ev_type <= last &&
663*a466cc55SCy Schubert 		    (sender == NULL || event->ev_sender == sender) &&
664*a466cc55SCy Schubert 		    (tag == NULL || event->ev_tag == tag) &&
665*a466cc55SCy Schubert 		    (!purging || PURGE_OK(event))) {
666*a466cc55SCy Schubert 			DEQUEUE(task->events, event, ev_link);
667*a466cc55SCy Schubert 			ENQUEUE(*events, event, ev_link);
668*a466cc55SCy Schubert 			count++;
669*a466cc55SCy Schubert 		}
670*a466cc55SCy Schubert 	}
671*a466cc55SCy Schubert 
672*a466cc55SCy Schubert 	UNLOCK(&task->lock);
673*a466cc55SCy Schubert 
674*a466cc55SCy Schubert 	return (count);
675*a466cc55SCy Schubert }
676*a466cc55SCy Schubert 
677*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
isc__task_purgerange(isc_task_t * task0,void * sender,isc_eventtype_t first,isc_eventtype_t last,void * tag)678*a466cc55SCy Schubert isc__task_purgerange(isc_task_t *task0, void *sender, isc_eventtype_t first,
679*a466cc55SCy Schubert 		     isc_eventtype_t last, void *tag)
680*a466cc55SCy Schubert {
681*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
682*a466cc55SCy Schubert 	unsigned int count;
683*a466cc55SCy Schubert 	isc_eventlist_t events;
684*a466cc55SCy Schubert 	isc_event_t *event, *next_event;
685*a466cc55SCy Schubert 
686*a466cc55SCy Schubert 	/*
687*a466cc55SCy Schubert 	 * Purge events from a task's event queue.
688*a466cc55SCy Schubert 	 */
689*a466cc55SCy Schubert 
690*a466cc55SCy Schubert 	XTRACE("isc_task_purgerange");
691*a466cc55SCy Schubert 
692*a466cc55SCy Schubert 	ISC_LIST_INIT(events);
693*a466cc55SCy Schubert 
694*a466cc55SCy Schubert 	count = dequeue_events(task, sender, first, last, tag, &events,
695*a466cc55SCy Schubert 			       ISC_TRUE);
696*a466cc55SCy Schubert 
697*a466cc55SCy Schubert 	for (event = HEAD(events); event != NULL; event = next_event) {
698*a466cc55SCy Schubert 		next_event = NEXT(event, ev_link);
699*a466cc55SCy Schubert 		isc_event_free(&event);
700*a466cc55SCy Schubert 	}
701*a466cc55SCy Schubert 
702*a466cc55SCy Schubert 	/*
703*a466cc55SCy Schubert 	 * Note that purging never changes the state of the task.
704*a466cc55SCy Schubert 	 */
705*a466cc55SCy Schubert 
706*a466cc55SCy Schubert 	return (count);
707*a466cc55SCy Schubert }
708*a466cc55SCy Schubert 
709*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
isc__task_purge(isc_task_t * task,void * sender,isc_eventtype_t type,void * tag)710*a466cc55SCy Schubert isc__task_purge(isc_task_t *task, void *sender, isc_eventtype_t type,
711*a466cc55SCy Schubert 		void *tag)
712*a466cc55SCy Schubert {
713*a466cc55SCy Schubert 	/*
714*a466cc55SCy Schubert 	 * Purge events from a task's event queue.
715*a466cc55SCy Schubert 	 */
716*a466cc55SCy Schubert 
717*a466cc55SCy Schubert 	XTRACE("isc_task_purge");
718*a466cc55SCy Schubert 
719*a466cc55SCy Schubert 	return (isc__task_purgerange(task, sender, type, type, tag));
720*a466cc55SCy Schubert }
721*a466cc55SCy Schubert 
722*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_boolean_t
isc__task_purgeevent(isc_task_t * task0,isc_event_t * event)723*a466cc55SCy Schubert isc__task_purgeevent(isc_task_t *task0, isc_event_t *event) {
724*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
725*a466cc55SCy Schubert 	isc_event_t *curr_event, *next_event;
726*a466cc55SCy Schubert 
727*a466cc55SCy Schubert 	/*
728*a466cc55SCy Schubert 	 * Purge 'event' from a task's event queue.
729*a466cc55SCy Schubert 	 *
730*a466cc55SCy Schubert 	 * XXXRTH:  WARNING:  This method may be removed before beta.
731*a466cc55SCy Schubert 	 */
732*a466cc55SCy Schubert 
733*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
734*a466cc55SCy Schubert 
735*a466cc55SCy Schubert 	/*
736*a466cc55SCy Schubert 	 * If 'event' is on the task's event queue, it will be purged,
737*a466cc55SCy Schubert 	 * unless it is marked as unpurgeable.  'event' does not have to be
738*a466cc55SCy Schubert 	 * on the task's event queue; in fact, it can even be an invalid
739*a466cc55SCy Schubert 	 * pointer.  Purging only occurs if the event is actually on the task's
740*a466cc55SCy Schubert 	 * event queue.
741*a466cc55SCy Schubert 	 *
742*a466cc55SCy Schubert 	 * Purging never changes the state of the task.
743*a466cc55SCy Schubert 	 */
744*a466cc55SCy Schubert 
745*a466cc55SCy Schubert 	LOCK(&task->lock);
746*a466cc55SCy Schubert 	for (curr_event = HEAD(task->events);
747*a466cc55SCy Schubert 	     curr_event != NULL;
748*a466cc55SCy Schubert 	     curr_event = next_event) {
749*a466cc55SCy Schubert 		next_event = NEXT(curr_event, ev_link);
750*a466cc55SCy Schubert 		if (curr_event == event && PURGE_OK(event)) {
751*a466cc55SCy Schubert 			DEQUEUE(task->events, curr_event, ev_link);
752*a466cc55SCy Schubert 			break;
753*a466cc55SCy Schubert 		}
754*a466cc55SCy Schubert 	}
755*a466cc55SCy Schubert 	UNLOCK(&task->lock);
756*a466cc55SCy Schubert 
757*a466cc55SCy Schubert 	if (curr_event == NULL)
758*a466cc55SCy Schubert 		return (ISC_FALSE);
759*a466cc55SCy Schubert 
760*a466cc55SCy Schubert 	isc_event_free(&curr_event);
761*a466cc55SCy Schubert 
762*a466cc55SCy Schubert 	return (ISC_TRUE);
763*a466cc55SCy Schubert }
764*a466cc55SCy Schubert 
765*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
isc__task_unsendrange(isc_task_t * task,void * sender,isc_eventtype_t first,isc_eventtype_t last,void * tag,isc_eventlist_t * events)766*a466cc55SCy Schubert isc__task_unsendrange(isc_task_t *task, void *sender, isc_eventtype_t first,
767*a466cc55SCy Schubert 		      isc_eventtype_t last, void *tag,
768*a466cc55SCy Schubert 		      isc_eventlist_t *events)
769*a466cc55SCy Schubert {
770*a466cc55SCy Schubert 	/*
771*a466cc55SCy Schubert 	 * Remove events from a task's event queue.
772*a466cc55SCy Schubert 	 */
773*a466cc55SCy Schubert 
774*a466cc55SCy Schubert 	XTRACE("isc_task_unsendrange");
775*a466cc55SCy Schubert 
776*a466cc55SCy Schubert 	return (dequeue_events((isc__task_t *)task, sender, first,
777*a466cc55SCy Schubert 			       last, tag, events, ISC_FALSE));
778*a466cc55SCy Schubert }
779*a466cc55SCy Schubert 
780*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE unsigned int
isc__task_unsend(isc_task_t * task,void * sender,isc_eventtype_t type,void * tag,isc_eventlist_t * events)781*a466cc55SCy Schubert isc__task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
782*a466cc55SCy Schubert 		 void *tag, isc_eventlist_t *events)
783*a466cc55SCy Schubert {
784*a466cc55SCy Schubert 	/*
785*a466cc55SCy Schubert 	 * Remove events from a task's event queue.
786*a466cc55SCy Schubert 	 */
787*a466cc55SCy Schubert 
788*a466cc55SCy Schubert 	XTRACE("isc_task_unsend");
789*a466cc55SCy Schubert 
790*a466cc55SCy Schubert 	return (dequeue_events((isc__task_t *)task, sender, type,
791*a466cc55SCy Schubert 			       type, tag, events, ISC_FALSE));
792*a466cc55SCy Schubert }
793*a466cc55SCy Schubert 
794*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
isc__task_onshutdown(isc_task_t * task0,isc_taskaction_t action,const void * arg)795*a466cc55SCy Schubert isc__task_onshutdown(isc_task_t *task0, isc_taskaction_t action,
796*a466cc55SCy Schubert 		     const void *arg)
797*a466cc55SCy Schubert {
798*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
799*a466cc55SCy Schubert 	isc_boolean_t disallowed = ISC_FALSE;
800*a466cc55SCy Schubert 	isc_result_t result = ISC_R_SUCCESS;
801*a466cc55SCy Schubert 	isc_event_t *event;
802*a466cc55SCy Schubert 
803*a466cc55SCy Schubert 	/*
804*a466cc55SCy Schubert 	 * Send a shutdown event with action 'action' and argument 'arg' when
805*a466cc55SCy Schubert 	 * 'task' is shutdown.
806*a466cc55SCy Schubert 	 */
807*a466cc55SCy Schubert 
808*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
809*a466cc55SCy Schubert 	REQUIRE(action != NULL);
810*a466cc55SCy Schubert 
811*a466cc55SCy Schubert 	event = isc_event_allocate(task->manager->mctx,
812*a466cc55SCy Schubert 				   NULL,
813*a466cc55SCy Schubert 				   ISC_TASKEVENT_SHUTDOWN,
814*a466cc55SCy Schubert 				   action,
815*a466cc55SCy Schubert 				   arg,
816*a466cc55SCy Schubert 				   sizeof(*event));
817*a466cc55SCy Schubert 	if (event == NULL)
818*a466cc55SCy Schubert 		return (ISC_R_NOMEMORY);
819*a466cc55SCy Schubert 
820*a466cc55SCy Schubert 	LOCK(&task->lock);
821*a466cc55SCy Schubert 	if (TASK_SHUTTINGDOWN(task)) {
822*a466cc55SCy Schubert 		disallowed = ISC_TRUE;
823*a466cc55SCy Schubert 		result = ISC_R_SHUTTINGDOWN;
824*a466cc55SCy Schubert 	} else
825*a466cc55SCy Schubert 		ENQUEUE(task->on_shutdown, event, ev_link);
826*a466cc55SCy Schubert 	UNLOCK(&task->lock);
827*a466cc55SCy Schubert 
828*a466cc55SCy Schubert 	if (disallowed)
829*a466cc55SCy Schubert 		isc_mem_put(task->manager->mctx, event, sizeof(*event));
830*a466cc55SCy Schubert 
831*a466cc55SCy Schubert 	return (result);
832*a466cc55SCy Schubert }
833*a466cc55SCy Schubert 
834*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_shutdown(isc_task_t * task0)835*a466cc55SCy Schubert isc__task_shutdown(isc_task_t *task0) {
836*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
837*a466cc55SCy Schubert 	isc_boolean_t was_idle;
838*a466cc55SCy Schubert 
839*a466cc55SCy Schubert 	/*
840*a466cc55SCy Schubert 	 * Shutdown 'task'.
841*a466cc55SCy Schubert 	 */
842*a466cc55SCy Schubert 
843*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
844*a466cc55SCy Schubert 
845*a466cc55SCy Schubert 	LOCK(&task->lock);
846*a466cc55SCy Schubert 	was_idle = task_shutdown(task);
847*a466cc55SCy Schubert 	UNLOCK(&task->lock);
848*a466cc55SCy Schubert 
849*a466cc55SCy Schubert 	if (was_idle)
850*a466cc55SCy Schubert 		task_ready(task);
851*a466cc55SCy Schubert }
852*a466cc55SCy Schubert 
853*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_destroy(isc_task_t ** taskp)854*a466cc55SCy Schubert isc__task_destroy(isc_task_t **taskp) {
855*a466cc55SCy Schubert 
856*a466cc55SCy Schubert 	/*
857*a466cc55SCy Schubert 	 * Destroy '*taskp'.
858*a466cc55SCy Schubert 	 */
859*a466cc55SCy Schubert 
860*a466cc55SCy Schubert 	REQUIRE(taskp != NULL);
861*a466cc55SCy Schubert 
862*a466cc55SCy Schubert 	isc_task_shutdown(*taskp);
863*a466cc55SCy Schubert 	isc_task_detach(taskp);
864*a466cc55SCy Schubert }
865*a466cc55SCy Schubert 
866*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_setname(isc_task_t * task0,const char * name,void * tag)867*a466cc55SCy Schubert isc__task_setname(isc_task_t *task0, const char *name, void *tag) {
868*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
869*a466cc55SCy Schubert 
870*a466cc55SCy Schubert 	/*
871*a466cc55SCy Schubert 	 * Name 'task'.
872*a466cc55SCy Schubert 	 */
873*a466cc55SCy Schubert 
874*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
875*a466cc55SCy Schubert 
876*a466cc55SCy Schubert 	LOCK(&task->lock);
877*a466cc55SCy Schubert 	memset(task->name, 0, sizeof(task->name));
878*a466cc55SCy Schubert 	strncpy(task->name, name, sizeof(task->name) - 1);
879*a466cc55SCy Schubert 	task->tag = tag;
880*a466cc55SCy Schubert 	UNLOCK(&task->lock);
881*a466cc55SCy Schubert }
882*a466cc55SCy Schubert 
883*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE const char *
isc__task_getname(isc_task_t * task0)884*a466cc55SCy Schubert isc__task_getname(isc_task_t *task0) {
885*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
886*a466cc55SCy Schubert 
887*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
888*a466cc55SCy Schubert 
889*a466cc55SCy Schubert 	return (task->name);
890*a466cc55SCy Schubert }
891*a466cc55SCy Schubert 
892*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void *
isc__task_gettag(isc_task_t * task0)893*a466cc55SCy Schubert isc__task_gettag(isc_task_t *task0) {
894*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
895*a466cc55SCy Schubert 
896*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
897*a466cc55SCy Schubert 
898*a466cc55SCy Schubert 	return (task->tag);
899*a466cc55SCy Schubert }
900*a466cc55SCy Schubert 
901*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_getcurrenttime(isc_task_t * task0,isc_stdtime_t * t)902*a466cc55SCy Schubert isc__task_getcurrenttime(isc_task_t *task0, isc_stdtime_t *t) {
903*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
904*a466cc55SCy Schubert 
905*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
906*a466cc55SCy Schubert 	REQUIRE(t != NULL);
907*a466cc55SCy Schubert 
908*a466cc55SCy Schubert 	LOCK(&task->lock);
909*a466cc55SCy Schubert 	*t = task->now;
910*a466cc55SCy Schubert 	UNLOCK(&task->lock);
911*a466cc55SCy Schubert }
912*a466cc55SCy Schubert 
913*a466cc55SCy Schubert /***
914*a466cc55SCy Schubert  *** Task Manager.
915*a466cc55SCy Schubert  ***/
916*a466cc55SCy Schubert 
917*a466cc55SCy Schubert /*
918*a466cc55SCy Schubert  * Return ISC_TRUE if the current ready list for the manager, which is
919*a466cc55SCy Schubert  * either ready_tasks or the ready_priority_tasks, depending on whether
920*a466cc55SCy Schubert  * the manager is currently in normal or privileged execution mode.
921*a466cc55SCy Schubert  *
922*a466cc55SCy Schubert  * Caller must hold the task manager lock.
923*a466cc55SCy Schubert  */
924*a466cc55SCy Schubert static inline isc_boolean_t
empty_readyq(isc__taskmgr_t * manager)925*a466cc55SCy Schubert empty_readyq(isc__taskmgr_t *manager) {
926*a466cc55SCy Schubert 	isc__tasklist_t queue;
927*a466cc55SCy Schubert 
928*a466cc55SCy Schubert 	if (manager->mode == isc_taskmgrmode_normal)
929*a466cc55SCy Schubert 		queue = manager->ready_tasks;
930*a466cc55SCy Schubert 	else
931*a466cc55SCy Schubert 		queue = manager->ready_priority_tasks;
932*a466cc55SCy Schubert 
933*a466cc55SCy Schubert 	return (ISC_TF(EMPTY(queue)));
934*a466cc55SCy Schubert }
935*a466cc55SCy Schubert 
936*a466cc55SCy Schubert /*
937*a466cc55SCy Schubert  * Dequeue and return a pointer to the first task on the current ready
938*a466cc55SCy Schubert  * list for the manager.
939*a466cc55SCy Schubert  * If the task is privileged, dequeue it from the other ready list
940*a466cc55SCy Schubert  * as well.
941*a466cc55SCy Schubert  *
942*a466cc55SCy Schubert  * Caller must hold the task manager lock.
943*a466cc55SCy Schubert  */
944*a466cc55SCy Schubert static inline isc__task_t *
pop_readyq(isc__taskmgr_t * manager)945*a466cc55SCy Schubert pop_readyq(isc__taskmgr_t *manager) {
946*a466cc55SCy Schubert 	isc__task_t *task;
947*a466cc55SCy Schubert 
948*a466cc55SCy Schubert 	if (manager->mode == isc_taskmgrmode_normal)
949*a466cc55SCy Schubert 		task = HEAD(manager->ready_tasks);
950*a466cc55SCy Schubert 	else
951*a466cc55SCy Schubert 		task = HEAD(manager->ready_priority_tasks);
952*a466cc55SCy Schubert 
953*a466cc55SCy Schubert 	if (task != NULL) {
954*a466cc55SCy Schubert 		DEQUEUE(manager->ready_tasks, task, ready_link);
955*a466cc55SCy Schubert 		if (ISC_LINK_LINKED(task, ready_priority_link))
956*a466cc55SCy Schubert 			DEQUEUE(manager->ready_priority_tasks, task,
957*a466cc55SCy Schubert 				ready_priority_link);
958*a466cc55SCy Schubert 	}
959*a466cc55SCy Schubert 
960*a466cc55SCy Schubert 	return (task);
961*a466cc55SCy Schubert }
962*a466cc55SCy Schubert 
963*a466cc55SCy Schubert /*
964*a466cc55SCy Schubert  * Push 'task' onto the ready_tasks queue.  If 'task' has the privilege
965*a466cc55SCy Schubert  * flag set, then also push it onto the ready_priority_tasks queue.
966*a466cc55SCy Schubert  *
967*a466cc55SCy Schubert  * Caller must hold the task manager lock.
968*a466cc55SCy Schubert  */
969*a466cc55SCy Schubert static inline void
push_readyq(isc__taskmgr_t * manager,isc__task_t * task)970*a466cc55SCy Schubert push_readyq(isc__taskmgr_t *manager, isc__task_t *task) {
971*a466cc55SCy Schubert 	ENQUEUE(manager->ready_tasks, task, ready_link);
972*a466cc55SCy Schubert 	if ((task->flags & TASK_F_PRIVILEGED) != 0)
973*a466cc55SCy Schubert 		ENQUEUE(manager->ready_priority_tasks, task,
974*a466cc55SCy Schubert 			ready_priority_link);
975*a466cc55SCy Schubert }
976*a466cc55SCy Schubert 
977*a466cc55SCy Schubert static void
dispatch(isc__taskmgr_t * manager)978*a466cc55SCy Schubert dispatch(isc__taskmgr_t *manager) {
979*a466cc55SCy Schubert 	isc__task_t *task;
980*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
981*a466cc55SCy Schubert 	unsigned int total_dispatch_count = 0;
982*a466cc55SCy Schubert 	isc__tasklist_t new_ready_tasks;
983*a466cc55SCy Schubert 	isc__tasklist_t new_priority_tasks;
984*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
985*a466cc55SCy Schubert 
986*a466cc55SCy Schubert 	REQUIRE(VALID_MANAGER(manager));
987*a466cc55SCy Schubert 
988*a466cc55SCy Schubert 	/*
989*a466cc55SCy Schubert 	 * Again we're trying to hold the lock for as short a time as possible
990*a466cc55SCy Schubert 	 * and to do as little locking and unlocking as possible.
991*a466cc55SCy Schubert 	 *
992*a466cc55SCy Schubert 	 * In both while loops, the appropriate lock must be held before the
993*a466cc55SCy Schubert 	 * while body starts.  Code which acquired the lock at the top of
994*a466cc55SCy Schubert 	 * the loop would be more readable, but would result in a lot of
995*a466cc55SCy Schubert 	 * extra locking.  Compare:
996*a466cc55SCy Schubert 	 *
997*a466cc55SCy Schubert 	 * Straightforward:
998*a466cc55SCy Schubert 	 *
999*a466cc55SCy Schubert 	 *	LOCK();
1000*a466cc55SCy Schubert 	 *	...
1001*a466cc55SCy Schubert 	 *	UNLOCK();
1002*a466cc55SCy Schubert 	 *	while (expression) {
1003*a466cc55SCy Schubert 	 *		LOCK();
1004*a466cc55SCy Schubert 	 *		...
1005*a466cc55SCy Schubert 	 *		UNLOCK();
1006*a466cc55SCy Schubert 	 *
1007*a466cc55SCy Schubert 	 *	       	Unlocked part here...
1008*a466cc55SCy Schubert 	 *
1009*a466cc55SCy Schubert 	 *		LOCK();
1010*a466cc55SCy Schubert 	 *		...
1011*a466cc55SCy Schubert 	 *		UNLOCK();
1012*a466cc55SCy Schubert 	 *	}
1013*a466cc55SCy Schubert 	 *
1014*a466cc55SCy Schubert 	 * Note how if the loop continues we unlock and then immediately lock.
1015*a466cc55SCy Schubert 	 * For N iterations of the loop, this code does 2N+1 locks and 2N+1
1016*a466cc55SCy Schubert 	 * unlocks.  Also note that the lock is not held when the while
1017*a466cc55SCy Schubert 	 * condition is tested, which may or may not be important, depending
1018*a466cc55SCy Schubert 	 * on the expression.
1019*a466cc55SCy Schubert 	 *
1020*a466cc55SCy Schubert 	 * As written:
1021*a466cc55SCy Schubert 	 *
1022*a466cc55SCy Schubert 	 *	LOCK();
1023*a466cc55SCy Schubert 	 *	while (expression) {
1024*a466cc55SCy Schubert 	 *		...
1025*a466cc55SCy Schubert 	 *		UNLOCK();
1026*a466cc55SCy Schubert 	 *
1027*a466cc55SCy Schubert 	 *	       	Unlocked part here...
1028*a466cc55SCy Schubert 	 *
1029*a466cc55SCy Schubert 	 *		LOCK();
1030*a466cc55SCy Schubert 	 *		...
1031*a466cc55SCy Schubert 	 *	}
1032*a466cc55SCy Schubert 	 *	UNLOCK();
1033*a466cc55SCy Schubert 	 *
1034*a466cc55SCy Schubert 	 * For N iterations of the loop, this code does N+1 locks and N+1
1035*a466cc55SCy Schubert 	 * unlocks.  The while expression is always protected by the lock.
1036*a466cc55SCy Schubert 	 */
1037*a466cc55SCy Schubert 
1038*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1039*a466cc55SCy Schubert 	ISC_LIST_INIT(new_ready_tasks);
1040*a466cc55SCy Schubert 	ISC_LIST_INIT(new_priority_tasks);
1041*a466cc55SCy Schubert #endif
1042*a466cc55SCy Schubert 	LOCK(&manager->lock);
1043*a466cc55SCy Schubert 
1044*a466cc55SCy Schubert 	while (!FINISHED(manager)) {
1045*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1046*a466cc55SCy Schubert 		/*
1047*a466cc55SCy Schubert 		 * For reasons similar to those given in the comment in
1048*a466cc55SCy Schubert 		 * isc_task_send() above, it is safe for us to dequeue
1049*a466cc55SCy Schubert 		 * the task while only holding the manager lock, and then
1050*a466cc55SCy Schubert 		 * change the task to running state while only holding the
1051*a466cc55SCy Schubert 		 * task lock.
1052*a466cc55SCy Schubert 		 *
1053*a466cc55SCy Schubert 		 * If a pause has been requested, don't do any work
1054*a466cc55SCy Schubert 		 * until it's been released.
1055*a466cc55SCy Schubert 		 */
1056*a466cc55SCy Schubert 		while ((empty_readyq(manager) || manager->pause_requested ||
1057*a466cc55SCy Schubert 			manager->exclusive_requested) && !FINISHED(manager))
1058*a466cc55SCy Schubert 		{
1059*a466cc55SCy Schubert 			XTHREADTRACE(isc_msgcat_get(isc_msgcat,
1060*a466cc55SCy Schubert 						    ISC_MSGSET_GENERAL,
1061*a466cc55SCy Schubert 						    ISC_MSG_WAIT, "wait"));
1062*a466cc55SCy Schubert 			WAIT(&manager->work_available, &manager->lock);
1063*a466cc55SCy Schubert 			XTHREADTRACE(isc_msgcat_get(isc_msgcat,
1064*a466cc55SCy Schubert 						    ISC_MSGSET_TASK,
1065*a466cc55SCy Schubert 						    ISC_MSG_AWAKE, "awake"));
1066*a466cc55SCy Schubert 		}
1067*a466cc55SCy Schubert #else /* USE_WORKER_THREADS */
1068*a466cc55SCy Schubert 		if (total_dispatch_count >= DEFAULT_TASKMGR_QUANTUM ||
1069*a466cc55SCy Schubert 		    empty_readyq(manager))
1070*a466cc55SCy Schubert 			break;
1071*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1072*a466cc55SCy Schubert 		XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TASK,
1073*a466cc55SCy Schubert 					    ISC_MSG_WORKING, "working"));
1074*a466cc55SCy Schubert 
1075*a466cc55SCy Schubert 		task = pop_readyq(manager);
1076*a466cc55SCy Schubert 		if (task != NULL) {
1077*a466cc55SCy Schubert 			unsigned int dispatch_count = 0;
1078*a466cc55SCy Schubert 			isc_boolean_t done = ISC_FALSE;
1079*a466cc55SCy Schubert 			isc_boolean_t requeue = ISC_FALSE;
1080*a466cc55SCy Schubert 			isc_boolean_t finished = ISC_FALSE;
1081*a466cc55SCy Schubert 			isc_event_t *event;
1082*a466cc55SCy Schubert 
1083*a466cc55SCy Schubert 			INSIST(VALID_TASK(task));
1084*a466cc55SCy Schubert 
1085*a466cc55SCy Schubert 			/*
1086*a466cc55SCy Schubert 			 * Note we only unlock the manager lock if we actually
1087*a466cc55SCy Schubert 			 * have a task to do.  We must reacquire the manager
1088*a466cc55SCy Schubert 			 * lock before exiting the 'if (task != NULL)' block.
1089*a466cc55SCy Schubert 			 */
1090*a466cc55SCy Schubert 			manager->tasks_running++;
1091*a466cc55SCy Schubert 			UNLOCK(&manager->lock);
1092*a466cc55SCy Schubert 
1093*a466cc55SCy Schubert 			LOCK(&task->lock);
1094*a466cc55SCy Schubert 			INSIST(task->state == task_state_ready);
1095*a466cc55SCy Schubert 			task->state = task_state_running;
1096*a466cc55SCy Schubert 			XTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1097*a466cc55SCy Schubert 					      ISC_MSG_RUNNING, "running"));
1098*a466cc55SCy Schubert 			isc_stdtime_get(&task->now);
1099*a466cc55SCy Schubert 			do {
1100*a466cc55SCy Schubert 				if (!EMPTY(task->events)) {
1101*a466cc55SCy Schubert 					event = HEAD(task->events);
1102*a466cc55SCy Schubert 					DEQUEUE(task->events, event, ev_link);
1103*a466cc55SCy Schubert 
1104*a466cc55SCy Schubert 					/*
1105*a466cc55SCy Schubert 					 * Execute the event action.
1106*a466cc55SCy Schubert 					 */
1107*a466cc55SCy Schubert 					XTRACE(isc_msgcat_get(isc_msgcat,
1108*a466cc55SCy Schubert 							    ISC_MSGSET_TASK,
1109*a466cc55SCy Schubert 							    ISC_MSG_EXECUTE,
1110*a466cc55SCy Schubert 							    "execute action"));
1111*a466cc55SCy Schubert 					if (event->ev_action != NULL) {
1112*a466cc55SCy Schubert 						UNLOCK(&task->lock);
1113*a466cc55SCy Schubert 						(event->ev_action)(
1114*a466cc55SCy Schubert 							(isc_task_t *)task,
1115*a466cc55SCy Schubert 							event);
1116*a466cc55SCy Schubert 						LOCK(&task->lock);
1117*a466cc55SCy Schubert 					}
1118*a466cc55SCy Schubert 					dispatch_count++;
1119*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1120*a466cc55SCy Schubert 					total_dispatch_count++;
1121*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1122*a466cc55SCy Schubert 				}
1123*a466cc55SCy Schubert 
1124*a466cc55SCy Schubert 				if (task->references == 0 &&
1125*a466cc55SCy Schubert 				    EMPTY(task->events) &&
1126*a466cc55SCy Schubert 				    !TASK_SHUTTINGDOWN(task)) {
1127*a466cc55SCy Schubert 					isc_boolean_t was_idle;
1128*a466cc55SCy Schubert 
1129*a466cc55SCy Schubert 					/*
1130*a466cc55SCy Schubert 					 * There are no references and no
1131*a466cc55SCy Schubert 					 * pending events for this task,
1132*a466cc55SCy Schubert 					 * which means it will not become
1133*a466cc55SCy Schubert 					 * runnable again via an external
1134*a466cc55SCy Schubert 					 * action (such as sending an event
1135*a466cc55SCy Schubert 					 * or detaching).
1136*a466cc55SCy Schubert 					 *
1137*a466cc55SCy Schubert 					 * We initiate shutdown to prevent
1138*a466cc55SCy Schubert 					 * it from becoming a zombie.
1139*a466cc55SCy Schubert 					 *
1140*a466cc55SCy Schubert 					 * We do this here instead of in
1141*a466cc55SCy Schubert 					 * the "if EMPTY(task->events)" block
1142*a466cc55SCy Schubert 					 * below because:
1143*a466cc55SCy Schubert 					 *
1144*a466cc55SCy Schubert 					 *	If we post no shutdown events,
1145*a466cc55SCy Schubert 					 *	we want the task to finish.
1146*a466cc55SCy Schubert 					 *
1147*a466cc55SCy Schubert 					 *	If we did post shutdown events,
1148*a466cc55SCy Schubert 					 *	will still want the task's
1149*a466cc55SCy Schubert 					 *	quantum to be applied.
1150*a466cc55SCy Schubert 					 */
1151*a466cc55SCy Schubert 					was_idle = task_shutdown(task);
1152*a466cc55SCy Schubert 					INSIST(!was_idle);
1153*a466cc55SCy Schubert 				}
1154*a466cc55SCy Schubert 
1155*a466cc55SCy Schubert 				if (EMPTY(task->events)) {
1156*a466cc55SCy Schubert 					/*
1157*a466cc55SCy Schubert 					 * Nothing else to do for this task
1158*a466cc55SCy Schubert 					 * right now.
1159*a466cc55SCy Schubert 					 */
1160*a466cc55SCy Schubert 					XTRACE(isc_msgcat_get(isc_msgcat,
1161*a466cc55SCy Schubert 							      ISC_MSGSET_TASK,
1162*a466cc55SCy Schubert 							      ISC_MSG_EMPTY,
1163*a466cc55SCy Schubert 							      "empty"));
1164*a466cc55SCy Schubert 					if (task->references == 0 &&
1165*a466cc55SCy Schubert 					    TASK_SHUTTINGDOWN(task)) {
1166*a466cc55SCy Schubert 						/*
1167*a466cc55SCy Schubert 						 * The task is done.
1168*a466cc55SCy Schubert 						 */
1169*a466cc55SCy Schubert 						XTRACE(isc_msgcat_get(
1170*a466cc55SCy Schubert 							       isc_msgcat,
1171*a466cc55SCy Schubert 							       ISC_MSGSET_TASK,
1172*a466cc55SCy Schubert 							       ISC_MSG_DONE,
1173*a466cc55SCy Schubert 							       "done"));
1174*a466cc55SCy Schubert 						finished = ISC_TRUE;
1175*a466cc55SCy Schubert 						task->state = task_state_done;
1176*a466cc55SCy Schubert 					} else
1177*a466cc55SCy Schubert 						task->state = task_state_idle;
1178*a466cc55SCy Schubert 					done = ISC_TRUE;
1179*a466cc55SCy Schubert 				} else if (dispatch_count >= task->quantum) {
1180*a466cc55SCy Schubert 					/*
1181*a466cc55SCy Schubert 					 * Our quantum has expired, but
1182*a466cc55SCy Schubert 					 * there is more work to be done.
1183*a466cc55SCy Schubert 					 * We'll requeue it to the ready
1184*a466cc55SCy Schubert 					 * queue later.
1185*a466cc55SCy Schubert 					 *
1186*a466cc55SCy Schubert 					 * We don't check quantum until
1187*a466cc55SCy Schubert 					 * dispatching at least one event,
1188*a466cc55SCy Schubert 					 * so the minimum quantum is one.
1189*a466cc55SCy Schubert 					 */
1190*a466cc55SCy Schubert 					XTRACE(isc_msgcat_get(isc_msgcat,
1191*a466cc55SCy Schubert 							      ISC_MSGSET_TASK,
1192*a466cc55SCy Schubert 							      ISC_MSG_QUANTUM,
1193*a466cc55SCy Schubert 							      "quantum"));
1194*a466cc55SCy Schubert 					task->state = task_state_ready;
1195*a466cc55SCy Schubert 					requeue = ISC_TRUE;
1196*a466cc55SCy Schubert 					done = ISC_TRUE;
1197*a466cc55SCy Schubert 				}
1198*a466cc55SCy Schubert 			} while (!done);
1199*a466cc55SCy Schubert 			UNLOCK(&task->lock);
1200*a466cc55SCy Schubert 
1201*a466cc55SCy Schubert 			if (finished)
1202*a466cc55SCy Schubert 				task_finished(task);
1203*a466cc55SCy Schubert 
1204*a466cc55SCy Schubert 			LOCK(&manager->lock);
1205*a466cc55SCy Schubert 			manager->tasks_running--;
1206*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1207*a466cc55SCy Schubert 			if (manager->exclusive_requested &&
1208*a466cc55SCy Schubert 			    manager->tasks_running == 1) {
1209*a466cc55SCy Schubert 				SIGNAL(&manager->exclusive_granted);
1210*a466cc55SCy Schubert 			} else if (manager->pause_requested &&
1211*a466cc55SCy Schubert 				   manager->tasks_running == 0) {
1212*a466cc55SCy Schubert 				SIGNAL(&manager->paused);
1213*a466cc55SCy Schubert 			}
1214*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1215*a466cc55SCy Schubert 			if (requeue) {
1216*a466cc55SCy Schubert 				/*
1217*a466cc55SCy Schubert 				 * We know we're awake, so we don't have
1218*a466cc55SCy Schubert 				 * to wakeup any sleeping threads if the
1219*a466cc55SCy Schubert 				 * ready queue is empty before we requeue.
1220*a466cc55SCy Schubert 				 *
1221*a466cc55SCy Schubert 				 * A possible optimization if the queue is
1222*a466cc55SCy Schubert 				 * empty is to 'goto' the 'if (task != NULL)'
1223*a466cc55SCy Schubert 				 * block, avoiding the ENQUEUE of the task
1224*a466cc55SCy Schubert 				 * and the subsequent immediate DEQUEUE
1225*a466cc55SCy Schubert 				 * (since it is the only executable task).
1226*a466cc55SCy Schubert 				 * We don't do this because then we'd be
1227*a466cc55SCy Schubert 				 * skipping the exit_requested check.  The
1228*a466cc55SCy Schubert 				 * cost of ENQUEUE is low anyway, especially
1229*a466cc55SCy Schubert 				 * when you consider that we'd have to do
1230*a466cc55SCy Schubert 				 * an extra EMPTY check to see if we could
1231*a466cc55SCy Schubert 				 * do the optimization.  If the ready queue
1232*a466cc55SCy Schubert 				 * were usually nonempty, the 'optimization'
1233*a466cc55SCy Schubert 				 * might even hurt rather than help.
1234*a466cc55SCy Schubert 				 */
1235*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1236*a466cc55SCy Schubert 				push_readyq(manager, task);
1237*a466cc55SCy Schubert #else
1238*a466cc55SCy Schubert 				ENQUEUE(new_ready_tasks, task, ready_link);
1239*a466cc55SCy Schubert 				if ((task->flags & TASK_F_PRIVILEGED) != 0)
1240*a466cc55SCy Schubert 					ENQUEUE(new_priority_tasks, task,
1241*a466cc55SCy Schubert 						ready_priority_link);
1242*a466cc55SCy Schubert #endif
1243*a466cc55SCy Schubert 			}
1244*a466cc55SCy Schubert 		}
1245*a466cc55SCy Schubert 
1246*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1247*a466cc55SCy Schubert 		/*
1248*a466cc55SCy Schubert 		 * If we are in privileged execution mode and there are no
1249*a466cc55SCy Schubert 		 * tasks remaining on the current ready queue, then
1250*a466cc55SCy Schubert 		 * we're stuck.  Automatically drop privileges at that
1251*a466cc55SCy Schubert 		 * point and continue with the regular ready queue.
1252*a466cc55SCy Schubert 		 */
1253*a466cc55SCy Schubert 		if (manager->tasks_running == 0 && empty_readyq(manager)) {
1254*a466cc55SCy Schubert 			manager->mode = isc_taskmgrmode_normal;
1255*a466cc55SCy Schubert 			if (!empty_readyq(manager))
1256*a466cc55SCy Schubert 				BROADCAST(&manager->work_available);
1257*a466cc55SCy Schubert 		}
1258*a466cc55SCy Schubert #endif
1259*a466cc55SCy Schubert 	}
1260*a466cc55SCy Schubert 
1261*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1262*a466cc55SCy Schubert 	ISC_LIST_APPENDLIST(manager->ready_tasks, new_ready_tasks, ready_link);
1263*a466cc55SCy Schubert 	ISC_LIST_APPENDLIST(manager->ready_priority_tasks, new_priority_tasks,
1264*a466cc55SCy Schubert 			    ready_priority_link);
1265*a466cc55SCy Schubert 	if (empty_readyq(manager))
1266*a466cc55SCy Schubert 		manager->mode = isc_taskmgrmode_normal;
1267*a466cc55SCy Schubert #endif
1268*a466cc55SCy Schubert 
1269*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1270*a466cc55SCy Schubert }
1271*a466cc55SCy Schubert 
1272*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1273*a466cc55SCy Schubert static isc_threadresult_t
1274*a466cc55SCy Schubert #ifdef _WIN32
1275*a466cc55SCy Schubert WINAPI
1276*a466cc55SCy Schubert #endif
run(void * uap)1277*a466cc55SCy Schubert run(void *uap) {
1278*a466cc55SCy Schubert 	isc__taskmgr_t *manager = uap;
1279*a466cc55SCy Schubert 
1280*a466cc55SCy Schubert 	XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1281*a466cc55SCy Schubert 				    ISC_MSG_STARTING, "starting"));
1282*a466cc55SCy Schubert 
1283*a466cc55SCy Schubert 	dispatch(manager);
1284*a466cc55SCy Schubert 
1285*a466cc55SCy Schubert 	XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1286*a466cc55SCy Schubert 				    ISC_MSG_EXITING, "exiting"));
1287*a466cc55SCy Schubert 
1288*a466cc55SCy Schubert #ifdef OPENSSL_LEAKS
1289*a466cc55SCy Schubert 	ERR_remove_state(0);
1290*a466cc55SCy Schubert #endif
1291*a466cc55SCy Schubert 
1292*a466cc55SCy Schubert 	return ((isc_threadresult_t)0);
1293*a466cc55SCy Schubert }
1294*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1295*a466cc55SCy Schubert 
1296*a466cc55SCy Schubert static void
manager_free(isc__taskmgr_t * manager)1297*a466cc55SCy Schubert manager_free(isc__taskmgr_t *manager) {
1298*a466cc55SCy Schubert 	isc_mem_t *mctx;
1299*a466cc55SCy Schubert 
1300*a466cc55SCy Schubert 	LOCK(&manager->lock);
1301*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1302*a466cc55SCy Schubert 	(void)isc_condition_destroy(&manager->exclusive_granted);
1303*a466cc55SCy Schubert 	(void)isc_condition_destroy(&manager->work_available);
1304*a466cc55SCy Schubert 	(void)isc_condition_destroy(&manager->paused);
1305*a466cc55SCy Schubert 	isc_mem_free(manager->mctx, manager->threads);
1306*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1307*a466cc55SCy Schubert 	manager->common.impmagic = 0;
1308*a466cc55SCy Schubert 	manager->common.magic = 0;
1309*a466cc55SCy Schubert 	mctx = manager->mctx;
1310*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1311*a466cc55SCy Schubert 	DESTROYLOCK(&manager->lock);
1312*a466cc55SCy Schubert 	isc_mem_put(mctx, manager, sizeof(*manager));
1313*a466cc55SCy Schubert 	isc_mem_detach(&mctx);
1314*a466cc55SCy Schubert 
1315*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1316*a466cc55SCy Schubert 	taskmgr = NULL;
1317*a466cc55SCy Schubert #endif	/* USE_SHARED_MANAGER */
1318*a466cc55SCy Schubert }
1319*a466cc55SCy Schubert 
1320*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
isc__taskmgr_create(isc_mem_t * mctx,unsigned int workers,unsigned int default_quantum,isc_taskmgr_t ** managerp)1321*a466cc55SCy Schubert isc__taskmgr_create(isc_mem_t *mctx, unsigned int workers,
1322*a466cc55SCy Schubert 		    unsigned int default_quantum, isc_taskmgr_t **managerp)
1323*a466cc55SCy Schubert {
1324*a466cc55SCy Schubert 	isc_result_t result;
1325*a466cc55SCy Schubert 	unsigned int i, started = 0;
1326*a466cc55SCy Schubert 	isc__taskmgr_t *manager;
1327*a466cc55SCy Schubert 
1328*a466cc55SCy Schubert 	/*
1329*a466cc55SCy Schubert 	 * Create a new task manager.
1330*a466cc55SCy Schubert 	 */
1331*a466cc55SCy Schubert 
1332*a466cc55SCy Schubert 	REQUIRE(workers > 0);
1333*a466cc55SCy Schubert 	REQUIRE(managerp != NULL && *managerp == NULL);
1334*a466cc55SCy Schubert 
1335*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1336*a466cc55SCy Schubert 	UNUSED(i);
1337*a466cc55SCy Schubert 	UNUSED(started);
1338*a466cc55SCy Schubert #endif
1339*a466cc55SCy Schubert 
1340*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1341*a466cc55SCy Schubert 	if (taskmgr != NULL) {
1342*a466cc55SCy Schubert 		if (taskmgr->refs == 0)
1343*a466cc55SCy Schubert 			return (ISC_R_SHUTTINGDOWN);
1344*a466cc55SCy Schubert 		taskmgr->refs++;
1345*a466cc55SCy Schubert 		*managerp = (isc_taskmgr_t *)taskmgr;
1346*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
1347*a466cc55SCy Schubert 	}
1348*a466cc55SCy Schubert #endif /* USE_SHARED_MANAGER */
1349*a466cc55SCy Schubert 
1350*a466cc55SCy Schubert 	manager = isc_mem_get(mctx, sizeof(*manager));
1351*a466cc55SCy Schubert 	if (manager == NULL)
1352*a466cc55SCy Schubert 		return (ISC_R_NOMEMORY);
1353*a466cc55SCy Schubert 	manager->common.methods = &taskmgrmethods;
1354*a466cc55SCy Schubert 	manager->common.impmagic = TASK_MANAGER_MAGIC;
1355*a466cc55SCy Schubert 	manager->common.magic = ISCAPI_TASKMGR_MAGIC;
1356*a466cc55SCy Schubert 	manager->mode = isc_taskmgrmode_normal;
1357*a466cc55SCy Schubert 	manager->mctx = NULL;
1358*a466cc55SCy Schubert 	result = isc_mutex_init(&manager->lock);
1359*a466cc55SCy Schubert 	if (result != ISC_R_SUCCESS)
1360*a466cc55SCy Schubert 		goto cleanup_mgr;
1361*a466cc55SCy Schubert 	LOCK(&manager->lock);
1362*a466cc55SCy Schubert 
1363*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1364*a466cc55SCy Schubert 	manager->workers = 0;
1365*a466cc55SCy Schubert 	manager->threads = isc_mem_allocate(mctx,
1366*a466cc55SCy Schubert 					    workers * sizeof(isc_thread_t));
1367*a466cc55SCy Schubert 	if (manager->threads == NULL) {
1368*a466cc55SCy Schubert 		result = ISC_R_NOMEMORY;
1369*a466cc55SCy Schubert 		goto cleanup_lock;
1370*a466cc55SCy Schubert 	}
1371*a466cc55SCy Schubert 	if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) {
1372*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
1373*a466cc55SCy Schubert 				 "isc_condition_init() %s",
1374*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1375*a466cc55SCy Schubert 						ISC_MSG_FAILED, "failed"));
1376*a466cc55SCy Schubert 		result = ISC_R_UNEXPECTED;
1377*a466cc55SCy Schubert 		goto cleanup_threads;
1378*a466cc55SCy Schubert 	}
1379*a466cc55SCy Schubert 	if (isc_condition_init(&manager->exclusive_granted) != ISC_R_SUCCESS) {
1380*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
1381*a466cc55SCy Schubert 				 "isc_condition_init() %s",
1382*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1383*a466cc55SCy Schubert 						ISC_MSG_FAILED, "failed"));
1384*a466cc55SCy Schubert 		result = ISC_R_UNEXPECTED;
1385*a466cc55SCy Schubert 		goto cleanup_workavailable;
1386*a466cc55SCy Schubert 	}
1387*a466cc55SCy Schubert 	if (isc_condition_init(&manager->paused) != ISC_R_SUCCESS) {
1388*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
1389*a466cc55SCy Schubert 				 "isc_condition_init() %s",
1390*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1391*a466cc55SCy Schubert 						ISC_MSG_FAILED, "failed"));
1392*a466cc55SCy Schubert 		result = ISC_R_UNEXPECTED;
1393*a466cc55SCy Schubert 		goto cleanup_exclusivegranted;
1394*a466cc55SCy Schubert 	}
1395*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1396*a466cc55SCy Schubert 	if (default_quantum == 0)
1397*a466cc55SCy Schubert 		default_quantum = DEFAULT_DEFAULT_QUANTUM;
1398*a466cc55SCy Schubert 	manager->default_quantum = default_quantum;
1399*a466cc55SCy Schubert 	INIT_LIST(manager->tasks);
1400*a466cc55SCy Schubert 	INIT_LIST(manager->ready_tasks);
1401*a466cc55SCy Schubert 	INIT_LIST(manager->ready_priority_tasks);
1402*a466cc55SCy Schubert 	manager->tasks_running = 0;
1403*a466cc55SCy Schubert 	manager->exclusive_requested = ISC_FALSE;
1404*a466cc55SCy Schubert 	manager->pause_requested = ISC_FALSE;
1405*a466cc55SCy Schubert 	manager->exiting = ISC_FALSE;
1406*a466cc55SCy Schubert 
1407*a466cc55SCy Schubert 	isc_mem_attach(mctx, &manager->mctx);
1408*a466cc55SCy Schubert 
1409*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1410*a466cc55SCy Schubert 	/*
1411*a466cc55SCy Schubert 	 * Start workers.
1412*a466cc55SCy Schubert 	 */
1413*a466cc55SCy Schubert 	for (i = 0; i < workers; i++) {
1414*a466cc55SCy Schubert 		if (isc_thread_create(run, manager,
1415*a466cc55SCy Schubert 				      &manager->threads[manager->workers]) ==
1416*a466cc55SCy Schubert 		    ISC_R_SUCCESS) {
1417*a466cc55SCy Schubert 			manager->workers++;
1418*a466cc55SCy Schubert 			started++;
1419*a466cc55SCy Schubert 		}
1420*a466cc55SCy Schubert 	}
1421*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1422*a466cc55SCy Schubert 
1423*a466cc55SCy Schubert 	if (started == 0) {
1424*a466cc55SCy Schubert 		manager_free(manager);
1425*a466cc55SCy Schubert 		return (ISC_R_NOTHREADS);
1426*a466cc55SCy Schubert 	}
1427*a466cc55SCy Schubert 	isc_thread_setconcurrency(workers);
1428*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1429*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1430*a466cc55SCy Schubert 	manager->refs = 1;
1431*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1432*a466cc55SCy Schubert 	taskmgr = manager;
1433*a466cc55SCy Schubert #endif /* USE_SHARED_MANAGER */
1434*a466cc55SCy Schubert 
1435*a466cc55SCy Schubert 	*managerp = (isc_taskmgr_t *)manager;
1436*a466cc55SCy Schubert 
1437*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
1438*a466cc55SCy Schubert 
1439*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1440*a466cc55SCy Schubert  cleanup_exclusivegranted:
1441*a466cc55SCy Schubert 	(void)isc_condition_destroy(&manager->exclusive_granted);
1442*a466cc55SCy Schubert  cleanup_workavailable:
1443*a466cc55SCy Schubert 	(void)isc_condition_destroy(&manager->work_available);
1444*a466cc55SCy Schubert  cleanup_threads:
1445*a466cc55SCy Schubert 	isc_mem_free(mctx, manager->threads);
1446*a466cc55SCy Schubert  cleanup_lock:
1447*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1448*a466cc55SCy Schubert 	DESTROYLOCK(&manager->lock);
1449*a466cc55SCy Schubert #endif
1450*a466cc55SCy Schubert  cleanup_mgr:
1451*a466cc55SCy Schubert 	isc_mem_put(mctx, manager, sizeof(*manager));
1452*a466cc55SCy Schubert 	return (result);
1453*a466cc55SCy Schubert }
1454*a466cc55SCy Schubert 
1455*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__taskmgr_destroy(isc_taskmgr_t ** managerp)1456*a466cc55SCy Schubert isc__taskmgr_destroy(isc_taskmgr_t **managerp) {
1457*a466cc55SCy Schubert 	isc__taskmgr_t *manager;
1458*a466cc55SCy Schubert 	isc__task_t *task;
1459*a466cc55SCy Schubert 	unsigned int i;
1460*a466cc55SCy Schubert 
1461*a466cc55SCy Schubert 	/*
1462*a466cc55SCy Schubert 	 * Destroy '*managerp'.
1463*a466cc55SCy Schubert 	 */
1464*a466cc55SCy Schubert 
1465*a466cc55SCy Schubert 	REQUIRE(managerp != NULL);
1466*a466cc55SCy Schubert 	manager = (void*)(*managerp);
1467*a466cc55SCy Schubert 	REQUIRE(VALID_MANAGER(manager));
1468*a466cc55SCy Schubert 
1469*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1470*a466cc55SCy Schubert 	UNUSED(i);
1471*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1472*a466cc55SCy Schubert 
1473*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1474*a466cc55SCy Schubert 	manager->refs--;
1475*a466cc55SCy Schubert 	if (manager->refs > 0) {
1476*a466cc55SCy Schubert 		*managerp = NULL;
1477*a466cc55SCy Schubert 		return;
1478*a466cc55SCy Schubert 	}
1479*a466cc55SCy Schubert #endif
1480*a466cc55SCy Schubert 
1481*a466cc55SCy Schubert 	XTHREADTRACE("isc_taskmgr_destroy");
1482*a466cc55SCy Schubert 	/*
1483*a466cc55SCy Schubert 	 * Only one non-worker thread may ever call this routine.
1484*a466cc55SCy Schubert 	 * If a worker thread wants to initiate shutdown of the
1485*a466cc55SCy Schubert 	 * task manager, it should ask some non-worker thread to call
1486*a466cc55SCy Schubert 	 * isc_taskmgr_destroy(), e.g. by signalling a condition variable
1487*a466cc55SCy Schubert 	 * that the startup thread is sleeping on.
1488*a466cc55SCy Schubert 	 */
1489*a466cc55SCy Schubert 
1490*a466cc55SCy Schubert 	/*
1491*a466cc55SCy Schubert 	 * Unlike elsewhere, we're going to hold this lock a long time.
1492*a466cc55SCy Schubert 	 * We need to do so, because otherwise the list of tasks could
1493*a466cc55SCy Schubert 	 * change while we were traversing it.
1494*a466cc55SCy Schubert 	 *
1495*a466cc55SCy Schubert 	 * This is also the only function where we will hold both the
1496*a466cc55SCy Schubert 	 * task manager lock and a task lock at the same time.
1497*a466cc55SCy Schubert 	 */
1498*a466cc55SCy Schubert 
1499*a466cc55SCy Schubert 	LOCK(&manager->lock);
1500*a466cc55SCy Schubert 
1501*a466cc55SCy Schubert 	/*
1502*a466cc55SCy Schubert 	 * Make sure we only get called once.
1503*a466cc55SCy Schubert 	 */
1504*a466cc55SCy Schubert 	INSIST(!manager->exiting);
1505*a466cc55SCy Schubert 	manager->exiting = ISC_TRUE;
1506*a466cc55SCy Schubert 
1507*a466cc55SCy Schubert 	/*
1508*a466cc55SCy Schubert 	 * If privileged mode was on, turn it off.
1509*a466cc55SCy Schubert 	 */
1510*a466cc55SCy Schubert 	manager->mode = isc_taskmgrmode_normal;
1511*a466cc55SCy Schubert 
1512*a466cc55SCy Schubert 	/*
1513*a466cc55SCy Schubert 	 * Post shutdown event(s) to every task (if they haven't already been
1514*a466cc55SCy Schubert 	 * posted).
1515*a466cc55SCy Schubert 	 */
1516*a466cc55SCy Schubert 	for (task = HEAD(manager->tasks);
1517*a466cc55SCy Schubert 	     task != NULL;
1518*a466cc55SCy Schubert 	     task = NEXT(task, link)) {
1519*a466cc55SCy Schubert 		LOCK(&task->lock);
1520*a466cc55SCy Schubert 		if (task_shutdown(task))
1521*a466cc55SCy Schubert 			push_readyq(manager, task);
1522*a466cc55SCy Schubert 		UNLOCK(&task->lock);
1523*a466cc55SCy Schubert 	}
1524*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1525*a466cc55SCy Schubert 	/*
1526*a466cc55SCy Schubert 	 * Wake up any sleeping workers.  This ensures we get work done if
1527*a466cc55SCy Schubert 	 * there's work left to do, and if there are already no tasks left
1528*a466cc55SCy Schubert 	 * it will cause the workers to see manager->exiting.
1529*a466cc55SCy Schubert 	 */
1530*a466cc55SCy Schubert 	BROADCAST(&manager->work_available);
1531*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1532*a466cc55SCy Schubert 
1533*a466cc55SCy Schubert 	/*
1534*a466cc55SCy Schubert 	 * Wait for all the worker threads to exit.
1535*a466cc55SCy Schubert 	 */
1536*a466cc55SCy Schubert 	for (i = 0; i < manager->workers; i++)
1537*a466cc55SCy Schubert 		(void)isc_thread_join(manager->threads[i], NULL);
1538*a466cc55SCy Schubert #else /* USE_WORKER_THREADS */
1539*a466cc55SCy Schubert 	/*
1540*a466cc55SCy Schubert 	 * Dispatch the shutdown events.
1541*a466cc55SCy Schubert 	 */
1542*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1543*a466cc55SCy Schubert 	while (isc__taskmgr_ready((isc_taskmgr_t *)manager))
1544*a466cc55SCy Schubert 		(void)isc__taskmgr_dispatch((isc_taskmgr_t *)manager);
1545*a466cc55SCy Schubert #ifdef BIND9
1546*a466cc55SCy Schubert 	if (!ISC_LIST_EMPTY(manager->tasks))
1547*a466cc55SCy Schubert 		isc_mem_printallactive(stderr);
1548*a466cc55SCy Schubert #endif
1549*a466cc55SCy Schubert 	INSIST(ISC_LIST_EMPTY(manager->tasks));
1550*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1551*a466cc55SCy Schubert 	taskmgr = NULL;
1552*a466cc55SCy Schubert #endif
1553*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1554*a466cc55SCy Schubert 
1555*a466cc55SCy Schubert 	manager_free(manager);
1556*a466cc55SCy Schubert 
1557*a466cc55SCy Schubert 	*managerp = NULL;
1558*a466cc55SCy Schubert }
1559*a466cc55SCy Schubert 
1560*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__taskmgr_setmode(isc_taskmgr_t * manager0,isc_taskmgrmode_t mode)1561*a466cc55SCy Schubert isc__taskmgr_setmode(isc_taskmgr_t *manager0, isc_taskmgrmode_t mode) {
1562*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1563*a466cc55SCy Schubert 
1564*a466cc55SCy Schubert 	LOCK(&manager->lock);
1565*a466cc55SCy Schubert 	manager->mode = mode;
1566*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1567*a466cc55SCy Schubert }
1568*a466cc55SCy Schubert 
1569*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_taskmgrmode_t
isc__taskmgr_mode(isc_taskmgr_t * manager0)1570*a466cc55SCy Schubert isc__taskmgr_mode(isc_taskmgr_t *manager0) {
1571*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1572*a466cc55SCy Schubert 	isc_taskmgrmode_t mode;
1573*a466cc55SCy Schubert 	LOCK(&manager->lock);
1574*a466cc55SCy Schubert 	mode = manager->mode;
1575*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1576*a466cc55SCy Schubert 	return (mode);
1577*a466cc55SCy Schubert }
1578*a466cc55SCy Schubert 
1579*a466cc55SCy Schubert #ifndef USE_WORKER_THREADS
1580*a466cc55SCy Schubert isc_boolean_t
isc__taskmgr_ready(isc_taskmgr_t * manager0)1581*a466cc55SCy Schubert isc__taskmgr_ready(isc_taskmgr_t *manager0) {
1582*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1583*a466cc55SCy Schubert 	isc_boolean_t is_ready;
1584*a466cc55SCy Schubert 
1585*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1586*a466cc55SCy Schubert 	if (manager == NULL)
1587*a466cc55SCy Schubert 		manager = taskmgr;
1588*a466cc55SCy Schubert #endif
1589*a466cc55SCy Schubert 	if (manager == NULL)
1590*a466cc55SCy Schubert 		return (ISC_FALSE);
1591*a466cc55SCy Schubert 
1592*a466cc55SCy Schubert 	LOCK(&manager->lock);
1593*a466cc55SCy Schubert 	is_ready = !empty_readyq(manager);
1594*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1595*a466cc55SCy Schubert 
1596*a466cc55SCy Schubert 	return (is_ready);
1597*a466cc55SCy Schubert }
1598*a466cc55SCy Schubert 
1599*a466cc55SCy Schubert isc_result_t
isc__taskmgr_dispatch(isc_taskmgr_t * manager0)1600*a466cc55SCy Schubert isc__taskmgr_dispatch(isc_taskmgr_t *manager0) {
1601*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1602*a466cc55SCy Schubert 
1603*a466cc55SCy Schubert #ifdef USE_SHARED_MANAGER
1604*a466cc55SCy Schubert 	if (manager == NULL)
1605*a466cc55SCy Schubert 		manager = taskmgr;
1606*a466cc55SCy Schubert #endif
1607*a466cc55SCy Schubert 	if (manager == NULL)
1608*a466cc55SCy Schubert 		return (ISC_R_NOTFOUND);
1609*a466cc55SCy Schubert 
1610*a466cc55SCy Schubert 	dispatch(manager);
1611*a466cc55SCy Schubert 
1612*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
1613*a466cc55SCy Schubert }
1614*a466cc55SCy Schubert 
1615*a466cc55SCy Schubert #else
1616*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__taskmgr_pause(isc_taskmgr_t * manager0)1617*a466cc55SCy Schubert isc__taskmgr_pause(isc_taskmgr_t *manager0) {
1618*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1619*a466cc55SCy Schubert 	LOCK(&manager->lock);
1620*a466cc55SCy Schubert 	while (manager->tasks_running > 0) {
1621*a466cc55SCy Schubert 		WAIT(&manager->paused, &manager->lock);
1622*a466cc55SCy Schubert 	}
1623*a466cc55SCy Schubert 	manager->pause_requested = ISC_TRUE;
1624*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1625*a466cc55SCy Schubert }
1626*a466cc55SCy Schubert 
1627*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__taskmgr_resume(isc_taskmgr_t * manager0)1628*a466cc55SCy Schubert isc__taskmgr_resume(isc_taskmgr_t *manager0) {
1629*a466cc55SCy Schubert 	isc__taskmgr_t *manager = (void*)manager0;
1630*a466cc55SCy Schubert 
1631*a466cc55SCy Schubert 	LOCK(&manager->lock);
1632*a466cc55SCy Schubert 	if (manager->pause_requested) {
1633*a466cc55SCy Schubert 		manager->pause_requested = ISC_FALSE;
1634*a466cc55SCy Schubert 		BROADCAST(&manager->work_available);
1635*a466cc55SCy Schubert 	}
1636*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1637*a466cc55SCy Schubert }
1638*a466cc55SCy Schubert #endif /* USE_WORKER_THREADS */
1639*a466cc55SCy Schubert 
1640*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_result_t
isc__task_beginexclusive(isc_task_t * task0)1641*a466cc55SCy Schubert isc__task_beginexclusive(isc_task_t *task0) {
1642*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1643*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
1644*a466cc55SCy Schubert 	isc__taskmgr_t *manager = task->manager;
1645*a466cc55SCy Schubert 	REQUIRE(task->state == task_state_running);
1646*a466cc55SCy Schubert 	LOCK(&manager->lock);
1647*a466cc55SCy Schubert 	if (manager->exclusive_requested) {
1648*a466cc55SCy Schubert 		UNLOCK(&manager->lock);
1649*a466cc55SCy Schubert 		return (ISC_R_LOCKBUSY);
1650*a466cc55SCy Schubert 	}
1651*a466cc55SCy Schubert 	manager->exclusive_requested = ISC_TRUE;
1652*a466cc55SCy Schubert 	while (manager->tasks_running > 1) {
1653*a466cc55SCy Schubert 		WAIT(&manager->exclusive_granted, &manager->lock);
1654*a466cc55SCy Schubert 	}
1655*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1656*a466cc55SCy Schubert #else
1657*a466cc55SCy Schubert 	UNUSED(task0);
1658*a466cc55SCy Schubert #endif
1659*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
1660*a466cc55SCy Schubert }
1661*a466cc55SCy Schubert 
1662*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_endexclusive(isc_task_t * task0)1663*a466cc55SCy Schubert isc__task_endexclusive(isc_task_t *task0) {
1664*a466cc55SCy Schubert #ifdef USE_WORKER_THREADS
1665*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
1666*a466cc55SCy Schubert 	isc__taskmgr_t *manager = task->manager;
1667*a466cc55SCy Schubert 
1668*a466cc55SCy Schubert 	REQUIRE(task->state == task_state_running);
1669*a466cc55SCy Schubert 	LOCK(&manager->lock);
1670*a466cc55SCy Schubert 	REQUIRE(manager->exclusive_requested);
1671*a466cc55SCy Schubert 	manager->exclusive_requested = ISC_FALSE;
1672*a466cc55SCy Schubert 	BROADCAST(&manager->work_available);
1673*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1674*a466cc55SCy Schubert #else
1675*a466cc55SCy Schubert 	UNUSED(task0);
1676*a466cc55SCy Schubert #endif
1677*a466cc55SCy Schubert }
1678*a466cc55SCy Schubert 
1679*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE void
isc__task_setprivilege(isc_task_t * task0,isc_boolean_t priv)1680*a466cc55SCy Schubert isc__task_setprivilege(isc_task_t *task0, isc_boolean_t priv) {
1681*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
1682*a466cc55SCy Schubert 	isc__taskmgr_t *manager = task->manager;
1683*a466cc55SCy Schubert 	isc_boolean_t oldpriv;
1684*a466cc55SCy Schubert 
1685*a466cc55SCy Schubert 	LOCK(&task->lock);
1686*a466cc55SCy Schubert 	oldpriv = ISC_TF((task->flags & TASK_F_PRIVILEGED) != 0);
1687*a466cc55SCy Schubert 	if (priv)
1688*a466cc55SCy Schubert 		task->flags |= TASK_F_PRIVILEGED;
1689*a466cc55SCy Schubert 	else
1690*a466cc55SCy Schubert 		task->flags &= ~TASK_F_PRIVILEGED;
1691*a466cc55SCy Schubert 	UNLOCK(&task->lock);
1692*a466cc55SCy Schubert 
1693*a466cc55SCy Schubert 	if (priv == oldpriv)
1694*a466cc55SCy Schubert 		return;
1695*a466cc55SCy Schubert 
1696*a466cc55SCy Schubert 	LOCK(&manager->lock);
1697*a466cc55SCy Schubert 	if (priv && ISC_LINK_LINKED(task, ready_link))
1698*a466cc55SCy Schubert 		ENQUEUE(manager->ready_priority_tasks, task,
1699*a466cc55SCy Schubert 			ready_priority_link);
1700*a466cc55SCy Schubert 	else if (!priv && ISC_LINK_LINKED(task, ready_priority_link))
1701*a466cc55SCy Schubert 		DEQUEUE(manager->ready_priority_tasks, task,
1702*a466cc55SCy Schubert 			ready_priority_link);
1703*a466cc55SCy Schubert 	UNLOCK(&manager->lock);
1704*a466cc55SCy Schubert }
1705*a466cc55SCy Schubert 
1706*a466cc55SCy Schubert ISC_TASKFUNC_SCOPE isc_boolean_t
isc__task_privilege(isc_task_t * task0)1707*a466cc55SCy Schubert isc__task_privilege(isc_task_t *task0) {
1708*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)task0;
1709*a466cc55SCy Schubert 	isc_boolean_t priv;
1710*a466cc55SCy Schubert 
1711*a466cc55SCy Schubert 	LOCK(&task->lock);
1712*a466cc55SCy Schubert 	priv = ISC_TF((task->flags & TASK_F_PRIVILEGED) != 0);
1713*a466cc55SCy Schubert 	UNLOCK(&task->lock);
1714*a466cc55SCy Schubert 	return (priv);
1715*a466cc55SCy Schubert }
1716*a466cc55SCy Schubert 
1717*a466cc55SCy Schubert #ifdef USE_SOCKETIMPREGISTER
1718*a466cc55SCy Schubert isc_result_t
isc__task_register()1719*a466cc55SCy Schubert isc__task_register() {
1720*a466cc55SCy Schubert 	return (isc_task_register(isc__taskmgr_create));
1721*a466cc55SCy Schubert }
1722*a466cc55SCy Schubert #endif
1723*a466cc55SCy Schubert 
1724*a466cc55SCy Schubert isc_boolean_t
isc_task_exiting(isc_task_t * t)1725*a466cc55SCy Schubert isc_task_exiting(isc_task_t *t) {
1726*a466cc55SCy Schubert 	isc__task_t *task = (isc__task_t *)t;
1727*a466cc55SCy Schubert 
1728*a466cc55SCy Schubert 	REQUIRE(VALID_TASK(task));
1729*a466cc55SCy Schubert 	return (TASK_SHUTTINGDOWN(task));
1730*a466cc55SCy Schubert }
1731*a466cc55SCy Schubert 
1732*a466cc55SCy Schubert 
1733*a466cc55SCy Schubert #if defined(HAVE_LIBXML2) && defined(BIND9)
1734*a466cc55SCy Schubert void
isc_taskmgr_renderxml(isc_taskmgr_t * mgr0,xmlTextWriterPtr writer)1735*a466cc55SCy Schubert isc_taskmgr_renderxml(isc_taskmgr_t *mgr0, xmlTextWriterPtr writer) {
1736*a466cc55SCy Schubert 	isc__taskmgr_t *mgr = (isc__taskmgr_t *)mgr0;
1737*a466cc55SCy Schubert 	isc__task_t *task;
1738*a466cc55SCy Schubert 
1739*a466cc55SCy Schubert 	LOCK(&mgr->lock);
1740*a466cc55SCy Schubert 
1741*a466cc55SCy Schubert 	/*
1742*a466cc55SCy Schubert 	 * Write out the thread-model, and some details about each depending
1743*a466cc55SCy Schubert 	 * on which type is enabled.
1744*a466cc55SCy Schubert 	 */
1745*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "thread-model");
1746*a466cc55SCy Schubert #ifdef ISC_PLATFORM_USETHREADS
1747*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "type");
1748*a466cc55SCy Schubert 	xmlTextWriterWriteString(writer, ISC_XMLCHAR "threaded");
1749*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* type */
1750*a466cc55SCy Schubert 
1751*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "worker-threads");
1752*a466cc55SCy Schubert 	xmlTextWriterWriteFormatString(writer, "%d", mgr->workers);
1753*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* worker-threads */
1754*a466cc55SCy Schubert #else /* ISC_PLATFORM_USETHREADS */
1755*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "type");
1756*a466cc55SCy Schubert 	xmlTextWriterWriteString(writer, ISC_XMLCHAR "non-threaded");
1757*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* type */
1758*a466cc55SCy Schubert 
1759*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
1760*a466cc55SCy Schubert 	xmlTextWriterWriteFormatString(writer, "%d", mgr->refs);
1761*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* references */
1762*a466cc55SCy Schubert #endif /* ISC_PLATFORM_USETHREADS */
1763*a466cc55SCy Schubert 
1764*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "default-quantum");
1765*a466cc55SCy Schubert 	xmlTextWriterWriteFormatString(writer, "%d", mgr->default_quantum);
1766*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* default-quantum */
1767*a466cc55SCy Schubert 
1768*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "tasks-running");
1769*a466cc55SCy Schubert 	xmlTextWriterWriteFormatString(writer, "%d", mgr->tasks_running);
1770*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* tasks-running */
1771*a466cc55SCy Schubert 
1772*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* thread-model */
1773*a466cc55SCy Schubert 
1774*a466cc55SCy Schubert 	xmlTextWriterStartElement(writer, ISC_XMLCHAR "tasks");
1775*a466cc55SCy Schubert 	task = ISC_LIST_HEAD(mgr->tasks);
1776*a466cc55SCy Schubert 	while (task != NULL) {
1777*a466cc55SCy Schubert 		LOCK(&task->lock);
1778*a466cc55SCy Schubert 		xmlTextWriterStartElement(writer, ISC_XMLCHAR "task");
1779*a466cc55SCy Schubert 
1780*a466cc55SCy Schubert 		if (task->name[0] != 0) {
1781*a466cc55SCy Schubert 			xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
1782*a466cc55SCy Schubert 			xmlTextWriterWriteFormatString(writer, "%s",
1783*a466cc55SCy Schubert 						       task->name);
1784*a466cc55SCy Schubert 			xmlTextWriterEndElement(writer); /* name */
1785*a466cc55SCy Schubert 		}
1786*a466cc55SCy Schubert 
1787*a466cc55SCy Schubert 		xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
1788*a466cc55SCy Schubert 		xmlTextWriterWriteFormatString(writer, "%d", task->references);
1789*a466cc55SCy Schubert 		xmlTextWriterEndElement(writer); /* references */
1790*a466cc55SCy Schubert 
1791*a466cc55SCy Schubert 		xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
1792*a466cc55SCy Schubert 		xmlTextWriterWriteFormatString(writer, "%p", task);
1793*a466cc55SCy Schubert 		xmlTextWriterEndElement(writer); /* id */
1794*a466cc55SCy Schubert 
1795*a466cc55SCy Schubert 		xmlTextWriterStartElement(writer, ISC_XMLCHAR "state");
1796*a466cc55SCy Schubert 		xmlTextWriterWriteFormatString(writer, "%s",
1797*a466cc55SCy Schubert 					       statenames[task->state]);
1798*a466cc55SCy Schubert 		xmlTextWriterEndElement(writer); /* state */
1799*a466cc55SCy Schubert 
1800*a466cc55SCy Schubert 		xmlTextWriterStartElement(writer, ISC_XMLCHAR "quantum");
1801*a466cc55SCy Schubert 		xmlTextWriterWriteFormatString(writer, "%d", task->quantum);
1802*a466cc55SCy Schubert 		xmlTextWriterEndElement(writer); /* quantum */
1803*a466cc55SCy Schubert 
1804*a466cc55SCy Schubert 		xmlTextWriterEndElement(writer);
1805*a466cc55SCy Schubert 
1806*a466cc55SCy Schubert 		UNLOCK(&task->lock);
1807*a466cc55SCy Schubert 		task = ISC_LIST_NEXT(task, link);
1808*a466cc55SCy Schubert 	}
1809*a466cc55SCy Schubert 	xmlTextWriterEndElement(writer); /* tasks */
1810*a466cc55SCy Schubert 
1811*a466cc55SCy Schubert 	UNLOCK(&mgr->lock);
1812*a466cc55SCy Schubert }
1813*a466cc55SCy Schubert #endif /* HAVE_LIBXML2 && BIND9 */
1814