1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1998-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
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.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* 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-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-10-16 20:20:31 +00:00
|
|
|
|
2001-01-09 22:01:04 +00:00
|
|
|
/* $Id: timer_test.c,v 1.36 2001/01/09 21:41:45 bwelling Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1998-10-16 20:20:31 +00:00
|
|
|
#include <stdlib.h>
|
2000-11-10 05:34:16 +00:00
|
|
|
#include <string.h>
|
1998-10-16 20:20:31 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1998-12-11 20:38:46 +00:00
|
|
|
#include <isc/mem.h>
|
1998-10-16 20:20:31 +00:00
|
|
|
#include <isc/task.h>
|
2000-04-25 19:34:52 +00:00
|
|
|
#include <isc/time.h>
|
1998-10-16 20:20:31 +00:00
|
|
|
#include <isc/timer.h>
|
2000-04-28 02:12:16 +00:00
|
|
|
#include <isc/util.h>
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1999-01-15 01:38:28 +00:00
|
|
|
isc_mem_t *mctx1, *mctx2, *mctx3;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_task_t *t1, *t2, *t3;
|
|
|
|
isc_timer_t *ti1, *ti2, *ti3;
|
1998-10-16 20:20:31 +00:00
|
|
|
int tick_count = 0;
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
shutdown_task(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
char *name = event->ev_arg;
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1998-10-21 01:57:35 +00:00
|
|
|
printf("task %p shutdown %s\n", task, name);
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-04-25 19:34:52 +00:00
|
|
|
tick(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
char *name = event->ev_arg;
|
1998-10-16 20:20:31 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
|
|
|
printf("task %s (%p) tick\n", name, task);
|
|
|
|
|
|
|
|
tick_count++;
|
1998-10-28 01:45:43 +00:00
|
|
|
if (ti3 != NULL && tick_count % 3 == 0)
|
1998-10-21 01:57:35 +00:00
|
|
|
isc_timer_touch(ti3);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1998-10-28 01:45:43 +00:00
|
|
|
if (ti3 != NULL && tick_count == 7) {
|
1999-06-12 01:12:35 +00:00
|
|
|
isc_time_t expires;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_interval_t interval;
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1998-10-23 23:00:40 +00:00
|
|
|
isc_interval_set(&interval, 5, 0);
|
1999-06-12 01:12:35 +00:00
|
|
|
(void)isc_time_nowplusinterval(&expires, &interval);
|
1998-10-23 23:00:40 +00:00
|
|
|
isc_interval_set(&interval, 4, 0);
|
1998-10-16 20:20:31 +00:00
|
|
|
printf("*** resetting ti3 ***\n");
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_timer_reset(ti3, isc_timertype_once,
|
|
|
|
&expires, &interval, ISC_TRUE) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|
|
|
|
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-04-25 19:34:52 +00:00
|
|
|
timeout(isc_task_t *task, isc_event_t *event) {
|
2000-04-17 19:22:44 +00:00
|
|
|
char *name = event->ev_arg;
|
2000-06-01 19:11:07 +00:00
|
|
|
const char *type;
|
1998-10-16 20:20:31 +00:00
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
INSIST(event->ev_type == ISC_TIMEREVENT_IDLE ||
|
2000-04-17 19:22:44 +00:00
|
|
|
event->ev_type == ISC_TIMEREVENT_LIFE);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
if (event->ev_type == ISC_TIMEREVENT_IDLE)
|
1998-10-16 20:20:31 +00:00
|
|
|
type = "idle";
|
|
|
|
else
|
|
|
|
type = "life";
|
|
|
|
printf("task %s (%p) %s timeout\n", name, task, type);
|
|
|
|
|
|
|
|
if (strcmp(name, "3") == 0) {
|
|
|
|
printf("*** saving task 3 ***\n");
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|
1998-10-28 01:45:43 +00:00
|
|
|
|
|
|
|
isc_event_free(&event);
|
1998-12-16 02:05:38 +00:00
|
|
|
isc_task_shutdown(task);
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|
|
|
|
|
1999-07-03 21:15:06 +00:00
|
|
|
int
|
1998-10-16 20:20:31 +00:00
|
|
|
main(int argc, char *argv[]) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_taskmgr_t *manager = NULL;
|
|
|
|
isc_timermgr_t *timgr = NULL;
|
1998-10-16 20:20:31 +00:00
|
|
|
unsigned int workers;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_time_t expires, now;
|
|
|
|
isc_interval_t interval;
|
1998-10-16 20:20:31 +00:00
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
workers = atoi(argv[1]);
|
|
|
|
else
|
|
|
|
workers = 2;
|
|
|
|
printf("%d workers\n", workers);
|
|
|
|
|
1999-01-15 01:38:28 +00:00
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx1) == ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(isc_taskmgr_create(mctx1, workers, 0, &manager) ==
|
|
|
|
ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(isc_timermgr_create(mctx1, &timgr) == ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
|
2000-04-12 01:41:21 +00:00
|
|
|
RUNTIME_CHECK(isc_task_create(manager, 0, &t1) ==
|
1999-01-15 01:38:28 +00:00
|
|
|
ISC_R_SUCCESS);
|
2000-04-12 01:41:21 +00:00
|
|
|
RUNTIME_CHECK(isc_task_create(manager, 0, &t2) ==
|
1999-01-15 01:38:28 +00:00
|
|
|
ISC_R_SUCCESS);
|
2000-04-12 01:41:21 +00:00
|
|
|
RUNTIME_CHECK(isc_task_create(manager, 0, &t3) ==
|
1999-01-15 01:38:28 +00:00
|
|
|
ISC_R_SUCCESS);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, "1") ==
|
|
|
|
ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t2, shutdown_task, "2") ==
|
|
|
|
ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t3, shutdown_task, "3") ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-12-16 02:05:38 +00:00
|
|
|
|
1998-10-16 20:20:31 +00:00
|
|
|
printf("task 1: %p\n", t1);
|
|
|
|
printf("task 2: %p\n", t2);
|
|
|
|
printf("task 3: %p\n", t3);
|
|
|
|
|
1999-06-12 01:12:35 +00:00
|
|
|
(void)isc_time_now(&now);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1998-10-23 23:00:40 +00:00
|
|
|
isc_interval_set(&interval, 2, 0);
|
1999-06-12 01:12:35 +00:00
|
|
|
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, NULL,
|
1999-01-06 20:26:18 +00:00
|
|
|
&interval, t2, timeout, "2", &ti2) ==
|
|
|
|
ISC_R_SUCCESS);
|
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, tick, "1", &ti1) ==
|
|
|
|
ISC_R_SUCCESS);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-10-23 23:00:40 +00:00
|
|
|
isc_interval_set(&interval, 10, 0);
|
2000-05-18 17:22:40 +00:00
|
|
|
RUNTIME_CHECK(isc_time_add(&now, &interval, &expires) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-10-23 23:00:40 +00:00
|
|
|
isc_interval_set(&interval, 2, 0);
|
1999-01-06 20:26:18 +00:00
|
|
|
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
|
|
|
|
&interval, t3, timeout, "3", &ti3) ==
|
|
|
|
ISC_R_SUCCESS);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_detach(&t1);
|
|
|
|
isc_task_detach(&t2);
|
|
|
|
isc_task_detach(&t3);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
|
|
|
sleep(15);
|
|
|
|
printf("destroy\n");
|
1998-10-21 01:57:35 +00:00
|
|
|
isc_timer_detach(&ti1);
|
|
|
|
isc_timer_detach(&ti2);
|
|
|
|
isc_timer_detach(&ti3);
|
1998-10-16 20:20:31 +00:00
|
|
|
sleep(2);
|
1998-10-21 01:57:35 +00:00
|
|
|
isc_timermgr_destroy(&timgr);
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_taskmgr_destroy(&manager);
|
1998-10-16 20:20:31 +00:00
|
|
|
printf("destroyed\n");
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-01-15 01:38:28 +00:00
|
|
|
printf("Statistics for mctx1:\n");
|
|
|
|
isc_mem_stats(mctx1, stdout);
|
|
|
|
isc_mem_destroy(&mctx1);
|
1999-07-03 21:15:06 +00:00
|
|
|
|
|
|
|
return (0);
|
1998-10-16 20:20:31 +00:00
|
|
|
}
|