callout.h revision 304882
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1990, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 4. Neither the name of the University nor the names of its contributors
191541Srgrimes *    may be used to endorse or promote products derived from this software
201541Srgrimes *    without specific prior written permission.
211541Srgrimes *
221541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321541Srgrimes * SUCH DAMAGE.
331541Srgrimes *
341541Srgrimes *	@(#)callout.h	8.2 (Berkeley) 1/21/94
3550477Speter * $FreeBSD: stable/11/sys/sys/callout.h 304882 2016-08-27 09:11:57Z kib $
361541Srgrimes */
371541Srgrimes
382165Spaul#ifndef _SYS_CALLOUT_H_
392165Spaul#define _SYS_CALLOUT_H_
402165Spaul
41221059Skib#include <sys/_callout.h>
4229683Sgibbs
4344510Swollman#define	CALLOUT_LOCAL_ALLOC	0x0001 /* was allocated from callfree */
4450673Sjlemon#define	CALLOUT_ACTIVE		0x0002 /* callout is currently active */
4550673Sjlemon#define	CALLOUT_PENDING		0x0004 /* callout is waiting for timeout */
46283291Sjkim#define	CALLOUT_MPSAFE		0x0008 /* deprecated */
47141428Siedowse#define	CALLOUT_RETURNUNLOCKED	0x0010 /* handler returns with mtx unlocked */
48277528Shselasky#define	CALLOUT_SHAREDLOCK	0x0020 /* callout lock held in shared mode */
49277528Shselasky#define	CALLOUT_DFRMIGRATION	0x0040 /* callout in deferred migration mode */
50247777Sdavide#define	CALLOUT_PROCESSED	0x0080 /* callout in wheel or processing list? */
51247777Sdavide#define	CALLOUT_DIRECT 		0x0100 /* allow exec from hw int context */
5244510Swollman
53247777Sdavide#define	C_DIRECT_EXEC		0x0001 /* direct execution of callout */
54247777Sdavide#define	C_PRELBITS		7
55247777Sdavide#define	C_PRELRANGE		((1 << C_PRELBITS) - 1)
56247777Sdavide#define	C_PREL(x)		(((x) + 1) << 1)
57247777Sdavide#define	C_PRELGET(x)		(int)((((x) >> 1) & C_PRELRANGE) - 1)
58247777Sdavide#define	C_HARDCLOCK		0x0100 /* align to hardclock() calls */
59247777Sdavide#define	C_ABSOLUTE		0x0200 /* event time is absolute. */
60304882Skib#define	C_PRECALC		0x0400 /* event time is pre-calculated. */
61247777Sdavide
6283045Sobrienstruct callout_handle {
6329683Sgibbs	struct callout *callout;
6429683Sgibbs};
6529683Sgibbs
66296320Skib/* Flags for callout_stop_safe() */
67296320Skib#define	CS_DRAIN		0x0001 /* callout_drain(), wait allowed */
68302350Sglebius#define	CS_EXECUTING		0x0002 /* Positive return value indicates that
69302350Sglebius					  the callout was executing */
70296320Skib
7155205Speter#ifdef _KERNEL
72280785Srrs/*
73280785Srrs * Note the flags field is actually *two* fields. The c_flags
74280785Srrs * field is the one that caller operations that may, or may not have
75280785Srrs * a lock touches i.e. callout_deactivate(). The other, the c_iflags,
76280785Srrs * is the internal flags that *must* be kept correct on which the
77280872Srrs * callout system depend on e.g. callout_pending().
78280872Srrs * The c_iflag is used internally by the callout system to determine which
79280872Srrs * list the callout is on and track internal state. Callers *should not*
80280872Srrs * use the c_flags field directly but should use the macros provided.
81280785Srrs *
82280872Srrs * The c_iflags field holds internal flags that are protected by internal
83280872Srrs * locks of the callout subsystem.  The c_flags field holds external flags.
84280872Srrs * The caller must hold its own lock while manipulating or reading external
85280872Srrs * flags via callout_active(), callout_deactivate(), callout_reset*(), or
86280872Srrs * callout_stop() to avoid races.
87280785Srrs */
8850673Sjlemon#define	callout_active(c)	((c)->c_flags & CALLOUT_ACTIVE)
8950673Sjlemon#define	callout_deactivate(c)	((c)->c_flags &= ~CALLOUT_ACTIVE)
90296320Skib#define	callout_drain(c)	_callout_stop_safe(c, CS_DRAIN, NULL)
9192719Salfredvoid	callout_init(struct callout *, int);
92173760Sattiliovoid	_callout_init_lock(struct callout *, struct lock_object *, int);
93173760Sattilio#define	callout_init_mtx(c, mtx, flags)					\
94173760Sattilio	_callout_init_lock((c), ((mtx) != NULL) ? &(mtx)->lock_object :	\
95173760Sattilio	    NULL, (flags))
96254712Sdavide#define	callout_init_rm(c, rm, flags)					\
97270259Sgavin	_callout_init_lock((c), ((rm) != NULL) ? &(rm)->lock_object : 	\
98254703Sdavide	    NULL, (flags))
99173760Sattilio#define	callout_init_rw(c, rw, flags)					\
100173760Sattilio	_callout_init_lock((c), ((rw) != NULL) ? &(rw)->lock_object :	\
101173760Sattilio	   NULL, (flags))
102280785Srrs#define	callout_pending(c)	((c)->c_iflags & CALLOUT_PENDING)
103247777Sdavideint	callout_reset_sbt_on(struct callout *, sbintime_t, sbintime_t,
104277528Shselasky	    void (*)(void *), void *, int, int);
105247777Sdavide#define	callout_reset_sbt(c, sbt, pr, fn, arg, flags)			\
106280785Srrs    callout_reset_sbt_on((c), (sbt), (pr), (fn), (arg), -1, (flags))
107247777Sdavide#define	callout_reset_sbt_curcpu(c, sbt, pr, fn, arg, flags)		\
108275046Savg    callout_reset_sbt_on((c), (sbt), (pr), (fn), (arg), PCPU_GET(cpuid),\
109275046Savg        (flags))
110247777Sdavide#define	callout_reset_on(c, to_ticks, fn, arg, cpu)			\
111275046Savg    callout_reset_sbt_on((c), tick_sbt * (to_ticks), 0, (fn), (arg),	\
112247777Sdavide        (cpu), C_HARDCLOCK)
113177859Sjeff#define	callout_reset(c, on_tick, fn, arg)				\
114280785Srrs    callout_reset_on((c), (on_tick), (fn), (arg), -1)
115177859Sjeff#define	callout_reset_curcpu(c, on_tick, fn, arg)			\
116177859Sjeff    callout_reset_on((c), (on_tick), (fn), (arg), PCPU_GET(cpuid))
117275045Savg#define	callout_schedule_sbt_on(c, sbt, pr, cpu, flags)			\
118275045Savg    callout_reset_sbt_on((c), (sbt), (pr), (c)->c_func, (c)->c_arg,	\
119275045Savg        (cpu), (flags))
120275045Savg#define	callout_schedule_sbt(c, sbt, pr, flags)				\
121280785Srrs    callout_schedule_sbt_on((c), (sbt), (pr), -1, (flags))
122275045Savg#define	callout_schedule_sbt_curcpu(c, sbt, pr, flags)			\
123275045Savg    callout_schedule_sbt_on((c), (sbt), (pr), PCPU_GET(cpuid), (flags))
124181191Ssamint	callout_schedule(struct callout *, int);
125181191Ssamint	callout_schedule_on(struct callout *, int, int);
126181191Ssam#define	callout_schedule_curcpu(c, on_tick)				\
127181191Ssam    callout_schedule_on((c), (on_tick), PCPU_GET(cpuid))
128290664Srrs#define	callout_stop(c)		_callout_stop_safe(c, 0, NULL)
129290664Srrsint	_callout_stop_safe(struct callout *, int, void (*)(void *));
130247777Sdavidevoid	callout_process(sbintime_t now);
131290664Srrs#define callout_async_drain(c, d)					\
132290664Srrs    _callout_stop_safe(c, 0, d)
133304882Skibvoid callout_when(sbintime_t sbt, sbintime_t precision, int flags,
134304882Skib    sbintime_t *sbt_res, sbintime_t *prec_res);
13555205Speter#endif
1362165Spaul
13729683Sgibbs#endif /* _SYS_CALLOUT_H_ */
138