xref: /freebsd/sys/sys/rmlock.h (revision febdb468801f35e51c6c5c22221cfce9197c6f3b)
1 /*-
2  * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_RMLOCK_H_
33 #define _SYS_RMLOCK_H_
34 
35 #include <sys/mutex.h>
36 #include <sys/sx.h>
37 #include <sys/_lock.h>
38 #include <sys/_rmlock.h>
39 
40 #ifdef _KERNEL
41 
42 /*
43  * Flags passed to rm_init_flags(9).
44  */
45 #define	RM_NOWITNESS	0x00000001
46 #define	RM_RECURSE	0x00000002
47 #define	RM_SLEEPABLE	0x00000004
48 
49 void	rm_init(struct rmlock *rm, const char *name);
50 void	rm_init_flags(struct rmlock *rm, const char *name, int opts);
51 void	rm_destroy(struct rmlock *rm);
52 int	rm_wowned(const struct rmlock *rm);
53 void	rm_sysinit(void *arg);
54 void	rm_sysinit_flags(void *arg);
55 
56 void	_rm_wlock_debug(struct rmlock *rm, const char *file, int line);
57 void	_rm_wunlock_debug(struct rmlock *rm, const char *file, int line);
58 int	_rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
59 	    int trylock, const char *file, int line);
60 void	_rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
61 	    const char *file, int line);
62 
63 void	_rm_wlock(struct rmlock *rm);
64 void	_rm_wunlock(struct rmlock *rm);
65 int	_rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker,
66 	    int trylock);
67 void	_rm_runlock(struct rmlock *rm,  struct rm_priotracker *tracker);
68 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
69 void	_rm_assert(const struct rmlock *rm, int what, const char *file,
70 	    int line);
71 #endif
72 
73 /*
74  * Public interface for lock operations.
75  */
76 #ifndef LOCK_DEBUG
77 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
78 #endif
79 
80 #if LOCK_DEBUG > 0
81 #define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
82 #define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
83 #define	rm_rlock(rm,tracker)  \
84     ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE ))
85 #define	rm_try_rlock(rm,tracker)  \
86     _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE )
87 #define	rm_runlock(rm,tracker)	\
88     _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
89 #else
90 #define	rm_wlock(rm)			_rm_wlock((rm))
91 #define	rm_wunlock(rm)			_rm_wunlock((rm))
92 #define	rm_rlock(rm,tracker)		((void)_rm_rlock((rm),(tracker), 0))
93 #define	rm_try_rlock(rm,tracker)	_rm_rlock((rm),(tracker), 1)
94 #define	rm_runlock(rm,tracker)		_rm_runlock((rm), (tracker))
95 #endif
96 #define	rm_sleep(chan, rm, pri, wmesg, timo)				\
97 	_sleep((chan), &(rm)->lock_object, (pri), (wmesg),		\
98 	    tick_sbt * (timo), 0, C_HARDCLOCK)
99 
100 struct rm_args {
101 	struct rmlock	*ra_rm;
102 	const char 	*ra_desc;
103 };
104 
105 struct rm_args_flags {
106 	struct rmlock	*ra_rm;
107 	const char 	*ra_desc;
108 	int		ra_opts;
109 };
110 
111 #define	RM_SYSINIT(name, rm, desc)       				\
112 	static struct rm_args name##_args = {				\
113 		(rm),							\
114 		(desc),							\
115 	};								\
116 	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
117 	    rm_sysinit, &name##_args);					\
118 	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
119 	    rm_destroy, (rm))
120 
121 
122 #define	RM_SYSINIT_FLAGS(name, rm, desc, opts)       			\
123 	static struct rm_args name##_args = {				\
124 		(rm),							\
125 		(desc),							\
126                 (opts),							\
127 	};								\
128 	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
129 	    rm_sysinit_flags, &name##_args);				\
130 	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
131 	    rm_destroy, (rm))
132 
133 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
134 #define	RA_LOCKED		LA_LOCKED
135 #define	RA_RLOCKED		LA_SLOCKED
136 #define	RA_WLOCKED		LA_XLOCKED
137 #define	RA_UNLOCKED		LA_UNLOCKED
138 #define	RA_RECURSED		LA_RECURSED
139 #define	RA_NOTRECURSED		LA_NOTRECURSED
140 #endif
141 
142 #ifdef INVARIANTS
143 #define	rm_assert(rm, what)	_rm_assert((rm), (what), LOCK_FILE, LOCK_LINE)
144 #else
145 #define	rm_assert(rm, what)
146 #endif
147 
148 #endif /* _KERNEL */
149 #endif /* !_SYS_RMLOCK_H_ */
150