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 331722 2018-03-29 02:50:57Z eadler $
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. */
61330850Shselasky#define	C_CATCH			0x0800 /* catch signals, used by pause_sbt(9) */
62247777Sdavide
6383045Sobrienstruct callout_handle {
6429683Sgibbs	struct callout *callout;
6529683Sgibbs};
6629683Sgibbs
67296320Skib/* Flags for callout_stop_safe() */
68296320Skib#define	CS_DRAIN		0x0001 /* callout_drain(), wait allowed */
69302350Sglebius#define	CS_EXECUTING		0x0002 /* Positive return value indicates that
70302350Sglebius					  the callout was executing */
71296320Skib
7255205Speter#ifdef _KERNEL
73280785Srrs/*
74280785Srrs * Note the flags field is actually *two* fields. The c_flags
75280785Srrs * field is the one that caller operations that may, or may not have
76280785Srrs * a lock touches i.e. callout_deactivate(). The other, the c_iflags,
77280785Srrs * is the internal flags that *must* be kept correct on which the
78280872Srrs * callout system depend on e.g. callout_pending().
79280872Srrs * The c_iflag is used internally by the callout system to determine which
80280872Srrs * list the callout is on and track internal state. Callers *should not*
81280872Srrs * use the c_flags field directly but should use the macros provided.
82280785Srrs *
83280872Srrs * The c_iflags field holds internal flags that are protected by internal
84280872Srrs * locks of the callout subsystem.  The c_flags field holds external flags.
85280872Srrs * The caller must hold its own lock while manipulating or reading external
86280872Srrs * flags via callout_active(), callout_deactivate(), callout_reset*(), or
87280872Srrs * callout_stop() to avoid races.
88280785Srrs */
8950673Sjlemon#define	callout_active(c)	((c)->c_flags & CALLOUT_ACTIVE)
9050673Sjlemon#define	callout_deactivate(c)	((c)->c_flags &= ~CALLOUT_ACTIVE)
91296320Skib#define	callout_drain(c)	_callout_stop_safe(c, CS_DRAIN, NULL)
9292719Salfredvoid	callout_init(struct callout *, int);
93173760Sattiliovoid	_callout_init_lock(struct callout *, struct lock_object *, int);
94173760Sattilio#define	callout_init_mtx(c, mtx, flags)					\
95173760Sattilio	_callout_init_lock((c), ((mtx) != NULL) ? &(mtx)->lock_object :	\
96173760Sattilio	    NULL, (flags))
97254712Sdavide#define	callout_init_rm(c, rm, flags)					\
98270259Sgavin	_callout_init_lock((c), ((rm) != NULL) ? &(rm)->lock_object : 	\
99254703Sdavide	    NULL, (flags))
100173760Sattilio#define	callout_init_rw(c, rw, flags)					\
101173760Sattilio	_callout_init_lock((c), ((rw) != NULL) ? &(rw)->lock_object :	\
102173760Sattilio	   NULL, (flags))
103280785Srrs#define	callout_pending(c)	((c)->c_iflags & CALLOUT_PENDING)
104247777Sdavideint	callout_reset_sbt_on(struct callout *, sbintime_t, sbintime_t,
105277528Shselasky	    void (*)(void *), void *, int, int);
106247777Sdavide#define	callout_reset_sbt(c, sbt, pr, fn, arg, flags)			\
107280785Srrs    callout_reset_sbt_on((c), (sbt), (pr), (fn), (arg), -1, (flags))
108247777Sdavide#define	callout_reset_sbt_curcpu(c, sbt, pr, fn, arg, flags)		\
109275046Savg    callout_reset_sbt_on((c), (sbt), (pr), (fn), (arg), PCPU_GET(cpuid),\
110275046Savg        (flags))
111247777Sdavide#define	callout_reset_on(c, to_ticks, fn, arg, cpu)			\
112275046Savg    callout_reset_sbt_on((c), tick_sbt * (to_ticks), 0, (fn), (arg),	\
113247777Sdavide        (cpu), C_HARDCLOCK)
114177859Sjeff#define	callout_reset(c, on_tick, fn, arg)				\
115280785Srrs    callout_reset_on((c), (on_tick), (fn), (arg), -1)
116177859Sjeff#define	callout_reset_curcpu(c, on_tick, fn, arg)			\
117177859Sjeff    callout_reset_on((c), (on_tick), (fn), (arg), PCPU_GET(cpuid))
118275045Savg#define	callout_schedule_sbt_on(c, sbt, pr, cpu, flags)			\
119275045Savg    callout_reset_sbt_on((c), (sbt), (pr), (c)->c_func, (c)->c_arg,	\
120275045Savg        (cpu), (flags))
121275045Savg#define	callout_schedule_sbt(c, sbt, pr, flags)				\
122280785Srrs    callout_schedule_sbt_on((c), (sbt), (pr), -1, (flags))
123275045Savg#define	callout_schedule_sbt_curcpu(c, sbt, pr, flags)			\
124275045Savg    callout_schedule_sbt_on((c), (sbt), (pr), PCPU_GET(cpuid), (flags))
125181191Ssamint	callout_schedule(struct callout *, int);
126181191Ssamint	callout_schedule_on(struct callout *, int, int);
127181191Ssam#define	callout_schedule_curcpu(c, on_tick)				\
128181191Ssam    callout_schedule_on((c), (on_tick), PCPU_GET(cpuid))
129290664Srrs#define	callout_stop(c)		_callout_stop_safe(c, 0, NULL)
130290664Srrsint	_callout_stop_safe(struct callout *, int, void (*)(void *));
131247777Sdavidevoid	callout_process(sbintime_t now);
132290664Srrs#define callout_async_drain(c, d)					\
133290664Srrs    _callout_stop_safe(c, 0, d)
134304882Skibvoid callout_when(sbintime_t sbt, sbintime_t precision, int flags,
135304882Skib    sbintime_t *sbt_res, sbintime_t *prec_res);
13655205Speter#endif
1372165Spaul
13829683Sgibbs#endif /* _SYS_CALLOUT_H_ */
139