etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / timer_test.c
blobd3177308c01454dc635c70694eabddbe41582a04
1 /* $NetBSD: timer_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
3 /*
4 * Copyright (C) 2004, 2007, 2013, 2014 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 static char one[] = "1";
100 static char two[] = "2";
101 static char three[] = "3";
104 main(int argc, char *argv[]) {
105 isc_taskmgr_t *manager = NULL;
106 isc_timermgr_t *timgr = NULL;
107 unsigned int workers;
108 isc_time_t expires, now;
109 isc_interval_t interval;
111 if (argc > 1) {
112 workers = atoi(argv[1]);
113 if (workers < 1)
114 workers = 1;
115 if (workers > 8192)
116 workers = 8192;
117 } else
118 workers = 2;
119 printf("%d workers\n", workers);
121 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx1) == ISC_R_SUCCESS);
122 RUNTIME_CHECK(isc_taskmgr_create(mctx1, workers, 0, &manager) ==
123 ISC_R_SUCCESS);
124 RUNTIME_CHECK(isc_timermgr_create(mctx1, &timgr) == ISC_R_SUCCESS);
126 RUNTIME_CHECK(isc_task_create(manager, 0, &t1) ==
127 ISC_R_SUCCESS);
128 RUNTIME_CHECK(isc_task_create(manager, 0, &t2) ==
129 ISC_R_SUCCESS);
130 RUNTIME_CHECK(isc_task_create(manager, 0, &t3) ==
131 ISC_R_SUCCESS);
132 RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, one) ==
133 ISC_R_SUCCESS);
134 RUNTIME_CHECK(isc_task_onshutdown(t2, shutdown_task, two) ==
135 ISC_R_SUCCESS);
136 RUNTIME_CHECK(isc_task_onshutdown(t3, shutdown_task, three) ==
137 ISC_R_SUCCESS);
139 printf("task 1: %p\n", t1);
140 printf("task 2: %p\n", t2);
141 printf("task 3: %p\n", t3);
143 TIME_NOW(&now);
145 isc_interval_set(&interval, 2, 0);
146 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, NULL,
147 &interval, t2, timeout, two, &ti2) ==
148 ISC_R_SUCCESS);
150 isc_interval_set(&interval, 1, 0);
151 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
152 &interval, t1, tick, one, &ti1) ==
153 ISC_R_SUCCESS);
155 isc_interval_set(&interval, 10, 0);
156 RUNTIME_CHECK(isc_time_add(&now, &interval, &expires) ==
157 ISC_R_SUCCESS);
158 isc_interval_set(&interval, 2, 0);
159 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
160 &interval, t3, timeout, three, &ti3) ==
161 ISC_R_SUCCESS);
163 isc_task_detach(&t1);
164 isc_task_detach(&t2);
165 isc_task_detach(&t3);
167 #ifndef WIN32
168 sleep(15);
169 #else
170 Sleep(15000);
171 #endif
172 printf("destroy\n");
173 isc_timer_detach(&ti1);
174 isc_timer_detach(&ti2);
175 isc_timer_detach(&ti3);
176 #ifndef WIN32
177 sleep(2);
178 #else
179 Sleep(2000);
180 #endif
181 isc_timermgr_destroy(&timgr);
182 isc_taskmgr_destroy(&manager);
183 printf("destroyed\n");
185 printf("Statistics for mctx1:\n");
186 isc_mem_stats(mctx1, stdout);
187 isc_mem_destroy(&mctx1);
189 return (0);