1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <fmd_alloc.h> 30*7c478bd9Sstevel@tonic-gate #include <fmd_eventq.h> 31*7c478bd9Sstevel@tonic-gate #include <fmd_module.h> 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate fmd_eventq_t * 34*7c478bd9Sstevel@tonic-gate fmd_eventq_create(fmd_module_t *mp, uint_t limit) 35*7c478bd9Sstevel@tonic-gate { 36*7c478bd9Sstevel@tonic-gate fmd_eventq_t *eq = fmd_zalloc(sizeof (fmd_eventq_t), FMD_SLEEP); 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_init(&eq->eq_lock, NULL); 39*7c478bd9Sstevel@tonic-gate (void) pthread_cond_init(&eq->eq_cv, NULL); 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate eq->eq_mod = mp; 42*7c478bd9Sstevel@tonic-gate eq->eq_limit = limit; 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate return (eq); 45*7c478bd9Sstevel@tonic-gate } 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate void 48*7c478bd9Sstevel@tonic-gate fmd_eventq_destroy(fmd_eventq_t *eq) 49*7c478bd9Sstevel@tonic-gate { 50*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate while ((eqe = fmd_list_next(&eq->eq_list)) != NULL) { 53*7c478bd9Sstevel@tonic-gate fmd_list_delete(&eq->eq_list, eqe); 54*7c478bd9Sstevel@tonic-gate fmd_event_rele(eqe->eqe_event); 55*7c478bd9Sstevel@tonic-gate fmd_free(eqe, sizeof (fmd_eventqelem_t)); 56*7c478bd9Sstevel@tonic-gate } 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate fmd_free(eq, sizeof (fmd_eventq_t)); 59*7c478bd9Sstevel@tonic-gate } 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static void 62*7c478bd9Sstevel@tonic-gate fmd_eventq_drop(fmd_eventq_t *eq, fmd_eventqelem_t *eqe) 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_mod->mod_stats_lock); 65*7c478bd9Sstevel@tonic-gate eq->eq_mod->mod_stats->ms_dropped.fmds_value.ui64++; 66*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_mod->mod_stats_lock); 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate fmd_event_rele(eqe->eqe_event); 69*7c478bd9Sstevel@tonic-gate fmd_free(eqe, sizeof (fmd_eventqelem_t)); 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate void 73*7c478bd9Sstevel@tonic-gate fmd_eventq_insert_at_head(fmd_eventq_t *eq, fmd_event_t *ep) 74*7c478bd9Sstevel@tonic-gate { 75*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe = fmd_alloc(sizeof (fmd_eventqelem_t), FMD_SLEEP); 76*7c478bd9Sstevel@tonic-gate uint_t evt = ((fmd_event_impl_t *)ep)->ev_type; 77*7c478bd9Sstevel@tonic-gate int ok; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate fmd_event_hold(ep); 80*7c478bd9Sstevel@tonic-gate eqe->eqe_event = ep; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_lock); 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if ((ok = eq->eq_size < eq->eq_limit || evt != FMD_EVT_PROTOCOL) != 0) { 85*7c478bd9Sstevel@tonic-gate if (evt != FMD_EVT_CTL) 86*7c478bd9Sstevel@tonic-gate fmd_modstat_eventq_dispatch(eq->eq_mod); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate fmd_list_prepend(&eq->eq_list, eqe); 89*7c478bd9Sstevel@tonic-gate eq->eq_size++; 90*7c478bd9Sstevel@tonic-gate } 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 93*7c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&eq->eq_cv); 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if (!ok) 96*7c478bd9Sstevel@tonic-gate fmd_eventq_drop(eq, eqe); 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate void 100*7c478bd9Sstevel@tonic-gate fmd_eventq_insert_at_time(fmd_eventq_t *eq, fmd_event_t *ep) 101*7c478bd9Sstevel@tonic-gate { 102*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe = fmd_alloc(sizeof (fmd_eventqelem_t), FMD_SLEEP); 103*7c478bd9Sstevel@tonic-gate uint_t evt = ((fmd_event_impl_t *)ep)->ev_type; 104*7c478bd9Sstevel@tonic-gate hrtime_t hrt = fmd_event_hrtime(ep); 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *oqe; 107*7c478bd9Sstevel@tonic-gate int ok; 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate fmd_event_hold(ep); 110*7c478bd9Sstevel@tonic-gate eqe->eqe_event = ep; 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_lock); 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate /* 115*7c478bd9Sstevel@tonic-gate * fmd makes no guarantees that events will be delivered in time order 116*7c478bd9Sstevel@tonic-gate * because its transport can make no such guarantees. Instead we make 117*7c478bd9Sstevel@tonic-gate * a looser guarantee that an enqueued event will be dequeued before 118*7c478bd9Sstevel@tonic-gate * any newer *pending* events according to event time. This permits us 119*7c478bd9Sstevel@tonic-gate * to state, for example, that a timer expiry event will be delivered 120*7c478bd9Sstevel@tonic-gate * prior to any enqueued event whose time is after the timer expired. 121*7c478bd9Sstevel@tonic-gate * We use a simple insertion sort for this task, as queue lengths are 122*7c478bd9Sstevel@tonic-gate * typically short and events do *tend* to be received chronologically. 123*7c478bd9Sstevel@tonic-gate */ 124*7c478bd9Sstevel@tonic-gate for (oqe = fmd_list_prev(&eq->eq_list); oqe; oqe = fmd_list_prev(oqe)) { 125*7c478bd9Sstevel@tonic-gate if (hrt >= fmd_event_hrtime(oqe->eqe_event)) 126*7c478bd9Sstevel@tonic-gate break; /* 'ep' is newer than the event in 'oqe' */ 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if ((ok = eq->eq_size < eq->eq_limit || evt != FMD_EVT_PROTOCOL) != 0) { 130*7c478bd9Sstevel@tonic-gate if (evt != FMD_EVT_CTL) 131*7c478bd9Sstevel@tonic-gate fmd_modstat_eventq_dispatch(eq->eq_mod); 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate fmd_list_insert_after(&eq->eq_list, oqe, eqe); 134*7c478bd9Sstevel@tonic-gate eq->eq_size++; 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 138*7c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&eq->eq_cv); 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate if (!ok) 141*7c478bd9Sstevel@tonic-gate fmd_eventq_drop(eq, eqe); 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate fmd_event_t * 145*7c478bd9Sstevel@tonic-gate fmd_eventq_delete(fmd_eventq_t *eq) 146*7c478bd9Sstevel@tonic-gate { 147*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe; 148*7c478bd9Sstevel@tonic-gate fmd_event_t *ep; 149*7c478bd9Sstevel@tonic-gate 150*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_lock); 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate while (eq->eq_size == 0 && eq->eq_abort == 0) 153*7c478bd9Sstevel@tonic-gate (void) pthread_cond_wait(&eq->eq_cv, &eq->eq_lock); 154*7c478bd9Sstevel@tonic-gate 155*7c478bd9Sstevel@tonic-gate if (eq->eq_abort) { 156*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 157*7c478bd9Sstevel@tonic-gate return (NULL); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate eqe = fmd_list_next(&eq->eq_list); 161*7c478bd9Sstevel@tonic-gate fmd_list_delete(&eq->eq_list, eqe); 162*7c478bd9Sstevel@tonic-gate eq->eq_size--; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate ep = eqe->eqe_event; 167*7c478bd9Sstevel@tonic-gate fmd_free(eqe, sizeof (fmd_eventqelem_t)); 168*7c478bd9Sstevel@tonic-gate 169*7c478bd9Sstevel@tonic-gate return (ep); 170*7c478bd9Sstevel@tonic-gate } 171*7c478bd9Sstevel@tonic-gate 172*7c478bd9Sstevel@tonic-gate void 173*7c478bd9Sstevel@tonic-gate fmd_eventq_cancel(fmd_eventq_t *eq, uint_t type, void *data) 174*7c478bd9Sstevel@tonic-gate { 175*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe, *nqe; 176*7c478bd9Sstevel@tonic-gate 177*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_lock); 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate for (eqe = fmd_list_next(&eq->eq_list); eqe != NULL; eqe = nqe) { 180*7c478bd9Sstevel@tonic-gate nqe = fmd_list_next(eqe); 181*7c478bd9Sstevel@tonic-gate 182*7c478bd9Sstevel@tonic-gate if (fmd_event_match(eqe->eqe_event, type, data)) { 183*7c478bd9Sstevel@tonic-gate fmd_list_delete(&eq->eq_list, eqe); 184*7c478bd9Sstevel@tonic-gate eq->eq_size--; 185*7c478bd9Sstevel@tonic-gate fmd_event_rele(eqe->eqe_event); 186*7c478bd9Sstevel@tonic-gate fmd_free(eqe, sizeof (fmd_eventqelem_t)); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate } 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 191*7c478bd9Sstevel@tonic-gate } 192*7c478bd9Sstevel@tonic-gate 193*7c478bd9Sstevel@tonic-gate void 194*7c478bd9Sstevel@tonic-gate fmd_eventq_abort(fmd_eventq_t *eq) 195*7c478bd9Sstevel@tonic-gate { 196*7c478bd9Sstevel@tonic-gate fmd_eventqelem_t *eqe; 197*7c478bd9Sstevel@tonic-gate 198*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_lock(&eq->eq_lock); 199*7c478bd9Sstevel@tonic-gate 200*7c478bd9Sstevel@tonic-gate while ((eqe = fmd_list_next(&eq->eq_list)) != NULL) { 201*7c478bd9Sstevel@tonic-gate fmd_list_delete(&eq->eq_list, eqe); 202*7c478bd9Sstevel@tonic-gate fmd_event_rele(eqe->eqe_event); 203*7c478bd9Sstevel@tonic-gate fmd_free(eqe, sizeof (fmd_eventqelem_t)); 204*7c478bd9Sstevel@tonic-gate } 205*7c478bd9Sstevel@tonic-gate 206*7c478bd9Sstevel@tonic-gate eq->eq_abort++; /* signal fmd_eventq_delete() to abort */ 207*7c478bd9Sstevel@tonic-gate 208*7c478bd9Sstevel@tonic-gate (void) pthread_mutex_unlock(&eq->eq_lock); 209*7c478bd9Sstevel@tonic-gate (void) pthread_cond_broadcast(&eq->eq_cv); 210*7c478bd9Sstevel@tonic-gate } 211