1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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 http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
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>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.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,
|
2018-04-17 08:29:14 -07:00
|
|
|
&expires, &interval, true) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-04-26 23:42:37 +10:00
|
|
|
static char one[] = "1";
|
|
|
|
static char two[] = "2";
|
|
|
|
static char three[] = "3";
|
|
|
|
|
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
|
|
|
|
2013-04-11 17:07:50 +10:00
|
|
|
if (argc > 1) {
|
1998-10-16 20:20:31 +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-10-16 20:20:31 +00:00
|
|
|
workers = 2;
|
2018-02-14 19:08:40 +11:00
|
|
|
printf("%u workers\n", workers);
|
1998-10-16 20:20:31 +00:00
|
|
|
|
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);
|
2014-04-26 23:42:37 +10:00
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, 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, shutdown_task, 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, shutdown_task, three) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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);
|
|
|
|
|
2001-11-30 01:59:49 +00:00
|
|
|
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,
|
2014-04-26 23:42:37 +10:00
|
|
|
&interval, t2, timeout, two, &ti2) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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,
|
2014-04-26 23:42:37 +10:00
|
|
|
&interval, t1, tick, one, &ti1) ==
|
1999-06-12 01:12:35 +00:00
|
|
|
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,
|
2014-04-26 23:42:37 +10:00
|
|
|
&interval, t3, timeout, three, &ti3) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifndef WIN32
|
1998-10-16 20:20:31 +00:00
|
|
|
sleep(15);
|
2013-12-04 12:47:23 +11:00
|
|
|
#else
|
|
|
|
Sleep(15000);
|
|
|
|
#endif
|
1998-10-16 20:20:31 +00:00
|
|
|
printf("destroy\n");
|
1998-10-21 01:57:35 +00:00
|
|
|
isc_timer_detach(&ti1);
|
|
|
|
isc_timer_detach(&ti2);
|
|
|
|
isc_timer_detach(&ti3);
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifndef WIN32
|
1998-10-16 20:20:31 +00:00
|
|
|
sleep(2);
|
2013-12-04 12:47:23 +11:00
|
|
|
#else
|
|
|
|
Sleep(2000);
|
|
|
|
#endif
|
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
|
|
|
}
|