1 /* $NetBSD: timer_test.c,v 1.8 2014/12/10 04:37:53 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004, 2007, 2013, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1998-2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id: timer_test.c,v 1.40 2007/06/19 23:46:59 tbox Exp */ 21 22 #include <config.h> 23 24 #include <stdlib.h> 25 #include <string.h> 26 #include <unistd.h> 27 28 #include <isc/mem.h> 29 #include <isc/task.h> 30 #include <isc/time.h> 31 #include <isc/timer.h> 32 #include <isc/util.h> 33 34 isc_mem_t *mctx1, *mctx2, *mctx3; 35 isc_task_t *t1, *t2, *t3; 36 isc_timer_t *ti1, *ti2, *ti3; 37 int tick_count = 0; 38 39 static void 40 shutdown_task(isc_task_t *task, isc_event_t *event) { 41 char *name = event->ev_arg; 42 43 printf("task %p shutdown %s\n", task, name); 44 isc_event_free(&event); 45 } 46 47 static void 48 tick(isc_task_t *task, isc_event_t *event) { 49 char *name = event->ev_arg; 50 51 INSIST(event->ev_type == ISC_TIMEREVENT_TICK); 52 53 printf("task %s (%p) tick\n", name, task); 54 55 tick_count++; 56 if (ti3 != NULL && tick_count % 3 == 0) 57 isc_timer_touch(ti3); 58 59 if (ti3 != NULL && tick_count == 7) { 60 isc_time_t expires; 61 isc_interval_t interval; 62 63 isc_interval_set(&interval, 5, 0); 64 (void)isc_time_nowplusinterval(&expires, &interval); 65 isc_interval_set(&interval, 4, 0); 66 printf("*** resetting ti3 ***\n"); 67 RUNTIME_CHECK(isc_timer_reset(ti3, isc_timertype_once, 68 &expires, &interval, ISC_TRUE) == 69 ISC_R_SUCCESS); 70 } 71 72 isc_event_free(&event); 73 } 74 75 static void 76 timeout(isc_task_t *task, isc_event_t *event) { 77 char *name = event->ev_arg; 78 const char *type; 79 80 INSIST(event->ev_type == ISC_TIMEREVENT_IDLE || 81 event->ev_type == ISC_TIMEREVENT_LIFE); 82 83 if (event->ev_type == ISC_TIMEREVENT_IDLE) 84 type = "idle"; 85 else 86 type = "life"; 87 printf("task %s (%p) %s timeout\n", name, task, type); 88 89 if (strcmp(name, "3") == 0) { 90 printf("*** saving task 3 ***\n"); 91 isc_event_free(&event); 92 return; 93 } 94 95 isc_event_free(&event); 96 isc_task_shutdown(task); 97 } 98 99 static char one[] = "1"; 100 static char two[] = "2"; 101 static char three[] = "3"; 102 103 int 104 main(int argc, char *argv[]) { 105 isc_taskmgr_t *manager = NULL; 106 isc_timermgr_t *timgr = NULL; 107 unsigned int workers; 108 isc_time_t expires, now; 109 isc_interval_t interval; 110 111 if (argc > 1) { 112 workers = atoi(argv[1]); 113 if (workers < 1) 114 workers = 1; 115 if (workers > 8192) 116 workers = 8192; 117 } else 118 workers = 2; 119 printf("%d workers\n", workers); 120 121 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx1) == ISC_R_SUCCESS); 122 RUNTIME_CHECK(isc_taskmgr_create(mctx1, workers, 0, &manager) == 123 ISC_R_SUCCESS); 124 RUNTIME_CHECK(isc_timermgr_create(mctx1, &timgr) == ISC_R_SUCCESS); 125 126 RUNTIME_CHECK(isc_task_create(manager, 0, &t1) == 127 ISC_R_SUCCESS); 128 RUNTIME_CHECK(isc_task_create(manager, 0, &t2) == 129 ISC_R_SUCCESS); 130 RUNTIME_CHECK(isc_task_create(manager, 0, &t3) == 131 ISC_R_SUCCESS); 132 RUNTIME_CHECK(isc_task_onshutdown(t1, shutdown_task, one) == 133 ISC_R_SUCCESS); 134 RUNTIME_CHECK(isc_task_onshutdown(t2, shutdown_task, two) == 135 ISC_R_SUCCESS); 136 RUNTIME_CHECK(isc_task_onshutdown(t3, shutdown_task, three) == 137 ISC_R_SUCCESS); 138 139 printf("task 1: %p\n", t1); 140 printf("task 2: %p\n", t2); 141 printf("task 3: %p\n", t3); 142 143 TIME_NOW(&now); 144 145 isc_interval_set(&interval, 2, 0); 146 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, NULL, 147 &interval, t2, timeout, two, &ti2) == 148 ISC_R_SUCCESS); 149 150 isc_interval_set(&interval, 1, 0); 151 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_ticker, NULL, 152 &interval, t1, tick, one, &ti1) == 153 ISC_R_SUCCESS); 154 155 isc_interval_set(&interval, 10, 0); 156 RUNTIME_CHECK(isc_time_add(&now, &interval, &expires) == 157 ISC_R_SUCCESS); 158 isc_interval_set(&interval, 2, 0); 159 RUNTIME_CHECK(isc_timer_create(timgr, isc_timertype_once, &expires, 160 &interval, t3, timeout, three, &ti3) == 161 ISC_R_SUCCESS); 162 163 isc_task_detach(&t1); 164 isc_task_detach(&t2); 165 isc_task_detach(&t3); 166 167 #ifndef WIN32 168 sleep(15); 169 #else 170 Sleep(15000); 171 #endif 172 printf("destroy\n"); 173 isc_timer_detach(&ti1); 174 isc_timer_detach(&ti2); 175 isc_timer_detach(&ti3); 176 #ifndef WIN32 177 sleep(2); 178 #else 179 Sleep(2000); 180 #endif 181 isc_timermgr_destroy(&timgr); 182 isc_taskmgr_destroy(&manager); 183 printf("destroyed\n"); 184 185 printf("Statistics for mctx1:\n"); 186 isc_mem_stats(mctx1, stdout); 187 isc_mem_destroy(&mctx1); 188 189 return (0); 190 } 191