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

194 lines
5.7 KiB
C
Raw Normal View History

1998-12-12 20:48:14 +00:00
/*
2000-02-03 23:08:31 +00:00
* Copyright (C) 1998, 1999, 2000 Internet Software Consortium.
1998-12-12 20:48:14 +00: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.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
* CONSORTIUM 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-08-17 22:05:58 +00:00
1998-12-12 19:25:20 +00:00
#include <config.h>
1998-08-17 22:05:58 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
1998-10-22 01:33:20 +00:00
#include <isc/assertions.h>
1999-01-06 20:26:18 +00:00
#include <isc/error.h>
1998-12-11 20:38:46 +00:00
#include <isc/mem.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>
1998-08-18 19:28:30 +00:00
#include <isc/thread.h>
1998-10-16 01:18:31 +00:00
#include <isc/result.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
}
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
if (argc > 1)
workers = atoi(argv[1]);
else
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);
1999-01-06 20:26:18 +00: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 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,
&interval, t1, my_tick, "foo", &ti1) ==
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,
&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);
sleep(2);
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.
*/
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "1",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t1, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t2, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t3, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t4, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "2",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t2, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "3",
1998-10-21 02:26:57 +00:00
sizeof *event);
isc_task_send(t3, &event);
1999-02-11 06:38:12 +00:00
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, "4",
1998-10-21 02:26:57 +00:00
sizeof *event);
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
1998-10-16 20:20:31 +00:00
sleep(10);
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-08-17 22:05:58 +00:00
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
}