etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / task_test.c
blob559a238b7d98a05ee0bc4c46ff25c1870dcc9e44
1 /* $NetBSD: task_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: task_test.c,v 1.51 2007/06/19 23:46:59 tbox Exp */
22 #include <config.h>
24 #include <stdlib.h>
25 #include <unistd.h>
27 #include <isc/mem.h>
28 #include <isc/task.h>
29 #include <isc/time.h>
30 #include <isc/timer.h>
31 #include <isc/util.h>
33 isc_mem_t *mctx = NULL;
35 static void
36 my_callback(isc_task_t *task, isc_event_t *event) {
37 int i, j;
38 char *name = event->ev_arg;
40 j = 0;
41 for (i = 0; i < 1000000; i++)
42 j += 100;
43 printf("task %s (%p): %d\n", name, task, j);
44 isc_event_free(&event);
47 static void
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);
55 static void
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";
70 int
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;
75 isc_event_t *event;
76 unsigned int workers;
77 isc_timermgr_t *timgr;
78 isc_timer_t *ti1, *ti2;
79 struct isc_interval interval;
81 if (argc > 1) {
82 workers = atoi(argv[1]);
83 if (workers < 1)
84 workers = 1;
85 if (workers > 8192)
86 workers = 8192;
87 } else
88 workers = 2;
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) ==
94 ISC_R_SUCCESS);
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) ==
102 ISC_R_SUCCESS);
103 RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, two) ==
104 ISC_R_SUCCESS);
105 RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, three) ==
106 ISC_R_SUCCESS);
107 RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, four) ==
108 ISC_R_SUCCESS);
110 timgr = NULL;
111 RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
112 ti1 = NULL;
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) ==
117 ISC_R_SUCCESS);
119 ti2 = NULL;
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) ==
123 ISC_R_SUCCESS);
125 printf("task 1 = %p\n", t1);
126 printf("task 2 = %p\n", t2);
127 #ifndef WIN32
128 sleep(2);
129 #else
130 Sleep(2000);
131 #endif
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
139 * program.
141 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
142 sizeof(*event));
143 isc_task_send(t1, &event);
144 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
145 sizeof(*event));
146 isc_task_send(t1, &event);
147 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
148 sizeof(*event));
149 isc_task_send(t1, &event);
150 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
151 sizeof(*event));
152 isc_task_send(t1, &event);
153 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
154 sizeof(*event));
155 isc_task_send(t1, &event);
156 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
157 sizeof(*event));
158 isc_task_send(t1, &event);
159 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
160 sizeof(*event));
161 isc_task_send(t1, &event);
162 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
163 sizeof(*event));
164 isc_task_send(t1, &event);
165 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
166 sizeof(*event));
167 isc_task_send(t1, &event);
168 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
169 sizeof(*event));
170 isc_task_send(t2, &event);
171 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
172 sizeof(*event));
173 isc_task_send(t3, &event);
174 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
175 sizeof(*event));
176 isc_task_send(t4, &event);
177 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
178 sizeof(*event));
179 isc_task_send(t2, &event);
180 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
181 sizeof(*event));
182 isc_task_send(t3, &event);
183 event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
184 sizeof(*event));
185 isc_task_send(t4, &event);
186 isc_task_purgerange(t3,
187 NULL,
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);
196 #ifndef WIN32
197 sleep(10);
198 #else
199 Sleep(10000);
200 #endif
201 printf("destroy\n");
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);
211 return (0);