bind9/bin/tests/task_test.c

192 lines
5.7 KiB
C
Raw Normal View History

1998-12-12 15:48:14 -05:00
/*
2004-03-05 00:14:21 -05:00
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
2001-01-09 17:01:04 -05:00
* Copyright (C) 1998-2001 Internet Software Consortium.
*
1998-12-12 15:48:14 -05:00
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
2004-03-05 00:14:21 -05:00
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
1998-12-12 15:48:14 -05:00
*/
1998-08-17 18:05:58 -04:00
2004-03-05 00:14:21 -05:00
/* $Id: task_test.c,v 1.49 2004/03/05 04:58:40 marka Exp $ */
2000-06-22 18:00:42 -04:00
1998-12-12 14:25:20 -05:00
#include <config.h>
1998-08-17 18:05:58 -04:00
#include <stdlib.h>
#include <unistd.h>
1998-12-11 15:38:46 -05:00
#include <isc/mem.h>
1998-08-17 20:47:55 -04:00
#include <isc/task.h>
2000-04-25 15:34:52 -04:00
#include <isc/time.h>
#include <isc/timer.h>
2000-04-27 22:12:16 -04:00
#include <isc/util.h>
1998-08-18 15:28:30 -04:00
1998-12-18 14:14:37 -05:00
isc_mem_t *mctx = NULL;
1998-08-17 18:05:58 -04:00
static void
2000-04-25 15:34:52 -04:00
my_callback(isc_task_t *task, isc_event_t *event) {
1998-08-17 19:15:50 -04:00
int i, j;
char *name = event->ev_arg;
1998-08-17 18:05:58 -04:00
1998-08-17 19:15:50 -04:00
j = 0;
1998-10-16 16:20:31 -04:00
for (i = 0; i < 1000000; i++)
1998-08-17 19:15:50 -04:00
j += 100;
1998-10-20 21:57:35 -04:00
printf("task %s (%p): %d\n", name, task, j);
isc_event_free(&event);
1998-08-17 18:05:58 -04:00
}
static void
1998-12-13 18:45:21 -05:00
my_shutdown(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-08-17 19:15:50 -04:00
1998-10-20 21:57:35 -04:00
printf("shutdown %s (%p)\n", name, task);
isc_event_free(&event);
1998-08-17 18:05:58 -04:00
}
static void
2000-04-25 15:34:52 -04:00
my_tick(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-08-18 15:28:30 -04:00
1998-10-13 16:22:22 -04:00
printf("task %p tick %s\n", task, name);
isc_event_free(&event);
1998-08-18 15:28:30 -04:00
}
1999-07-03 17:15:06 -04:00
int
1998-08-17 19:15:50 -04:00
main(int argc, char *argv[]) {
1998-12-13 18:45:21 -05:00
isc_taskmgr_t *manager = NULL;
isc_task_t *t1 = NULL, *t2 = NULL;
isc_task_t *t3 = NULL, *t4 = NULL;
isc_event_t *event;
1998-08-17 19:15:50 -04:00
unsigned int workers;
1998-12-13 18:45:21 -05:00
isc_timermgr_t *timgr;
isc_timer_t *ti1, *ti2;
1998-10-23 19:00:40 -04:00
struct isc_interval interval;
1998-08-17 19:15:50 -04:00
if (argc > 1)
workers = atoi(argv[1]);
else
workers = 2;
printf("%d workers\n", workers);
1998-08-17 18:05:58 -04:00
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
1998-08-17 18:05:58 -04:00
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
ISC_R_SUCCESS);
1998-08-17 18:05:58 -04:00
2000-04-11 21:41:21 -04:00
RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_create(manager, 0, &t3) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_create(manager, 0, &t4) == ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, "1") ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, "2") ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, "3") ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, "4") ==
ISC_R_SUCCESS);
1998-08-18 04:05:45 -04:00
1998-10-15 21:18:31 -04:00
timgr = NULL;
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
1998-10-15 21:18:31 -04:00
ti1 = NULL;
1999-06-11 21:12:35 -04:00
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 1, 0);
1999-06-11 21:12:35 -04:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
&interval, t1, my_tick, "foo", &ti1) ==
ISC_R_SUCCESS);
1998-10-15 21:18:31 -04:00
ti2 = NULL;
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 1, 0);
1999-06-11 21:12:35 -04:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
&interval, t2, my_tick, "bar", &ti2) ==
1999-01-06 15:26:18 -05:00
ISC_R_SUCCESS);
1998-10-15 21:18:31 -04:00
1998-08-18 15:28:30 -04:00
printf("task 1 = %p\n", t1);
printf("task 2 = %p\n", t2);
sleep(2);
1999-02-11 01:38:12 -05:00
/*
* Note: (void *)1 is used as a sender here, since some compilers
* don't like casting a function pointer to a (void *).
*
* In a real use, it is more likely the sender would be a
* structure (socket, timer, task, etc) but this is just a test
* program.
*/
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t1, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t2, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t3, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t4, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t2, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t3, &event);
1999-02-11 01:38:12 -05:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
2001-11-26 20:56:32 -05:00
sizeof(*event));
1998-10-20 22:26:57 -04:00
isc_task_send(t4, &event);
1999-05-10 19:01:05 -04:00
isc_task_purgerange(t3,
NULL,
ISC_EVENTTYPE_FIRSTEVENT,
1999-07-09 21:05:54 -04:00
ISC_EVENTTYPE_LASTEVENT, NULL);
1998-10-20 22:26:57 -04:00
isc_task_detach(&t1);
isc_task_detach(&t2);
isc_task_detach(&t3);
isc_task_detach(&t4);
1998-08-18 04:05:45 -04:00
1998-10-16 16:20:31 -04:00
sleep(10);
1998-08-17 19:15:50 -04:00
printf("destroy\n");
1998-10-20 21:57:35 -04:00
isc_timer_detach(&ti1);
isc_timer_detach(&ti2);
isc_timermgr_destroy(&timgr);
1998-10-20 22:26:57 -04:00
isc_taskmgr_destroy(&manager);
1998-08-17 19:15:50 -04:00
printf("destroyed\n");
1998-10-21 18:01:08 -04:00
isc_mem_stats(mctx, stdout);
1998-12-18 14:14:37 -05:00
isc_mem_destroy(&mctx);
1999-07-03 17:15:06 -04:00
return (0);
1998-08-17 18:05:58 -04:00
}