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