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 */
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
);
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
;
108 workers
= atoi(argv
[1]);
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
) ==
116 RUNTIME_CHECK(isc_timermgr_create(mctx1
, &timgr
) == ISC_R_SUCCESS
);
118 RUNTIME_CHECK(isc_task_create(manager
, 0, &t1
) ==
120 RUNTIME_CHECK(isc_task_create(manager
, 0, &t2
) ==
122 RUNTIME_CHECK(isc_task_create(manager
, 0, &t3
) ==
124 RUNTIME_CHECK(isc_task_onshutdown(t1
, shutdown_task
, "1") ==
126 RUNTIME_CHECK(isc_task_onshutdown(t2
, shutdown_task
, "2") ==
128 RUNTIME_CHECK(isc_task_onshutdown(t3
, shutdown_task
, "3") ==
131 printf("task 1: %p\n", t1
);
132 printf("task 2: %p\n", t2
);
133 printf("task 3: %p\n", t3
);
137 isc_interval_set(&interval
, 2, 0);
138 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_once
, NULL
,
139 &interval
, t2
, timeout
, "2", &ti2
) ==
142 isc_interval_set(&interval
, 1, 0);
143 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_ticker
, NULL
,
144 &interval
, t1
, tick
, "1", &ti1
) ==
147 isc_interval_set(&interval
, 10, 0);
148 RUNTIME_CHECK(isc_time_add(&now
, &interval
, &expires
) ==
150 isc_interval_set(&interval
, 2, 0);
151 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_once
, &expires
,
152 &interval
, t3
, timeout
, "3", &ti3
) ==
155 isc_task_detach(&t1
);
156 isc_task_detach(&t2
);
157 isc_task_detach(&t3
);
161 isc_timer_detach(&ti1
);
162 isc_timer_detach(&ti2
);
163 isc_timer_detach(&ti3
);
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
);