xref: /NextBSD/sys/sys/rwlock.h (revision 0569111ffe6bb43b73cf6960112bada864347326)
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _SYS_RWLOCK_H_
30 #define _SYS_RWLOCK_H_
31 
32 #include <sys/_lock.h>
33 #include <sys/_rwlock.h>
34 #include <sys/lock_profile.h>
35 #include <sys/lockstat.h>
36 
37 #ifdef _KERNEL
38 #include <sys/pcpu.h>
39 #include <machine/atomic.h>
40 #include <sys/proc.h>
41 #endif
42 
43 /*
44  * The rw_lock field consists of several fields.  The low bit indicates
45  * if the lock is locked with a read (shared) or write (exclusive) lock.
46  * A value of 0 indicates a write lock, and a value of 1 indicates a read
47  * lock.  Bit 1 is a boolean indicating if there are any threads waiting
48  * for a read lock.  Bit 2 is a boolean indicating if there are any threads
49  * waiting for a write lock.  The rest of the variable's definition is
50  * dependent on the value of the first bit.  For a write lock, it is a
51  * pointer to the thread holding the lock, similar to the mtx_lock field of
52  * mutexes.  For read locks, it is a count of read locks that are held.
53  *
54  * When the lock is not locked by any thread, it is encoded as a read lock
55  * with zero waiters.
56  */
57 
58 #define	RW_LOCK_READ		0x01
59 #define	RW_LOCK_READ_WAITERS	0x02
60 #define	RW_LOCK_WRITE_WAITERS	0x04
61 #define	RW_LOCK_WRITE_SPINNER	0x08
62 #define	RW_LOCK_FLAGMASK						\
63 	(RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |	\
64 	RW_LOCK_WRITE_SPINNER)
65 #define	RW_LOCK_WAITERS		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
66 
67 #define	RW_OWNER(x)		((x) & ~RW_LOCK_FLAGMASK)
68 #define	RW_READERS_SHIFT	4
69 #define	RW_READERS(x)		(RW_OWNER((x)) >> RW_READERS_SHIFT)
70 #define	RW_READERS_LOCK(x)	((x) << RW_READERS_SHIFT | RW_LOCK_READ)
71 #define	RW_ONE_READER		(1 << RW_READERS_SHIFT)
72 
73 #define	RW_UNLOCKED		RW_READERS_LOCK(0)
74 #define	RW_DESTROYED		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
75 
76 #ifdef _KERNEL
77 
78 #define	rw_recurse	lock_object.lo_data
79 
80 /* Very simple operations on rw_lock. */
81 
82 /* Try to obtain a write lock once. */
83 #define	_rw_write_lock(rw, tid)						\
84 	atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
85 
86 /* Release a write lock quickly if there are no waiters. */
87 #define	_rw_write_unlock(rw, tid)					\
88 	atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
89 
90 /*
91  * Full lock operations that are suitable to be inlined in non-debug
92  * kernels.  If the lock cannot be acquired or released trivially then
93  * the work is deferred to another function.
94  */
95 
96 /* Acquire a write lock. */
97 #define	__rw_wlock(rw, tid, file, line) do {				\
98 	uintptr_t _tid = (uintptr_t)(tid);				\
99 									\
100 	if (!_rw_write_lock((rw), _tid))				\
101 		_rw_wlock_hard((rw), _tid, (file), (line));		\
102 	else 								\
103 		LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw,	\
104 		    0, 0, file, line, LOCKSTAT_WRITER);			\
105 } while (0)
106 
107 /* Release a write lock. */
108 #define	__rw_wunlock(rw, tid, file, line) do {				\
109 	uintptr_t _tid = (uintptr_t)(tid);				\
110 									\
111 	if ((rw)->rw_recurse)						\
112 		(rw)->rw_recurse--;					\
113 	else {								\
114 		LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw,	\
115 		    LOCKSTAT_WRITER);					\
116 		if (!_rw_write_unlock((rw), _tid))			\
117 			_rw_wunlock_hard((rw), _tid, (file), (line));	\
118 	}								\
119 } while (0)
120 
121 /*
122  * Return the rwlock address when the lock cookie address is provided.
123  * This functionality assumes that struct rwlock* have a member named rw_lock.
124  */
125 #define	rwlock2rw(c)	(__containerof(c, struct rwlock, rw_lock))
126 
127 /*
128  * Determines whether a new reader can acquire a lock.  Succeeds if the
129  * reader already owns a read lock and the lock is locked for read to
130  * prevent deadlock from reader recursion.  Also succeeds if the lock
131  * is unlocked and has no writer waiters or spinners.  Failing otherwise
132  * prioritizes writers before readers.
133  */
134 #define	RW_CAN_READ_TD(td, _rw)										\
135     ((td->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) &	\
136     (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) ==	\
137     RW_LOCK_READ)
138 
139 #define RW_CAN_READ(_rw) RW_CAN_READ_TD(curthread, _rw)
140 
141 
142 /*
143  * Function prototypes.  Routines that start with _ are not part of the
144  * external API and should not be called directly.  Wrapper macros should
145  * be used instead.
146  */
147 void	_rw_init_flags(volatile uintptr_t *c, const char *name, int opts);
148 void	_rw_destroy(volatile uintptr_t *c);
149 void	rw_sysinit(void *arg);
150 void	rw_sysinit_flags(void *arg);
151 int	_rw_wowned(const volatile uintptr_t *c);
152 void	_rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line);
153 int	__rw_try_wlock(volatile uintptr_t *c, const char *file, int line);
154 void	_rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line);
155 void	__rw_rlock_hard(volatile uintptr_t *c, const char *file, int line);
156 int	__rw_try_rlock(volatile uintptr_t *c, const char *file, int line);
157 void	_rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line);
158 void	__rw_wlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
159 	    int line);
160 void	__rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid,
161 	    const char *file, int line);
162 int	__rw_try_upgrade(volatile uintptr_t *c, const char *file, int line);
163 void	__rw_downgrade(volatile uintptr_t *c, const char *file, int line);
164 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
165 void	__rw_assert(const volatile uintptr_t *c, int what, const char *file,
166 	    int line);
167 #endif
168 
169 #ifdef WITNESS
170 #define __rw_rlock(c, file, line) 	__rw_rlock_hard(c, file, line)
171 #define __rw_runlock(c, file, line) _rw_runlock_cookie(c, file, line)
172 #else
173 static inline void
__rw_rlock(volatile uintptr_t * c,const char * file,int line)174 __rw_rlock(volatile uintptr_t *c, const char *file, int line)
175 {
176 	struct rwlock *rw;
177 	uintptr_t v;
178 	struct thread *td = curthread;
179 
180 	rw = rwlock2rw(c);
181 	v = rw->rw_lock;
182 
183 	if (RW_CAN_READ_TD(td, v)  && atomic_cmpset_acq_ptr(&rw->rw_lock, v, v + RW_ONE_READER)) {
184 		td->td_rw_rlocks++;
185 		TD_LOCKS_INC(td);
186 		return;
187 	}
188 	__rw_rlock_hard(c, file, line);
189 }
190 
191 
192 static inline void
__rw_runlock(volatile uintptr_t * c,const char * file,int line)193 __rw_runlock(volatile uintptr_t *c, const char *file, int line)
194 {
195 	struct rwlock *rw;
196 	uintptr_t v;
197 	struct thread *td;
198 
199 	rw = rwlock2rw(c);
200 	v = rw->rw_lock;
201 
202 	if ((!(v & RW_LOCK_WAITERS) || (RW_READERS(v) > 1)) && atomic_cmpset_rel_ptr(&rw->rw_lock, v, v - RW_ONE_READER)) {
203 		td = curthread;
204 		TD_LOCKS_DEC(td);
205 		td->td_rw_rlocks--;
206 		return;
207 	}
208 	_rw_runlock_cookie(c, file, line);
209 }
210 #endif
211 /*
212  * Top-level macros to provide lock cookie once the actual rwlock is passed.
213  * They will also prevent passing a malformed object to the rwlock KPI by
214  * failing compilation as the rw_lock reserved member will not be found.
215  */
216 #define	rw_init(rw, n)							\
217 	_rw_init_flags(&(rw)->rw_lock, n, 0)
218 #define	rw_init_flags(rw, n, o)						\
219 	_rw_init_flags(&(rw)->rw_lock, n, o)
220 #define	rw_destroy(rw)							\
221 	_rw_destroy(&(rw)->rw_lock)
222 #define	rw_wowned(rw)							\
223 	_rw_wowned(&(rw)->rw_lock)
224 #define	_rw_wlock(rw, f, l)						\
225 	_rw_wlock_cookie(&(rw)->rw_lock, f, l)
226 #define	_rw_try_wlock(rw, f, l)						\
227 	__rw_try_wlock(&(rw)->rw_lock, f, l)
228 #define	_rw_wunlock(rw, f, l)						\
229 	_rw_wunlock_cookie(&(rw)->rw_lock, f, l)
230 #define	_rw_rlock(rw, f, l)						\
231 	__rw_rlock(&(rw)->rw_lock, f, l)
232 #define	_rw_try_rlock(rw, f, l)						\
233 	__rw_try_rlock(&(rw)->rw_lock, f, l)
234 #define	_rw_runlock(rw, f, l)						\
235 	__rw_runlock(&(rw)->rw_lock, f, l)
236 #define	_rw_wlock_hard(rw, t, f, l)					\
237 	__rw_wlock_hard(&(rw)->rw_lock, t, f, l)
238 #define	_rw_wunlock_hard(rw, t, f, l)					\
239 	__rw_wunlock_hard(&(rw)->rw_lock, t, f, l)
240 #define	_rw_try_upgrade(rw, f, l)					\
241 	__rw_try_upgrade(&(rw)->rw_lock, f, l)
242 #define	_rw_downgrade(rw, f, l)						\
243 	__rw_downgrade(&(rw)->rw_lock, f, l)
244 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
245 #define	_rw_assert(rw, w, f, l)						\
246 	__rw_assert(&(rw)->rw_lock, w, f, l)
247 #endif
248 
249 
250 /*
251  * Public interface for lock operations.
252  */
253 
254 #ifndef LOCK_DEBUG
255 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
256 #endif
257 #if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
258 #define	rw_wlock(rw)		_rw_wlock((rw), LOCK_FILE, LOCK_LINE)
259 #define	rw_wunlock(rw)		_rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
260 #else
261 #define	rw_wlock(rw)							\
262 	__rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
263 #define	rw_wunlock(rw)							\
264 	__rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
265 #endif
266 #define	rw_rlock(rw)		_rw_rlock((rw), LOCK_FILE, LOCK_LINE)
267 #define	rw_runlock(rw)		_rw_runlock((rw), LOCK_FILE, LOCK_LINE)
268 #define	rw_try_rlock(rw)	_rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
269 #define	rw_try_upgrade(rw)	_rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
270 #define	rw_try_wlock(rw)	_rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
271 #define	rw_downgrade(rw)	_rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
272 #define	rw_unlock(rw)	do {						\
273 	if (rw_wowned(rw))						\
274 		rw_wunlock(rw);						\
275 	else								\
276 		rw_runlock(rw);						\
277 } while (0)
278 #define	rw_sleep(chan, rw, pri, wmesg, timo)				\
279 	_sleep((chan), &(rw)->lock_object, (pri), (wmesg),		\
280 	    tick_sbt * (timo), 0, C_HARDCLOCK)
281 
282 #define	rw_initialized(rw)	lock_initialized(&(rw)->lock_object)
283 
284 struct rw_args {
285 	void		*ra_rw;
286 	const char 	*ra_desc;
287 };
288 
289 struct rw_args_flags {
290 	void		*ra_rw;
291 	const char 	*ra_desc;
292 	int		ra_flags;
293 };
294 
295 #define	RW_SYSINIT(name, rw, desc)					\
296 	static struct rw_args name##_args = {				\
297 		(rw),							\
298 		(desc),							\
299 	};								\
300 	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
301 	    rw_sysinit, &name##_args);					\
302 	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
303 	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
304 
305 
306 #define	RW_SYSINIT_FLAGS(name, rw, desc, flags)				\
307 	static struct rw_args_flags name##_args = {			\
308 		(rw),							\
309 		(desc),							\
310 		(flags),						\
311 	};								\
312 	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
313 	    rw_sysinit_flags, &name##_args);				\
314 	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
315 	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
316 
317 /*
318  * Options passed to rw_init_flags().
319  */
320 #define	RW_DUPOK	0x01
321 #define	RW_NOPROFILE	0x02
322 #define	RW_NOWITNESS	0x04
323 #define	RW_QUIET	0x08
324 #define	RW_RECURSE	0x10
325 #define	RW_NEW		0x20
326 
327 /*
328  * The INVARIANTS-enabled rw_assert() functionality.
329  *
330  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
331  * support as _rw_assert() itself uses them and the latter implies that
332  * _rw_assert() must build.
333  */
334 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
335 #define	RA_LOCKED		LA_LOCKED
336 #define	RA_RLOCKED		LA_SLOCKED
337 #define	RA_WLOCKED		LA_XLOCKED
338 #define	RA_UNLOCKED		LA_UNLOCKED
339 #define	RA_RECURSED		LA_RECURSED
340 #define	RA_NOTRECURSED		LA_NOTRECURSED
341 #endif
342 
343 #ifdef INVARIANTS
344 #define	rw_assert(rw, what)	_rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
345 #else
346 #define	rw_assert(rw, what)
347 #endif
348 
349 #endif /* _KERNEL */
350 #endif /* !_SYS_RWLOCK_H_ */
351