task.c 26.5 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>
Bob Halley's avatar
base  
Bob Halley committed
37

38
#include "util.h"
Bob Halley's avatar
base  
Bob Halley committed
39

Bob Halley's avatar
Bob Halley committed
40
#ifdef ISC_TASK_TRACE
41 42 43 44 45 46
#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
47 48
#else
#define XTRACE(m)
49
#define XTTRACE(t, m)
50
#define XTHREADTRACE(m)
Bob Halley's avatar
Bob Halley committed
51
#endif
Bob Halley's avatar
base  
Bob Halley committed
52

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

57 58 59 60
typedef enum {
	detach_result_ok, detach_result_finished, detach_result_wasidle
} detach_result_t;

Bob Halley's avatar
Bob Halley committed
61 62
typedef enum {
	task_state_idle, task_state_ready, task_state_running,
63
	task_state_done
Bob Halley's avatar
Bob Halley committed
64 65 66 67 68 69
} 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
70
struct isc_task {
Bob Halley's avatar
Bob Halley committed
71 72
	/* Not locked. */
	unsigned int			magic;
Bob Halley's avatar
Bob Halley committed
73
	isc_taskmgr_t *			manager;
Bob Halley's avatar
Bob Halley committed
74
	isc_mutex_t			lock;
Bob Halley's avatar
Bob Halley committed
75
	isc_mem_t *			mctx;
Bob Halley's avatar
Bob Halley committed
76 77 78
	/* Locked by task lock. */
	task_state_t			state;
	unsigned int			references;
Bob Halley's avatar
Bob Halley committed
79
	isc_eventlist_t			events;
80
	isc_eventlist_t			on_shutdown;
Bob Halley's avatar
Bob Halley committed
81
	unsigned int			quantum;
82
	unsigned int			flags;
Bob Halley's avatar
Bob Halley committed
83
	/* Locked by task manager lock. */
Bob Halley's avatar
Bob Halley committed
84 85
	LINK(isc_task_t)		link;
	LINK(isc_task_t)		ready_link;
Bob Halley's avatar
Bob Halley committed
86 87
};

88 89 90 91 92
#define TASK_F_DONEOK			0x01
#define TASK_F_SENDOK			0x02
#define TASK_F_SHUTTINGDOWN		0x04

#define DONE_FLAGS			(TASK_F_DONEOK|TASK_F_SHUTTINGDOWN)
93
#define TASK_WANTDONE(t)		(((t)->flags & DONE_FLAGS) == \
94
					 DONE_FLAGS)
95 96
#define TASK_SHUTTINGDOWN(t)		(((t)->flags & TASK_F_SHUTTINGDOWN) \
					 != 0)
97

Bob Halley's avatar
Bob Halley committed
98 99 100 101
#define TASK_MANAGER_MAGIC		0x54534B4DU	/* TSKM. */
#define VALID_MANAGER(m)		((m) != NULL && \
					 (m)->magic == TASK_MANAGER_MAGIC)

Bob Halley's avatar
Bob Halley committed
102
struct isc_taskmgr {
Bob Halley's avatar
Bob Halley committed
103 104
	/* Not locked. */
	unsigned int			magic;
Bob Halley's avatar
Bob Halley committed
105
	isc_mem_t *			mctx;
Bob Halley's avatar
Bob Halley committed
106
	isc_mutex_t			lock;
107 108
	unsigned int			workers;
	isc_thread_t *			threads;
Bob Halley's avatar
Bob Halley committed
109 110
	/* Locked by task manager lock. */
	unsigned int			default_quantum;
Bob Halley's avatar
Bob Halley committed
111 112
	LIST(isc_task_t)		tasks;
	LIST(isc_task_t)		ready_tasks;
Bob Halley's avatar
Bob Halley committed
113
	isc_condition_t			work_available;
Bob Halley's avatar
Bob Halley committed
114
	isc_boolean_t			exiting;
Bob Halley's avatar
Bob Halley committed
115
};
Bob Halley's avatar
Bob Halley committed
116

Bob Halley's avatar
Bob Halley committed
117 118
#define DEFAULT_DEFAULT_QUANTUM		5
#define FINISHED(m)			((m)->exiting && EMPTY((m)->tasks))
Bob Halley's avatar
Bob Halley committed
119

Bob Halley's avatar
base  
Bob Halley committed
120 121 122 123 124
/***
 *** Tasks.
 ***/

static void
125
task_finished(isc_task_t *task) {
Bob Halley's avatar
Bob Halley committed
126
	isc_taskmgr_t *manager = task->manager;
Bob Halley's avatar
base  
Bob Halley committed
127 128

	REQUIRE(EMPTY(task->events));
129
	REQUIRE(EMPTY(task->on_shutdown));
130 131 132 133
	REQUIRE(task->references == 0);
	REQUIRE(task->state == task_state_done);

	XTRACE("task_finished");
Bob Halley's avatar
base  
Bob Halley committed
134 135 136 137 138 139 140 141 142 143 144 145 146

	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);
147

Bob Halley's avatar
Bob Halley committed
148
	(void)isc_mutex_destroy(&task->lock);
Bob Halley's avatar
base  
Bob Halley committed
149
	task->magic = 0;
150
	isc_mem_put(task->mctx, task, sizeof *task);
Bob Halley's avatar
base  
Bob Halley committed
151 152
}

Bob Halley's avatar
Bob Halley committed
153
isc_result_t
Bob Halley's avatar
Bob Halley committed
154
isc_task_create(isc_taskmgr_t *manager, isc_mem_t *mctx, unsigned int quantum,
155
		isc_task_t **taskp)
Bob Halley's avatar
Bob Halley committed
156
{
Bob Halley's avatar
Bob Halley committed
157
	isc_task_t *task;
Bob Halley's avatar
base  
Bob Halley committed
158 159 160 161

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

Bob Halley's avatar
Bob Halley committed
162 163 164
	if (mctx == NULL)
		mctx = manager->mctx;
	task = isc_mem_get(mctx, sizeof *task);
Bob Halley's avatar
base  
Bob Halley committed
165
	if (task == NULL)
Bob Halley's avatar
Bob Halley committed
166
		return (ISC_R_NOMEMORY);
167
	XTRACE("create");
Bob Halley's avatar
base  
Bob Halley committed
168
	task->manager = manager;
Bob Halley's avatar
Bob Halley committed
169
	task->mctx = mctx;
Bob Halley's avatar
Bob Halley committed
170
	if (isc_mutex_init(&task->lock) != ISC_R_SUCCESS) {
Bob Halley's avatar
Bob Halley committed
171
		isc_mem_put(mctx, task, sizeof *task);
Bob Halley's avatar
Bob Halley committed
172 173 174
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_mutex_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
175
	}
Bob Halley's avatar
base  
Bob Halley committed
176 177 178
	task->state = task_state_idle;
	task->references = 1;
	INIT_LIST(task->events);
179
	INIT_LIST(task->on_shutdown);
Bob Halley's avatar
base  
Bob Halley committed
180
	task->quantum = quantum;
181
	task->flags = (TASK_F_DONEOK|TASK_F_SENDOK);
Bob Halley's avatar
base  
Bob Halley committed
182 183 184 185
	INIT_LINK(task, link);
	INIT_LINK(task, ready_link);

	LOCK(&manager->lock);
Bob Halley's avatar
Bob Halley committed
186
	/* XXX Should disallow if task manager is exiting. */
Bob Halley's avatar
base  
Bob Halley committed
187 188 189 190 191
	if (task->quantum == 0)
		task->quantum = manager->default_quantum;
	APPEND(manager->tasks, task, link);
	UNLOCK(&manager->lock);

192
	task->magic = TASK_MAGIC;
Bob Halley's avatar
base  
Bob Halley committed
193 194
	*taskp = task;

Bob Halley's avatar
Bob Halley committed
195
	return (ISC_R_SUCCESS);
Bob Halley's avatar
base  
Bob Halley committed
196 197
}

Bob Halley's avatar
update  
Bob Halley committed
198
void
199
isc_task_attach(isc_task_t *source, isc_task_t **targetp) {
Bob Halley's avatar
base  
Bob Halley committed
200

201 202 203
	/*
	 * Attach *targetp to source.
	 */
Bob Halley's avatar
base  
Bob Halley committed
204

205 206
	REQUIRE(VALID_TASK(source));
	REQUIRE(targetp != NULL && *targetp == NULL);
Bob Halley's avatar
base  
Bob Halley committed
207

208 209
	XTTRACE(source, "attach");

210 211 212 213 214
	LOCK(&source->lock);
	source->references++;
	UNLOCK(&source->lock);

	*targetp = source;
Bob Halley's avatar
base  
Bob Halley committed
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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
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);
}

static inline detach_result_t
task_detach(isc_task_t *task) {
	detach_result_t dresult = detach_result_ok;

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

	REQUIRE(task->references > 0);

	XTRACE("detach");

	task->references--;
	if (task->references == 0) {
		if (task->state == task_state_done)
			dresult = detach_result_finished;
		else if (task->state == task_state_idle) {
			INSIST(EMPTY(task->events));
			/*
			 * There are no references to this task, and no
			 * pending events.  We initiate shutdown, since
			 * otherwise this task would just sit around until
			 * the task manager was destroyed.
			 */
			if (task_shutdown(task))
				dresult = detach_result_wasidle;
		}
	}

	return (dresult);
}

Bob Halley's avatar
Bob Halley committed
302
void
Bob Halley's avatar
Bob Halley committed
303 304
isc_task_detach(isc_task_t **taskp) {
	isc_task_t *task;
305
	detach_result_t dresult;
Bob Halley's avatar
base  
Bob Halley committed
306

307 308 309 310
	/*
	 * Detach *taskp from its task.
	 */

Bob Halley's avatar
base  
Bob Halley committed
311 312 313 314
	REQUIRE(taskp != NULL);
	task = *taskp;
	REQUIRE(VALID_TASK(task));

315 316
	XTRACE("isc_task_detach");

Bob Halley's avatar
base  
Bob Halley committed
317
	LOCK(&task->lock);
318
	dresult = task_detach(task);
Bob Halley's avatar
base  
Bob Halley committed
319 320
	UNLOCK(&task->lock);

321 322 323 324
	if (dresult == detach_result_finished)
		task_finished(task);
	else if (dresult == detach_result_wasidle)
		task_ready(task);
Bob Halley's avatar
base  
Bob Halley committed
325 326 327 328

	*taskp = NULL;
}

Bob Halley's avatar
Bob Halley committed
329 330 331
isc_mem_t *
isc_task_mem(isc_task_t *task) {

332 333 334 335
	/*
	 * Get the task's memory context.
	 */

Bob Halley's avatar
Bob Halley committed
336 337 338 339 340
	REQUIRE(VALID_TASK(task));
	
	return (task->mctx);
}

341 342
static inline isc_result_t
task_send(isc_task_t *task, isc_event_t **eventp, isc_boolean_t *was_idlep) {
343
	isc_result_t result = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
344
	isc_event_t *event;
345
	
346
	/*
347
	 * Caller must be holding the task lock.
348 349
	 */

Bob Halley's avatar
update  
Bob Halley committed
350 351
	REQUIRE(eventp != NULL);
	event = *eventp;
Bob Halley's avatar
base  
Bob Halley committed
352
	REQUIRE(event != NULL);
Bob Halley's avatar
Bob Halley committed
353 354
	REQUIRE(event->sender != NULL);
	REQUIRE(event->type > 0);
Bob Halley's avatar
base  
Bob Halley committed
355

356 357
	XTRACE("task_send");

358
	/*
359 360
	 * Note: we require that task->state == task_state_done implies
	 * (task->flags & TASK_F_SENDOK) == 0.
361
	 */
362
	*was_idlep = ISC_FALSE;
363
	if ((task->flags & TASK_F_SENDOK) != 0) {
Bob Halley's avatar
base  
Bob Halley committed
364
		if (task->state == task_state_idle) {
365
			*was_idlep = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
366 367 368 369 370 371
			INSIST(EMPTY(task->events));
			task->state = task_state_ready;
		}
		INSIST(task->state == task_state_ready ||
		       task->state == task_state_running);
		ENQUEUE(task->events, event, link);
372
		*eventp = NULL;
373
	} else {
374 375
		if (task->state == task_state_done)
			result = ISC_R_TASKDONE;
376 377 378
		else
			result = ISC_R_TASKNOSEND;
	}
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	
	return (result);
}	


isc_result_t
isc_task_send(isc_task_t *task, isc_event_t **eventp) {
	isc_boolean_t was_idle;
	isc_result_t result;

	/*
	 * 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);
	result = task_send(task, eventp, &was_idle);
Bob Halley's avatar
base  
Bob Halley committed
404 405
	UNLOCK(&task->lock);

406
	if (result != ISC_R_SUCCESS)
407
		return (result);
Bob Halley's avatar
base  
Bob Halley committed
408 409 410 411 412

	if (was_idle) {
		/*
		 * We need to add this task to the ready queue.
		 *
413 414 415
		 * 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
416 417
		 *
		 * We've changed the state to ready, so no one else will
418 419 420
		 * 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,
421
		 * removed, or a shutdown is started in the interval
422 423
		 * 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
424
		 */
425
		task_ready(task);
Bob Halley's avatar
base  
Bob Halley committed
426 427
	}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_task_sendanddetach(isc_task_t **taskp, isc_event_t **eventp) {
	isc_boolean_t was_idle;
	isc_result_t result;
	isc_task_t *task;
	detach_result_t dresult = detach_result_ok;

	/*
	 * 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
448

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	LOCK(&task->lock);
	result = task_send(task, eventp, &was_idle);
	if (result == ISC_R_SUCCESS)
		dresult = task_detach(task);
	UNLOCK(&task->lock);

	if (result != ISC_R_SUCCESS)
		return (result);

	if (dresult == detach_result_finished) {
		/*
		 * If was_idle is true, then the task is ready with at least
		 * one event in the queue, and nothing will happen until
		 * we call task_ready().  In particular, the task cannot
		 * be executing or have entered the done state, so if
		 * dresult is detach_result_finished, was_idle must have been
		 * false.  We INSIST on it.
		 */
		INSIST(!was_idle);
		task_finished(task);
	} else {
		/*
		 * If was_idle, then dresult shouldn't be	
		 * detach_result_wasidle, since that would mean someone else
		 * changed the task's state from ready back to idle, which
		 * should never happen.  We INSIST on it.
		 */
		INSIST(!(was_idle && dresult == detach_result_wasidle));
		if (was_idle || dresult == detach_result_wasidle)
			task_ready(task);
	}

	*taskp = NULL;
482 483

	return (ISC_R_SUCCESS);
Bob Halley's avatar
base  
Bob Halley committed
484 485
}

486 487 488 489 490 491
#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)
492
{
Bob Halley's avatar
Bob Halley committed
493
	isc_event_t *event, *next_event;
494
	unsigned int count = 0;
495

Bob Halley's avatar
Bob Halley committed
496
	REQUIRE(VALID_TASK(task));
497
	REQUIRE(last >= first);
Bob Halley's avatar
Bob Halley committed
498

499
	XTRACE("dequeue_events");
Bob Halley's avatar
Bob Halley committed
500

Bob Halley's avatar
Bob Halley committed
501
	/*
502 503 504
	 * 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.
505
	 *
506
	 * sender == NULL means "any sender", and tag == NULL means "any tag".
Bob Halley's avatar
Bob Halley committed
507 508 509
	 */

	LOCK(&task->lock);
510 511

	for (event = HEAD(task->events); event != NULL; event = next_event) {
Bob Halley's avatar
Bob Halley committed
512
		next_event = NEXT(event, link);
Bob Halley's avatar
Bob Halley committed
513 514
		if (event->type >= first && event->type <= last &&
		    (sender == NULL || event->sender == sender) &&
515 516
		    (tag == NULL || event->tag == tag) &&
		    (!purging || PURGE_OK(event))) {
Bob Halley's avatar
Bob Halley committed
517
			DEQUEUE(task->events, event, link);
518 519
			ENQUEUE(*events, event, link);
			count++;
Bob Halley's avatar
Bob Halley committed
520 521
		}
	}
522

Bob Halley's avatar
Bob Halley committed
523 524
	UNLOCK(&task->lock);

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
	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
548
		next_event = NEXT(event, link);
Bob Halley's avatar
Bob Halley committed
549
		isc_event_free(&event);
Bob Halley's avatar
Bob Halley committed
550
	}
551

552 553 554 555 556
	/*
	 * Note that purging never changes the state of the task.
	 */

	return (count);
Bob Halley's avatar
Bob Halley committed
557 558
}

559
unsigned int
Bob Halley's avatar
Bob Halley committed
560
isc_task_purge(isc_task_t *task, void *sender, isc_eventtype_t type,
561
	       void *tag)
Bob Halley's avatar
Bob Halley committed
562
{
563 564 565 566
	/*
	 * Purge events from a task's event queue.
	 */

567 568
	XTRACE("isc_task_purge");

Bob Halley's avatar
Bob Halley committed
569
	return (isc_task_purgerange(task, sender, type, type, tag));
570 571
}

Bob Halley's avatar
Bob Halley committed
572 573 574 575 576 577
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.
578 579
	 *
	 * XXXRTH:  WARNING:  This method may be removed before beta.
Bob Halley's avatar
Bob Halley committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
	 */

	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);
599
		if (curr_event == event && PURGE_OK(event)) {
Bob Halley's avatar
Bob Halley committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613
			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);
}

614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
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));
}

643
isc_result_t
644
isc_task_allowsend(isc_task_t *task, isc_boolean_t allowed) {
645 646
	isc_result_t result = ISC_R_SUCCESS;

647 648
	/*
	 * Allow or disallow sending events to 'task'.
649 650
	 *
	 * XXXRTH:  WARNING:  This method may be removed before beta.
651 652
	 */

653 654 655
	REQUIRE(VALID_TASK(task));

	LOCK(&task->lock);
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
	if (task->state == task_state_done)
		result = ISC_R_TASKDONE;
	else {
		if (allowed)
			task->flags |= TASK_F_SENDOK;
		else
			task->flags &= ~TASK_F_SENDOK;
	}
	UNLOCK(&task->lock);

	return (result);
}

isc_result_t
isc_task_allowdone(isc_task_t *task, isc_boolean_t allowed) {
	isc_result_t result = ISC_R_SUCCESS;
672
	isc_boolean_t was_idle = ISC_FALSE;
673

674 675 676 677
	/*
	 * Allow or disallow automatic termination of 'task'.
	 */

678 679 680 681 682
	REQUIRE(VALID_TASK(task));

	LOCK(&task->lock);
	if (task->state == task_state_done)
		result = ISC_R_TASKDONE;
683 684
	else {
		if (allowed) {
685
			task->flags |= TASK_F_DONEOK;
686 687 688 689 690 691 692 693 694 695 696 697
			/*
			 * To simply things, transition to the done state
			 * only occurs after running the task, so we do not
			 * attempt to go directly to the done state here.
			 */
			if (TASK_WANTDONE(task) &&
			    task->state == task_state_idle) {
				INSIST(EMPTY(task->events));
				task->state = task_state_ready;
				was_idle = ISC_TRUE;
			}
		} else
698 699
			task->flags &= ~TASK_F_DONEOK;
	}
700 701
	UNLOCK(&task->lock);

702 703 704
	if (was_idle)
		task_ready(task);

705 706 707 708 709 710 711 712 713
	return (result);
}

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;

714 715 716 717 718
	/*
	 * Send a shutdown event with action 'action' and argument 'arg' when
	 * 'task' is shutdown.
	 */

719
	REQUIRE(VALID_TASK(task));
720
	REQUIRE(action != NULL);
721

722 723 724 725 726 727
	event = isc_event_allocate(task->mctx,
				   NULL,
				   ISC_TASKEVENT_SHUTDOWN,
				   action,
				   arg,
				   sizeof *event);
728 729 730 731
	if (event == NULL)
		return (ISC_R_NOMEMORY);

	LOCK(&task->lock);
732
	if (task->state == task_state_done) {
733
		disallowed = ISC_TRUE;
734
		result = ISC_R_TASKDONE;
735
	} else if (TASK_SHUTTINGDOWN(task)) {
736
		disallowed = ISC_TRUE;
737
		result = ISC_R_SHUTTINGDOWN;
738 739 740 741 742
	} else
		ENQUEUE(task->on_shutdown, event, link);
	UNLOCK(&task->lock);

	if (disallowed)
Bob Halley's avatar
Bob Halley committed
743
		isc_mem_put(task->mctx, event, sizeof *event);
744 745 746 747

	return (result);
}

Bob Halley's avatar
Bob Halley committed
748
void
Bob Halley's avatar
Bob Halley committed
749
isc_task_shutdown(isc_task_t *task) {
750
	isc_boolean_t was_idle;
Bob Halley's avatar
base  
Bob Halley committed
751

752 753 754 755
	/*
	 * Shutdown 'task'.
	 */

Bob Halley's avatar
base  
Bob Halley committed
756 757 758
	REQUIRE(VALID_TASK(task));

	LOCK(&task->lock);
759
	was_idle = task_shutdown(task);
Bob Halley's avatar
base  
Bob Halley committed
760 761
	UNLOCK(&task->lock);

762 763
	if (was_idle)
		task_ready(task);
Bob Halley's avatar
Bob Halley committed
764
}
Bob Halley's avatar
base  
Bob Halley committed
765

Bob Halley's avatar
Bob Halley committed
766
void
Bob Halley's avatar
Bob Halley committed
767
isc_task_destroy(isc_task_t **taskp) {
Bob Halley's avatar
Bob Halley committed
768

769 770 771 772
	/*
	 * Destroy '*taskp'.
	 */

Bob Halley's avatar
Bob Halley committed
773 774
	REQUIRE(taskp != NULL);

Bob Halley's avatar
Bob Halley committed
775 776
	isc_task_shutdown(*taskp);
	isc_task_detach(taskp);
Bob Halley's avatar
base  
Bob Halley committed
777 778 779
}


Bob Halley's avatar
Bob Halley committed
780

Bob Halley's avatar
base  
Bob Halley committed
781 782 783 784
/***
 *** Task Manager.
 ***/

Bob Halley's avatar
update  
Bob Halley committed
785
static isc_threadresult_t
Bob Halley's avatar
Bob Halley committed
786 787 788
#ifdef _WIN32
WINAPI
#endif
Bob Halley's avatar
update  
Bob Halley committed
789
run(void *uap) {
Bob Halley's avatar
Bob Halley committed
790 791
	isc_taskmgr_t *manager = uap;
	isc_task_t *task;
Bob Halley's avatar
base  
Bob Halley committed
792

793
	XTHREADTRACE("start");
Bob Halley's avatar
base  
Bob Halley committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850

	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
851
		 * isc_task_send() above, it is safe for us to dequeue
Bob Halley's avatar
base  
Bob Halley committed
852 853 854 855 856
		 * 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)) {
857
			XTHREADTRACE("wait");
Bob Halley's avatar
base  
Bob Halley committed
858
			WAIT(&manager->work_available, &manager->lock);
859
			XTHREADTRACE("awake");
Bob Halley's avatar
base  
Bob Halley committed
860
		}
861
		XTHREADTRACE("working");
Bob Halley's avatar
base  
Bob Halley committed
862 863 864
		
		task = HEAD(manager->ready_tasks);
		if (task != NULL) {
Bob Halley's avatar
Bob Halley committed
865
			unsigned int dispatch_count = 0;
Bob Halley's avatar
Bob Halley committed
866 867
			isc_boolean_t done = ISC_FALSE;
			isc_boolean_t requeue = ISC_FALSE;
868
			isc_boolean_t finished = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
869
			isc_event_t *event;
Bob Halley's avatar
base  
Bob Halley committed
870 871 872 873 874 875 876 877 878 879 880 881

			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
882
			INSIST(task->state == task_state_ready);
883 884 885 886 887 888
			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
889

890 891 892 893 894 895 896 897 898 899
					/*
					 * Execute the event action.
					 */
					XTRACE("execute action");
					if (event->action != NULL) {
						UNLOCK(&task->lock);
						(event->action)(task, event);
						LOCK(&task->lock);
					}
					dispatch_count++;
900
				}
Bob Halley's avatar
update  
Bob Halley committed
901
				
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
				if (task->references == 0 &&
				    EMPTY(task->events)) {
					if (! TASK_SHUTTINGDOWN(task)) {
						isc_boolean_t was_idle;

						was_idle = task_shutdown(task);
						INSIST(!was_idle);
					} else {
						/*
						 * We force the DONEOK flag
						 * to true so this task does
						 * not become a zombie.
						 */
						task->flags |= TASK_F_DONEOK;
					}
				}

919
				if (EMPTY(task->events)) {
Bob Halley's avatar
Bob Halley committed
920
					/*
921 922 923 924
					 * Nothing else to do for this task
					 * right now.  If it is shutting down,
					 * then it is done, otherwise we just
					 * put it to sleep.
Bob Halley's avatar
Bob Halley committed
925
					 */
Bob Halley's avatar
Bob Halley committed
926
					XTRACE("empty");
927
					if (TASK_WANTDONE(task)) {
928
						XTRACE("done");
929
						if (task->references == 0)
930
							finished = ISC_TRUE;
931 932 933
						task->flags &=
							~TASK_F_SENDOK;
						task->state = task_state_done;
934 935
					} else
						task->state = task_state_idle;
Bob Halley's avatar
Bob Halley committed
936
					done = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
937 938 939 940 941 942 943 944 945 946 947
				} 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
948
					XTRACE("quantum");
Bob Halley's avatar
base  
Bob Halley committed
949
					task->state = task_state_ready;
Bob Halley's avatar
Bob Halley committed
950 951
					requeue = ISC_TRUE;
					done = ISC_TRUE;
Bob Halley's avatar
base  
Bob Halley committed
952
				}
953
			} while (!done);
Bob Halley's avatar
base  
Bob Halley committed
954 955
			UNLOCK(&task->lock);

956 957
			if (finished)
				task_finished(task);
Bob Halley's avatar
base  
Bob Halley committed
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

			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);

987
	XTHREADTRACE("exit");
Bob Halley's avatar
base  
Bob Halley committed
988

Bob Halley's avatar
update  
Bob Halley committed
989
	return ((isc_threadresult_t)0);
Bob Halley's avatar
base  
Bob Halley committed
990 991 992
}

static void
Bob Halley's avatar
Bob Halley committed
993
manager_free(isc_taskmgr_t *manager) {
Bob Halley's avatar
Bob Halley committed
994 995
	(void)isc_condition_destroy(&manager->work_available);
	(void)isc_mutex_destroy(&manager->lock);
996 997
	isc_mem_put(manager->mctx, manager->threads,
		    manager->workers * sizeof (isc_thread_t));
Bob Halley's avatar
base  
Bob Halley committed
998
	manager->magic = 0;
Bob Halley's avatar
Bob Halley committed
999
	isc_mem_put(manager->mctx, manager, sizeof *manager);
Bob Halley's avatar
base  
Bob Halley committed
1000 1001
}

Bob Halley's avatar
Bob Halley committed
1002
isc_result_t
Bob Halley's avatar
Bob Halley committed
1003
isc_taskmgr_create(isc_mem_t *mctx, unsigned int workers, 
Bob Halley's avatar
Bob Halley committed
1004
		   unsigned int default_quantum, isc_taskmgr_t **managerp)
Bob Halley's avatar
Bob Halley committed
1005 1006
{
	unsigned int i, started = 0;
Bob Halley's avatar
Bob Halley committed
1007
	isc_taskmgr_t *manager;
1008
	isc_thread_t *threads;
Bob Halley's avatar
Bob Halley committed
1009

1010 1011 1012 1013
	/*
	 * Create a new task manager.
	 */

Bob Halley's avatar
Bob Halley committed
1014
	REQUIRE(workers > 0);
Bob Halley's avatar
Bob Halley committed
1015
	REQUIRE(managerp != NULL && *managerp == NULL);
Bob Halley's avatar
base  
Bob Halley committed
1016

Bob Halley's avatar
Bob Halley committed
1017
	manager = isc_mem_get(mctx, sizeof *manager);
Bob Halley's avatar
base  
Bob Halley committed
1018
	if (manager == NULL)
Bob Halley's avatar
Bob Halley committed
1019
		return (ISC_R_NOMEMORY);
Bob Halley's avatar
base  
Bob Halley committed
1020 1021
	manager->magic = TASK_MANAGER_MAGIC;
	manager->mctx = mctx;
1022 1023 1024 1025 1026 1027 1028
	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
1029
	if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) {
1030
		isc_mem_put(mctx, threads, workers * sizeof (isc_thread_t));
Bob Halley's avatar
Bob Halley committed
1031
		isc_mem_put(mctx, manager, sizeof *manager);
Bob Halley's avatar
Bob Halley committed
1032 1033 1034
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_mutex_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
1035
	}
Bob Halley's avatar
base  
Bob Halley committed
1036 1037 1038 1039 1040
	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
1041 1042
	if (isc_condition_init(&manager->work_available) != ISC_R_SUCCESS) {
		(void)isc_mutex_destroy(&manager->lock);
1043
		isc_mem_put(mctx, threads, workers * sizeof (isc_thread_t));
Bob Halley's avatar
Bob Halley committed
1044
		isc_mem_put(mctx, manager, sizeof *manager);
Bob Halley's avatar
Bob Halley committed
1045 1046 1047
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_condition_init() failed");
		return (ISC_R_UNEXPECTED);
Bob Halley's avatar
update  
Bob Halley committed
1048
	}
Bob Halley's avatar
Bob Halley committed
1049
	manager->exiting = ISC_FALSE;
Bob Halley's avatar
base  
Bob Halley committed
1050 1051 1052 1053 1054 1055 1056
	manager->workers = 0;

	LOCK(&manager->lock);
	/*
	 * Start workers.
	 */
	for (i = 0; i < workers; i++) {
1057 1058
		if (isc_thread_create(run, manager,
				      &manager->threads[manager->workers]) == 
Bob Halley's avatar
Bob Halley committed
1059
		    ISC_R_SUCCESS) {
Bob Halley's avatar
base  
Bob Halley committed
1060 1061 1062 1063 1064 1065 1066 1067
			manager->workers++;
			started++;
		}
	}
	UNLOCK(&manager->lock);

	if (started == 0) {
		manager_free(manager);
Bob Halley's avatar
Bob Halley committed
1068
		return (ISC_R_NOTHREADS);
Bob Halley's avatar
base  
Bob Halley committed
1069 1070 1071 1072
	}		

	*managerp = manager;

Bob Halley's avatar
Bob Halley committed
1073
	return (ISC_R_SUCCESS);
Bob Halley's avatar
base  
Bob Halley committed
1074 1075
}

Bob Halley's avatar
update  
Bob Halley committed
1076
void
Bob Halley's avatar
Bob Halley committed
1077 1078 1079
isc_taskmgr_destroy(isc_taskmgr_t **managerp) {
	isc_taskmgr_t *manager;
	isc_task_t *task;
1080
	unsigned int i;
Bob Halley's avatar
base  
Bob Halley committed
1081

1082 1083 1084 1085
	/*
	 * Destroy '*managerp'.
	 */

Bob Halley's avatar
base  
Bob Halley committed
1086 1087 1088 1089
	REQUIRE(managerp != NULL);
	manager = *managerp;
	REQUIRE(VALID_MANAGER(manager));

1090
	XTHREADTRACE("isc_taskmgr_destroy");
Bob Halley's avatar
base  
Bob Halley committed
1091 1092 1093 1094
	/*
	 * 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
1095
	 * isc_taskmgr_destroy(), e.g. by signalling a condition variable
Bob Halley's avatar
base  
Bob Halley committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
	 * that the startup thread is sleeping on.
	 */