1 /* $NetBSD: timer_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */
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 */
31 #include <isc/timer.h>
34 isc_mem_t
*mctx1
, *mctx2
, *mctx3
;
35 isc_task_t
*t1
, *t2
, *t3
;
36 isc_timer_t
*ti1
, *ti2
, *ti3
;
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
);
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
);
56 if (ti3
!= NULL
&& tick_count
% 3 == 0)
59 if (ti3
!= NULL
&& tick_count
== 7) {
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
) ==
72 isc_event_free(&event
);
76 timeout(isc_task_t
*task
, isc_event_t
*event
) {
77 char *name
= event
->ev_arg
;
80 INSIST(event
->ev_type
== ISC_TIMEREVENT_IDLE
||
81 event
->ev_type
== ISC_TIMEREVENT_LIFE
);
83 if (event
->ev_type
== ISC_TIMEREVENT_IDLE
)
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
);
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
;
112 workers
= atoi(argv
[1]);
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
) ==
124 RUNTIME_CHECK(isc_timermgr_create(mctx1
, &timgr
) == ISC_R_SUCCESS
);
126 RUNTIME_CHECK(isc_task_create(manager
, 0, &t1
) ==
128 RUNTIME_CHECK(isc_task_create(manager
, 0, &t2
) ==
130 RUNTIME_CHECK(isc_task_create(manager
, 0, &t3
) ==
132 RUNTIME_CHECK(isc_task_onshutdown(t1
, shutdown_task
, one
) ==
134 RUNTIME_CHECK(isc_task_onshutdown(t2
, shutdown_task
, two
) ==
136 RUNTIME_CHECK(isc_task_onshutdown(t3
, shutdown_task
, three
) ==
139 printf("task 1: %p\n", t1
);
140 printf("task 2: %p\n", t2
);
141 printf("task 3: %p\n", t3
);
145 isc_interval_set(&interval
, 2, 0);
146 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_once
, NULL
,
147 &interval
, t2
, timeout
, two
, &ti2
) ==
150 isc_interval_set(&interval
, 1, 0);
151 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_ticker
, NULL
,
152 &interval
, t1
, tick
, one
, &ti1
) ==
155 isc_interval_set(&interval
, 10, 0);
156 RUNTIME_CHECK(isc_time_add(&now
, &interval
, &expires
) ==
158 isc_interval_set(&interval
, 2, 0);
159 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_once
, &expires
,
160 &interval
, t3
, timeout
, three
, &ti3
) ==
163 isc_task_detach(&t1
);
164 isc_task_detach(&t2
);
165 isc_task_detach(&t3
);
173 isc_timer_detach(&ti1
);
174 isc_timer_detach(&ti2
);
175 isc_timer_detach(&ti3
);
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
);