2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/bin/tests/task_test.c

212 lines
6.0 KiB
C
Raw Normal View History

1998-12-12 20:48:14 +00:00
/*
* Copyright (C) 2004, 2007, 2013-2015 Internet Systems Consortium, Inc. ("ISC")
2001-01-09 22:01:04 +00:00
* Copyright (C) 1998-2001 Internet Software Consortium.
*
2007-06-18 23:47:57 +00:00
* Permission to use, copy, modify, and/or distribute this software for any
1998-12-12 20:48:14 +00:00
* 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 05:14:21 +00: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 20:48:14 +00:00
*/
1998-08-17 22:05:58 +00:00
2007-06-19 23:47:24 +00:00
/* $Id: task_test.c,v 1.51 2007/06/19 23:46:59 tbox Exp $ */
2000-06-22 22:00:42 +00:00
1998-12-12 19:25:20 +00:00
#include <config.h>
1998-08-17 22:05:58 +00:00
#include <stdlib.h>
#include <unistd.h>
1998-12-11 20:38:46 +00:00
#include <isc/mem.h>
#include <isc/print.h>
1998-08-18 00:47:55 +00:00
#include <isc/task.h>
2000-04-25 19:34:52 +00:00
#include <isc/time.h>
#include <isc/timer.h>
2000-04-28 02:12:16 +00:00
#include <isc/util.h>
1998-08-18 19:28:30 +00:00
1998-12-18 19:14:37 +00:00
isc_mem_t *mctx = NULL;
1998-08-17 22:05:58 +00:00
static void
2000-04-25 19:34:52 +00:00
my_callback(isc_task_t *task, isc_event_t *event) {
1998-08-17 23:15:50 +00:00
int i, j;
char *name = event->ev_arg;
1998-08-17 22:05:58 +00:00
1998-08-17 23:15:50 +00:00
j = 0;
1998-10-16 20:20:31 +00:00
for (i = 0; i < 1000000; i++)
1998-08-17 23:15:50 +00:00
j += 100;
1998-10-21 01:57:35 +00:00
printf("task %s (%p): %d\n", name, task, j);
isc_event_free(&event);
1998-08-17 22:05:58 +00:00
}
static void
1998-12-13 23:45:21 +00:00
my_shutdown(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-08-17 23:15:50 +00:00
1998-10-21 01:57:35 +00:00
printf("shutdown %s (%p)\n", name, task);
isc_event_free(&event);
1998-08-17 22:05:58 +00:00
}
static void
2000-04-25 19:34:52 +00:00
my_tick(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-08-18 19:28:30 +00:00
1998-10-13 20:22:22 +00:00
printf("task %p tick %s\n", task, name);
isc_event_free(&event);
1998-08-18 19:28:30 +00:00
}
2014-04-26 23:42:37 +10:00
static char one[] = "1";
static char two[] = "2";
static char three[] = "3";
static char four[] = "4";
static char foo[] = "foo";
static char bar[] = "bar";
1999-07-03 21:15:06 +00:00
int
1998-08-17 23:15:50 +00:00
main(int argc, char *argv[]) {
1998-12-13 23:45:21 +00: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 23:15:50 +00:00
unsigned int workers;
1998-12-13 23:45:21 +00:00
isc_timermgr_t *timgr;
isc_timer_t *ti1, *ti2;
1998-10-23 23:00:40 +00:00
struct isc_interval interval;
1998-08-17 23:15:50 +00:00
2013-04-11 17:07:50 +10:00
if (argc > 1) {
1998-08-17 23:15:50 +00:00
workers = atoi(argv[1]);
2013-04-11 17:07:50 +10:00
if (workers < 1)
workers = 1;
if (workers > 8192)
workers = 8192;
} else
1998-08-17 23:15:50 +00:00
workers = 2;
printf("%d workers\n", workers);
1998-08-17 22:05:58 +00:00
1999-01-06 20:26:18 +00:00
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
1998-08-17 22:05:58 +00:00
1999-01-06 20:26:18 +00:00
RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
ISC_R_SUCCESS);
1998-08-17 22:05:58 +00:00
2000-04-12 01:41:21 +00: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);
2014-04-26 23:42:37 +10:00
RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, one) ==
1999-01-06 20:26:18 +00:00
ISC_R_SUCCESS);
2014-04-26 23:42:37 +10:00
RUNTIME_CHECK(isc_task_onshutdown(t2, my_shutdown, two) ==
1999-01-06 20:26:18 +00:00
ISC_R_SUCCESS);
2014-04-26 23:42:37 +10:00
RUNTIME_CHECK(isc_task_onshutdown(t3, my_shutdown, three) ==
1999-01-06 20:26:18 +00:00
ISC_R_SUCCESS);
2014-04-26 23:42:37 +10:00
RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, four) ==
1999-01-06 20:26:18 +00:00
ISC_R_SUCCESS);
1998-08-18 08:05:45 +00:00
1998-10-16 01:18:31 +00:00
timgr = NULL;
1999-01-06 20:26:18 +00:00
RUNTIME_CHECK(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
1998-10-16 01:18:31 +00:00
ti1 = NULL;
1999-06-12 01:12:35 +00:00
1998-10-23 23:00:40 +00:00
isc_interval_set(&interval, 1, 0);
1999-06-12 01:12:35 +00:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
2014-04-26 23:42:37 +10:00
&interval, t1, my_tick, foo, &ti1) ==
1999-06-12 01:12:35 +00:00
ISC_R_SUCCESS);
1998-10-16 01:18:31 +00:00
ti2 = NULL;
1998-10-23 23:00:40 +00:00
isc_interval_set(&interval, 1, 0);
1999-06-12 01:12:35 +00:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL,
2014-04-26 23:42:37 +10:00
&interval, t2, my_tick, bar, &ti2) ==
1999-01-06 20:26:18 +00:00
ISC_R_SUCCESS);
1998-10-16 01:18:31 +00:00
1998-08-18 19:28:30 +00:00
printf("task 1 = %p\n", t1);
printf("task 2 = %p\n", t2);
#ifndef WIN32
1998-08-18 19:28:30 +00:00
sleep(2);
#else
Sleep(2000);
#endif
1998-08-18 19:28:30 +00:00
1999-02-11 06:38:12 +00: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.
*/
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t1, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t2, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t3, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t4, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t2, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t3, &event);
2014-04-26 23:42:37 +10:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
2001-11-27 01:56:32 +00:00
sizeof(*event));
1998-10-21 02:26:57 +00:00
isc_task_send(t4, &event);
1999-05-10 23:01:05 +00:00
isc_task_purgerange(t3,
NULL,
ISC_EVENTTYPE_FIRSTEVENT,
1999-07-10 01:05:54 +00:00
ISC_EVENTTYPE_LASTEVENT, NULL);
1998-10-21 02:26:57 +00:00
isc_task_detach(&t1);
isc_task_detach(&t2);
isc_task_detach(&t3);
isc_task_detach(&t4);
1998-08-18 08:05:45 +00:00
#ifndef WIN32
1998-10-16 20:20:31 +00:00
sleep(10);
#else
Sleep(10000);
#endif
1998-08-17 23:15:50 +00:00
printf("destroy\n");
1998-10-21 01:57:35 +00:00
isc_timer_detach(&ti1);
isc_timer_detach(&ti2);
isc_timermgr_destroy(&timgr);
1998-10-21 02:26:57 +00:00
isc_taskmgr_destroy(&manager);
1998-08-17 23:15:50 +00:00
printf("destroyed\n");
1998-10-21 22:01:08 +00:00
isc_mem_stats(mctx, stdout);
1998-12-18 19:14:37 +00:00
isc_mem_destroy(&mctx);
1999-07-03 21:15:06 +00:00
return (0);
1998-08-17 22:05:58 +00:00
}