1/*
2 * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: task_test.c,v 1.51 2007/06/19 23:46:59 tbox Exp $ */
19
20#include <config.h>
21
22#include <stdlib.h>
23#include <unistd.h>
24
25#include <isc/mem.h>
26#include <isc/task.h>
27#include <isc/time.h>
28#include <isc/timer.h>
29#include <isc/util.h>
30
31isc_mem_t *mctx = NULL;
32
33static void
34my_callback(isc_task_t *task, isc_event_t *event) {
35	int i, j;
36	char *name = event->ev_arg;
37
38	j = 0;
39	for (i = 0; i < 1000000; i++)
40		j += 100;
41	printf("task %s (%p): %d\n", name, task, j);
42	isc_event_free(&event);
43}
44
45static void
46my_shutdown(isc_task_t *task, isc_event_t *event) {
47	char *name = event->ev_arg;
48
49	printf("shutdown %s (%p)\n", name, task);
50	isc_event_free(&event);
51}
52
53static void
54my_tick(isc_task_t *task, isc_event_t *event) {
55	char *name = event->ev_arg;
56
57	printf("task %p tick %s\n", task, name);
58	isc_event_free(&event);
59}
60
61int
62main(int argc, char *argv[]) {
63	isc_taskmgr_t *manager = NULL;
64	isc_task_t *t1 = NULL, *t2 = NULL;
65	isc_task_t *t3 = NULL, *t4 = NULL;
66	isc_event_t *event;
67	unsigned int workers;
68	isc_timermgr_t *timgr;
69	isc_timer_t *ti1, *ti2;
70	struct isc_interval interval;
71
72	if (argc > 1)
73		workers = atoi(argv[1]);
74	else
75		workers = 2;
76	printf("%d workers\n", workers);
77
78	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
79
80	RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
81		      ISC_R_SUCCESS);
82
83	RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == ISC_R_SUCCESS);
84	RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == ISC_R_SUCCESS);
85	RUNTIME_CHECK(isc_task_create(manager, 0, &t3) == ISC_R_SUCCESS);
86	RUNTIME_CHECK(isc_task_create(manager, 0, &t4) == ISC_R_SUCCESS);
87
88	RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
89		      ISC_R_SUCCESS);
90	RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
91		      ISC_R_SUCCESS);
92	RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, "3") ==
93		      ISC_R_SUCCESS);
94	RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, "4") ==
95		      ISC_R_SUCCESS);
96
97	timgr = NULL;
98	RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
99	ti1 = NULL;
100
101	isc_interval_set(&interval, 1, 0);
102	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
103				       &interval, t1, my_tick, "foo", &ti1) ==
104		      ISC_R_SUCCESS);
105
106	ti2 = NULL;
107	isc_interval_set(&interval, 1, 0);
108	RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
109				       &interval, t2, my_tick, "bar", &ti2) ==
110		      ISC_R_SUCCESS);
111
112	printf("task 1 = %p\n", t1);
113	printf("task 2 = %p\n", t2);
114	sleep(2);
115
116	/*
117	 * Note:  (void *)1 is used as a sender here, since some compilers
118	 * don't like casting a function pointer to a (void *).
119	 *
120	 * In a real use, it is more likely the sender would be a
121	 * structure (socket, timer, task, etc) but this is just a test
122	 * program.
123	 */
124	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
125				   sizeof(*event));
126	isc_task_send(t1, &event);
127	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
128				   sizeof(*event));
129	isc_task_send(t1, &event);
130	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
131				   sizeof(*event));
132	isc_task_send(t1, &event);
133	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
134				   sizeof(*event));
135	isc_task_send(t1, &event);
136	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
137				   sizeof(*event));
138	isc_task_send(t1, &event);
139	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
140				   sizeof(*event));
141	isc_task_send(t1, &event);
142	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
143				   sizeof(*event));
144	isc_task_send(t1, &event);
145	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
146				   sizeof(*event));
147	isc_task_send(t1, &event);
148	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
149				   sizeof(*event));
150	isc_task_send(t1, &event);
151	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
152				   sizeof(*event));
153	isc_task_send(t2, &event);
154	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
155				   sizeof(*event));
156	isc_task_send(t3, &event);
157	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
158				   sizeof(*event));
159	isc_task_send(t4, &event);
160	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
161				   sizeof(*event));
162	isc_task_send(t2, &event);
163	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
164				   sizeof(*event));
165	isc_task_send(t3, &event);
166	event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
167				   sizeof(*event));
168	isc_task_send(t4, &event);
169	isc_task_purgerange(t3,
170			    NULL,
171			    ISC_EVENTTYPE_FIRSTEVENT,
172			    ISC_EVENTTYPE_LASTEVENT, NULL);
173
174	isc_task_detach(&t1);
175	isc_task_detach(&t2);
176	isc_task_detach(&t3);
177	isc_task_detach(&t4);
178
179	sleep(10);
180	printf("destroy\n");
181	isc_timer_detach(&ti1);
182	isc_timer_detach(&ti2);
183	isc_timermgr_destroy(&timgr);
184	isc_taskmgr_destroy(&manager);
185	printf("destroyed\n");
186
187	isc_mem_stats(mctx, stdout);
188	isc_mem_destroy(&mctx);
189
190	return (0);
191}
192