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

143 lines
3.3 KiB
C
Raw Normal View History

1998-10-16 20:20:31 +00:00
#include "attribute.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <isc/memcluster.h>
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/timer.h>
1998-10-21 22:01:08 +00:00
isc_memctx_t mctx = NULL;
1998-10-21 02:26:57 +00:00
isc_task_t t1, t2, t3;
1998-10-21 01:57:35 +00:00
isc_timer_t ti1, ti2, ti3;
1998-10-16 20:20:31 +00:00
int tick_count = 0;
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
shutdown_task(isc_task_t task, isc_event_t event) {
1998-10-16 20:20:31 +00:00
char *name = event->arg;
1998-10-21 01:57:35 +00:00
printf("task %p shutdown %s\n", task, name);
1998-10-21 01:13:50 +00:00
return (ISC_TRUE);
1998-10-16 20:20:31 +00:00
}
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
tick(isc_task_t task, isc_event_t event)
1998-10-16 20:20:31 +00:00
{
char *name = event->arg;
1998-10-21 01:57:35 +00:00
INSIST(event->type == ISC_TIMEREVENT_TICK);
1998-10-16 20:20:31 +00:00
printf("task %s (%p) tick\n", name, task);
tick_count++;
if (tick_count % 3 == 0)
1998-10-21 01:57:35 +00:00
isc_timer_touch(ti3);
1998-10-16 20:20:31 +00:00
if (tick_count == 7) {
os_time_t expires, interval, now;
(void)os_time_get(&now);
expires.seconds = 5;
expires.nanoseconds = 0;
os_time_add(&now, &expires, &expires);
interval.seconds = 4;
interval.nanoseconds = 0;
printf("*** resetting ti3 ***\n");
1998-10-21 01:57:35 +00:00
INSIST(isc_timer_reset(ti3, isc_timertype_once, expires,
interval, ISC_TRUE)
1998-10-16 20:20:31 +00:00
== ISC_R_SUCCESS);
}
1998-10-21 01:13:50 +00:00
return (ISC_FALSE);
1998-10-16 20:20:31 +00:00
}
1998-10-21 01:13:50 +00:00
static isc_boolean_t
1998-10-21 02:26:57 +00:00
timeout(isc_task_t task, isc_event_t event)
1998-10-16 20:20:31 +00:00
{
char *name = event->arg;
char *type;
1998-10-21 01:57:35 +00:00
INSIST(event->type == ISC_TIMEREVENT_IDLE ||
event->type == ISC_TIMEREVENT_LIFE);
1998-10-16 20:20:31 +00:00
1998-10-21 01:57:35 +00:00
if (event->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-21 01:13:50 +00:00
return (ISC_FALSE);
1998-10-16 20:20:31 +00:00
}
1998-10-21 01:13:50 +00:00
return (ISC_TRUE);
1998-10-16 20:20:31 +00:00
}
void
main(int argc, char *argv[]) {
1998-10-21 02:26:57 +00:00
isc_taskmgr_t manager = NULL;
1998-10-21 01:57:35 +00:00
isc_timermgr_t timgr = NULL;
1998-10-16 20:20:31 +00:00
unsigned int workers;
os_time_t expires, interval, now;
if (argc > 1)
workers = atoi(argv[1]);
else
workers = 2;
printf("%d workers\n", workers);
1998-10-21 22:01:08 +00:00
INSIST(isc_memctx_create(0, 0, &mctx) == ISC_R_SUCCESS);
1998-10-21 02:26:57 +00:00
INSIST(isc_taskmgr_create(mctx, workers, 0, &manager) == workers);
INSIST(isc_task_create(manager, shutdown_task, "1", 0, &t1));
INSIST(isc_task_create(manager, shutdown_task, "2", 0, &t2));
INSIST(isc_task_create(manager, shutdown_task, "3", 0, &t3));
1998-10-21 01:57:35 +00:00
INSIST(isc_timermgr_create(mctx, &timgr) == ISC_R_SUCCESS);
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);
(void)os_time_get(&now);
expires.seconds = 0;
expires.nanoseconds = 0;
interval.seconds = 2;
interval.nanoseconds = 0;
1998-10-21 01:57:35 +00:00
INSIST(isc_timer_create(timgr, isc_timertype_once, expires, interval,
t2, timeout, "2", &ti2) == ISC_R_SUCCESS);
1998-10-16 20:20:31 +00:00
expires.seconds = 0;
expires.nanoseconds = 0;
interval.seconds = 1;
interval.nanoseconds = 0;
1998-10-21 01:57:35 +00:00
INSIST(isc_timer_create(timgr, isc_timertype_ticker, expires, interval,
t1, tick, "1", &ti1) == ISC_R_SUCCESS);
1998-10-16 20:20:31 +00:00
expires.seconds = 10;
expires.nanoseconds = 0;
os_time_add(&now, &expires, &expires);
interval.seconds = 2;
interval.nanoseconds = 0;
1998-10-21 01:57:35 +00:00
INSIST(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");
1998-10-21 22:01:08 +00:00
isc_mem_stats(mctx, stdout);
isc_memctx_destroy(&mctx);
1998-10-16 20:20:31 +00:00
}