task.c 23.9 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1
/*
Bob Halley's avatar
Bob Halley committed
2
 * Copyright (C) 1998, 1999  Internet Software Consortium.
Bob Halley's avatar
Bob Halley committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */
Bob Halley's avatar
base  
Bob Halley committed
17

Bob Halley's avatar
Bob Halley committed
18 19 20 21
/*
 * Principal Author: Bob Halley
 */

22 23 24 25 26
/*
 * XXXRTH  Need to document the states a task can be in, and the rules
 * for changing states.
 */

Bob Halley's avatar
Bob Halley committed
27 28
#include <config.h>

Bob Halley's avatar
base  
Bob Halley committed
29
#include <isc/assertions.h>
30
#include <isc/boolean.h>
Bob Halley's avatar
Bob Halley committed
31
#include <isc/thread.h>
32 33
#include <isc/mutex.h>
#include <isc/condition.h>
34
#include <isc/error.h>
35
#include <isc/event.h>
Bob Halley's avatar
Bob Halley committed
36
#include <isc/task.h>
Michael Graff's avatar
Michael Graff committed
37
#include <isc/util.h>
Bob Halley's avatar
base  
Bob Halley committed
38

Bob Halley's avatar
Bob Halley committed
39
#ifdef ISC_TASK_TRACE
40 41 42 43 44 45
#define XTRACE(m)		printf("task %p thread %lu: %s\n", \
				       task, isc_thread_self(), (m))
#define XTTRACE(t, m)		printf("task %p thread %lu: %s\n", \
				       (t), isc_thread_self(), (m))
#define XTHREADTRACE(m)		printf("thread %lu: %s\n", \
				       isc_thread_self(), (m))
Bob Halley's avatar
Bob Halley committed
46 47
#else
#define XTRACE(m)
48
#define XTTRACE(t, m)
49
#define XTHREADTRACE(m)
Bob Halley's avatar
Bob Halley committed
50
#endif
Bob Halley's avatar
base  
Bob Halley committed
51

Bob Halley's avatar
Bob Halley committed
52
/***
Bob Halley's avatar
Bob Halley committed
53
 *** Types.
Bob Halley's avatar
Bob Halley committed
54 55
 ***/

Bob Halley's avatar
Bob Halley committed
56 57
typedef enum {
	task_state_idle, task_state_ready, task_state_running,
58
	task_state_done
Bob Halley's avatar
Bob Halley committed
59 60 61 62 63 64
} task_state_t;

#define TASK_MAGIC			0x5441534BU	/* TASK. */
#define VALID_TASK(t)			((t) != NULL && \
					 (t)->magic == TASK_MAGIC)

Bob Halley's avatar
Bob Halley committed
65
struct isc_task {
Bob Halley's avatar
Bob Halley committed
66 67
	/* Not locked. */
	unsigned int			magic;
Bob Halley's avatar
Bob Halley committed
68
	isc_taskmgr_t *			manager;
Bob Halley's avatar
Bob Halley committed
69
	isc_mutex_t			lock;
Bob Halley's avatar
Bob Halley committed
70
	isc_mem_t *			mctx;
Bob Halley's avatar
Bob Halley committed
71 72 73
	/* Locked by task lock. */
	task_state_t			state;
	unsigned int			references;
Bob Halley's avatar
Bob Halley committed
74
	isc_eventlist_t			events;
75
	isc_eventlist_t			on_shutdown;
Bob Halley's avatar
Bob Halley committed
76
	unsigned int			quantum;
77
	unsigned int			flags;
Bob Halley's avatar
Bob Halley committed
78
	/* Locked by task manager lock. */
Bob Halley's avatar
Bob Halley committed
79 80
	LINK(isc_task_t)		link;
	LINK(isc_task_t)		ready_link;
Bob Halley's avatar
Bob Halley committed
81 82
};

Bob Halley's avatar
Bob Halley committed
83
#define TASK_F_SHUTTINGDOWN		0x01
84

85 86
#define TASK_SHUTTINGDOWN(t)		(((t)->flags & TASK_F_SHUTTINGDOWN) \
					 != 0)
87

Bob Halley's avatar
Bob Halley committed
88 89 90 91
#define TASK_MANAGER_MAGIC		0x54534B4DU	/* TSKM. */
#define VALID_MANAGER(m)		((m) != NULL && \
					 (m)->magic == TASK_MANAGER_MAGIC)

Bob Halley's avatar
Bob Halley committed
92
struct isc_taskmgr {
Bob Halley's avatar
Bob Halley committed
93 94
	/* Not locked. */
	unsigned int			magic;
Bob Halley's avatar
Bob Halley committed
95
	isc_mem_t *			mctx;
Bob Halley's avatar
Bob Halley committed
96
	isc_mutex_t			lock;
97 98
	unsigned int			workers;
	isc_thread_t *			threads;
Bob Halley's avatar
Bob Halley committed
99 100
	/* Locked by task manager lock. */
	unsigned int			default_quantum;
Bob Halley's avatar
Bob Halley committed
101 102
	LIST(isc_task_t)		tasks;
	LIST(isc_task_t)		ready_tasks;
Bob Halley's avatar
Bob Halley committed
103
	isc_condition_t			work_available;
Bob Halley's avatar
Bob Halley committed
104
	isc_boolean_t			exiting;
Bob Halley's avatar
Bob Halley committed
105
};
Bob Halley's avatar
Bob Halley committed
106

Bob Halley's avatar
Bob Halley committed
107 108
#define DEFAULT_DEFAULT_QUANTUM		5
#define FINISHED(m)			((m)->exiting && EMPTY((m)->tasks))
Bob Halley's avatar
Bob Halley committed
109

Bob Halley's avatar
base  
Bob Halley committed
110 111 112 113 114
/***
 *** Tasks.
 ***/

static void
115
task_finished(isc_task_t *task) {
Bob Halley's avatar
Bob Halley committed
116
	isc_taskmgr_t *manager = task->manager;
Bob Halley's avatar
base  
Bob Halley committed
117 118

	REQUIRE(EMPTY(task->events));
119
	REQUIRE(EMPTY(task->on_shutdown));
120 121 122 123
	REQUIRE(task->references == 0);
	REQUIRE(task->state == task_state_done);

	XTRACE("task_finished");
Bob Halley's avatar
base  
Bob Halley committed
124 125 126 127 128 129 130 131 132 133 134 135 136

	LOCK(&manager->lock);
	UNLINK(manager->tasks, task, link);
	if (FINISHED(manager)) {
		/*
		 * All tasks have completed and the
		 * task manager is exiting.  Wake up
		 * any idle worker threads so they
		 * can exit.
		 */
		BROADCAST(&manager->work_available);
	}
	UNLOCK(&manager->lock);
137

Bob Halley's avatar
Bob Halley committed
138
	(void)isc_mutex_destroy(&task->lock);
Bob Halley's avatar
base  
Bob Halley committed
139
	task->magic = 0;
140
	isc_mem_put(task->mctx, task, sizeof *task);
Bob Halley's avatar
base  
Bob Halley committed
141 142
}

Bob Halley's avatar
Bob Halley committed
143
isc_result_t
Bob Halley's avatar
Bob Halley committed
144
isc_task_create(isc_taskmgr_t *manager, isc_mem_t *mctx, unsigned int quantum,
145
		isc_task_t **taskp)
Bob Halley's avatar
Bob Halley committed
146
{
Bob Halley's avatar
Bob Halley committed
147
	isc_task_t *task;
Bob Halley's avatar
base  
Bob Halley committed
148 149 150 151

	REQUIRE(VALID_MANAGER(manager));
	REQUIRE(taskp != NULL && *taskp == NULL);

Bob Halley's avatar
Bob Halley committed
152 153 154
	if (mctx == NULL)
		mctx = manager->mctx;
	task = isc_mem_get(mctx, sizeof *task);
Bob Halley's avatar
base  
Bob Halley committed
155
	if (task == NULL)
Bob Halley's avatar
Bob Halley committed
156
		return (ISC_R_NOMEMORY);
157
	XTRACE("create");
Bob Halley's avatar
base  
Bob Halley committed
158
	task->manager = manager;
Bob Halley's avatar
Bob Halley committed
159
	task->mctx = mctx;
Bob Halley's avatar
Bob Halley committed
160
	if (isc_mutex_init(&task->lock) != ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
161
		isc_mem_put(mctx, task, sizeof *task);
Bob Halley's avatar
Bob Halley committed
162 163 164
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_mutex_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
165
	}
Bob Halley's avatar
base  
Bob Halley committed
166 167 168
	task->state = task_state_idle;
	task->references = 1;
	INIT_LIST(task->events);
169
	INIT_LIST(task->on_shutdown);
Bob Halley's avatar
base  
Bob Halley committed
170
	task->quantum = quantum;
Bob Halley's avatar
Bob Halley committed
171
	task->flags = 0;
Bob Halley's avatar
base  
Bob Halley committed
172 173 174 175
	INIT_LINK(task, link);
	INIT_LINK(task, ready_link);

	LOCK(&manager->lock);
Bob Halley's avatar
Bob Halley committed
176
	/* XXX Should disallow if task manager is exiting. */
Bob Halley's avatar
base  
Bob Halley committed
177 178 179 180 181
	if (task->quantum == 0)
		task->quantum = manager->default_quantum;
	APPEND(manager->tasks, task, link);
	UNLOCK(&manager->lock);

182
	task->magic = TASK_MAGIC;
Bob Halley's avatar
base  
Bob Halley committed
183 184
	*taskp = task;

Bob Halley's avatar
Bob Halley committed
185
	return (ISC_R_SUCCESS);
Bob Halley's avatar
base  
Bob Halley committed
186 187
}

Bob Halley's avatar
update  
Bob Halley committed
188
void
189
isc_task_attach(isc_task_t *source, isc_task_t **targetp) {
Bob Halley's avatar
base  
Bob Halley committed
190

191 192 193
	/*
	 * Attach *targetp to source.
	 */
Bob Halley's avatar
base  
Bob Halley committed
194

195 196
	REQUIRE(VALID_TASK(source));
	REQUIRE(targetp != NULL && *targetp == NULL);
Bob Halley's avatar
base  
Bob Halley committed
197

198 199
	XTTRACE(source, "attach");

200 201 202 203 204
	LOCK(&source->lock);
	source->references++;
	UNLOCK(&source->lock);

	*targetp = source;
Bob Halley's avatar
base  
Bob Halley committed
205 206
}

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
static inline isc_boolean_t
task_shutdown(isc_task_t *task) {
	isc_boolean_t was_idle = ISC_FALSE;
	isc_event_t *event, *prev;
	
	/*
	 * Caller must be holding the task's lock.
	 */

	XTRACE("task_shutdown");

	if (! TASK_SHUTTINGDOWN(task)) {
		XTRACE("shutting down");
		task->flags |= TASK_F_SHUTTINGDOWN;
		if (task->state == task_state_idle) {
			INSIST(EMPTY(task->events));
			task->state = task_state_ready;
			was_idle = ISC_TRUE;
		}
		INSIST(task->state == task_state_ready ||
		       task->state == task_state_running);
		/*
		 * Note that we post shutdown events LIFO.
		 */
		for (event = TAIL(task->on_shutdown);
		     event != NULL;
		     event = prev) {
			prev = PREV(event, link);
			DEQUEUE(task->on_shutdown, event, link);
			ENQUEUE(task->events, event, link);
		}
	}

	return (was_idle);
}

static inline void
task_ready(isc_task_t *task) {
	isc_taskmgr_t *manager = task->manager;

	REQUIRE(VALID_MANAGER(manager));
	REQUIRE(task->state == task_state_ready);

	XTRACE("task_ready");

	LOCK(&manager->lock);

	ENQUEUE(manager->ready_tasks, task, ready_link);
	SIGNAL(&manager->work_available);

	UNLOCK(&manager->lock);
}

Bob Halley's avatar
Bob Halley committed
260
static inline isc_boolean_t
261 262 263 264 265 266 267 268 269 270 271
task_detach(isc_task_t *task) {

	/*
	 * Caller must be holding the task lock.
	 */

	REQUIRE(task->references > 0);

	XTRACE("detach");

	task->references--;
Bob Halley's avatar
Bob Halley committed
272 273 274 275 276 277 278 279 280 281 282 283
	if (task->references == 0 && task->state == task_state_idle) {
		INSIST(EMPTY(task->events));
		/*
		 * There are no references to this task, and no
		 * pending events.  We could try to optimize and
		 * either initiate shutdown or clean up the task,
		 * depending on its state, but it's easier to just
		 * make the task ready and allow run() to deal with
		 * shutting down and termination.
		 */
		task->state = task_state_ready;
		return (ISC_TRUE);
284 285
	}

Bob Halley's avatar
Bob Halley committed
286
	return (ISC_FALSE);
287 288
}

Bob Halley's avatar
Bob Halley committed
289
void
Bob Halley's avatar
Bob Halley committed
290 291
isc_task_detach(isc_task_t **taskp) {
	isc_task_t *task;
Bob Halley's avatar
Bob Halley committed
292
	isc_boolean_t was_idle;
Bob Halley's avatar
base  
Bob Halley committed
293

294 295 296 297
	/*
	 * Detach *taskp from its task.
	 */

Bob Halley's avatar
base  
Bob Halley committed
298 299 300 301
	REQUIRE(taskp != NULL);
	task = *taskp;
	REQUIRE(VALID_TASK(task));

302 303
	XTRACE("isc_task_detach");

Bob Halley's avatar
base  
Bob Halley committed
304
	LOCK(&task->lock);
Bob Halley's avatar
Bob Halley committed
305
	was_idle = task_detach(task);
Bob Halley's avatar
base  
Bob Halley committed
306 307
	UNLOCK(&task->lock);

Bob Halley's avatar
Bob Halley committed
308
	if (was_idle)
309
		task_ready(task);
Bob Halley's avatar
base  
Bob Halley committed
310 311 312 313

	*taskp = NULL;
}

Bob Halley's avatar
Bob Halley committed
314 315 316
isc_mem_t *
isc_task_mem(isc_task_t *task) {

317 318 319 320
	/*
	 * Get the task's memory context.
	 */

Bob Halley's avatar
Bob Halley committed
321 322 323 324 325
	REQUIRE(VALID_TASK(task));
	
	return (task->mctx);
}

Bob Halley's avatar
Bob Halley committed
326 327 328
static inline isc_boolean_t
task_send(isc_task_t *task, isc_event_t **eventp) {
	isc_boolean_t was_idle = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
329
	isc_event_t *event;
330
	
331
	/*
332
	 * Caller must be holding the task lock.
333 334
	 */

Bob Halley's avatar
update  
Bob Halley committed
335 336
	REQUIRE(eventp != NULL);
	event = *eventp;
Bob Halley's avatar
base  
Bob Halley committed
337
	REQUIRE(event != NULL);
Bob Halley's avatar
Bob Halley committed
338 339
	REQUIRE(event->sender != NULL);
	REQUIRE(event->type > 0);
Bob Halley's avatar
Bob Halley committed
340
	REQUIRE(task->state != task_state_done);
Bob Halley's avatar
base  
Bob Halley committed
341

342 343
	XTRACE("task_send");

Bob Halley's avatar
Bob Halley committed
344 345 346 347
	if (task->state == task_state_idle) {
		was_idle = ISC_TRUE;
		INSIST(EMPTY(task->events));
		task->state = task_state_ready;
348
	}
Bob Halley's avatar
Bob Halley committed
349 350 351 352
	INSIST(task->state == task_state_ready ||
	       task->state == task_state_running);
	ENQUEUE(task->events, event, link);
	*eventp = NULL;
353

Bob Halley's avatar
Bob Halley committed
354 355
	return (was_idle);
}	
356

Bob Halley's avatar
Bob Halley committed
357
void
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
isc_task_send(isc_task_t *task, isc_event_t **eventp) {
	isc_boolean_t was_idle;

	/*
	 * Send '*event' to 'task'.
	 */

	REQUIRE(VALID_TASK(task));

	XTRACE("isc_task_send");

	/*
	 * We're trying hard to hold locks for as short a time as possible.
	 * We're also trying to hold as few locks as possible.  This is why
	 * some processing is deferred until after the lock is released.
	 */
	LOCK(&task->lock);
Bob Halley's avatar
Bob Halley committed
375
	was_idle = task_send(task, eventp);
Bob Halley's avatar
base  
Bob Halley committed
376 377 378 379 380 381
	UNLOCK(&task->lock);

	if (was_idle) {
		/*
		 * We need to add this task to the ready queue.
		 *
382 383 384
		 * We've waited until now to do it because making a task
		 * ready requires locking the manager.  If we tried to do
		 * this while holding the task lock, we could deadlock.
Bob Halley's avatar
base  
Bob Halley committed
385 386
		 *
		 * We've changed the state to ready, so no one else will
387 388 389
		 * be trying to add this task to the ready queue.  The
		 * only way to leave the ready state is by executing the
		 * task.  It thus doesn't matter if events are added,
390
		 * removed, or a shutdown is started in the interval
391 392
		 * between the time we released the task lock, and the time
		 * we add the task to the ready queue.
Bob Halley's avatar
base  
Bob Halley committed
393
		 */
394
		task_ready(task);
Bob Halley's avatar
base  
Bob Halley committed
395
	}
396 397
}

Bob Halley's avatar
Bob Halley committed
398
void
399
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
Bob Halley's avatar
Bob Halley committed
400
	isc_boolean_t idle1, idle2;
401 402 403 404 405 406 407 408 409 410 411 412
	isc_task_t *task;

	/*
	 * Send '*event' to '*taskp' and then detach '*taskp' from its
	 * task.
	 */

	REQUIRE(taskp != NULL);
	task = *taskp;
	REQUIRE(VALID_TASK(task));

	XTRACE("isc_task_sendanddetach");
Bob Halley's avatar
update  
Bob Halley committed
413

414
	LOCK(&task->lock);
Bob Halley's avatar
Bob Halley committed
415 416
	idle1 = task_send(task, eventp);
	idle2 = task_detach(task);
417 418
	UNLOCK(&task->lock);

Bob Halley's avatar
Bob Halley committed
419 420 421 422 423 424
	/*
	 * If idle1, then idle2 shouldn't be true as well since we're holding
	 * the task lock, and thus the task cannot switch from ready back to
	 * idle.
	 */
	INSIST(!(idle1 && idle2));
425

Bob Halley's avatar
Bob Halley committed
426 427
	if (idle1 || idle2)
		task_ready(task);
428 429

	*taskp = NULL;
Bob Halley's avatar
base  
Bob Halley committed
430 431
}

432 433 434 435 436 437
#define PURGE_OK(event)	(((event)->attributes & ISC_EVENTATTR_NOPURGE) == 0)

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)
438
{
Bob Halley's avatar
Bob Halley committed
439
	isc_event_t *event, *next_event;
440
	unsigned int count = 0;
441

Bob Halley's avatar
Bob Halley committed
442
	REQUIRE(VALID_TASK(task));
443
	REQUIRE(last >= first);
Bob Halley's avatar
Bob Halley committed
444

445
	XTRACE("dequeue_events");
Bob Halley's avatar
Bob Halley committed
446

Bob Halley's avatar
Bob Halley committed
447
	/*
448 449 450
	 * Events matching 'sender', whose type is >= first and <= last, and
	 * whose tag is 'tag' will be dequeued.  If 'purging', matching events
	 * which are marked as unpurgable will not be dequeued.
451
	 *
452
	 * sender == NULL means "any sender", and tag == NULL means "any tag".
Bob Halley's avatar
Bob Halley committed
453 454 455
	 */

	LOCK(&task->lock);
456 457

	for (event = HEAD(task->events); event != NULL; event = next_event) {
Bob Halley's avatar
Bob Halley committed
458
		next_event = NEXT(event, link);
Bob Halley's avatar
Bob Halley committed
459 460
		if (event->type >= first && event->type <= last &&
		    (sender == NULL || event->sender == sender) &&
461 462
		    (tag == NULL || event->tag == tag) &&
		    (!purging || PURGE_OK(event))) {
Bob Halley's avatar
Bob Halley committed
463
			DEQUEUE(task->events, event, link);
464 465
			ENQUEUE(*events, event, link);
			count++;
Bob Halley's avatar
Bob Halley committed
466 467
		}
	}
468

Bob Halley's avatar
Bob Halley committed
469 470
	UNLOCK(&task->lock);

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	return (count);
}

unsigned int
isc_task_purgerange(isc_task_t *task, void *sender, isc_eventtype_t first,
		    isc_eventtype_t last, void *tag)
{
	unsigned int count;
	isc_eventlist_t events;
	isc_event_t *event, *next_event;

	/*
	 * Purge events from a task's event queue.
	 */

	XTRACE("isc_task_purgerange");

	ISC_LIST_INIT(events);

	count = dequeue_events(task, sender, first, last, tag, &events,
			       ISC_TRUE);

	for (event = HEAD(events); event != NULL; event = next_event) {
Bob Halley's avatar
Bob Halley committed
494
		next_event = NEXT(event, link);
Bob Halley's avatar
Bob Halley committed
495
		isc_event_free(&event);
Bob Halley's avatar
Bob Halley committed
496
	}
497

498 499 500 501 502
	/*
	 * Note that purging never changes the state of the task.
	 */

	return (count);
Bob Halley's avatar
Bob Halley committed
503 504
}

505
unsigned int
Bob Halley's avatar
Bob Halley committed
506
isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type,
507
	       void *tag)
Bob Halley's avatar
Bob Halley committed
508
{
509 510 511 512
	/*
	 * Purge events from a task's event queue.
	 */

513 514
	XTRACE("isc_task_purge");

Bob Halley's avatar
Bob Halley committed
515
	return (isc_task_purgerange(task, sender, type, type, tag));
516 517
}

Bob Halley's avatar
Bob Halley committed
518 519 520 521 522 523
isc_boolean_t
isc_task_purgeevent(isc_task_t *task, isc_event_t *event) {
	isc_event_t *curr_event, *next_event;

	/*
	 * Purge 'event' from a task's event queue.
524 525
	 *
	 * XXXRTH:  WARNING:  This method may be removed before beta.
Bob Halley's avatar
Bob Halley committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	 */

	REQUIRE(VALID_TASK(task));

	/*
	 * If 'event' is on the task's event queue, it will be purged,
	 * unless it is marked as unpurgeable.  'event' does not have to be
	 * on the task's event queue; in fact, it can even be an invalid
	 * pointer.  Purging only occurs if the event is actually on the task's
	 * event queue.
	 *
	 * Purging never changes the state of the task.
	 */

	LOCK(&task->lock);
	for (curr_event = HEAD(task->events);
	     curr_event != NULL;
	     curr_event = next_event) {
		next_event = NEXT(curr_event, link);
545
		if (curr_event == event && PURGE_OK(event)) {
Bob Halley's avatar
Bob Halley committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559
			DEQUEUE(task->events, curr_event, link);
			break;
		}
	}
	UNLOCK(&task->lock);

	if (curr_event == NULL)
		return (ISC_FALSE);

	isc_event_free(&curr_event);

	return (ISC_TRUE);
}

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
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)
{
	/*
	 * Remove events from a task's event queue.
	 */

	XTRACE("isc_task_unsendrange");

	return (dequeue_events(task, sender, first, last, tag, events,
			       ISC_FALSE));
}

unsigned int
isc_task_unsend(isc_task_t *task, void *sender, isc_eventtype_t type,
		void *tag, isc_eventlist_t *events)
{
	/*
	 * Remove events from a task's event queue.
	 */

	XTRACE("isc_task_unsend");

	return (dequeue_events(task, sender, type, type, tag, events,
			       ISC_FALSE));
}

589 590 591 592 593 594
isc_result_t
isc_task_onshutdown(isc_task_t *task, isc_taskaction_t action, void *arg) {
	isc_boolean_t disallowed = ISC_FALSE;
	isc_result_t result = ISC_R_SUCCESS;
	isc_event_t *event;

595 596 597 598 599
	/*
	 * Send a shutdown event with action 'action' and argument 'arg' when
	 * 'task' is shutdown.
	 */

600
	REQUIRE(VALID_TASK(task));
601
	REQUIRE(action != NULL);
602

603 604 605 606 607 608
	event = isc_event_allocate(task->mctx,
				   NULL,
				   ISC_TASKEVENT_SHUTDOWN,
				   action,
				   arg,
				   sizeof *event);
609 610 611 612
	if (event == NULL)
		return (ISC_R_NOMEMORY);

	LOCK(&task->lock);
Bob Halley's avatar
Bob Halley committed
613
	if (TASK_SHUTTINGDOWN(task)) {
614
		disallowed = ISC_TRUE;
615
		result = ISC_R_SHUTTINGDOWN;
616 617 618 619 620
	} else
		ENQUEUE(task->on_shutdown, event, link);
	UNLOCK(&task->lock);

	if (disallowed)
Bob Halley's avatar
Bob Halley committed
621
		isc_mem_put(task->mctx, event, sizeof *event);
622 623 624 625

	return (result);
}

Bob Halley's avatar
Bob Halley committed
626
void
Bob Halley's avatar
Bob Halley committed
627
isc_task_shutdown(isc_task_t *task) {
628
	isc_boolean_t was_idle;
Bob Halley's avatar
base  
Bob Halley committed
629

630 631 632 633
	/*
	 * Shutdown 'task'.
	 */

Bob Halley's avatar
base  
Bob Halley committed
634 635 636
	REQUIRE(VALID_TASK(task));

	LOCK(&task->lock);
637
	was_idle = task_shutdown(task);
Bob Halley's avatar
base  
Bob Halley committed
638 639
	UNLOCK(&task->lock);

640 641
	if (was_idle)
		task_ready(task);
Bob Halley's avatar
Bob Halley committed
642
}
Bob Halley's avatar
base  
Bob Halley committed
643

Bob Halley's avatar
Bob Halley committed
644
void
Bob Halley's avatar
Bob Halley committed
645
isc_task_destroy(isc_task_t **taskp) {
Bob Halley's avatar
Bob Halley committed
646

647 648 649 650
	/*
	 * Destroy '*taskp'.
	 */

Bob Halley's avatar
Bob Halley committed
651 652
	REQUIRE(taskp != NULL);

Bob Halley's avatar
Bob Halley committed
653 654
	isc_task_shutdown(*taskp);
	isc_task_detach(taskp);
Bob Halley's avatar
base  
Bob Halley committed
655 656 657
}


Bob Halley's avatar
Bob Halley committed
658

Bob Halley's avatar
base  
Bob Halley committed
659 660 661 662
/***
 *** Task Manager.
 ***/

Bob Halley's avatar
update  
Bob Halley committed
663
static isc_threadresult_t
Bob Halley's avatar
Bob Halley committed
664 665 666
#ifdef _WIN32
WINAPI
#endif
Bob Halley's avatar
update  
Bob Halley committed
667
run(void *uap) {
Bob Halley's avatar
Bob Halley committed
668 669
	isc_taskmgr_t *manager = uap;
	isc_task_t *task;
Bob Halley's avatar
base  
Bob Halley committed
670

671
	XTHREADTRACE("start");
Bob Halley's avatar
base  
Bob Halley committed
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

	REQUIRE(VALID_MANAGER(manager));

	/*
	 * Again we're trying to hold the lock for as short a time as possible
	 * and to do as little locking and unlocking as possible.
	 * 
	 * In both while loops, the appropriate lock must be held before the
	 * while body starts.  Code which acquired the lock at the top of
	 * the loop would be more readable, but would result in a lot of
	 * extra locking.  Compare:
	 *
	 * Straightforward:
	 *
	 *	LOCK();
	 *	...
	 *	UNLOCK();
	 *	while (expression) {
	 *		LOCK();
	 *		...
	 *		UNLOCK();
	 *
	 *	       	Unlocked part here...
	 *
	 *		LOCK();
	 *		...
	 *		UNLOCK();
	 *	}
	 *
	 * Note how if the loop continues we unlock and then immediately lock.
	 * For N iterations of the loop, this code does 2N+1 locks and 2N+1
	 * unlocks.  Also note that the lock is not held when the while
	 * condition is tested, which may or may not be important, depending
	 * on the expression.
	 * 
	 * As written:
	 *
	 *	LOCK();
	 *	while (expression) {
	 *		...
	 *		UNLOCK();
	 *
	 *	       	Unlocked part here...
	 *
	 *		LOCK();
	 *		...
	 *	}
	 *	UNLOCK();
	 *
	 * For N iterations of the loop, this code does N+1 locks and N+1
	 * unlocks.  The while expression is always protected by the lock.
	 */

	LOCK(&manager->lock);
	while (!FINISHED(manager)) {
		/*
		 * For reasons similar to those given in the comment in
729
		 * isc_task_send() above, it is safe for us to dequeue
Bob Halley's avatar
base  
Bob Halley committed
730 731 732 733 734
		 * the task while only holding the manager lock, and then
		 * change the task to running state while only holding the
		 * task lock.
		 */
		while (EMPTY(manager->ready_tasks) && !FINISHED(manager)) {
735
			XTHREADTRACE("wait");
Bob Halley's avatar
base  
Bob Halley committed
736
			WAIT(&manager->work_available, &manager->lock);
737
			XTHREADTRACE("awake");
Bob Halley's avatar
base  
Bob Halley committed
738
		}
739
		XTHREADTRACE("working");
Bob Halley's avatar
base  
Bob Halley committed
740 741 742
		
		task = HEAD(manager->ready_tasks);
		if (task != NULL) {
Bob Halley's avatar
Bob Halley committed
743
			unsigned int dispatch_count = 0;
Bob Halley's avatar
Bob Halley committed
744 745
			isc_boolean_t done = ISC_FALSE;
			isc_boolean_t requeue = ISC_FALSE;
746
			isc_boolean_t finished = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
747
			isc_event_t *event;
Bob Halley's avatar
base  
Bob Halley committed
748 749 750 751 752 753 754 755 756 757 758 759

			INSIST(VALID_TASK(task));

			/*
			 * Note we only unlock the manager lock if we actually
			 * have a task to do.  We must reacquire the manager 
			 * lock before exiting the 'if (task != NULL)' block.
			 */
			DEQUEUE(manager->ready_tasks, task, ready_link);
			UNLOCK(&manager->lock);

			LOCK(&task->lock);
Bob Halley's avatar
Bob Halley committed
760
			INSIST(task->state == task_state_ready);
761 762 763 764 765 766
			task->state = task_state_running;
			XTRACE("running");
			do {
				if (!EMPTY(task->events)) {
					event = HEAD(task->events);
					DEQUEUE(task->events, event, link);
Bob Halley's avatar
base  
Bob Halley committed
767

768 769 770 771 772 773 774 775 776 777
					/*
					 * Execute the event action.
					 */
					XTRACE("execute action");
					if (event->action != NULL) {
						UNLOCK(&task->lock);
						(event->action)(task, event);
						LOCK(&task->lock);
					}
					dispatch_count++;
778
				}
779

Bob Halley's avatar
Bob Halley committed
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
				if (task->references == 0 &&
				    EMPTY(task->events) &&
				    !TASK_SHUTTINGDOWN(task)) {
					isc_boolean_t was_idle;
					
					/*
					 * There are no references and no
					 * pending events for this task,
					 * which means it will not become
					 * runnable again via an external
					 * action (such as sending an event
					 * or detaching).
					 *
					 * We initiate shutdown to prevent
					 * it from becoming a zombie.
					 *
					 * We do this here instead of in
					 * the "if EMPTY(task->events)" block
					 * below because:
					 *
					 *	If we post no shutdown events,
					 *	we want the task to finish.
					 *
					 *	If we did post shutdown events,
					 *	will still want the task's
					 *	quantum to be applied.
					 */
					was_idle = task_shutdown(task);
					INSIST(!was_idle);
809 810
				}

811
				if (EMPTY(task->events)) {
Bob Halley's avatar
Bob Halley committed
812
					/*
813
					 * Nothing else to do for this task
Bob Halley's avatar
Bob Halley committed
814
					 * right now.
Bob Halley's avatar
Bob Halley committed
815
					 */
Bob Halley's avatar
Bob Halley committed
816
					XTRACE("empty");
Bob Halley's avatar
Bob Halley committed
817 818 819 820 821
					if (task->references == 0 &&
					    TASK_SHUTTINGDOWN(task)) {
						/*
						 * The task is done.
						 */
822
						XTRACE("done");
Bob Halley's avatar
Bob Halley committed
823
						finished = ISC_TRUE;
824
						task->state = task_state_done;
825 826
					} else
						task->state = task_state_idle;
Bob Halley's avatar
Bob Halley committed
827
					done = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
828 829 830 831 832 833 834 835 836 837 838
				} else if (dispatch_count >= task->quantum) {
					/*
					 * Our quantum has expired, but
					 * there is more work to be done.
					 * We'll requeue it to the ready
					 * queue later.
					 *
					 * We don't check quantum until
					 * dispatching at least one event,
					 * so the minimum quantum is one.
					 */
Bob Halley's avatar
Bob Halley committed
839
					XTRACE("quantum");
Bob Halley's avatar
base  
Bob Halley committed
840
					task->state = task_state_ready;
Bob Halley's avatar
Bob Halley committed
841 842
					requeue = ISC_TRUE;
					done = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
843
				}
844
			} while (!done);
Bob Halley's avatar
base  
Bob Halley committed
845 846
			UNLOCK(&task->lock);

847 848
			if (finished)
				task_finished(task);
Bob Halley's avatar
base  
Bob Halley committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

			LOCK(&manager->lock);
			if (requeue) {
				/*
				 * We know we're awake, so we don't have
				 * to wakeup any sleeping threads if the
				 * ready queue is empty before we requeue.
				 *
				 * A possible optimization if the queue is
				 * empty is to 'goto' the 'if (task != NULL)'
				 * block, avoiding the ENQUEUE of the task
				 * and the subsequent immediate DEQUEUE
				 * (since it is the only executable task).
				 * We don't do this because then we'd be
				 * skipping the exit_requested check.  The
				 * cost of ENQUEUE is low anyway, especially
				 * when you consider that we'd have to do
				 * an extra EMPTY check to see if we could
				 * do the optimization.  If the ready queue
				 * were usually nonempty, the 'optimization'
				 * might even hurt rather than help.
				 */
				ENQUEUE(manager->ready_tasks, task,
					ready_link);
			}
		}
	}
	UNLOCK(&manager->lock);

878
	XTHREADTRACE("exit");
Bob Halley's avatar
base  
Bob Halley committed
879

Bob Halley's avatar
update  
Bob Halley committed
880
	return ((isc_threadresult_t)0);
Bob Halley's avatar
base  
Bob Halley committed
881 882 883
}

static void
Bob Halley's avatar
Bob Halley committed
884
manager_free(isc_taskmgr_t *manager) {
Bob Halley's avatar
Bob Halley committed
885 886
	(void)isc_condition_destroy(&manager->work_available);
	(void)isc_mutex_destroy(&manager->lock);
887 888
	isc_mem_put(manager->mctx, manager->threads,
		    manager->workers * sizeof (isc_thread_t));
Bob Halley's avatar
base  
Bob Halley committed
889
	manager->magic = 0;
Bob Halley's avatar
Bob Halley committed
890
	isc_mem_put(manager->mctx, manager, sizeof *manager);
Bob Halley's avatar
base  
Bob Halley committed
891 892
}

Bob Halley's avatar
Bob Halley committed
893
isc_result_t
Bob Halley's avatar
Bob Halley committed
894
isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers, 
Bob Halley's avatar
Bob Halley committed
895
		   unsigned int default_quantum, isc_taskmgr_t **managerp)
Bob Halley's avatar
Bob Halley committed
896 897
{
	unsigned int i, started = 0;
Bob Halley's avatar
Bob Halley committed
898
	isc_taskmgr_t *manager;
899
	isc_thread_t *threads;
Bob Halley's avatar
Bob Halley committed
900

901 902 903 904
	/*
	 * Create a new task manager.
	 */

Bob Halley's avatar
Bob Halley committed
905
	REQUIRE(workers > 0);
Bob Halley's avatar
Bob Halley committed
906
	REQUIRE(managerp != NULL && *managerp == NULL);
Bob Halley's avatar
base  
Bob Halley committed
907

Bob Halley's avatar
Bob Halley committed
908
	manager = isc_mem_get(mctx, sizeof *manager);
Bob Halley's avatar
base  
Bob Halley committed
909
	if (manager == NULL)
Bob Halley's avatar
Bob Halley committed
910
		return (ISC_R_NOMEMORY);
Bob Halley's avatar
base  
Bob Halley committed
911 912
	manager->magic = TASK_MANAGER_MAGIC;
	manager->mctx = mctx;
913 914 915 916 917 918 919
	threads = isc_mem_get(mctx, workers * sizeof (isc_thread_t));
	if (threads == NULL) {
		isc_mem_put(mctx, manager, sizeof *manager);
		return (ISC_R_NOMEMORY);
	}
	manager->threads = threads;
	manager->workers = 0;
Bob Halley's avatar
Bob Halley committed
920
	if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) {
921
		isc_mem_put(mctx, threads, workers * sizeof (isc_thread_t));
Bob Halley's avatar
Bob Halley committed
922
		isc_mem_put(mctx, manager, sizeof *manager);
Bob Halley's avatar
Bob Halley committed
923 924 925
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_mutex_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
926
	}
Bob Halley's avatar
base  
Bob Halley committed
927 928 929 930 931
	if (default_quantum == 0)
		default_quantum = DEFAULT_DEFAULT_QUANTUM;
	manager->default_quantum = default_quantum;
	INIT_LIST(manager->tasks);
	INIT_LIST(manager->ready_tasks);
Bob Halley's avatar
Bob Halley committed
932 933
	if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) {
		(void)isc_mutex_destroy(&manager->lock);
934
		isc_mem_put(mctx, threads, workers * sizeof (isc_thread_t));
Bob Halley's avatar
Bob Halley committed
935
		isc_mem_put(mctx, manager, sizeof *manager);
Bob Halley's avatar
Bob Halley committed
936 937 938
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_condition_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
939
	}
Bob Halley's avatar
Bob Halley committed
940
	manager->exiting = ISC_FALSE;
Bob Halley's avatar
base  
Bob Halley committed
941 942 943 944 945 946 947
	manager->workers = 0;

	LOCK(&manager->lock);
	/*
	 * Start workers.
	 */
	for (i = 0; i < workers; i++) {
948 949
		if (isc_thread_create(run, manager,
				      &manager->threads[manager->workers]) == 
Bob Halley's avatar
Bob Halley committed
950
		    ISC_R_SUCCESS) {
Bob Halley's avatar
base  
Bob Halley committed
951 952 953 954 955 956 957 958
			manager->workers++;
			started++;
		}
	}
	UNLOCK(&manager->lock);

	if (started == 0) {
		manager_free(manager);
Bob Halley's avatar
Bob Halley committed
959
		return (ISC_R_NOTHREADS);
Bob Halley's avatar
base  
Bob Halley committed
960 961 962 963
	}		

	*managerp = manager;

Bob Halley's avatar
Bob Halley committed
964
	return (ISC_R_SUCCESS);
Bob Halley's avatar
base  
Bob Halley committed
965 966
}

Bob Halley's avatar
update  
Bob Halley committed
967
void
Bob Halley's avatar
Bob Halley committed
968 969 970
isc_taskmgr_destroy(isc_taskmgr_t **managerp) {
	isc_taskmgr_t *manager;
	isc_task_t *task;
971
	unsigned int i;
Bob Halley's avatar
base  
Bob Halley committed
972

973 974 975 976
	/*
	 * Destroy '*managerp'.
	 */

Bob Halley's avatar
base  
Bob Halley committed
977 978 979 980
	REQUIRE(managerp != NULL);
	manager = *managerp;
	REQUIRE(VALID_MANAGER(manager));

981
	XTHREADTRACE("isc_taskmgr_destroy");
Bob Halley's avatar
base  
Bob Halley committed
982 983 984 985
	/*
	 * Only one non-worker thread may ever call this routine.
	 * If a worker thread wants to initiate shutdown of the
	 * task manager, it should ask some non-worker thread to call
Bob Halley's avatar
Bob Halley committed
986
	 * isc_taskmgr_destroy(), e.g. by signalling a condition variable
Bob Halley's avatar
base  
Bob Halley committed
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
	 * that the startup thread is sleeping on.
	 */

	/*
	 * Unlike elsewhere, we're going to hold this lock a long time.
	 * We need to do so, because otherwise the list of tasks could
	 * change while we were traversing it.
	 *
	 * This is also the only function where we will hold both the
	 * task manager lock and a task lock at the same time.
	 */

	LOCK(&manager->lock);

	/*
	 * Make sure we only get called once.
	 */
	INSIST(!manager->exiting);
Bob Halley's avatar
Bob Halley committed
1005
	manager->exiting = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
1006 1007

	/*
1008
	 * Post shutdown event(s) to every task (if they haven't already been
Bob Halley's avatar
update  
Bob Halley committed
1009
	 * posted).
Bob Halley's avatar
base  
Bob Halley committed
1010 1011 1012 1013 1014
	 */
	for (task = HEAD(manager->tasks);
	     task != NULL;
	     task = NEXT(task, link)) {
		LOCK(&task->lock);
1015 1016
		if (task_shutdown(task))
			ENQUEUE(manager->ready_tasks, task, ready_link);
Bob Halley's avatar
base  
Bob Halley committed
1017 1018 1019 1020 1021 1022 1023 1024 1025
		UNLOCK(&task->lock);
	}

	/*
	 * Wake up any sleeping workers.  This ensures we get work done if
	 * there's work left to do, and if there are already no tasks left
	 * it will cause the workers to see manager->exiting.
	 */
	BROADCAST(&manager->work_available);
1026
	UNLOCK(&manager->lock);
Bob Halley's avatar
base  
Bob Halley committed
1027 1028 1029 1030

	/*
	 * Wait for all the worker threads to exit.
	 */
1031 1032
	for (i = 0; i < manager->workers; i++)
		(void)isc_thread_join(manager->threads[i], NULL);
Bob Halley's avatar
base  
Bob Halley committed
1033 1034 1035 1036 1037

	manager_free(manager);

	*managerp = NULL;
}