1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include <stdlib.h> 30*0Sstevel@tonic-gate #include <limits.h> 31*0Sstevel@tonic-gate #include <sys/time.h> 32*0Sstevel@tonic-gate #include <sys/types.h> 33*0Sstevel@tonic-gate #include <sys/sysmacros.h> 34*0Sstevel@tonic-gate #include <sys/stropts.h> /* INFTIM */ 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include <libinetutil.h> 37*0Sstevel@tonic-gate #include "libinetutil_impl.h" 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate static iu_timer_node_t *pending_delete_chain = NULL; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate static void destroy_timer(iu_tq_t *, iu_timer_node_t *); 42*0Sstevel@tonic-gate static iu_timer_id_t get_timer_id(iu_tq_t *); 43*0Sstevel@tonic-gate static void release_timer_id(iu_tq_t *, iu_timer_id_t); 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * iu_tq_create(): creates, initializes and returns a timer queue for use 47*0Sstevel@tonic-gate * 48*0Sstevel@tonic-gate * input: void 49*0Sstevel@tonic-gate * output: iu_tq_t *: the new timer queue 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate iu_tq_t * 53*0Sstevel@tonic-gate iu_tq_create(void) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate return (calloc(1, sizeof (iu_tq_t))); 56*0Sstevel@tonic-gate } 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate /* 59*0Sstevel@tonic-gate * iu_tq_destroy(): destroys an existing timer queue 60*0Sstevel@tonic-gate * 61*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue to destroy 62*0Sstevel@tonic-gate * output: void 63*0Sstevel@tonic-gate */ 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate void 66*0Sstevel@tonic-gate iu_tq_destroy(iu_tq_t *tq) 67*0Sstevel@tonic-gate { 68*0Sstevel@tonic-gate iu_timer_node_t *node, *next_node; 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate for (node = tq->iutq_head; node != NULL; node = next_node) { 71*0Sstevel@tonic-gate next_node = node->iutn_next; 72*0Sstevel@tonic-gate destroy_timer(tq, node); 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate free(tq); 76*0Sstevel@tonic-gate } 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate /* 79*0Sstevel@tonic-gate * insert_timer(): inserts a timer node into a tq's timer list 80*0Sstevel@tonic-gate * 81*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 82*0Sstevel@tonic-gate * iu_timer_node_t *: the timer node to insert into the list 83*0Sstevel@tonic-gate * uint64_t: the number of milliseconds before this timer fires 84*0Sstevel@tonic-gate * output: void 85*0Sstevel@tonic-gate */ 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate static void 88*0Sstevel@tonic-gate insert_timer(iu_tq_t *tq, iu_timer_node_t *node, uint64_t msec) 89*0Sstevel@tonic-gate { 90*0Sstevel@tonic-gate iu_timer_node_t *after = NULL; 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * find the node to insert this new node "after". we do this 94*0Sstevel@tonic-gate * instead of the more intuitive "insert before" because with 95*0Sstevel@tonic-gate * the insert before approach, a null `before' node pointer 96*0Sstevel@tonic-gate * is overloaded in meaning (it could be null because there 97*0Sstevel@tonic-gate * are no items in the list, or it could be null because this 98*0Sstevel@tonic-gate * is the last item on the list, which are very different cases). 99*0Sstevel@tonic-gate */ 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate node->iutn_abs_timeout = gethrtime() + (msec * (NANOSEC / MILLISEC)); 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate if (tq->iutq_head != NULL && 104*0Sstevel@tonic-gate tq->iutq_head->iutn_abs_timeout < node->iutn_abs_timeout) 105*0Sstevel@tonic-gate for (after = tq->iutq_head; after->iutn_next != NULL; 106*0Sstevel@tonic-gate after = after->iutn_next) 107*0Sstevel@tonic-gate if (after->iutn_next->iutn_abs_timeout > 108*0Sstevel@tonic-gate node->iutn_abs_timeout) 109*0Sstevel@tonic-gate break; 110*0Sstevel@tonic-gate 111*0Sstevel@tonic-gate node->iutn_next = after ? after->iutn_next : tq->iutq_head; 112*0Sstevel@tonic-gate node->iutn_prev = after; 113*0Sstevel@tonic-gate if (after == NULL) 114*0Sstevel@tonic-gate tq->iutq_head = node; 115*0Sstevel@tonic-gate else 116*0Sstevel@tonic-gate after->iutn_next = node; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate if (node->iutn_next != NULL) 119*0Sstevel@tonic-gate node->iutn_next->iutn_prev = node; 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate /* 123*0Sstevel@tonic-gate * remove_timer(): removes a timer node from the tq's timer list 124*0Sstevel@tonic-gate * 125*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 126*0Sstevel@tonic-gate * iu_timer_node_t *: the timer node to remove from the list 127*0Sstevel@tonic-gate * output: void 128*0Sstevel@tonic-gate */ 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate static void 131*0Sstevel@tonic-gate remove_timer(iu_tq_t *tq, iu_timer_node_t *node) 132*0Sstevel@tonic-gate { 133*0Sstevel@tonic-gate if (node->iutn_next != NULL) 134*0Sstevel@tonic-gate node->iutn_next->iutn_prev = node->iutn_prev; 135*0Sstevel@tonic-gate if (node->iutn_prev != NULL) 136*0Sstevel@tonic-gate node->iutn_prev->iutn_next = node->iutn_next; 137*0Sstevel@tonic-gate else 138*0Sstevel@tonic-gate tq->iutq_head = node->iutn_next; 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate /* 142*0Sstevel@tonic-gate * destroy_timer(): destroy a timer node 143*0Sstevel@tonic-gate * 144*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue the timer node is associated with 145*0Sstevel@tonic-gate * iu_timer_node_t *: the node to free 146*0Sstevel@tonic-gate * output: void 147*0Sstevel@tonic-gate */ 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate static void 150*0Sstevel@tonic-gate destroy_timer(iu_tq_t *tq, iu_timer_node_t *node) 151*0Sstevel@tonic-gate { 152*0Sstevel@tonic-gate release_timer_id(tq, node->iutn_timer_id); 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* 155*0Sstevel@tonic-gate * if we're in expire, don't delete the node yet, since it may 156*0Sstevel@tonic-gate * still be referencing it (through the expire_next pointers) 157*0Sstevel@tonic-gate */ 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate if (tq->iutq_in_expire) { 160*0Sstevel@tonic-gate node->iutn_pending_delete++; 161*0Sstevel@tonic-gate node->iutn_next = pending_delete_chain; 162*0Sstevel@tonic-gate pending_delete_chain = node; 163*0Sstevel@tonic-gate } else 164*0Sstevel@tonic-gate free(node); 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate } 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate /* 169*0Sstevel@tonic-gate * iu_schedule_timer(): creates and inserts a timer in the tq's timer list 170*0Sstevel@tonic-gate * 171*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 172*0Sstevel@tonic-gate * uint32_t: the number of seconds before this timer fires 173*0Sstevel@tonic-gate * iu_tq_callback_t *: the function to call when the timer fires 174*0Sstevel@tonic-gate * void *: an argument to pass to the called back function 175*0Sstevel@tonic-gate * output: iu_timer_id_t: the new timer's timer id on success, -1 on failure 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate iu_timer_id_t 179*0Sstevel@tonic-gate iu_schedule_timer(iu_tq_t *tq, uint32_t sec, iu_tq_callback_t *callback, 180*0Sstevel@tonic-gate void *arg) 181*0Sstevel@tonic-gate { 182*0Sstevel@tonic-gate return (iu_schedule_timer_ms(tq, sec * MILLISEC, callback, arg)); 183*0Sstevel@tonic-gate } 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate /* 186*0Sstevel@tonic-gate * iu_schedule_ms_timer(): creates and inserts a timer in the tq's timer list, 187*0Sstevel@tonic-gate * using millisecond granularity 188*0Sstevel@tonic-gate * 189*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 190*0Sstevel@tonic-gate * uint64_t: the number of milliseconds before this timer fires 191*0Sstevel@tonic-gate * iu_tq_callback_t *: the function to call when the timer fires 192*0Sstevel@tonic-gate * void *: an argument to pass to the called back function 193*0Sstevel@tonic-gate * output: iu_timer_id_t: the new timer's timer id on success, -1 on failure 194*0Sstevel@tonic-gate */ 195*0Sstevel@tonic-gate iu_timer_id_t 196*0Sstevel@tonic-gate iu_schedule_timer_ms(iu_tq_t *tq, uint64_t ms, iu_tq_callback_t *callback, 197*0Sstevel@tonic-gate void *arg) 198*0Sstevel@tonic-gate { 199*0Sstevel@tonic-gate iu_timer_node_t *node = calloc(1, sizeof (iu_timer_node_t)); 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate if (node == NULL) 202*0Sstevel@tonic-gate return (-1); 203*0Sstevel@tonic-gate 204*0Sstevel@tonic-gate node->iutn_callback = callback; 205*0Sstevel@tonic-gate node->iutn_arg = arg; 206*0Sstevel@tonic-gate node->iutn_timer_id = get_timer_id(tq); 207*0Sstevel@tonic-gate if (node->iutn_timer_id == -1) { 208*0Sstevel@tonic-gate free(node); 209*0Sstevel@tonic-gate return (-1); 210*0Sstevel@tonic-gate } 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate insert_timer(tq, node, ms); 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate return (node->iutn_timer_id); 215*0Sstevel@tonic-gate } 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gate /* 218*0Sstevel@tonic-gate * iu_cancel_timer(): cancels a pending timer from a timer queue's timer list 219*0Sstevel@tonic-gate * 220*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 221*0Sstevel@tonic-gate * iu_timer_id_t: the timer id returned from iu_schedule_timer 222*0Sstevel@tonic-gate * void **: if non-NULL, a place to return the argument passed to 223*0Sstevel@tonic-gate * iu_schedule_timer 224*0Sstevel@tonic-gate * output: int: 1 on success, 0 on failure 225*0Sstevel@tonic-gate */ 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate int 228*0Sstevel@tonic-gate iu_cancel_timer(iu_tq_t *tq, iu_timer_id_t timer_id, void **arg) 229*0Sstevel@tonic-gate { 230*0Sstevel@tonic-gate iu_timer_node_t *node; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate if (timer_id == -1) 233*0Sstevel@tonic-gate return (0); 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate for (node = tq->iutq_head; node != NULL; node = node->iutn_next) { 236*0Sstevel@tonic-gate if (node->iutn_timer_id == timer_id) { 237*0Sstevel@tonic-gate if (arg != NULL) 238*0Sstevel@tonic-gate *arg = node->iutn_arg; 239*0Sstevel@tonic-gate remove_timer(tq, node); 240*0Sstevel@tonic-gate destroy_timer(tq, node); 241*0Sstevel@tonic-gate return (1); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate return (0); 245*0Sstevel@tonic-gate } 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate /* 248*0Sstevel@tonic-gate * iu_adjust_timer(): adjusts the fire time of a timer in the tq's timer list 249*0Sstevel@tonic-gate * 250*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 251*0Sstevel@tonic-gate * iu_timer_id_t: the timer id returned from iu_schedule_timer 252*0Sstevel@tonic-gate * uint32_t: the number of seconds before this timer fires 253*0Sstevel@tonic-gate * output: int: 1 on success, 0 on failure 254*0Sstevel@tonic-gate */ 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate int 257*0Sstevel@tonic-gate iu_adjust_timer(iu_tq_t *tq, iu_timer_id_t timer_id, uint32_t sec) 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate iu_timer_node_t *node; 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate if (timer_id == -1) 262*0Sstevel@tonic-gate return (0); 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate for (node = tq->iutq_head; node != NULL; node = node->iutn_next) { 265*0Sstevel@tonic-gate if (node->iutn_timer_id == timer_id) { 266*0Sstevel@tonic-gate remove_timer(tq, node); 267*0Sstevel@tonic-gate insert_timer(tq, node, sec * MILLISEC); 268*0Sstevel@tonic-gate return (1); 269*0Sstevel@tonic-gate } 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate return (0); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate /* 275*0Sstevel@tonic-gate * iu_earliest_timer(): returns the time until the next timer fires on a tq 276*0Sstevel@tonic-gate * 277*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 278*0Sstevel@tonic-gate * output: int: the number of milliseconds until the next timer (up to 279*0Sstevel@tonic-gate * a maximum value of INT_MAX), or INFTIM if no timers are pending. 280*0Sstevel@tonic-gate */ 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate int 283*0Sstevel@tonic-gate iu_earliest_timer(iu_tq_t *tq) 284*0Sstevel@tonic-gate { 285*0Sstevel@tonic-gate unsigned long long timeout_interval; 286*0Sstevel@tonic-gate hrtime_t current_time = gethrtime(); 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate if (tq->iutq_head == NULL) 289*0Sstevel@tonic-gate return (INFTIM); 290*0Sstevel@tonic-gate 291*0Sstevel@tonic-gate /* 292*0Sstevel@tonic-gate * event might've already happened if we haven't gotten a chance to 293*0Sstevel@tonic-gate * run in a while; return zero and pretend it just expired. 294*0Sstevel@tonic-gate */ 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate if (tq->iutq_head->iutn_abs_timeout <= current_time) 297*0Sstevel@tonic-gate return (0); 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gate /* 300*0Sstevel@tonic-gate * since the timers are ordered in absolute time-to-fire, just 301*0Sstevel@tonic-gate * subtract from the head of the list. 302*0Sstevel@tonic-gate */ 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gate timeout_interval = 305*0Sstevel@tonic-gate (tq->iutq_head->iutn_abs_timeout - current_time) / 1000000; 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate return (MIN(timeout_interval, INT_MAX)); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate /* 311*0Sstevel@tonic-gate * iu_expire_timers(): expires all pending timers on a given timer queue 312*0Sstevel@tonic-gate * 313*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 314*0Sstevel@tonic-gate * output: int: the number of timers expired 315*0Sstevel@tonic-gate */ 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gate int 318*0Sstevel@tonic-gate iu_expire_timers(iu_tq_t *tq) 319*0Sstevel@tonic-gate { 320*0Sstevel@tonic-gate iu_timer_node_t *node, *next_node; 321*0Sstevel@tonic-gate int n_expired = 0; 322*0Sstevel@tonic-gate hrtime_t current_time = gethrtime(); 323*0Sstevel@tonic-gate 324*0Sstevel@tonic-gate /* 325*0Sstevel@tonic-gate * in_expire is in the iu_tq_t instead of being passed through as 326*0Sstevel@tonic-gate * an argument to remove_timer() below since the callback 327*0Sstevel@tonic-gate * function may call iu_cancel_timer() itself as well. 328*0Sstevel@tonic-gate */ 329*0Sstevel@tonic-gate 330*0Sstevel@tonic-gate tq->iutq_in_expire++; 331*0Sstevel@tonic-gate 332*0Sstevel@tonic-gate /* 333*0Sstevel@tonic-gate * this function builds another linked list of timer nodes 334*0Sstevel@tonic-gate * through `expire_next' because the normal linked list 335*0Sstevel@tonic-gate * may be changed as a result of callbacks canceling and 336*0Sstevel@tonic-gate * scheduling timeouts, and thus can't be trusted. 337*0Sstevel@tonic-gate */ 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate for (node = tq->iutq_head; node != NULL; node = node->iutn_next) 340*0Sstevel@tonic-gate node->iutn_expire_next = node->iutn_next; 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate for (node = tq->iutq_head; node != NULL; 343*0Sstevel@tonic-gate node = node->iutn_expire_next) { 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate if (node->iutn_abs_timeout > current_time) 346*0Sstevel@tonic-gate break; 347*0Sstevel@tonic-gate 348*0Sstevel@tonic-gate /* 349*0Sstevel@tonic-gate * fringe condition: two timers fire at the "same 350*0Sstevel@tonic-gate * time" (i.e., they're both scheduled called back in 351*0Sstevel@tonic-gate * this loop) and one cancels the other. in this 352*0Sstevel@tonic-gate * case, the timer which has already been "cancelled" 353*0Sstevel@tonic-gate * should not be called back. 354*0Sstevel@tonic-gate */ 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate if (node->iutn_pending_delete) 357*0Sstevel@tonic-gate continue; 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate /* 360*0Sstevel@tonic-gate * we remove the timer before calling back the callback 361*0Sstevel@tonic-gate * so that a callback which accidentally tries to cancel 362*0Sstevel@tonic-gate * itself (through whatever means) doesn't succeed. 363*0Sstevel@tonic-gate */ 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate n_expired++; 366*0Sstevel@tonic-gate remove_timer(tq, node); 367*0Sstevel@tonic-gate destroy_timer(tq, node); 368*0Sstevel@tonic-gate node->iutn_callback(tq, node->iutn_arg); 369*0Sstevel@tonic-gate } 370*0Sstevel@tonic-gate 371*0Sstevel@tonic-gate tq->iutq_in_expire--; 372*0Sstevel@tonic-gate 373*0Sstevel@tonic-gate /* 374*0Sstevel@tonic-gate * any cancels that took place whilst we were expiring timeouts 375*0Sstevel@tonic-gate * ended up on the `pending_delete_chain'. delete them now 376*0Sstevel@tonic-gate * that it's safe. 377*0Sstevel@tonic-gate */ 378*0Sstevel@tonic-gate 379*0Sstevel@tonic-gate for (node = pending_delete_chain; node != NULL; node = next_node) { 380*0Sstevel@tonic-gate next_node = node->iutn_next; 381*0Sstevel@tonic-gate free(node); 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate pending_delete_chain = NULL; 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gate return (n_expired); 386*0Sstevel@tonic-gate } 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate /* 389*0Sstevel@tonic-gate * get_timer_id(): allocates a timer id from the pool 390*0Sstevel@tonic-gate * 391*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 392*0Sstevel@tonic-gate * output: iu_timer_id_t: the allocated timer id, or -1 if none available 393*0Sstevel@tonic-gate */ 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gate static iu_timer_id_t 396*0Sstevel@tonic-gate get_timer_id(iu_tq_t *tq) 397*0Sstevel@tonic-gate { 398*0Sstevel@tonic-gate unsigned int map_index; 399*0Sstevel@tonic-gate unsigned char map_bit; 400*0Sstevel@tonic-gate boolean_t have_wrapped = B_FALSE; 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gate for (; ; tq->iutq_next_timer_id++) { 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate if (tq->iutq_next_timer_id >= IU_TIMER_ID_MAX) { 405*0Sstevel@tonic-gate if (have_wrapped) 406*0Sstevel@tonic-gate return (-1); 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gate have_wrapped = B_TRUE; 409*0Sstevel@tonic-gate tq->iutq_next_timer_id = 0; 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate 412*0Sstevel@tonic-gate map_index = tq->iutq_next_timer_id / CHAR_BIT; 413*0Sstevel@tonic-gate map_bit = tq->iutq_next_timer_id % CHAR_BIT; 414*0Sstevel@tonic-gate 415*0Sstevel@tonic-gate if ((tq->iutq_timer_id_map[map_index] & (1 << map_bit)) == 0) 416*0Sstevel@tonic-gate break; 417*0Sstevel@tonic-gate } 418*0Sstevel@tonic-gate 419*0Sstevel@tonic-gate tq->iutq_timer_id_map[map_index] |= (1 << map_bit); 420*0Sstevel@tonic-gate return (tq->iutq_next_timer_id++); 421*0Sstevel@tonic-gate } 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate /* 424*0Sstevel@tonic-gate * release_timer_id(): releases a timer id back into the pool 425*0Sstevel@tonic-gate * 426*0Sstevel@tonic-gate * input: iu_tq_t *: the timer queue 427*0Sstevel@tonic-gate * iu_timer_id_t: the timer id to release 428*0Sstevel@tonic-gate * output: void 429*0Sstevel@tonic-gate */ 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate static void 432*0Sstevel@tonic-gate release_timer_id(iu_tq_t *tq, iu_timer_id_t timer_id) 433*0Sstevel@tonic-gate { 434*0Sstevel@tonic-gate unsigned int map_index = timer_id / CHAR_BIT; 435*0Sstevel@tonic-gate unsigned char map_bit = timer_id % CHAR_BIT; 436*0Sstevel@tonic-gate 437*0Sstevel@tonic-gate tq->iutq_timer_id_map[map_index] &= ~(1 << map_bit); 438*0Sstevel@tonic-gate } 439