xref: /freebsd/sys/kern/kern_timeout.c (revision b336df68d1445a7b211bfdc50a3c5485f554c6a3)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38acc8326dSGarrett Wollman  *	From: @(#)kern_clock.c	8.5 (Berkeley) 1/21/94
39c3aac50fSPeter Wemm  * $FreeBSD$
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
4415b7a470SPoul-Henning Kamp #include <sys/callout.h>
45df8bae1dSRodney W. Grimes #include <sys/kernel.h>
46f34fa851SJohn Baldwin #include <sys/lock.h>
47cb799bfeSJohn Baldwin #include <sys/mutex.h>
48df8bae1dSRodney W. Grimes 
4915b7a470SPoul-Henning Kamp /*
5015b7a470SPoul-Henning Kamp  * TODO:
5115b7a470SPoul-Henning Kamp  *	allocate more timeout table slots when table overflows.
5215b7a470SPoul-Henning Kamp  */
5315b7a470SPoul-Henning Kamp 
5415b7a470SPoul-Henning Kamp /* Exported to machdep.c and/or kern_clock.c.  */
55ab36c067SJustin T. Gibbs struct callout *callout;
56ab36c067SJustin T. Gibbs struct callout_list callfree;
57ab36c067SJustin T. Gibbs int callwheelsize, callwheelbits, callwheelmask;
58ab36c067SJustin T. Gibbs struct callout_tailq *callwheel;
5915b7a470SPoul-Henning Kamp int softticks;			/* Like ticks, but for softclock(). */
60fa2fbc3dSJake Burkholder struct mtx callout_lock;
61f23b4c91SGarrett Wollman 
62ab36c067SJustin T. Gibbs static struct callout *nextsoftcheck;	/* Next callout to be checked. */
63df8bae1dSRodney W. Grimes 
64df8bae1dSRodney W. Grimes /*
65219d632cSMatthew Dillon  * kern_timeout_callwheel_alloc() - kernel low level callwheel initialization
66219d632cSMatthew Dillon  *
67219d632cSMatthew Dillon  *	This code is called very early in the kernel initialization sequence,
68219d632cSMatthew Dillon  *	and may be called more then once.
69219d632cSMatthew Dillon  */
70219d632cSMatthew Dillon caddr_t
71219d632cSMatthew Dillon kern_timeout_callwheel_alloc(caddr_t v)
72219d632cSMatthew Dillon {
73219d632cSMatthew Dillon 	/*
74219d632cSMatthew Dillon 	 * Calculate callout wheel size
75219d632cSMatthew Dillon 	 */
76219d632cSMatthew Dillon 	for (callwheelsize = 1, callwheelbits = 0;
77219d632cSMatthew Dillon 	     callwheelsize < ncallout;
78219d632cSMatthew Dillon 	     callwheelsize <<= 1, ++callwheelbits)
79219d632cSMatthew Dillon 		;
80219d632cSMatthew Dillon 	callwheelmask = callwheelsize - 1;
81219d632cSMatthew Dillon 
82219d632cSMatthew Dillon 	callout = (struct callout *)v;
83219d632cSMatthew Dillon 	v = (caddr_t)(callout + ncallout);
84219d632cSMatthew Dillon 	callwheel = (struct callout_tailq *)v;
85219d632cSMatthew Dillon 	v = (caddr_t)(callwheel + callwheelsize);
86219d632cSMatthew Dillon 	return(v);
87219d632cSMatthew Dillon }
88219d632cSMatthew Dillon 
89219d632cSMatthew Dillon /*
90219d632cSMatthew Dillon  * kern_timeout_callwheel_init() - initialize previously reserved callwheel
91219d632cSMatthew Dillon  *				   space.
92219d632cSMatthew Dillon  *
93219d632cSMatthew Dillon  *	This code is called just once, after the space reserved for the
94219d632cSMatthew Dillon  *	callout wheel has been finalized.
95219d632cSMatthew Dillon  */
96219d632cSMatthew Dillon void
97219d632cSMatthew Dillon kern_timeout_callwheel_init(void)
98219d632cSMatthew Dillon {
99219d632cSMatthew Dillon 	int i;
100219d632cSMatthew Dillon 
101219d632cSMatthew Dillon 	SLIST_INIT(&callfree);
102219d632cSMatthew Dillon 	for (i = 0; i < ncallout; i++) {
103219d632cSMatthew Dillon 		callout_init(&callout[i], 0);
104219d632cSMatthew Dillon 		callout[i].c_flags = CALLOUT_LOCAL_ALLOC;
105219d632cSMatthew Dillon 		SLIST_INSERT_HEAD(&callfree, &callout[i], c_links.sle);
106219d632cSMatthew Dillon 	}
107219d632cSMatthew Dillon 	for (i = 0; i < callwheelsize; i++) {
108219d632cSMatthew Dillon 		TAILQ_INIT(&callwheel[i]);
109219d632cSMatthew Dillon 	}
1106008862bSJohn Baldwin 	mtx_init(&callout_lock, "callout", NULL, MTX_SPIN | MTX_RECURSE);
111219d632cSMatthew Dillon }
112219d632cSMatthew Dillon 
113219d632cSMatthew Dillon /*
114ab36c067SJustin T. Gibbs  * The callout mechanism is based on the work of Adam M. Costello and
115ab36c067SJustin T. Gibbs  * George Varghese, published in a technical report entitled "Redesigning
116ab36c067SJustin T. Gibbs  * the BSD Callout and Timer Facilities" and modified slightly for inclusion
117ab36c067SJustin T. Gibbs  * in FreeBSD by Justin T. Gibbs.  The original work on the data structures
118ab36c067SJustin T. Gibbs  * used in this implementation was published by G.Varghese and A. Lauck in
119ab36c067SJustin T. Gibbs  * the paper "Hashed and Hierarchical Timing Wheels: Data Structures for
120ab36c067SJustin T. Gibbs  * the Efficient Implementation of a Timer Facility" in the Proceedings of
121ab36c067SJustin T. Gibbs  * the 11th ACM Annual Symposium on Operating Systems Principles,
122ab36c067SJustin T. Gibbs  * Austin, Texas Nov 1987.
123ab36c067SJustin T. Gibbs  */
124a50ec505SPoul-Henning Kamp 
125ab36c067SJustin T. Gibbs /*
126df8bae1dSRodney W. Grimes  * Software (low priority) clock interrupt.
127df8bae1dSRodney W. Grimes  * Run periodic events from timeout queue.
128df8bae1dSRodney W. Grimes  */
129df8bae1dSRodney W. Grimes void
1308088699fSJohn Baldwin softclock(void *dummy)
131df8bae1dSRodney W. Grimes {
132b336df68SPoul-Henning Kamp 	struct callout *c;
133b336df68SPoul-Henning Kamp 	struct callout_tailq *bucket;
134b336df68SPoul-Henning Kamp 	int curticks;
135b336df68SPoul-Henning Kamp 	int steps;	/* #steps since we last allowed interrupts */
136b336df68SPoul-Henning Kamp #ifdef DIAGNOSTIC
137b336df68SPoul-Henning Kamp 	struct bintime bt1, bt2;
138b336df68SPoul-Henning Kamp 	struct timespec ts2;
139b336df68SPoul-Henning Kamp 	static uint64_t maxdt = 18446744073709551LL;	/* 1 msec */
140b336df68SPoul-Henning Kamp #endif
141df8bae1dSRodney W. Grimes 
14215b7a470SPoul-Henning Kamp #ifndef MAX_SOFTCLOCK_STEPS
14315b7a470SPoul-Henning Kamp #define MAX_SOFTCLOCK_STEPS 100 /* Maximum allowed value of steps. */
14415b7a470SPoul-Henning Kamp #endif /* MAX_SOFTCLOCK_STEPS */
145ab36c067SJustin T. Gibbs 
146ab36c067SJustin T. Gibbs 	steps = 0;
1479ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
148ab36c067SJustin T. Gibbs 	while (softticks != ticks) {
14945327611SJustin T. Gibbs 		softticks++;
15045327611SJustin T. Gibbs 		/*
15145327611SJustin T. Gibbs 		 * softticks may be modified by hard clock, so cache
15245327611SJustin T. Gibbs 		 * it while we work on a given bucket.
15345327611SJustin T. Gibbs 		 */
15445327611SJustin T. Gibbs 		curticks = softticks;
15545327611SJustin T. Gibbs 		bucket = &callwheel[curticks & callwheelmask];
15645327611SJustin T. Gibbs 		c = TAILQ_FIRST(bucket);
157ab36c067SJustin T. Gibbs 		while (c) {
15845327611SJustin T. Gibbs 			if (c->c_time != curticks) {
159ab36c067SJustin T. Gibbs 				c = TAILQ_NEXT(c, c_links.tqe);
160ab36c067SJustin T. Gibbs 				++steps;
161ab36c067SJustin T. Gibbs 				if (steps >= MAX_SOFTCLOCK_STEPS) {
162ab36c067SJustin T. Gibbs 					nextsoftcheck = c;
16345327611SJustin T. Gibbs 					/* Give interrupts a chance. */
1649ed346baSBosko Milekic 					mtx_unlock_spin(&callout_lock);
165ab32297dSJohn Baldwin 					;	/* nothing */
1669ed346baSBosko Milekic 					mtx_lock_spin(&callout_lock);
167ab36c067SJustin T. Gibbs 					c = nextsoftcheck;
168ab36c067SJustin T. Gibbs 					steps = 0;
169df8bae1dSRodney W. Grimes 				}
170ab36c067SJustin T. Gibbs 			} else {
171ab36c067SJustin T. Gibbs 				void (*c_func)(void *);
172ab36c067SJustin T. Gibbs 				void *c_arg;
173fa2fbc3dSJake Burkholder 				int c_flags;
174ab36c067SJustin T. Gibbs 
175ab36c067SJustin T. Gibbs 				nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
17645327611SJustin T. Gibbs 				TAILQ_REMOVE(bucket, c, c_links.tqe);
177ab36c067SJustin T. Gibbs 				c_func = c->c_func;
178ab36c067SJustin T. Gibbs 				c_arg = c->c_arg;
179fa2fbc3dSJake Burkholder 				c_flags = c->c_flags;
180ab36c067SJustin T. Gibbs 				c->c_func = NULL;
181acc8326dSGarrett Wollman 				if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
182acc8326dSGarrett Wollman 					c->c_flags = CALLOUT_LOCAL_ALLOC;
183acc8326dSGarrett Wollman 					SLIST_INSERT_HEAD(&callfree, c,
184acc8326dSGarrett Wollman 							  c_links.sle);
185acc8326dSGarrett Wollman 				} else {
186acc8326dSGarrett Wollman 					c->c_flags =
1879b8b58e0SJonathan Lemon 					    (c->c_flags & ~CALLOUT_PENDING);
188acc8326dSGarrett Wollman 				}
1899ed346baSBosko Milekic 				mtx_unlock_spin(&callout_lock);
190fa2fbc3dSJake Burkholder 				if (!(c_flags & CALLOUT_MPSAFE))
1919ed346baSBosko Milekic 					mtx_lock(&Giant);
192b336df68SPoul-Henning Kamp #ifdef DIAGNOSTIC
193b336df68SPoul-Henning Kamp 				binuptime(&bt1);
194b336df68SPoul-Henning Kamp #endif
195ab36c067SJustin T. Gibbs 				c_func(c_arg);
196b336df68SPoul-Henning Kamp #ifdef DIAGNOSTIC
197b336df68SPoul-Henning Kamp 				binuptime(&bt2);
198b336df68SPoul-Henning Kamp 				bintime_sub(&bt2, &bt1);
199b336df68SPoul-Henning Kamp 				if (bt2.frac > maxdt) {
200b336df68SPoul-Henning Kamp 					bintime2timespec(&bt2, &ts2);
201b336df68SPoul-Henning Kamp 					printf(
202b336df68SPoul-Henning Kamp 			"Expensive timeout(9) function: %p(%p) %d.%09d\n",
203b336df68SPoul-Henning Kamp 					c_func, c_arg,
204b336df68SPoul-Henning Kamp 					ts2.tv_sec, ts2.tv_nsec);
205b336df68SPoul-Henning Kamp 				}
206b336df68SPoul-Henning Kamp #endif
207fa2fbc3dSJake Burkholder 				if (!(c_flags & CALLOUT_MPSAFE))
2089ed346baSBosko Milekic 					mtx_unlock(&Giant);
2099ed346baSBosko Milekic 				mtx_lock_spin(&callout_lock);
210ab36c067SJustin T. Gibbs 				steps = 0;
211ab36c067SJustin T. Gibbs 				c = nextsoftcheck;
212ab36c067SJustin T. Gibbs 			}
213ab36c067SJustin T. Gibbs 		}
214ab36c067SJustin T. Gibbs 	}
215ab36c067SJustin T. Gibbs 	nextsoftcheck = NULL;
2169ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes /*
220df8bae1dSRodney W. Grimes  * timeout --
221df8bae1dSRodney W. Grimes  *	Execute a function after a specified length of time.
222df8bae1dSRodney W. Grimes  *
223df8bae1dSRodney W. Grimes  * untimeout --
224df8bae1dSRodney W. Grimes  *	Cancel previous timeout function call.
225df8bae1dSRodney W. Grimes  *
226ab36c067SJustin T. Gibbs  * callout_handle_init --
227ab36c067SJustin T. Gibbs  *	Initialize a handle so that using it with untimeout is benign.
228ab36c067SJustin T. Gibbs  *
229df8bae1dSRodney W. Grimes  *	See AT&T BCI Driver Reference Manual for specification.  This
230ab36c067SJustin T. Gibbs  *	implementation differs from that one in that although an
231ab36c067SJustin T. Gibbs  *	identification value is returned from timeout, the original
232ab36c067SJustin T. Gibbs  *	arguments to timeout as well as the identifier are used to
233ab36c067SJustin T. Gibbs  *	identify entries for untimeout.
234df8bae1dSRodney W. Grimes  */
235ab36c067SJustin T. Gibbs struct callout_handle
236ab36c067SJustin T. Gibbs timeout(ftn, arg, to_ticks)
2378f03c6f1SBruce Evans 	timeout_t *ftn;
238df8bae1dSRodney W. Grimes 	void *arg;
239e82ac18eSJonathan Lemon 	int to_ticks;
240df8bae1dSRodney W. Grimes {
241ab36c067SJustin T. Gibbs 	struct callout *new;
242ab36c067SJustin T. Gibbs 	struct callout_handle handle;
243df8bae1dSRodney W. Grimes 
2449ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	/* Fill in the next free callout structure. */
247ab36c067SJustin T. Gibbs 	new = SLIST_FIRST(&callfree);
248ab36c067SJustin T. Gibbs 	if (new == NULL)
249ab36c067SJustin T. Gibbs 		/* XXX Attempt to malloc first */
250df8bae1dSRodney W. Grimes 		panic("timeout table full");
251ab36c067SJustin T. Gibbs 	SLIST_REMOVE_HEAD(&callfree, c_links.sle);
252df8bae1dSRodney W. Grimes 
253acc8326dSGarrett Wollman 	callout_reset(new, to_ticks, ftn, arg);
254acc8326dSGarrett Wollman 
255ab36c067SJustin T. Gibbs 	handle.callout = new;
2569ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
257ab36c067SJustin T. Gibbs 	return (handle);
258df8bae1dSRodney W. Grimes }
259df8bae1dSRodney W. Grimes 
260df8bae1dSRodney W. Grimes void
261ab36c067SJustin T. Gibbs untimeout(ftn, arg, handle)
2628f03c6f1SBruce Evans 	timeout_t *ftn;
263df8bae1dSRodney W. Grimes 	void *arg;
264ab36c067SJustin T. Gibbs 	struct callout_handle handle;
265df8bae1dSRodney W. Grimes {
266df8bae1dSRodney W. Grimes 
267ab36c067SJustin T. Gibbs 	/*
268ab36c067SJustin T. Gibbs 	 * Check for a handle that was initialized
269ab36c067SJustin T. Gibbs 	 * by callout_handle_init, but never used
270ab36c067SJustin T. Gibbs 	 * for a real timeout.
271ab36c067SJustin T. Gibbs 	 */
272ab36c067SJustin T. Gibbs 	if (handle.callout == NULL)
273ab36c067SJustin T. Gibbs 		return;
274df8bae1dSRodney W. Grimes 
2759ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
276acc8326dSGarrett Wollman 	if (handle.callout->c_func == ftn && handle.callout->c_arg == arg)
277acc8326dSGarrett Wollman 		callout_stop(handle.callout);
2789ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
279df8bae1dSRodney W. Grimes }
280df8bae1dSRodney W. Grimes 
2813c816944SBruce Evans void
282ab36c067SJustin T. Gibbs callout_handle_init(struct callout_handle *handle)
283ab36c067SJustin T. Gibbs {
284ab36c067SJustin T. Gibbs 	handle->callout = NULL;
285ab36c067SJustin T. Gibbs }
286ab36c067SJustin T. Gibbs 
287acc8326dSGarrett Wollman /*
288acc8326dSGarrett Wollman  * New interface; clients allocate their own callout structures.
289acc8326dSGarrett Wollman  *
290acc8326dSGarrett Wollman  * callout_reset() - establish or change a timeout
291acc8326dSGarrett Wollman  * callout_stop() - disestablish a timeout
292acc8326dSGarrett Wollman  * callout_init() - initialize a callout structure so that it can
293acc8326dSGarrett Wollman  *	safely be passed to callout_reset() and callout_stop()
294acc8326dSGarrett Wollman  *
2959b8b58e0SJonathan Lemon  * <sys/callout.h> defines three convenience macros:
296acc8326dSGarrett Wollman  *
2979b8b58e0SJonathan Lemon  * callout_active() - returns truth if callout has not been serviced
2989b8b58e0SJonathan Lemon  * callout_pending() - returns truth if callout is still waiting for timeout
2999b8b58e0SJonathan Lemon  * callout_deactivate() - marks the callout as having been serviced
300acc8326dSGarrett Wollman  */
301acc8326dSGarrett Wollman void
302e82ac18eSJonathan Lemon callout_reset(c, to_ticks, ftn, arg)
303acc8326dSGarrett Wollman 	struct	callout *c;
304acc8326dSGarrett Wollman 	int	to_ticks;
3054d77a549SAlfred Perlstein 	void	(*ftn)(void *);
306acc8326dSGarrett Wollman 	void	*arg;
307acc8326dSGarrett Wollman {
308acc8326dSGarrett Wollman 
3099ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
310acc8326dSGarrett Wollman 	if (c->c_flags & CALLOUT_PENDING)
311acc8326dSGarrett Wollman 		callout_stop(c);
312acc8326dSGarrett Wollman 
313acc8326dSGarrett Wollman 	/*
314ab32297dSJohn Baldwin 	 * We could unlock callout_lock here and lock it again before the
315ab32297dSJohn Baldwin 	 * TAILQ_INSERT_TAIL, but there's no point since doing this setup
316ab32297dSJohn Baldwin 	 * doesn't take much time.
317acc8326dSGarrett Wollman 	 */
318acc8326dSGarrett Wollman 	if (to_ticks <= 0)
319acc8326dSGarrett Wollman 		to_ticks = 1;
320acc8326dSGarrett Wollman 
321acc8326dSGarrett Wollman 	c->c_arg = arg;
322e82ac18eSJonathan Lemon 	c->c_flags |= (CALLOUT_ACTIVE | CALLOUT_PENDING);
323acc8326dSGarrett Wollman 	c->c_func = ftn;
324acc8326dSGarrett Wollman 	c->c_time = ticks + to_ticks;
325acc8326dSGarrett Wollman 	TAILQ_INSERT_TAIL(&callwheel[c->c_time & callwheelmask],
326acc8326dSGarrett Wollman 			  c, c_links.tqe);
3279ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
328acc8326dSGarrett Wollman }
329acc8326dSGarrett Wollman 
330a45982d2SJohn Baldwin int
331acc8326dSGarrett Wollman callout_stop(c)
332acc8326dSGarrett Wollman 	struct	callout *c;
333acc8326dSGarrett Wollman {
334acc8326dSGarrett Wollman 
3359ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
336acc8326dSGarrett Wollman 	/*
337acc8326dSGarrett Wollman 	 * Don't attempt to delete a callout that's not on the queue.
338acc8326dSGarrett Wollman 	 */
339acc8326dSGarrett Wollman 	if (!(c->c_flags & CALLOUT_PENDING)) {
3409b8b58e0SJonathan Lemon 		c->c_flags &= ~CALLOUT_ACTIVE;
3419ed346baSBosko Milekic 		mtx_unlock_spin(&callout_lock);
342a45982d2SJohn Baldwin 		return (0);
343acc8326dSGarrett Wollman 	}
3449b8b58e0SJonathan Lemon 	c->c_flags &= ~(CALLOUT_ACTIVE | CALLOUT_PENDING);
345acc8326dSGarrett Wollman 
346acc8326dSGarrett Wollman 	if (nextsoftcheck == c) {
347acc8326dSGarrett Wollman 		nextsoftcheck = TAILQ_NEXT(c, c_links.tqe);
348acc8326dSGarrett Wollman 	}
349acc8326dSGarrett Wollman 	TAILQ_REMOVE(&callwheel[c->c_time & callwheelmask], c, c_links.tqe);
350acc8326dSGarrett Wollman 	c->c_func = NULL;
351acc8326dSGarrett Wollman 
352acc8326dSGarrett Wollman 	if (c->c_flags & CALLOUT_LOCAL_ALLOC) {
353acc8326dSGarrett Wollman 		SLIST_INSERT_HEAD(&callfree, c, c_links.sle);
354acc8326dSGarrett Wollman 	}
3559ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
356a45982d2SJohn Baldwin 	return (1);
357acc8326dSGarrett Wollman }
358acc8326dSGarrett Wollman 
359acc8326dSGarrett Wollman void
360e82ac18eSJonathan Lemon callout_init(c, mpsafe)
361acc8326dSGarrett Wollman 	struct	callout *c;
362e82ac18eSJonathan Lemon 	int mpsafe;
363acc8326dSGarrett Wollman {
3647347e1c6SGarrett Wollman 	bzero(c, sizeof *c);
365e82ac18eSJonathan Lemon 	if (mpsafe)
366e82ac18eSJonathan Lemon 		c->c_flags |= CALLOUT_MPSAFE;
367acc8326dSGarrett Wollman }
368acc8326dSGarrett Wollman 
369e1d6dc65SNate Williams #ifdef APM_FIXUP_CALLTODO
370e1d6dc65SNate Williams /*
371e1d6dc65SNate Williams  * Adjust the kernel calltodo timeout list.  This routine is used after
372e1d6dc65SNate Williams  * an APM resume to recalculate the calltodo timer list values with the
373e1d6dc65SNate Williams  * number of hz's we have been sleeping.  The next hardclock() will detect
374e1d6dc65SNate Williams  * that there are fired timers and run softclock() to execute them.
375e1d6dc65SNate Williams  *
376e1d6dc65SNate Williams  * Please note, I have not done an exhaustive analysis of what code this
377e1d6dc65SNate Williams  * might break.  I am motivated to have my select()'s and alarm()'s that
378e1d6dc65SNate Williams  * have expired during suspend firing upon resume so that the applications
379e1d6dc65SNate Williams  * which set the timer can do the maintanence the timer was for as close
380e1d6dc65SNate Williams  * as possible to the originally intended time.  Testing this code for a
381e1d6dc65SNate Williams  * week showed that resuming from a suspend resulted in 22 to 25 timers
382e1d6dc65SNate Williams  * firing, which seemed independant on whether the suspend was 2 hours or
383e1d6dc65SNate Williams  * 2 days.  Your milage may vary.   - Ken Key <key@cs.utk.edu>
384e1d6dc65SNate Williams  */
385e1d6dc65SNate Williams void
386e1d6dc65SNate Williams adjust_timeout_calltodo(time_change)
387e1d6dc65SNate Williams     struct timeval *time_change;
388e1d6dc65SNate Williams {
389e1d6dc65SNate Williams 	register struct callout *p;
390e1d6dc65SNate Williams 	unsigned long delta_ticks;
391e1d6dc65SNate Williams 
392e1d6dc65SNate Williams 	/*
393e1d6dc65SNate Williams 	 * How many ticks were we asleep?
394c8b47828SBruce Evans 	 * (stolen from tvtohz()).
395e1d6dc65SNate Williams 	 */
396e1d6dc65SNate Williams 
397e1d6dc65SNate Williams 	/* Don't do anything */
398e1d6dc65SNate Williams 	if (time_change->tv_sec < 0)
399e1d6dc65SNate Williams 		return;
400e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / 1000000)
401e1d6dc65SNate Williams 		delta_ticks = (time_change->tv_sec * 1000000 +
402e1d6dc65SNate Williams 			       time_change->tv_usec + (tick - 1)) / tick + 1;
403e1d6dc65SNate Williams 	else if (time_change->tv_sec <= LONG_MAX / hz)
404e1d6dc65SNate Williams 		delta_ticks = time_change->tv_sec * hz +
405e1d6dc65SNate Williams 			      (time_change->tv_usec + (tick - 1)) / tick + 1;
406e1d6dc65SNate Williams 	else
407e1d6dc65SNate Williams 		delta_ticks = LONG_MAX;
408e1d6dc65SNate Williams 
409e1d6dc65SNate Williams 	if (delta_ticks > INT_MAX)
410e1d6dc65SNate Williams 		delta_ticks = INT_MAX;
411e1d6dc65SNate Williams 
412e1d6dc65SNate Williams 	/*
413e1d6dc65SNate Williams 	 * Now rip through the timer calltodo list looking for timers
414e1d6dc65SNate Williams 	 * to expire.
415e1d6dc65SNate Williams 	 */
416e1d6dc65SNate Williams 
417e1d6dc65SNate Williams 	/* don't collide with softclock() */
4189ed346baSBosko Milekic 	mtx_lock_spin(&callout_lock);
419e1d6dc65SNate Williams 	for (p = calltodo.c_next; p != NULL; p = p->c_next) {
420e1d6dc65SNate Williams 		p->c_time -= delta_ticks;
421e1d6dc65SNate Williams 
422e1d6dc65SNate Williams 		/* Break if the timer had more time on it than delta_ticks */
423e1d6dc65SNate Williams 		if (p->c_time > 0)
424e1d6dc65SNate Williams 			break;
425e1d6dc65SNate Williams 
426e1d6dc65SNate Williams 		/* take back the ticks the timer didn't use (p->c_time <= 0) */
427e1d6dc65SNate Williams 		delta_ticks = -p->c_time;
428e1d6dc65SNate Williams 	}
4299ed346baSBosko Milekic 	mtx_unlock_spin(&callout_lock);
430e1d6dc65SNate Williams 
431e1d6dc65SNate Williams 	return;
432e1d6dc65SNate Williams }
433e1d6dc65SNate Williams #endif /* APM_FIXUP_CALLTODO */
434