bind9/bin/tests/optional/timer_test.c

183 lines
4.4 KiB
C
Raw Normal View History

1998-12-12 15:48:14 -05:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1998-12-12 15:48:14 -05:00
*/
1998-10-16 16:20:31 -04:00
#include <stdlib.h>
2000-11-10 00:34:16 -05:00
#include <string.h>
1998-10-16 16:20:31 -04:00
#include <unistd.h>
#include <isc/managers.h>
1998-12-11 15:38:46 -05:00
#include <isc/mem.h>
#include <isc/print.h>
1998-10-16 16:20:31 -04:00
#include <isc/task.h>
2000-04-25 15:34:52 -04:00
#include <isc/time.h>
1998-10-16 16:20:31 -04:00
#include <isc/timer.h>
2000-04-27 22:12:16 -04:00
#include <isc/util.h>
1998-10-16 16:20:31 -04:00
isc_mem_t *mctx1, *mctx2, *mctx3;
isc_task_t *t1, *t2, *t3;
1998-12-13 18:45:21 -05:00
isc_timer_t *ti1, *ti2, *ti3;
int tick_count = 0;
1998-10-16 16:20:31 -04:00
static void
shutdown_task(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-10-16 16:20:31 -04:00
1998-10-20 21:57:35 -04:00
printf("task %p shutdown %s\n", task, name);
isc_event_free(&event);
1998-10-16 16:20:31 -04:00
}
static void
tick(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
1998-10-16 16:20:31 -04:00
INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
1998-10-16 16:20:31 -04:00
printf("task %s (%p) tick\n", name, task);
tick_count++;
if (ti3 != NULL && tick_count % 3 == 0) {
1998-10-20 21:57:35 -04:00
isc_timer_touch(ti3);
}
1998-10-16 16:20:31 -04:00
if (ti3 != NULL && tick_count == 7) {
isc_time_t expires;
1998-12-13 18:45:21 -05:00
isc_interval_t interval;
1998-10-16 16:20:31 -04:00
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 5, 0);
1999-06-11 21:12:35 -04:00
(void)isc_time_nowplusinterval(&expires, &interval);
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 4, 0);
1998-10-16 16:20:31 -04:00
printf("*** resetting ti3 ***\n");
RUNTIME_CHECK(isc_timer_reset(ti3, isc_timertype_once, &expires,
&interval,
true) == ISC_R_SUCCESS);
1998-10-16 16:20:31 -04:00
}
isc_event_free(&event);
1998-10-16 16:20:31 -04:00
}
static void
timeout(isc_task_t *task, isc_event_t *event) {
char *name = event->ev_arg;
const char *type;
1998-10-16 16:20:31 -04:00
INSIST(event->ev_type == ISC_TIMEREVENT_IDLE ||
event->ev_type == ISC_TIMEREVENT_LIFE);
1998-10-16 16:20:31 -04:00
if (event->ev_type == ISC_TIMEREVENT_IDLE) {
1998-10-16 16:20:31 -04:00
type = "idle";
} else {
1998-10-16 16:20:31 -04:00
type = "life";
}
1998-10-16 16:20:31 -04:00
printf("task %s (%p) %s timeout\n", name, task, type);
if (strcmp(name, "3") == 0) {
printf("*** saving task 3 ***\n");
isc_event_free(&event);
return;
1998-10-16 16:20:31 -04:00
}
isc_event_free(&event);
isc_task_shutdown(task);
1998-10-16 16:20:31 -04:00
}
2014-04-26 09:42:37 -04:00
static char one[] = "1";
static char two[] = "2";
static char three[] = "3";
1999-07-03 17:15:06 -04:00
int
main(int argc, char *argv[]) {
isc_nm_t *netmgr = NULL;
isc_taskmgr_t *taskmgr = NULL;
1998-12-13 18:45:21 -05:00
isc_timermgr_t *timgr = NULL;
unsigned int workers;
isc_time_t expires, now;
isc_interval_t interval;
1998-10-16 16:20:31 -04:00
2013-04-11 03:07:50 -04:00
if (argc > 1) {
1998-10-16 16:20:31 -04:00
workers = atoi(argv[1]);
if (workers < 1) {
2013-04-11 03:07:50 -04:00
workers = 1;
}
if (workers > 8192) {
2013-04-11 03:07:50 -04:00
workers = 8192;
}
} else {
1998-10-16 16:20:31 -04:00
workers = 2;
}
2018-02-14 03:08:40 -05:00
printf("%u workers\n", workers);
1998-10-16 16:20:31 -04:00
isc_mem_create(&mctx1);
RUNTIME_CHECK(isc_managers_create(mctx1, workers, 0, &netmgr,
&taskmgr) == ISC_R_SUCCESS);
1999-01-14 20:38:28 -05:00
RUNTIME_CHECK(isc_timermgr_create(mctx1, &timgr) == ISC_R_SUCCESS);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_task_create(taskmgr, 0, &t1) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_create(taskmgr, 0, &t2) == ISC_R_SUCCESS);
RUNTIME_CHECK(isc_task_create(taskmgr, 0, &t3) == ISC_R_SUCCESS);
2014-04-26 09:42:37 -04:00
RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, one) ==
1999-01-06 15:26:18 -05:00
ISC_R_SUCCESS);
2014-04-26 09:42:37 -04:00
RUNTIME_CHECK(isc_task_onshutdown(t2, shutdown_task, two) ==
1999-01-06 15:26:18 -05:00
ISC_R_SUCCESS);
2014-04-26 09:42:37 -04:00
RUNTIME_CHECK(isc_task_onshutdown(t3, shutdown_task, three) ==
1999-01-06 15:26:18 -05:00
ISC_R_SUCCESS);
1998-10-16 16:20:31 -04:00
printf("task 1: %p\n", t1);
printf("task 2: %p\n", t2);
printf("task 3: %p\n", t3);
TIME_NOW(&now);
1998-10-16 16:20:31 -04:00
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 2, 0);
1999-06-11 21:12:35 -04:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, NULL,
&interval, t2, timeout, two,
&ti2) == ISC_R_SUCCESS);
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, tick, one,
&ti1) == ISC_R_SUCCESS);
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 10, 0);
RUNTIME_CHECK(isc_time_add(&now, &interval, &expires) == ISC_R_SUCCESS);
1998-10-23 19:00:40 -04:00
isc_interval_set(&interval, 2, 0);
1999-01-06 15:26:18 -05:00
RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires,
&interval, t3, timeout, three,
&ti3) == ISC_R_SUCCESS);
1998-10-16 16:20:31 -04:00
1998-10-20 22:26:57 -04:00
isc_task_detach(&t1);
isc_task_detach(&t2);
isc_task_detach(&t3);
1998-10-16 16:20:31 -04:00
#ifndef WIN32
1998-10-16 16:20:31 -04:00
sleep(15);
#else /* ifndef WIN32 */
Sleep(15000);
#endif /* ifndef WIN32 */
1998-10-16 16:20:31 -04:00
printf("destroy\n");
1998-10-20 21:57:35 -04:00
isc_timer_detach(&ti1);
isc_timer_detach(&ti2);
isc_timer_detach(&ti3);
#ifndef WIN32
1998-10-16 16:20:31 -04:00
sleep(2);
#else /* ifndef WIN32 */
Sleep(2000);
#endif /* ifndef WIN32 */
1998-10-20 21:57:35 -04:00
isc_timermgr_destroy(&timgr);
isc_managers_destroy(&netmgr, &taskmgr);
1998-10-16 16:20:31 -04:00
printf("destroyed\n");
1999-01-14 20:38:28 -05:00
printf("Statistics for mctx1:\n");
isc_mem_stats(mctx1, stdout);
isc_mem_destroy(&mctx1);
1999-07-03 17:15:06 -04:00
return (0);
1998-10-16 16:20:31 -04:00
}