xref: /illumos-gate/usr/src/cmd/fm/fmd/common/fmd_timerq.c (revision 2a8bcb4e)
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 #include <signal.h>
28*7c478bd9Sstevel@tonic-gate #include <strings.h>
29*7c478bd9Sstevel@tonic-gate #include <limits.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <fmd_alloc.h>
32*7c478bd9Sstevel@tonic-gate #include <fmd_subr.h>
33*7c478bd9Sstevel@tonic-gate #include <fmd_thread.h>
34*7c478bd9Sstevel@tonic-gate #include <fmd_timerq.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <fmd.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate /*
39*7c478bd9Sstevel@tonic-gate  * Install a new timer to fire after at least 'delta' nanoseconds have elapsed.
40*7c478bd9Sstevel@tonic-gate  * Timers are associated with persistent integer identifiers in some idspace.
41*7c478bd9Sstevel@tonic-gate  * We allocate a new timer structure or re-use one from our freelist, and then
42*7c478bd9Sstevel@tonic-gate  * place it on the queue's list in sorted order by expiration time.  If the new
43*7c478bd9Sstevel@tonic-gate  * timer is now the earliest to expire, we awaken the fmd_timerq_exec() thread.
44*7c478bd9Sstevel@tonic-gate  */
45*7c478bd9Sstevel@tonic-gate id_t
fmd_timerq_install(fmd_timerq_t * tmq,fmd_idspace_t * ids,fmd_timer_f * func,void * arg,fmd_event_t * ep,hrtime_t delta)46*7c478bd9Sstevel@tonic-gate fmd_timerq_install(fmd_timerq_t *tmq, fmd_idspace_t *ids,
47*7c478bd9Sstevel@tonic-gate     fmd_timer_f *func, void *arg, fmd_event_t *ep, hrtime_t delta)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	hrtime_t now = fmd_time_gethrtime();
50*7c478bd9Sstevel@tonic-gate 	hrtime_t base = ep ? fmd_event_hrtime(ep) : now;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	fmd_timer_t *tp, *up;
53*7c478bd9Sstevel@tonic-gate 	hrtime_t hrt;
54*7c478bd9Sstevel@tonic-gate 	id_t id;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmq->tmq_lock);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if ((tp = fmd_list_next(&tmq->tmq_free)) == NULL) {
59*7c478bd9Sstevel@tonic-gate 		tp = fmd_zalloc(sizeof (fmd_timer_t), FMD_SLEEP);
60*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_init(&tp->tmr_cv, NULL);
61*7c478bd9Sstevel@tonic-gate 	} else
62*7c478bd9Sstevel@tonic-gate 		fmd_list_delete(&tmq->tmq_free, tp);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	if ((id = fmd_idspace_alloc(ids, tp)) == -1) {
65*7c478bd9Sstevel@tonic-gate 		fmd_list_prepend(&tmq->tmq_free, tp);
66*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&tmq->tmq_lock);
67*7c478bd9Sstevel@tonic-gate 		return (id);
68*7c478bd9Sstevel@tonic-gate 	}
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if (delta < 0)
71*7c478bd9Sstevel@tonic-gate 		delta = 0; /* ensure delta is at least 0ns from now */
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	if (base + delta < base)
74*7c478bd9Sstevel@tonic-gate 		hrt = INT64_MAX; /* if wrap-around, set timer for apocalypse */
75*7c478bd9Sstevel@tonic-gate 	else
76*7c478bd9Sstevel@tonic-gate 		hrt = base + delta;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	tp->tmr_hrt = hrt;
79*7c478bd9Sstevel@tonic-gate 	tp->tmr_ids = ids;
80*7c478bd9Sstevel@tonic-gate 	tp->tmr_id = id;
81*7c478bd9Sstevel@tonic-gate 	tp->tmr_func = func;
82*7c478bd9Sstevel@tonic-gate 	tp->tmr_arg = arg;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	/*
85*7c478bd9Sstevel@tonic-gate 	 * For now we use a simple insertion sort for tmq_list.  If we have
86*7c478bd9Sstevel@tonic-gate 	 * scaling problems here due to heavy use of our timer subsystem,
87*7c478bd9Sstevel@tonic-gate 	 * then tmq_list can and should be replaced with a O(logN) heap.
88*7c478bd9Sstevel@tonic-gate 	 */
89*7c478bd9Sstevel@tonic-gate 	for (up = fmd_list_next(&tmq->tmq_list); up; up = fmd_list_next(up)) {
90*7c478bd9Sstevel@tonic-gate 		if (tp->tmr_hrt < up->tmr_hrt)
91*7c478bd9Sstevel@tonic-gate 			break;
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	if (up != NULL)
95*7c478bd9Sstevel@tonic-gate 		fmd_list_insert_before(&tmq->tmq_list, up, tp);
96*7c478bd9Sstevel@tonic-gate 	else
97*7c478bd9Sstevel@tonic-gate 		fmd_list_insert_after(&tmq->tmq_list, up, tp);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	if (up != NULL && fmd_list_next(&tmq->tmq_list) == tp)
100*7c478bd9Sstevel@tonic-gate 		fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
101*7c478bd9Sstevel@tonic-gate 	else if (up == NULL && fmd_list_next(&tmq->tmq_list) == tp)
102*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&tmq->tmq_cv);
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_TMR, "timer %s:%ld insert +%lldns",
107*7c478bd9Sstevel@tonic-gate 	    ids->ids_name, id, delta));
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	return (id);
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * Remove the specified timer.  If the 'id' is invalid, we'll panic inside of
114*7c478bd9Sstevel@tonic-gate  * fmd_idspace_free().  If the timer is still set, we move it to the freelist
115*7c478bd9Sstevel@tonic-gate  * and update the timer thread as needed.  If the timer 'id' is valid but
116*7c478bd9Sstevel@tonic-gate  * tmr_id is not equal to id, then the timer callback is running: we wait for
117*7c478bd9Sstevel@tonic-gate  * tmr_id to change to zero (indicating tmr_func is done) before returning.
118*7c478bd9Sstevel@tonic-gate  */
119*7c478bd9Sstevel@tonic-gate void *
fmd_timerq_remove(fmd_timerq_t * tmq,fmd_idspace_t * ids,id_t id)120*7c478bd9Sstevel@tonic-gate fmd_timerq_remove(fmd_timerq_t *tmq, fmd_idspace_t *ids, id_t id)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	hrtime_t delta = 0;
123*7c478bd9Sstevel@tonic-gate 	void *arg = NULL;
124*7c478bd9Sstevel@tonic-gate 	fmd_timer_t *tp;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmq->tmq_lock);
127*7c478bd9Sstevel@tonic-gate 	tp = fmd_idspace_free(ids, id);
128*7c478bd9Sstevel@tonic-gate 	ASSERT(tp == NULL || tp->tmr_ids == ids);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (tp == NULL) {
131*7c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&tmq->tmq_lock);
132*7c478bd9Sstevel@tonic-gate 		return (NULL); /* timer is no longer active */
133*7c478bd9Sstevel@tonic-gate 	}
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	if (tp->tmr_id == id) {
136*7c478bd9Sstevel@tonic-gate 		fmd_list_delete(&tmq->tmq_list, tp);
137*7c478bd9Sstevel@tonic-gate 		delta = tp->tmr_hrt - fmd_time_gethrtime();
138*7c478bd9Sstevel@tonic-gate 		arg = tp->tmr_arg;
139*7c478bd9Sstevel@tonic-gate 		tp->tmr_id = 0;
140*7c478bd9Sstevel@tonic-gate 		fmd_list_append(&tmq->tmq_free, tp);
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 		/*
143*7c478bd9Sstevel@tonic-gate 		 * If tmq_list is now empty, we must awaken the exec thread so
144*7c478bd9Sstevel@tonic-gate 		 * it will sleep on tmq_cv waiting for the list to change.  We
145*7c478bd9Sstevel@tonic-gate 		 * could also awaken the exec thread if we removed the head of
146*7c478bd9Sstevel@tonic-gate 		 * tmq_list, but an early wakeup is harmless so we do nothing.
147*7c478bd9Sstevel@tonic-gate 		 */
148*7c478bd9Sstevel@tonic-gate 		if (fmd_list_next(&tmq->tmq_list) == NULL)
149*7c478bd9Sstevel@tonic-gate 			fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
150*7c478bd9Sstevel@tonic-gate 	} else {
151*7c478bd9Sstevel@tonic-gate 		/*
152*7c478bd9Sstevel@tonic-gate 		 * Wait until tmr_id is zero, indicating that tmr_func is done.
153*7c478bd9Sstevel@tonic-gate 		 * This relies on expired fmd_timer_t's being returned to our
154*7c478bd9Sstevel@tonic-gate 		 * free list rather than having the data structure deallocated.
155*7c478bd9Sstevel@tonic-gate 		 */
156*7c478bd9Sstevel@tonic-gate 		while (tp->tmr_id != 0)
157*7c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&tp->tmr_cv, &tmq->tmq_lock);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_TMR, "timer %s:%ld remove -%lldns",
163*7c478bd9Sstevel@tonic-gate 	    ids->ids_name, id, delta > 0 ? delta : 0LL));
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	return (arg);
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate /*
169*7c478bd9Sstevel@tonic-gate  * fmd_timerq_exec() is the main loop of the thread that runs the timer queue.
170*7c478bd9Sstevel@tonic-gate  * We sleep on tmq_cv waiting for timers to show up on tmq_list.  When the list
171*7c478bd9Sstevel@tonic-gate  * is non-empty, we execute the callback function for each expired timer.  If
172*7c478bd9Sstevel@tonic-gate  * timers remain that are not yet expired, we nanosleep() until the next expiry
173*7c478bd9Sstevel@tonic-gate  * time.  We awaken whenever nanosleep() expires or we are interrupted by a
174*7c478bd9Sstevel@tonic-gate  * SIGALRM from fmd_timerq_install indicating that we need to rescan our list.
175*7c478bd9Sstevel@tonic-gate  */
176*7c478bd9Sstevel@tonic-gate static void
fmd_timerq_exec(fmd_timerq_t * tmq)177*7c478bd9Sstevel@tonic-gate fmd_timerq_exec(fmd_timerq_t *tmq)
178*7c478bd9Sstevel@tonic-gate {
179*7c478bd9Sstevel@tonic-gate 	fmd_timer_t *tp;
180*7c478bd9Sstevel@tonic-gate 	sigset_t set;
181*7c478bd9Sstevel@tonic-gate 	hrtime_t now;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	/*
184*7c478bd9Sstevel@tonic-gate 	 * fmd_thread_create() initializes threads with all signals blocked.
185*7c478bd9Sstevel@tonic-gate 	 * We must unblock SIGALRM (whose disposition has been to set to call
186*7c478bd9Sstevel@tonic-gate 	 * an empty function by fmd_timerq_init()) in order to permit directed
187*7c478bd9Sstevel@tonic-gate 	 * signals to interrupt our nanosleep() and make it return EINTR.
188*7c478bd9Sstevel@tonic-gate 	 * This SIGALRM mechanism is used by the native clock (see fmd_time.c).
189*7c478bd9Sstevel@tonic-gate 	 */
190*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&set);
191*7c478bd9Sstevel@tonic-gate 	(void) sigaddset(&set, SIGALRM);
192*7c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_UNBLOCK, &set, NULL);
193*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmq->tmq_lock);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	for (;;) {
196*7c478bd9Sstevel@tonic-gate 		while (!tmq->tmq_abort && fmd_list_next(&tmq->tmq_list) == NULL)
197*7c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&tmq->tmq_cv, &tmq->tmq_lock);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		if (tmq->tmq_abort) {
200*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
201*7c478bd9Sstevel@tonic-gate 			return; /* abort timerq thread */
202*7c478bd9Sstevel@tonic-gate 		}
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 		for (now = fmd_time_gethrtime(); (tp = fmd_list_next(
205*7c478bd9Sstevel@tonic-gate 		    &tmq->tmq_list)) != NULL; now = fmd_time_gethrtime()) {
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 			if (now == INT64_MAX || tp->tmr_hrt > now)
208*7c478bd9Sstevel@tonic-gate 				break; /* no more timers left to expire */
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 			tp->tmr_id = -tp->tmr_id;
211*7c478bd9Sstevel@tonic-gate 			fmd_list_delete(&tmq->tmq_list, tp);
212*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 			TRACE((FMD_DBG_TMR, "tmr %s:%ld exec start (hrt=%llx)",
215*7c478bd9Sstevel@tonic-gate 			    tp->tmr_ids->ids_name, -tp->tmr_id, tp->tmr_hrt));
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 			tp->tmr_func(tp->tmr_arg, -tp->tmr_id, tp->tmr_hrt);
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 			TRACE((FMD_DBG_TMR, "tmr %s:%ld exec end",
220*7c478bd9Sstevel@tonic-gate 			    tp->tmr_ids->ids_name, -tp->tmr_id));
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&tmq->tmq_lock);
223*7c478bd9Sstevel@tonic-gate 			(void) fmd_idspace_free(tp->tmr_ids, -tp->tmr_id);
224*7c478bd9Sstevel@tonic-gate 			fmd_list_append(&tmq->tmq_free, tp);
225*7c478bd9Sstevel@tonic-gate 			tp->tmr_id = 0; /* for fmd_timer_remove() */
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 			(void) pthread_cond_broadcast(&tp->tmr_cv);
228*7c478bd9Sstevel@tonic-gate 		}
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		if (tp != NULL) {
231*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&tmq->tmq_lock);
232*7c478bd9Sstevel@tonic-gate 			fmd_time_waithrtime(tp->tmr_hrt - now);
233*7c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_lock(&tmq->tmq_lock);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate static void
fmd_timerq_alrm(int sig)239*7c478bd9Sstevel@tonic-gate fmd_timerq_alrm(int sig)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	TRACE((FMD_DBG_TMR, "timer thread received alarm sig#%d", sig));
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate fmd_timerq_t *
fmd_timerq_create(void)245*7c478bd9Sstevel@tonic-gate fmd_timerq_create(void)
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	fmd_timerq_t *tmq = fmd_zalloc(sizeof (fmd_timerq_t), FMD_SLEEP);
248*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&tmq->tmq_lock, NULL);
251*7c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&tmq->tmq_cv, NULL);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 	act.sa_handler = fmd_timerq_alrm;
254*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
255*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
256*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	if ((tmq->tmq_thread = fmd_thread_create(fmd.d_rmod,
259*7c478bd9Sstevel@tonic-gate 	    (fmd_thread_f *)fmd_timerq_exec, tmq)) == NULL)
260*7c478bd9Sstevel@tonic-gate 		fmd_panic("failed to create timer thread");
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	return (tmq);
263*7c478bd9Sstevel@tonic-gate }
264*7c478bd9Sstevel@tonic-gate 
265*7c478bd9Sstevel@tonic-gate void
fmd_timerq_destroy(fmd_timerq_t * tmq)266*7c478bd9Sstevel@tonic-gate fmd_timerq_destroy(fmd_timerq_t *tmq)
267*7c478bd9Sstevel@tonic-gate {
268*7c478bd9Sstevel@tonic-gate 	struct sigaction act;
269*7c478bd9Sstevel@tonic-gate 	fmd_timer_t *tmr;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmq->tmq_lock);
272*7c478bd9Sstevel@tonic-gate 	tmq->tmq_abort++;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 	if (fmd_list_next(&tmq->tmq_list) != NULL)
275*7c478bd9Sstevel@tonic-gate 		fmd_time_waitcancel(tmq->tmq_thread->thr_tid);
276*7c478bd9Sstevel@tonic-gate 	else
277*7c478bd9Sstevel@tonic-gate 		(void) pthread_cond_signal(&tmq->tmq_cv);
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&tmq->tmq_lock);
280*7c478bd9Sstevel@tonic-gate 	fmd_thread_destroy(tmq->tmq_thread, FMD_THREAD_JOIN);
281*7c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&tmq->tmq_lock);
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	while ((tmr = fmd_list_next(&tmq->tmq_list)) != NULL) {
284*7c478bd9Sstevel@tonic-gate 		fmd_list_delete(&tmq->tmq_list, tmr);
285*7c478bd9Sstevel@tonic-gate 		(void) fmd_idspace_free(tmr->tmr_ids, tmr->tmr_id);
286*7c478bd9Sstevel@tonic-gate 		fmd_free(tmr, sizeof (fmd_timer_t));
287*7c478bd9Sstevel@tonic-gate 	}
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate 	while ((tmr = fmd_list_next(&tmq->tmq_free)) != NULL) {
290*7c478bd9Sstevel@tonic-gate 		fmd_list_delete(&tmq->tmq_free, tmr);
291*7c478bd9Sstevel@tonic-gate 		ASSERT(tmr->tmr_id == 0);
292*7c478bd9Sstevel@tonic-gate 		fmd_free(tmr, sizeof (fmd_timer_t));
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 
295*7c478bd9Sstevel@tonic-gate 	act.sa_handler = SIG_DFL;
296*7c478bd9Sstevel@tonic-gate 	act.sa_flags = 0;
297*7c478bd9Sstevel@tonic-gate 	(void) sigemptyset(&act.sa_mask);
298*7c478bd9Sstevel@tonic-gate 	(void) sigaction(SIGALRM, &act, NULL);
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 	fmd_free(tmq, sizeof (fmd_timerq_t));
301*7c478bd9Sstevel@tonic-gate }
302