task_test.c 32.5 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10
11
12
13
14
15
16
17
 */

/*! \file */

#include <config.h>

#include <atf-c.h>

18
19
20
21
#ifdef HAVE_INTTYPES_H
#include <inttypes.h> 	/* uintptr_t */
#endif
#include <stdlib.h>
22
23
#include <unistd.h>

24
25
26
#include <isc/condition.h>
#include <isc/mem.h>
#include <isc/platform.h>
Evan Hunt's avatar
Evan Hunt committed
27
#include <isc/print.h>
28
#include <isc/task.h>
29
30
#include <isc/time.h>
#include <isc/timer.h>
31
32
33
34
35
36
37
38
#include <isc/util.h>

#include "isctest.h"

/*
 * Helper functions
 */

39
static isc_mutex_t lock;
40
int counter = 0;
41
42
static int active[10];
static isc_boolean_t done = ISC_FALSE;
43

44
45
46
47
#ifdef ISC_PLATFORM_USETHREADS
static isc_condition_t cv;
#endif

48
49
50
51
52
53
54
static void
set(isc_task_t *task, isc_event_t *event) {
	int *value = (int *) event->ev_arg;

	UNUSED(task);

	isc_event_free(&event);
55
	LOCK(&lock);
56
	*value = counter++;
57
	UNLOCK(&lock);
58
59
60
61
62
63
64
65
66
}

static void
set_and_drop(isc_task_t *task, isc_event_t *event) {
	int *value = (int *) event->ev_arg;

	UNUSED(task);

	isc_event_free(&event);
67
	LOCK(&lock);
68
69
	*value = (int) isc_taskmgr_mode(taskmgr);
	counter++;
70
	UNLOCK(&lock);
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_normal);
}

/*
 * Individual unit tests
 */

/* Create a task */
ATF_TC(create_task);
ATF_TC_HEAD(create_task, tc) {
	atf_tc_set_md_var(tc, "descr", "create and destroy a task");
}
ATF_TC_BODY(create_task, tc) {
	isc_result_t result;
	isc_task_t *task = NULL;

	UNUSED(tc);

89
	result = isc_test_begin(NULL, ISC_TRUE, 0);
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	isc_task_destroy(&task);
	ATF_REQUIRE_EQ(task, NULL);

	isc_test_end();
}

/* Process events */
ATF_TC(all_events);
ATF_TC_HEAD(all_events, tc) {
	atf_tc_set_md_var(tc, "descr", "process task events");
}
ATF_TC_BODY(all_events, tc) {
	isc_result_t result;
	isc_task_t *task = NULL;
109
	isc_event_t *event = NULL;
110
111
112
113
114
115
116
	int a = 0, b = 0;
	int i = 0;

	UNUSED(tc);

	counter = 1;

117
	result = isc_mutex_init(&lock);
Mark Andrews's avatar
Mark Andrews committed
118
119
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

120
	result = isc_test_begin(NULL, ISC_TRUE, 0);
121
122
123
124
125
126
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	/* First event */
Automatic Updater's avatar
Automatic Updater committed
127
	event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
128
129
130
131
132
133
				   set, &a, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(a, 0);
	isc_task_send(task, &event);

Automatic Updater's avatar
Automatic Updater committed
134
	event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
135
136
137
138
139
140
				   set, &b, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(b, 0);
	isc_task_send(task, &event);

141
	while ((a == 0 || b == 0) && i++ < 5000) {
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef ISC_PLATFORM_USETHREADS
		while (isc__taskmgr_ready(taskmgr))
			isc__taskmgr_dispatch(taskmgr);
#endif
		isc_test_nap(1000);
	}

	ATF_CHECK(a != 0);
	ATF_CHECK(b != 0);

	isc_task_destroy(&task);
	ATF_REQUIRE_EQ(task, NULL);

	isc_test_end();
}

/* Privileged events */
ATF_TC(privileged_events);
ATF_TC_HEAD(privileged_events, tc) {
	atf_tc_set_md_var(tc, "descr", "process privileged events");
}
ATF_TC_BODY(privileged_events, tc) {
	isc_result_t result;
	isc_task_t *task1 = NULL, *task2 = NULL;
166
	isc_event_t *event = NULL;
167
168
169
170
171
172
	int a = 0, b = 0, c = 0, d = 0, e = 0;
	int i = 0;

	UNUSED(tc);

	counter = 1;
173
	result = isc_mutex_init(&lock);
174
175
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

176
	result = isc_test_begin(NULL, ISC_TRUE, 0);
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

#ifdef ISC_PLATFORM_USETHREADS
	/*
	 * Pause the task manager so we can fill up the work queue
	 * without things happening while we do it.
	 */
	isc__taskmgr_pause(taskmgr);
#endif

	result = isc_task_create(taskmgr, 0, &task1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_task_setname(task1, "privileged", NULL);
	ATF_CHECK(!isc_task_privilege(task1));
	isc_task_setprivilege(task1, ISC_TRUE);
	ATF_CHECK(isc_task_privilege(task1));

	result = isc_task_create(taskmgr, 0, &task2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_task_setname(task2, "normal", NULL);
	ATF_CHECK(!isc_task_privilege(task2));

	/* First event: privileged */
Automatic Updater's avatar
Automatic Updater committed
200
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
201
202
203
204
205
206
207
				   set, &a, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(a, 0);
	isc_task_send(task1, &event);

	/* Second event: not privileged */
Automatic Updater's avatar
Automatic Updater committed
208
	event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
209
210
211
212
213
214
215
				   set, &b, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(b, 0);
	isc_task_send(task2, &event);

	/* Third event: privileged */
Automatic Updater's avatar
Automatic Updater committed
216
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
217
218
219
220
221
222
223
				   set, &c, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(c, 0);
	isc_task_send(task1, &event);

	/* Fourth event: privileged */
Automatic Updater's avatar
Automatic Updater committed
224
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
225
226
227
228
229
230
231
				   set, &d, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(d, 0);
	isc_task_send(task1, &event);

	/* Fifth event: not privileged */
Automatic Updater's avatar
Automatic Updater committed
232
	event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
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
				   set, &e, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

	ATF_CHECK_EQ(e, 0);
	isc_task_send(task2, &event);

	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
	isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);

#ifdef ISC_PLATFORM_USETHREADS
	isc__taskmgr_resume(taskmgr);
#endif

	/* We're waiting for *all* variables to be set */
	while ((a == 0 || b == 0 || c == 0 || d == 0 || e == 0) && i++ < 5000) {
#ifndef ISC_PLATFORM_USETHREADS
		while (isc__taskmgr_ready(taskmgr))
			isc__taskmgr_dispatch(taskmgr);
#endif
		isc_test_nap(1000);
	}

	/*
	 * We can't guarantee what order the events fire, but
	 * we do know the privileged tasks that set a, c, and d
	 * would have fired first.
	 */
	ATF_CHECK(a <= 3);
	ATF_CHECK(c <= 3);
	ATF_CHECK(d <= 3);

	/* ...and the non-privileged tasks that set b and e, last */
	ATF_CHECK(b >= 4);
	ATF_CHECK(e >= 4);

	ATF_CHECK_EQ(counter, 6);

	isc_task_setprivilege(task1, ISC_FALSE);
	ATF_CHECK(!isc_task_privilege(task1));

	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);

	isc_task_destroy(&task1);
	ATF_REQUIRE_EQ(task1, NULL);
	isc_task_destroy(&task2);
	ATF_REQUIRE_EQ(task2, NULL);

	isc_test_end();
}

/*
 * Edge case: this tests that the task manager behaves as expected when
 * we explicitly set it into normal mode *while* running privileged.
 */
ATF_TC(privilege_drop);
ATF_TC_HEAD(privilege_drop, tc) {
	atf_tc_set_md_var(tc, "descr", "process privileged events");
}
ATF_TC_BODY(privilege_drop, tc) {
	isc_result_t result;
	isc_task_t *task1 = NULL, *task2 = NULL;
295
	isc_event_t *event = NULL;
296
	int a = -1, b = -1, c = -1, d = -1, e = -1;	/* non valid states */
297
298
299
300
301
	int i = 0;

	UNUSED(tc);

	counter = 1;
302
	result = isc_mutex_init(&lock);
303
304
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

305
	result = isc_test_begin(NULL, ISC_TRUE, 0);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

#ifdef ISC_PLATFORM_USETHREADS
	/*
	 * Pause the task manager so we can fill up the work queue
	 * without things happening while we do it.
	 */
	isc__taskmgr_pause(taskmgr);
#endif

	result = isc_task_create(taskmgr, 0, &task1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_task_setname(task1, "privileged", NULL);
	ATF_CHECK(!isc_task_privilege(task1));
	isc_task_setprivilege(task1, ISC_TRUE);
	ATF_CHECK(isc_task_privilege(task1));

	result = isc_task_create(taskmgr, 0, &task2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	isc_task_setname(task2, "normal", NULL);
	ATF_CHECK(!isc_task_privilege(task2));

	/* First event: privileged */
Automatic Updater's avatar
Automatic Updater committed
329
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
330
331
332
				   set_and_drop, &a, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

333
	ATF_CHECK_EQ(a, -1);
334
335
336
	isc_task_send(task1, &event);

	/* Second event: not privileged */
Automatic Updater's avatar
Automatic Updater committed
337
	event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
338
339
340
				   set_and_drop, &b, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

341
	ATF_CHECK_EQ(b, -1);
342
343
344
	isc_task_send(task2, &event);

	/* Third event: privileged */
Automatic Updater's avatar
Automatic Updater committed
345
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
346
347
348
				   set_and_drop, &c, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

349
	ATF_CHECK_EQ(c, -1);
350
351
352
	isc_task_send(task1, &event);

	/* Fourth event: privileged */
Automatic Updater's avatar
Automatic Updater committed
353
	event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
354
355
356
				   set_and_drop, &d, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

357
	ATF_CHECK_EQ(d, -1);
358
359
360
	isc_task_send(task1, &event);

	/* Fifth event: not privileged */
Automatic Updater's avatar
Automatic Updater committed
361
	event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
362
363
364
				   set_and_drop, &e, sizeof (isc_event_t));
	ATF_REQUIRE(event != NULL);

365
	ATF_CHECK_EQ(e, -1);
366
367
368
369
370
371
372
373
374
375
	isc_task_send(task2, &event);

	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
	isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);

#ifdef ISC_PLATFORM_USETHREADS
	isc__taskmgr_resume(taskmgr);
#endif

376
377
378
	/* We're waiting for all variables to be set. */
	while ((a == -1 || b == -1 || c == -1 || d == -1 || e == -1) &&
	       i++ < 5000) {
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
404
405
406
407
408
409
410
#ifndef ISC_PLATFORM_USETHREADS
		while (isc__taskmgr_ready(taskmgr))
			isc__taskmgr_dispatch(taskmgr);
#endif
		isc_test_nap(1000);
	}

	/*
	 * We can't guarantee what order the events fire, but
	 * we do know *exactly one* of the privileged tasks will
	 * have run in privileged mode...
	 */
	ATF_CHECK(a == isc_taskmgrmode_privileged ||
		  c == isc_taskmgrmode_privileged ||
		  d == isc_taskmgrmode_privileged);
	ATF_CHECK(a + c + d == isc_taskmgrmode_privileged);

	/* ...and neither of the non-privileged tasks did... */
	ATF_CHECK(b == isc_taskmgrmode_normal || e == isc_taskmgrmode_normal);

	/* ...but all five of them did run. */
	ATF_CHECK_EQ(counter, 6);

	ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);

	isc_task_destroy(&task1);
	ATF_REQUIRE_EQ(task1, NULL);
	isc_task_destroy(&task2);
	ATF_REQUIRE_EQ(task2, NULL);

	isc_test_end();
}
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
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
643
644
645
646
647
648
/*
 * Basic task functions:
 */
static void
basic_cb(isc_task_t *task, isc_event_t *event) {
	int i;
	int j;

	UNUSED(task);

	j = 0;
	for (i = 0; i < 1000000; i++) {
		j += 100;
	}

	printf("task %s\n", (char *)event->ev_arg);
	isc_event_free(&event);
}

static void
basic_shutdown(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	printf("shutdown %s\n", (char *)event->ev_arg);
	isc_event_free(&event);
}

static void
basic_tick(isc_task_t *task, isc_event_t *event) {

	UNUSED(task);

	printf("%s\n", (char *)event->ev_arg);
	isc_event_free(&event);
}

static char one[] = "1";
static char two[] = "2";
static char three[] = "3";
static char four[] = "4";
static char tick[] = "tick";
static char tock[] = "tock";


ATF_TC(basic);
ATF_TC_HEAD(basic, tc) {
	atf_tc_set_md_var(tc, "descr", "basic task system check");
}
ATF_TC_BODY(basic, tc) {
	isc_result_t result;
	isc_task_t *task1 = NULL;
	isc_task_t *task2 = NULL;
	isc_task_t *task3 = NULL;
	isc_task_t *task4 = NULL;
	isc_event_t *event = NULL;
	isc_timer_t *ti1 = NULL;
	isc_timer_t *ti2 = NULL;
	isc_time_t absolute;
	isc_interval_t interval;
	char *testarray[] = {
		one, one, one, one, one, one, one, one, one,
		two, three, four, two, three, four, NULL
	};
	int i;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE, 2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_create(taskmgr, 0, &task1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_create(taskmgr, 0, &task2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_create(taskmgr, 0, &task3);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_create(taskmgr, 0, &task4);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_onshutdown(task1, basic_shutdown, one);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_onshutdown(task2, basic_shutdown, two);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_onshutdown(task3, basic_shutdown, three);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
	result = isc_task_onshutdown(task4, basic_shutdown, four);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	isc_time_settoepoch(&absolute);
	isc_interval_set(&interval, 1, 0);
	result = isc_timer_create(timermgr, isc_timertype_ticker,
				&absolute, &interval,
				task1, basic_tick, tick, &ti1);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	ti2 = NULL;
	isc_time_settoepoch(&absolute);
	isc_interval_set(&interval, 1, 0);
	result = isc_timer_create(timermgr, isc_timertype_ticker,
				  &absolute, &interval,
				  task2, basic_tick, tock, &ti2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

#ifndef WIN32
	sleep(2);
#else
	Sleep(2000);
#endif

	for (i = 0; testarray[i] != NULL; i++) {
		/*
		 * Note:  (void *)1 is used as a sender here, since some
		 * compilers don't like casting a function pointer to a
		 * (void *).
		 *
		 * In a real use, it is more likely the sender would be a
		 * structure (socket, timer, task, etc) but this is just a
		 * test program.
		 */
		event = isc_event_allocate(mctx, (void *)1, 1, basic_cb,
					   testarray[i], sizeof(*event));
		ATF_REQUIRE(event != NULL);
		isc_task_send(task1, &event);
	}

	(void)isc_task_purge(task3, NULL, 0, 0);

	isc_task_detach(&task1);
	isc_task_detach(&task2);
	isc_task_detach(&task3);
	isc_task_detach(&task4);

#ifndef WIN32
	sleep(10);
#else
	Sleep(10000);
#endif
	isc_timer_detach(&ti1);
	isc_timer_detach(&ti2);

	isc_test_end();
}

/*
 * Exclusive mode test:
 * When one task enters exclusive mode, all other active
 * tasks complete first.
 */
static
int spin(int n) {
	int i;
	int r = 0;
	for (i = 0; i < n; i++) {
		r += i;
		if (r > 1000000)
			r = 0;
	}
	return (r);
}

static void
exclusive_cb(isc_task_t *task, isc_event_t *event) {
	int taskno = *(int *)(event->ev_arg);

	printf("task enter %d\n", taskno);

	/* task chosen from the middle of the range */
	if (taskno == 6) {
		isc_result_t result;
		int i;

		result = isc_task_beginexclusive(task);
		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

		for (i = 0; i < 10; i++) {
			ATF_CHECK(active[i] == 0);
		}

		isc_task_endexclusive(task);
		done = ISC_TRUE;
	} else {
		active[taskno]++;
		(void) spin(10000000);
		active[taskno]--;
	}

	printf("task exit %d\n", taskno);

	if (done) {
		isc_mem_put(event->ev_destroy_arg, event->ev_arg, sizeof (int));
		isc_event_free(&event);
	} else {
		isc_task_send(task, &event);
	}
}

ATF_TC(task_exclusive);
ATF_TC_HEAD(task_exclusive, tc) {
	atf_tc_set_md_var(tc, "descr", "test exclusive mode");
}
ATF_TC_BODY(task_exclusive, tc) {
	isc_task_t *tasks[10];
	isc_result_t result;
	int i;

	UNUSED(tc);

	result = isc_test_begin(NULL, ISC_TRUE, 4);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	for (i = 0; i < 10; i++) {
		isc_event_t *event = NULL;
		int *v;

		tasks[i] = NULL;

		result = isc_task_create(taskmgr, 0, &tasks[i]);
		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

		v = isc_mem_get(mctx, sizeof *v);
		ATF_REQUIRE(v != NULL);

		*v = i;

		event = isc_event_allocate(mctx, NULL, 1, exclusive_cb,
					   v, sizeof(*event));
		ATF_REQUIRE(event != NULL);

		isc_task_send(tasks[i], &event);
	}

	for (i = 0; i < 10; i++) {
		isc_task_detach(&tasks[i]);
	}
	isc_test_end();
}

649
650
651
652
/*
 * The remainder of these tests require threads
 */
#ifdef ISC_PLATFORM_USETHREADS
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
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
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
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
 * Max tasks test:
 * The task system can create and execute many tasks. Tests with 10000.
 */
static void
maxtask_shutdown(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	if (event->ev_arg != NULL) {
		isc_task_destroy((isc_task_t**) &event->ev_arg);
	} else {
		LOCK(&lock);
		done = ISC_TRUE;
		SIGNAL(&cv);
		UNLOCK(&lock);

		isc_event_free(&event);
		isc_taskmgr_destroy(&taskmgr);
		isc_mem_destroy(&mctx);

		isc_condition_destroy(&cv);
		DESTROYLOCK(&lock);
	}
}

static void
maxtask_cb(isc_task_t *task, isc_event_t *event) {
	isc_result_t result;

	if (event->ev_arg != NULL) {
		isc_task_t *newtask = NULL;

		event->ev_arg = (void *)(((uintptr_t) event->ev_arg) - 1);

		/*
		 * Create a new task and forward the message.
		 */
		result = isc_task_create(taskmgr, 0, &newtask);
		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

		result = isc_task_onshutdown(newtask, maxtask_shutdown,
						 (void *)task);
		ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

		isc_task_send(newtask, &event);
	} else if (task != NULL) {
		isc_task_destroy(&task);
	}
}

ATF_TC(manytasks);
ATF_TC_HEAD(manytasks, tc) {
	atf_tc_set_md_var(tc, "descr", "many tasks");
}
ATF_TC_BODY(manytasks, tc) {
	isc_result_t result;
	isc_event_t *event = NULL;
	uintptr_t ntasks = 10000;

	UNUSED(tc);

	printf("Testing with %lu tasks\n", (unsigned long)ntasks);

	result = isc_mutex_init(&lock);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_condition_init(&cv);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mem_create(0, 0, &mctx);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_taskmgr_create(mctx, 4, 0, &taskmgr);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	event = isc_event_allocate(mctx, (void *)1, 1, maxtask_cb,
				   (void *)ntasks, sizeof(*event));
	ATF_REQUIRE(event != NULL);

	LOCK(&lock);
	maxtask_cb(NULL, event);
	while (!done) {
		WAIT(&cv, &lock);
	}
}


/*
 * Shutdown test:
 * When isc_task_shutdown() is called, shutdown events are posted
 * in LIFO order.
 */

static int senders[4];
static int nevents = 0;
static int nsdevents = 0;

static void
sd_sde1(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	ATF_CHECK_EQ(nevents, 256);
	ATF_REQUIRE_EQ(nsdevents, 1);
	++nsdevents;
	printf("shutdown 1\n");

	isc_event_free(&event);
}

static void
sd_sde2(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	ATF_CHECK_EQ(nevents, 256);
	ATF_REQUIRE_EQ(nsdevents, 0);
	++nsdevents;
	printf("shutdown 2\n");

	isc_event_free(&event);
}

static void
sd_event1(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	LOCK(&lock);
	while (!done) {
		WAIT(&cv, &lock);
	}

	printf("event 1\n");

	isc_event_free(&event);
}

static void
sd_event2(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	++nevents;

	printf("event 2\n");

	isc_event_free(&event);
}

ATF_TC(shutdown);
ATF_TC_HEAD(shutdown, tc) {
	atf_tc_set_md_var(tc, "descr", "task shutdown");
}
ATF_TC_BODY(shutdown, tc) {
	isc_result_t result;
	isc_eventtype_t event_type;
	isc_event_t *event = NULL;
	isc_task_t *task = NULL;
	int i;

	nevents = nsdevents = 0;
	done = ISC_FALSE;

	event_type = 3;

	result = isc_test_begin(NULL, ISC_TRUE, 4);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_mutex_init(&lock);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_condition_init(&cv);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	LOCK(&lock);

	task = NULL;
	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	/*
	 * This event causes the task to wait on cv.
	 */
	event = isc_event_allocate(mctx, &senders[1], event_type, sd_event1,
				   NULL, sizeof(*event));
	ATF_REQUIRE(event != NULL);
	isc_task_send(task, &event);

	/*
	 * Now we fill up the task's event queue with some events.
	 */
	for (i = 0; i < 256; ++i) {
		event = isc_event_allocate(mctx, &senders[1], event_type,
					   sd_event2, NULL, sizeof(*event));
		ATF_REQUIRE(event != NULL);
		isc_task_send(task, &event);
	}

	/*
	 * Now we register two shutdown events.
	 */
	result = isc_task_onshutdown(task, sd_sde1, NULL);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_task_onshutdown(task, sd_sde2, NULL);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	isc_task_shutdown(task);

	/*
	 * Now we free the task by signaling cv.
	 */
	done = ISC_TRUE;
	SIGNAL(&cv);
	UNLOCK(&lock);

	isc_task_detach(&task);

	isc_test_end();

	ATF_REQUIRE_EQ(nsdevents, 2);
}

/*
 * Post-shutdown test:
 * After isc_task_shutdown() has been called, any call to
 * isc_task_onshutdown() will return ISC_R_SHUTTINGDOWN.
 */
static void
psd_event1(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	LOCK(&lock);

	while (!done) {
		WAIT(&cv, &lock);
	}

	UNLOCK(&lock);

	isc_event_free(&event);
}

static void
psd_sde(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	isc_event_free(&event);
}

ATF_TC(post_shutdown);
ATF_TC_HEAD(post_shutdown, tc) {
	atf_tc_set_md_var(tc, "descr", "post-shutdown");
}
ATF_TC_BODY(post_shutdown, tc) {
	isc_result_t result;
	isc_eventtype_t event_type;
	isc_event_t *event;
	isc_task_t *task;

	done = ISC_FALSE;
	event_type = 4;

	result = isc_mutex_init(&lock);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_condition_init(&cv);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	result = isc_test_begin(NULL, ISC_TRUE, 2);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	LOCK(&lock);

	task = NULL;
	result = isc_task_create(taskmgr, 0, &task);
	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);

	/*
	 * This event causes the task to wait on cv.
	 */
	event = isc_event_allocate(mctx, &senders[1], event_type, psd_event1,
				   NULL, sizeof(*event));
	ATF_REQUIRE(event != NULL);
	isc_task_send(task, &event);

	isc_task_shutdown(task);

	result = isc_task_onshutdown(task, psd_sde, NULL);
	ATF_CHECK_EQ(result, ISC_R_SHUTTINGDOWN);

	/*
	 * Release the task.
	 */
	done = ISC_TRUE;

	SIGNAL(&cv);
	UNLOCK(&lock);

	isc_task_detach(&task);
	isc_test_end();

	(void) isc_condition_destroy(&cv);
	DESTROYLOCK(&lock);
}

/*
 * Helper for the purge tests below:
 */

#define	SENDERCNT 3
#define	TYPECNT 4
#define	TAGCNT 5
#define	NEVENTS	(SENDERCNT * TYPECNT * TAGCNT)

static isc_boolean_t testrange;
static void *purge_sender;
static isc_eventtype_t purge_type_first;
static isc_eventtype_t purge_type_last;
static void *purge_tag;
static int eventcnt;

isc_boolean_t started = ISC_FALSE;

static void
pg_event1(isc_task_t *task, isc_event_t *event) {
	UNUSED(task);

	LOCK(&lock);
	while (!started) {
		WAIT(&cv, &lock);
	}
	UNLOCK(&lock);

	isc_event_free(&event);
}

static void
pg_event2(isc_task_t *task, isc_event_t *event) {
	isc_boolean_t sender_match = ISC_FALSE;
	isc_boolean_t type_match = ISC_FALSE;
	isc_boolean_t tag_match = ISC_FALSE;

	UNUSED(task);

	if ((purge_sender == NULL) || (purge_sender == event->ev_sender)) {
		sender_match = ISC_TRUE;
	}

	if (testrange) {
		if ((purge_type_first <= event->ev_type) &&