xref: /freebsd/sys/sys/rwlock.h (revision f05cddf9)
1 /*-
2  * Copyright (c) 2006 John Baldwin <jhb@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_RWLOCK_H_
33 #define _SYS_RWLOCK_H_
34 
35 #include <sys/_lock.h>
36 #include <sys/_rwlock.h>
37 #include <sys/lock_profile.h>
38 #include <sys/lockstat.h>
39 
40 #ifdef _KERNEL
41 #include <sys/pcpu.h>
42 #include <machine/atomic.h>
43 #endif
44 
45 /*
46  * The rw_lock field consists of several fields.  The low bit indicates
47  * if the lock is locked with a read (shared) or write (exclusive) lock.
48  * A value of 0 indicates a write lock, and a value of 1 indicates a read
49  * lock.  Bit 1 is a boolean indicating if there are any threads waiting
50  * for a read lock.  Bit 2 is a boolean indicating if there are any threads
51  * waiting for a write lock.  The rest of the variable's definition is
52  * dependent on the value of the first bit.  For a write lock, it is a
53  * pointer to the thread holding the lock, similar to the mtx_lock field of
54  * mutexes.  For read locks, it is a count of read locks that are held.
55  *
56  * When the lock is not locked by any thread, it is encoded as a read lock
57  * with zero waiters.
58  */
59 
60 #define	RW_LOCK_READ		0x01
61 #define	RW_LOCK_READ_WAITERS	0x02
62 #define	RW_LOCK_WRITE_WAITERS	0x04
63 #define	RW_LOCK_WRITE_SPINNER	0x08
64 #define	RW_LOCK_FLAGMASK						\
65 	(RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |	\
66 	RW_LOCK_WRITE_SPINNER)
67 #define	RW_LOCK_WAITERS		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
68 
69 #define	RW_OWNER(x)		((x) & ~RW_LOCK_FLAGMASK)
70 #define	RW_READERS_SHIFT	4
71 #define	RW_READERS(x)		(RW_OWNER((x)) >> RW_READERS_SHIFT)
72 #define	RW_READERS_LOCK(x)	((x) << RW_READERS_SHIFT | RW_LOCK_READ)
73 #define	RW_ONE_READER		(1 << RW_READERS_SHIFT)
74 
75 #define	RW_UNLOCKED		RW_READERS_LOCK(0)
76 #define	RW_DESTROYED		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
77 
78 #ifdef _KERNEL
79 
80 #define	rw_recurse	lock_object.lo_data
81 
82 /* Very simple operations on rw_lock. */
83 
84 /* Try to obtain a write lock once. */
85 #define	_rw_write_lock(rw, tid)						\
86 	atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
87 
88 /* Release a write lock quickly if there are no waiters. */
89 #define	_rw_write_unlock(rw, tid)					\
90 	atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
91 
92 /*
93  * Full lock operations that are suitable to be inlined in non-debug
94  * kernels.  If the lock cannot be acquired or released trivially then
95  * the work is deferred to another function.
96  */
97 
98 /* Acquire a write lock. */
99 #define	__rw_wlock(rw, tid, file, line) do {				\
100 	uintptr_t _tid = (uintptr_t)(tid);				\
101 						                        \
102 	if (!_rw_write_lock((rw), _tid))				\
103 		_rw_wlock_hard((rw), _tid, (file), (line));		\
104 	else 								\
105 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, \
106 		    rw, 0, 0, (file), (line));				\
107 } while (0)
108 
109 /* Release a write lock. */
110 #define	__rw_wunlock(rw, tid, file, line) do {				\
111 	uintptr_t _tid = (uintptr_t)(tid);				\
112 									\
113 	if ((rw)->rw_recurse)						\
114 		(rw)->rw_recurse--;					\
115 	else if (!_rw_write_unlock((rw), _tid))				\
116 		_rw_wunlock_hard((rw), _tid, (file), (line));		\
117 } while (0)
118 
119 /*
120  * Function prototypes.  Routines that start with _ are not part of the
121  * external API and should not be called directly.  Wrapper macros should
122  * be used instead.
123  */
124 void	_rw_init_flags(volatile uintptr_t *c, const char *name, int opts);
125 void	_rw_destroy(volatile uintptr_t *c);
126 void	rw_sysinit(void *arg);
127 void	rw_sysinit_flags(void *arg);
128 int	_rw_wowned(const volatile uintptr_t *c);
129 void	_rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line);
130 int	__rw_try_wlock(volatile uintptr_t *c, const char *file, int line);
131 void	_rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line);
132 void	__rw_rlock(volatile uintptr_t *c, const char *file, int line);
133 int	__rw_try_rlock(volatile uintptr_t *c, const char *file, int line);
134 void	_rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line);
135 void	__rw_wlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
136 	    int line);
137 void	__rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid,
138 	    const char *file, int line);
139 int	__rw_try_upgrade(volatile uintptr_t *c, const char *file, int line);
140 void	__rw_downgrade(volatile uintptr_t *c, const char *file, int line);
141 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
142 void	__rw_assert(const volatile uintptr_t *c, int what, const char *file,
143 	    int line);
144 #endif
145 
146 /*
147  * Top-level macros to provide lock cookie once the actual rwlock is passed.
148  * They will also prevent passing a malformed object to the rwlock KPI by
149  * failing compilation as the rw_lock reserved member will not be found.
150  */
151 #define	rw_init(rw, n)							\
152 	_rw_init_flags(&(rw)->rw_lock, n, 0)
153 #define	rw_init_flags(rw, n, o)						\
154 	_rw_init_flags(&(rw)->rw_lock, n, o)
155 #define	rw_destroy(rw)							\
156 	_rw_destroy(&(rw)->rw_lock)
157 #define	rw_wowned(rw)							\
158 	_rw_wowned(&(rw)->rw_lock)
159 #define	_rw_wlock(rw, f, l)						\
160 	_rw_wlock_cookie(&(rw)->rw_lock, f, l)
161 #define	_rw_try_wlock(rw, f, l)						\
162 	__rw_try_wlock(&(rw)->rw_lock, f, l)
163 #define	_rw_wunlock(rw, f, l)						\
164 	_rw_wunlock_cookie(&(rw)->rw_lock, f, l)
165 #define	_rw_rlock(rw, f, l)						\
166 	__rw_rlock(&(rw)->rw_lock, f, l)
167 #define	_rw_try_rlock(rw, f, l)						\
168 	__rw_try_rlock(&(rw)->rw_lock, f, l)
169 #define	_rw_runlock(rw, f, l)						\
170 	_rw_runlock_cookie(&(rw)->rw_lock, f, l)
171 #define	_rw_wlock_hard(rw, t, f, l)					\
172 	__rw_wlock_hard(&(rw)->rw_lock, t, f, l)
173 #define	_rw_wunlock_hard(rw, t, f, l)					\
174 	__rw_wunlock_hard(&(rw)->rw_lock, t, f, l)
175 #define	_rw_try_upgrade(rw, f, l)					\
176 	__rw_try_upgrade(&(rw)->rw_lock, f, l)
177 #define	_rw_downgrade(rw, f, l)						\
178 	__rw_downgrade(&(rw)->rw_lock, f, l)
179 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
180 #define	_rw_assert(rw, w, f, l)						\
181 	__rw_assert(&(rw)->rw_lock, w, f, l)
182 #endif
183 
184 
185 /*
186  * Public interface for lock operations.
187  */
188 
189 #ifndef LOCK_DEBUG
190 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
191 #endif
192 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
193 #define	rw_wlock(rw)		_rw_wlock((rw), LOCK_FILE, LOCK_LINE)
194 #define	rw_wunlock(rw)		_rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
195 #else
196 #define	rw_wlock(rw)							\
197 	__rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
198 #define	rw_wunlock(rw)							\
199 	__rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
200 #endif
201 #define	rw_rlock(rw)		_rw_rlock((rw), LOCK_FILE, LOCK_LINE)
202 #define	rw_runlock(rw)		_rw_runlock((rw), LOCK_FILE, LOCK_LINE)
203 #define	rw_try_rlock(rw)	_rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
204 #define	rw_try_upgrade(rw)	_rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
205 #define	rw_try_wlock(rw)	_rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
206 #define	rw_downgrade(rw)	_rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
207 #define	rw_unlock(rw)	do {						\
208 	if (rw_wowned(rw))						\
209 		rw_wunlock(rw);						\
210 	else								\
211 		rw_runlock(rw);						\
212 } while (0)
213 #define	rw_sleep(chan, rw, pri, wmesg, timo)				\
214 	_sleep((chan), &(rw)->lock_object, (pri), (wmesg),		\
215 	    tick_sbt * (timo), 0, C_HARDCLOCK)
216 
217 #define	rw_initialized(rw)	lock_initalized(&(rw)->lock_object)
218 
219 struct rw_args {
220 	void		*ra_rw;
221 	const char 	*ra_desc;
222 };
223 
224 struct rw_args_flags {
225 	void		*ra_rw;
226 	const char 	*ra_desc;
227 	int		ra_flags;
228 };
229 
230 #define	RW_SYSINIT(name, rw, desc)					\
231 	static struct rw_args name##_args = {				\
232 		(rw),							\
233 		(desc),							\
234 	};								\
235 	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
236 	    rw_sysinit, &name##_args);					\
237 	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
238 	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
239 
240 
241 #define	RW_SYSINIT_FLAGS(name, rw, desc, flags)				\
242 	static struct rw_args_flags name##_args = {			\
243 		(rw),							\
244 		(desc),							\
245 		(flags),						\
246 	};								\
247 	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
248 	    rw_sysinit_flags, &name##_args);				\
249 	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
250 	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
251 
252 /*
253  * Options passed to rw_init_flags().
254  */
255 #define	RW_DUPOK	0x01
256 #define	RW_NOPROFILE	0x02
257 #define	RW_NOWITNESS	0x04
258 #define	RW_QUIET	0x08
259 #define	RW_RECURSE	0x10
260 
261 /*
262  * The INVARIANTS-enabled rw_assert() functionality.
263  *
264  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
265  * support as _rw_assert() itself uses them and the latter implies that
266  * _rw_assert() must build.
267  */
268 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
269 #define	RA_LOCKED		LA_LOCKED
270 #define	RA_RLOCKED		LA_SLOCKED
271 #define	RA_WLOCKED		LA_XLOCKED
272 #define	RA_UNLOCKED		LA_UNLOCKED
273 #define	RA_RECURSED		LA_RECURSED
274 #define	RA_NOTRECURSED		LA_NOTRECURSED
275 #endif
276 
277 #ifdef INVARIANTS
278 #define	rw_assert(rw, what)	_rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
279 #else
280 #define	rw_assert(rw, what)
281 #endif
282 
283 #endif /* _KERNEL */
284 #endif /* !_SYS_RWLOCK_H_ */
285