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-08-17 22:05:58 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#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-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>
|
2000-04-28 02:12:16 +00:00
|
|
|
#include <isc/util.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
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
my_callback(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
|
|
|
int i, j;
|
2000-04-17 19:22:44 +00:00
|
|
|
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);
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
my_shutdown(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
2000-04-17 19:22:44 +00:00
|
|
|
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);
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-08-17 22:05:58 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
my_tick(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
2000-04-17 19:22:44 +00:00
|
|
|
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);
|
1998-10-28 01:45:43 +00:00
|
|
|
isc_event_free(&event);
|
1998-08-18 19:28:30 +00:00
|
|
|
}
|
|
|
|
|
2014-04-26 23:42:37 +10:00
|
|
|
static char one[] = "1";
|
|
|
|
static char two[] = "2";
|
|
|
|
static char three[] = "3";
|
|
|
|
static char four[] = "4";
|
|
|
|
static char foo[] = "foo";
|
|
|
|
static char bar[] = "bar";
|
|
|
|
|
1999-07-03 21:15:06 +00:00
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
isc_taskmgr_t * manager = NULL;
|
|
|
|
isc_task_t * t1 = NULL, *t2 = NULL;
|
|
|
|
isc_task_t * t3 = NULL, *t4 = NULL;
|
|
|
|
isc_event_t * event;
|
|
|
|
unsigned int workers;
|
|
|
|
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
|
|
|
|
2013-04-11 17:07:50 +10:00
|
|
|
if (argc > 1) {
|
1998-08-17 23:15:50 +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-08-17 23:15:50 +00:00
|
|
|
workers = 2;
|
2018-02-14 19:08:18 +11:00
|
|
|
printf("%u workers\n", workers);
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2019-09-05 18:40:57 +02:00
|
|
|
isc_mem_create(&mctx);
|
1998-08-17 22:05:58 +00:00
|
|
|
|
2019-11-05 15:23:33 -08:00
|
|
|
RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, NULL, &manager) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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);
|
1998-12-16 02:05:38 +00:00
|
|
|
|
2014-04-26 23:42:37 +10:00
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t1, my_shutdown, 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, my_shutdown, 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, my_shutdown, three) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
ISC_R_SUCCESS);
|
2014-04-26 23:42:37 +10:00
|
|
|
RUNTIME_CHECK(isc_task_onshutdown(t4, my_shutdown, four) ==
|
1999-01-06 20:26:18 +00:00
|
|
|
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,
|
2020-02-12 13:59:18 +01:00
|
|
|
&interval, t1, my_tick, foo,
|
|
|
|
&ti1) == ISC_R_SUCCESS);
|
1999-06-12 01:12:35 +00:00
|
|
|
|
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,
|
2020-02-12 13:59:18 +01:00
|
|
|
&interval, t2, my_tick, bar,
|
|
|
|
&ti2) == 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);
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifndef WIN32
|
1998-08-18 19:28:30 +00:00
|
|
|
sleep(2);
|
2013-12-04 12:47:23 +11:00
|
|
|
#else
|
|
|
|
Sleep(2000);
|
|
|
|
#endif
|
1998-08-18 19:28:30 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, one,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t1, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t2, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t3, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t4, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, two,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t2, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, three,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t3, &event);
|
2014-04-26 23:42:37 +10:00
|
|
|
event = isc_event_allocate(mctx, (void *)1, 1, my_callback, four,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*event));
|
1998-10-21 02:26:57 +00:00
|
|
|
isc_task_send(t4, &event);
|
2020-02-12 13:59:18 +01: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
|
|
|
|
2013-12-04 12:47:23 +11:00
|
|
|
#ifndef WIN32
|
1998-10-16 20:20:31 +00:00
|
|
|
sleep(10);
|
2013-12-04 12:47:23 +11:00
|
|
|
#else
|
|
|
|
Sleep(10000);
|
|
|
|
#endif
|
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");
|
2000-08-01 01:33:37 +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
|
|
|
}
|