1/*
2 * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: timer_test.c,v 1.40 2007/06/19 23:46:59 tbox Exp $ */
19
20#include <config.h>
21
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#include <isc/mem.h>
27#include <isc/task.h>
28#include <isc/time.h>
29#include <isc/timer.h>
30#include <isc/util.h>
31
32isc_mem_t *mctx1, *mctx2, *mctx3;
33isc_task_t *t1, *t2, *t3;
34isc_timer_t *ti1, *ti2, *ti3;
35int tick_count = 0;
36
37static void
38shutdown_task(isc_task_t *task, isc_event_t *event) {
39	char *name = event->ev_arg;
40
41	printf("task %p shutdown %s\n", task, name);
42	isc_event_free(&event);
43}
44
45static void
46tick(isc_task_t *task, isc_event_t *event) {
47	char *name = event->ev_arg;
48
49	INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
50
51	printf("task %s (%p) tick\n", name, task);
52
53	tick_count++;
54	if (ti3 != NULL && tick_count % 3 == 0)
55		isc_timer_touch(ti3);
56
57	if (ti3 != NULL && tick_count == 7) {
58		isc_time_t expires;
59		isc_interval_t interval;
60
61		isc_interval_set(&interval, 5, 0);
62		(void)isc_time_nowplusinterval(&expires, &interval);
63		isc_interval_set(&interval, 4, 0);
64		printf("*** resetting ti3 ***\n");
65		RUNTIME_CHECK(isc_timer_reset(ti3, isc_timertype_once,
66					      &expires, &interval, ISC_TRUE) ==
67			      ISC_R_SUCCESS);
68	}
69
70	isc_event_free(&event);
71}
72
73static void
74timeout(isc_task_t *task, isc_event_t *event) {
75	char *name = event->ev_arg;
76	const char *type;
77
78	INSIST(event->ev_type == ISC_TIMEREVENT_IDLE ||
79	       event->ev_type == ISC_TIMEREVENT_LIFE);
80
81	if (event->ev_type == ISC_TIMEREVENT_IDLE)
82		type = "idle";
83	else
84		type = "life";
85	printf("task %s (%p) %s timeout\n", name, task, type);
86
87	if (strcmp(name, "3") == 0) {
88		printf("*** saving task 3 ***\n");
89		isc_event_free(&event);
90		return;
91	}
92
93	isc_event_free(&event);
94	isc_task_shutdown(task);
95}
96
97int
98main(int argc, char *argv[]) {
99	isc_taskmgr_t *manager = NULL;
100	isc_timermgr_t *timgr = NULL;
101	unsigned int workers;
102	isc_time_t expires, now;
103	isc_interval_t interval;
104
105	if (argc > 1)
106		workers = atoi(argv[1]);
107	else
108		workers = 2;
109	printf("%d workers\n", workers);
110
111	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx1) == ISC_R_SUCCESS);
112	RUNTIME_CHECK(isc_taskmgr_create(mctx1, workers, 0, &manager) ==
113		      ISC_R_SUCCESS);
114	RUNTIME_CHECK(isc_timermgr_create(mctx1, &timgr) == ISC_R_SUCCESS);
115
116	RUNTIME_CHECK(isc_task_create(manager, 0, &t1) ==
117		      ISC_R_SUCCESS);
118	RUNTIME_CHECK(isc_task_create(manager, 0, &t2) ==
119		      ISC_R_SUCCESS);
120	RUNTIME_CHECK(isc_task_create(manager, 0, &t3) ==
121		      ISC_R_SUCCESS);
122	RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, "1") ==
123		      ISC_R_SUCCESS);
124	RUNTIME_CHECK(isc_task_onshutdown(t2, shutdown_task, "2") ==
125		      ISC_R_SUCCESS);
126	RUNTIME_CHECK(isc_task_onshutdown(t3, shutdown_task, "3") ==
127		      ISC_R_SUCCESS);
128
129	printf("task 1: %p\n", t1);
130	printf("task 2: %p\n", t2);
131	printf("task 3: %p\n", t3);
132
133	TIME_NOW(&now);
134
135	isc_interval_set(&interval, 2, 0);
136	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, NULL,
137				       &interval, t2, timeout, "2", &ti2) ==
138		      ISC_R_SUCCESS);
139
140	isc_interval_set(&interval, 1, 0);
141	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
142				       &interval, t1, tick, "1", &ti1) ==
143		      ISC_R_SUCCESS);
144
145	isc_interval_set(&interval, 10, 0);
146	RUNTIME_CHECK(isc_time_add(&now, &interval, &expires) ==
147		      ISC_R_SUCCESS);
148	isc_interval_set(&interval, 2, 0);
149	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
150				       &interval, t3, timeout, "3", &ti3) ==
151		      ISC_R_SUCCESS);
152
153	isc_task_detach(&t1);
154	isc_task_detach(&t2);
155	isc_task_detach(&t3);
156
157	sleep(15);
158	printf("destroy\n");
159	isc_timer_detach(&ti1);
160	isc_timer_detach(&ti2);
161	isc_timer_detach(&ti3);
162	sleep(2);
163	isc_timermgr_destroy(&timgr);
164	isc_taskmgr_destroy(&manager);
165	printf("destroyed\n");
166
167	printf("Statistics for mctx1:\n");
168	isc_mem_stats(mctx1, stdout);
169	isc_mem_destroy(&mctx1);
170
171	return (0);
172}
173