1 /* $NetBSD: task_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: task_test.c,v 1.51 2007/06/19 23:46:59 tbox Exp */
30 #include <isc/timer.h>
33 isc_mem_t
*mctx
= NULL
;
36 my_callback(isc_task_t
*task
, isc_event_t
*event
) {
38 char *name
= event
->ev_arg
;
41 for (i
= 0; i
< 1000000; i
++)
43 printf("task %s (%p): %d\n", name
, task
, j
);
44 isc_event_free(&event
);
48 my_shutdown(isc_task_t
*task
, isc_event_t
*event
) {
49 char *name
= event
->ev_arg
;
51 printf("shutdown %s (%p)\n", name
, task
);
52 isc_event_free(&event
);
56 my_tick(isc_task_t
*task
, isc_event_t
*event
) {
57 char *name
= event
->ev_arg
;
59 printf("task %p tick %s\n", task
, name
);
60 isc_event_free(&event
);
63 static char one
[] = "1";
64 static char two
[] = "2";
65 static char three
[] = "3";
66 static char four
[] = "4";
67 static char foo
[] = "foo";
68 static char bar
[] = "bar";
71 main(int argc
, char *argv
[]) {
72 isc_taskmgr_t
*manager
= NULL
;
73 isc_task_t
*t1
= NULL
, *t2
= NULL
;
74 isc_task_t
*t3
= NULL
, *t4
= NULL
;
77 isc_timermgr_t
*timgr
;
78 isc_timer_t
*ti1
, *ti2
;
79 struct isc_interval interval
;
82 workers
= atoi(argv
[1]);
89 printf("%d workers\n", workers
);
91 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
93 RUNTIME_CHECK(isc_taskmgr_create(mctx
, workers
, 0, &manager
) ==
96 RUNTIME_CHECK(isc_task_create(manager
, 0, &t1
) == ISC_R_SUCCESS
);
97 RUNTIME_CHECK(isc_task_create(manager
, 0, &t2
) == ISC_R_SUCCESS
);
98 RUNTIME_CHECK(isc_task_create(manager
, 0, &t3
) == ISC_R_SUCCESS
);
99 RUNTIME_CHECK(isc_task_create(manager
, 0, &t4
) == ISC_R_SUCCESS
);
101 RUNTIME_CHECK(isc_task_onshutdown(t1
, my_shutdown
, one
) ==
103 RUNTIME_CHECK(isc_task_onshutdown(t2
, my_shutdown
, two
) ==
105 RUNTIME_CHECK(isc_task_onshutdown(t3
, my_shutdown
, three
) ==
107 RUNTIME_CHECK(isc_task_onshutdown(t4
, my_shutdown
, four
) ==
111 RUNTIME_CHECK(isc_timermgr_create(mctx
, &timgr
) == ISC_R_SUCCESS
);
114 isc_interval_set(&interval
, 1, 0);
115 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_ticker
, NULL
,
116 &interval
, t1
, my_tick
, foo
, &ti1
) ==
120 isc_interval_set(&interval
, 1, 0);
121 RUNTIME_CHECK(isc_timer_create(timgr
, isc_timertype_ticker
, NULL
,
122 &interval
, t2
, my_tick
, bar
, &ti2
) ==
125 printf("task 1 = %p\n", t1
);
126 printf("task 2 = %p\n", t2
);
134 * Note: (void *)1 is used as a sender here, since some compilers
135 * don't like casting a function pointer to a (void *).
137 * In a real use, it is more likely the sender would be a
138 * structure (socket, timer, task, etc) but this is just a test
141 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
143 isc_task_send(t1
, &event
);
144 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
146 isc_task_send(t1
, &event
);
147 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
149 isc_task_send(t1
, &event
);
150 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
152 isc_task_send(t1
, &event
);
153 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
155 isc_task_send(t1
, &event
);
156 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
158 isc_task_send(t1
, &event
);
159 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
161 isc_task_send(t1
, &event
);
162 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
164 isc_task_send(t1
, &event
);
165 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, one
,
167 isc_task_send(t1
, &event
);
168 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, two
,
170 isc_task_send(t2
, &event
);
171 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, three
,
173 isc_task_send(t3
, &event
);
174 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, four
,
176 isc_task_send(t4
, &event
);
177 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, two
,
179 isc_task_send(t2
, &event
);
180 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, three
,
182 isc_task_send(t3
, &event
);
183 event
= isc_event_allocate(mctx
, (void *)1, 1, my_callback
, four
,
185 isc_task_send(t4
, &event
);
186 isc_task_purgerange(t3
,
188 ISC_EVENTTYPE_FIRSTEVENT
,
189 ISC_EVENTTYPE_LASTEVENT
, NULL
);
191 isc_task_detach(&t1
);
192 isc_task_detach(&t2
);
193 isc_task_detach(&t3
);
194 isc_task_detach(&t4
);
202 isc_timer_detach(&ti1
);
203 isc_timer_detach(&ti2
);
204 isc_timermgr_destroy(&timgr
);
205 isc_taskmgr_destroy(&manager
);
206 printf("destroyed\n");
208 isc_mem_stats(mctx
, stdout
);
209 isc_mem_destroy(&mctx
);