xref: /dragonfly/sys/sys/callout.h (revision efc67d97)
1d0f829f6SMatthew Dillon /*
2eb67213aSMatthew Dillon  * Copyright (c) 2014,2018,2019 The DragonFly Project.  All rights reserved.
3d0f829f6SMatthew Dillon  *
4d0f829f6SMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5d0f829f6SMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6d0f829f6SMatthew Dillon  *
7d0f829f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8d0f829f6SMatthew Dillon  * modification, are permitted provided that the following conditions
9d0f829f6SMatthew Dillon  * are met:
10d0f829f6SMatthew Dillon  *
11d0f829f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12d0f829f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13d0f829f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14d0f829f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15d0f829f6SMatthew Dillon  *    the documentation and/or other materials provided with the
16d0f829f6SMatthew Dillon  *    distribution.
17d0f829f6SMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18d0f829f6SMatthew Dillon  *    contributors may be used to endorse or promote products derived
19d0f829f6SMatthew Dillon  *    from this software without specific, prior written permission.
20d0f829f6SMatthew Dillon  *
21d0f829f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22d0f829f6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23d0f829f6SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24d0f829f6SMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25d0f829f6SMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26d0f829f6SMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27d0f829f6SMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28d0f829f6SMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29d0f829f6SMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30d0f829f6SMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31d0f829f6SMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d0f829f6SMatthew Dillon  * SUCH DAMAGE.
33d0f829f6SMatthew Dillon  */
34984263bcSMatthew Dillon 
35984263bcSMatthew Dillon #ifndef _SYS_CALLOUT_H_
36984263bcSMatthew Dillon #define _SYS_CALLOUT_H_
37984263bcSMatthew Dillon 
38d0f829f6SMatthew Dillon #ifndef _SYS_QUEUE_H_
39984263bcSMatthew Dillon #include <sys/queue.h>
40d0f829f6SMatthew Dillon #endif
41d0f829f6SMatthew Dillon #ifndef _SYS_LOCK_H_
42d0f829f6SMatthew Dillon #include <sys/lock.h>
43d0f829f6SMatthew Dillon #endif
4419ccf7e8SMatthew Dillon #ifndef _SYS_SPINLOCK_H_
4579044c5fSzrj #include <sys/spinlock.h>
4619ccf7e8SMatthew Dillon #endif
47fac0eb3cSMatthew Dillon #ifndef _SYS_EXISLOCK_H_
48fac0eb3cSMatthew Dillon #include <sys/exislock.h>
49fac0eb3cSMatthew Dillon #endif
50984263bcSMatthew Dillon 
51eb67213aSMatthew Dillon struct callout;
52fac0eb3cSMatthew Dillon struct softclock_pcpu;
53eb67213aSMatthew Dillon 
54eb67213aSMatthew Dillon struct _callout {
55fac0eb3cSMatthew Dillon 	struct spinlock	spin;		/* typestable spinlock */
56fac0eb3cSMatthew Dillon 	exislock_t	exis;
57eb67213aSMatthew Dillon 	TAILQ_ENTRY(_callout) entry;
58eb67213aSMatthew Dillon 	struct callout	*verifier;
59eb67213aSMatthew Dillon 	uint32_t	flags;
60eb67213aSMatthew Dillon 	uint32_t	lineno;
61eb67213aSMatthew Dillon 	struct lock	*lk;
62eb67213aSMatthew Dillon 	const char	*ident;
63eb67213aSMatthew Dillon 
64eb67213aSMatthew Dillon 	struct softclock_pcpu *rsc;	/* request info */
65eb67213aSMatthew Dillon 	void		*rarg;
66eb67213aSMatthew Dillon 	void		(*rfunc)(void *);
67eb67213aSMatthew Dillon 	int		rtick;
68fac0eb3cSMatthew Dillon 	uint32_t	unused01;
69eb67213aSMatthew Dillon 
70eb67213aSMatthew Dillon 	struct softclock_pcpu *qsc;	/* active info */
71eb67213aSMatthew Dillon 	void		*qarg;
72eb67213aSMatthew Dillon 	void		(*qfunc)(void *);
73eb67213aSMatthew Dillon 	int		qtick;
7419ccf7e8SMatthew Dillon 	int		waiters;
75984263bcSMatthew Dillon };
76984263bcSMatthew Dillon 
77eb67213aSMatthew Dillon struct callout {
78fac0eb3cSMatthew Dillon 	struct _callout *toc;		/* opaque internal pointer */
79fac0eb3cSMatthew Dillon 	struct lock	*lk;		/* callout_init() copy data */
80fac0eb3cSMatthew Dillon 	uint32_t	flags;		/* callout_init() copy data */
81fac0eb3cSMatthew Dillon 	uint32_t	unused01;
82eb67213aSMatthew Dillon };
837b045ca8SMatthew Dillon 
84c3d884aeSMatthew Dillon /*
85c3d884aeSMatthew Dillon  * Legacy access/setting of the function and argument.  Used only
86c3d884aeSMatthew Dillon  * by netgraph7 and ieee80211_dfs.c.  DO NOT USE FOR NEW CODE!
87c3d884aeSMatthew Dillon  */
88fac0eb3cSMatthew Dillon #define callout_set_arg(cc, _arg)	do {	\
89fac0eb3cSMatthew Dillon 	if ((cc)->toc)				\
90*efc67d97SSascha Wildner 		((cc)->toc->qarg = (cc)->toc->rarg = (_arg));	\
91fac0eb3cSMatthew Dillon } while(0)
92fac0eb3cSMatthew Dillon #define callout_arg(cc)			((cc)->toc ? (cc)->toc->rarg : NULL)
93fac0eb3cSMatthew Dillon #define callout_func(cc)		((cc)->toc ? (cc)->toc->rfunc : NULL)
9492b561b7SMatthew Dillon 
9592b561b7SMatthew Dillon #ifdef _KERNEL
969a97a5a6SFrançois Tigeot 
971a986f71SMatthew Dillon #define CALLOUT_DEBUG
981a986f71SMatthew Dillon #ifdef CALLOUT_DEBUG
99eb67213aSMatthew Dillon #define CALLOUT_DEBUG_ARGS	, const char *ident, int lineno
100eb67213aSMatthew Dillon #define CALLOUT_DEBUG_PASSTHRU	, ident, lineno
101eb67213aSMatthew Dillon #else
102eb67213aSMatthew Dillon #define CALLOUT_DEBUG_ARGS
103eb67213aSMatthew Dillon #define CALLOUT_DEBUG_PASSTHRU
1041a986f71SMatthew Dillon #endif
1051a986f71SMatthew Dillon 
106eb67213aSMatthew Dillon struct globaldata;
107eb67213aSMatthew Dillon 
108eb67213aSMatthew Dillon extern int ncallout;
109eb67213aSMatthew Dillon 
110eb67213aSMatthew Dillon int	callout_active(struct callout *ext);
111eb67213aSMatthew Dillon int	callout_pending(struct callout *ext);
112eb67213aSMatthew Dillon void	callout_deactivate(struct callout *ext);
113eb67213aSMatthew Dillon 
114eb67213aSMatthew Dillon void	hardclock_softtick(struct globaldata *);
115eb67213aSMatthew Dillon void	_callout_init (struct callout *cc
116eb67213aSMatthew Dillon 			CALLOUT_DEBUG_ARGS);
117eb67213aSMatthew Dillon void	_callout_init_mp (struct callout *cc
118eb67213aSMatthew Dillon 			CALLOUT_DEBUG_ARGS);
119eb67213aSMatthew Dillon void	_callout_init_lk (struct callout *cc, struct lock *lk
120eb67213aSMatthew Dillon 			CALLOUT_DEBUG_ARGS);
121fac0eb3cSMatthew Dillon 
122fac0eb3cSMatthew Dillon void	_callout_setup_quick(struct callout *cc, struct _callout *c,
123fac0eb3cSMatthew Dillon 			int ticks, void (*)(void *), void *);
124fac0eb3cSMatthew Dillon void	_callout_cancel_quick(struct _callout *c);
125fac0eb3cSMatthew Dillon 
126eb67213aSMatthew Dillon void	callout_reset (struct callout *, int,
127eb67213aSMatthew Dillon 			void (*)(void *), void *);
128eb67213aSMatthew Dillon void	callout_reset_bycpu (struct callout *, int,
129eb67213aSMatthew Dillon 			void (*)(void *), void *,
130eb67213aSMatthew Dillon 			int);
131eb67213aSMatthew Dillon int	callout_stop (struct callout *cc);
132eb67213aSMatthew Dillon int	callout_stop_async (struct callout *cc);
133eb67213aSMatthew Dillon void	callout_terminate (struct callout *cc);
134eb67213aSMatthew Dillon int	callout_cancel (struct callout *cc);
135eb67213aSMatthew Dillon int	callout_drain (struct callout *cc);
136eb67213aSMatthew Dillon 
137eb67213aSMatthew Dillon #ifdef CALLOUT_DEBUG
138eb67213aSMatthew Dillon #define	callout_init(co)	_callout_init(co, __FILE__, __LINE__)
139eb67213aSMatthew Dillon #define	callout_init_mp(co)	_callout_init_mp(co, __FILE__, __LINE__)
140eb67213aSMatthew Dillon #define	callout_init_lk(co, lk)	_callout_init_lk(co, lk, __FILE__, __LINE__)
141eb67213aSMatthew Dillon #else
142eb67213aSMatthew Dillon #define	callout_init(co)	_callout_init(co)
143eb67213aSMatthew Dillon #define	callout_init_mp(co)	_callout_init_mp(co)
144eb67213aSMatthew Dillon #define	callout_init_lk(co, lk)	_callout_init_lk(co, lk)
145984263bcSMatthew Dillon #endif
146984263bcSMatthew Dillon 
147eb67213aSMatthew Dillon #endif	/* _KERNEL */
148eb67213aSMatthew Dillon 
149984263bcSMatthew Dillon #endif	/* _SYS_CALLOUT_H_ */
150