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