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