1154941Sjhb/*-
2154941Sjhb * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3154941Sjhb * All rights reserved.
4154941Sjhb *
5154941Sjhb * Redistribution and use in source and binary forms, with or without
6154941Sjhb * modification, are permitted provided that the following conditions
7154941Sjhb * are met:
8154941Sjhb * 1. Redistributions of source code must retain the above copyright
9154941Sjhb *    notice, this list of conditions and the following disclaimer.
10154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11154941Sjhb *    notice, this list of conditions and the following disclaimer in the
12154941Sjhb *    documentation and/or other materials provided with the distribution.
13154941Sjhb *
14154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17154941Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24154941Sjhb * SUCH DAMAGE.
25154941Sjhb *
26154941Sjhb * $FreeBSD: stable/10/sys/sys/rwlock.h 323870 2017-09-21 19:24:11Z marius $
27154941Sjhb */
28154941Sjhb
29154941Sjhb#ifndef _SYS_RWLOCK_H_
30154941Sjhb#define _SYS_RWLOCK_H_
31154941Sjhb
32154941Sjhb#include <sys/_lock.h>
33154941Sjhb#include <sys/_rwlock.h>
34171516Sattilio#include <sys/lock_profile.h>
35192853Ssson#include <sys/lockstat.h>
36154941Sjhb
37154941Sjhb#ifdef _KERNEL
38163320Sglebius#include <sys/pcpu.h>
39154941Sjhb#include <machine/atomic.h>
40154941Sjhb#endif
41154941Sjhb
42154941Sjhb/*
43154941Sjhb * The rw_lock field consists of several fields.  The low bit indicates
44154941Sjhb * if the lock is locked with a read (shared) or write (exclusive) lock.
45154941Sjhb * A value of 0 indicates a write lock, and a value of 1 indicates a read
46154941Sjhb * lock.  Bit 1 is a boolean indicating if there are any threads waiting
47154941Sjhb * for a read lock.  Bit 2 is a boolean indicating if there are any threads
48154941Sjhb * waiting for a write lock.  The rest of the variable's definition is
49154941Sjhb * dependent on the value of the first bit.  For a write lock, it is a
50154941Sjhb * pointer to the thread holding the lock, similar to the mtx_lock field of
51154941Sjhb * mutexes.  For read locks, it is a count of read locks that are held.
52154941Sjhb *
53154941Sjhb * When the lock is not locked by any thread, it is encoded as a read lock
54154941Sjhb * with zero waiters.
55154941Sjhb */
56154941Sjhb
57154941Sjhb#define	RW_LOCK_READ		0x01
58154941Sjhb#define	RW_LOCK_READ_WAITERS	0x02
59154941Sjhb#define	RW_LOCK_WRITE_WAITERS	0x04
60176017Sjeff#define	RW_LOCK_WRITE_SPINNER	0x08
61154941Sjhb#define	RW_LOCK_FLAGMASK						\
62171052Sattilio	(RW_LOCK_READ | RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS |	\
63176017Sjeff	RW_LOCK_WRITE_SPINNER)
64176017Sjeff#define	RW_LOCK_WAITERS		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
65154941Sjhb
66154941Sjhb#define	RW_OWNER(x)		((x) & ~RW_LOCK_FLAGMASK)
67171052Sattilio#define	RW_READERS_SHIFT	4
68154941Sjhb#define	RW_READERS(x)		(RW_OWNER((x)) >> RW_READERS_SHIFT)
69154941Sjhb#define	RW_READERS_LOCK(x)	((x) << RW_READERS_SHIFT | RW_LOCK_READ)
70154941Sjhb#define	RW_ONE_READER		(1 << RW_READERS_SHIFT)
71154941Sjhb
72154941Sjhb#define	RW_UNLOCKED		RW_READERS_LOCK(0)
73169394Sjhb#define	RW_DESTROYED		(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)
74154941Sjhb
75154941Sjhb#ifdef _KERNEL
76154941Sjhb
77179025Sattilio#define	rw_recurse	lock_object.lo_data
78179025Sattilio
79154941Sjhb/* Very simple operations on rw_lock. */
80154941Sjhb
81154941Sjhb/* Try to obtain a write lock once. */
82154941Sjhb#define	_rw_write_lock(rw, tid)						\
83154941Sjhb	atomic_cmpset_acq_ptr(&(rw)->rw_lock, RW_UNLOCKED, (tid))
84154941Sjhb
85154941Sjhb/* Release a write lock quickly if there are no waiters. */
86154941Sjhb#define	_rw_write_unlock(rw, tid)					\
87154941Sjhb	atomic_cmpset_rel_ptr(&(rw)->rw_lock, (tid), RW_UNLOCKED)
88154941Sjhb
89154941Sjhb/*
90154941Sjhb * Full lock operations that are suitable to be inlined in non-debug
91154941Sjhb * kernels.  If the lock cannot be acquired or released trivially then
92154941Sjhb * the work is deferred to another function.
93154941Sjhb */
94154941Sjhb
95154941Sjhb/* Acquire a write lock. */
96171516Sattilio#define	__rw_wlock(rw, tid, file, line) do {				\
97154941Sjhb	uintptr_t _tid = (uintptr_t)(tid);				\
98167012Skmacy						                        \
99310979Smjg	if ((rw)->rw_lock != RW_UNLOCKED || !_rw_write_lock((rw), _tid))\
100154941Sjhb		_rw_wlock_hard((rw), _tid, (file), (line));		\
101192853Ssson	else 								\
102192853Ssson		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, \
103192853Ssson		    rw, 0, 0, (file), (line));				\
104154941Sjhb} while (0)
105154941Sjhb
106154941Sjhb/* Release a write lock. */
107154941Sjhb#define	__rw_wunlock(rw, tid, file, line) do {				\
108154941Sjhb	uintptr_t _tid = (uintptr_t)(tid);				\
109154941Sjhb									\
110176017Sjeff	if ((rw)->rw_recurse)						\
111176017Sjeff		(rw)->rw_recurse--;					\
112320885Smjg	else if (!_rw_write_unlock((rw), _tid))				\
113154941Sjhb		_rw_wunlock_hard((rw), _tid, (file), (line));		\
114154941Sjhb} while (0)
115154941Sjhb
116154941Sjhb/*
117154941Sjhb * Function prototypes.  Routines that start with _ are not part of the
118154941Sjhb * external API and should not be called directly.  Wrapper macros should
119154941Sjhb * be used instead.
120154941Sjhb */
121242515Sattiliovoid	_rw_init_flags(volatile uintptr_t *c, const char *name, int opts);
122242515Sattiliovoid	_rw_destroy(volatile uintptr_t *c);
123154941Sjhbvoid	rw_sysinit(void *arg);
124185778Skmacyvoid	rw_sysinit_flags(void *arg);
125242515Sattilioint	_rw_wowned(const volatile uintptr_t *c);
126242515Sattiliovoid	_rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line);
127242515Sattilioint	__rw_try_wlock(volatile uintptr_t *c, const char *file, int line);
128242515Sattiliovoid	_rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line);
129242515Sattiliovoid	__rw_rlock(volatile uintptr_t *c, const char *file, int line);
130242515Sattilioint	__rw_try_rlock(volatile uintptr_t *c, const char *file, int line);
131242515Sattiliovoid	_rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line);
132242515Sattiliovoid	__rw_wlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
133154941Sjhb	    int line);
134242515Sattiliovoid	__rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid,
135242515Sattilio	    const char *file, int line);
136242515Sattilioint	__rw_try_upgrade(volatile uintptr_t *c, const char *file, int line);
137242515Sattiliovoid	__rw_downgrade(volatile uintptr_t *c, const char *file, int line);
138242515Sattilio#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
139242515Sattiliovoid	__rw_assert(const volatile uintptr_t *c, int what, const char *file,
140154941Sjhb	    int line);
141242515Sattilio#endif
142242515Sattilio
143242515Sattilio/*
144242515Sattilio * Top-level macros to provide lock cookie once the actual rwlock is passed.
145242515Sattilio * They will also prevent passing a malformed object to the rwlock KPI by
146242515Sattilio * failing compilation as the rw_lock reserved member will not be found.
147242515Sattilio */
148242515Sattilio#define	rw_init(rw, n)							\
149242515Sattilio	_rw_init_flags(&(rw)->rw_lock, n, 0)
150242515Sattilio#define	rw_init_flags(rw, n, o)						\
151242515Sattilio	_rw_init_flags(&(rw)->rw_lock, n, o)
152242515Sattilio#define	rw_destroy(rw)							\
153242515Sattilio	_rw_destroy(&(rw)->rw_lock)
154242515Sattilio#define	rw_wowned(rw)							\
155242515Sattilio	_rw_wowned(&(rw)->rw_lock)
156242515Sattilio#define	_rw_wlock(rw, f, l)						\
157242515Sattilio	_rw_wlock_cookie(&(rw)->rw_lock, f, l)
158242515Sattilio#define	_rw_try_wlock(rw, f, l)						\
159242515Sattilio	__rw_try_wlock(&(rw)->rw_lock, f, l)
160242515Sattilio#define	_rw_wunlock(rw, f, l)						\
161242515Sattilio	_rw_wunlock_cookie(&(rw)->rw_lock, f, l)
162242515Sattilio#define	_rw_rlock(rw, f, l)						\
163242515Sattilio	__rw_rlock(&(rw)->rw_lock, f, l)
164242515Sattilio#define	_rw_try_rlock(rw, f, l)						\
165242515Sattilio	__rw_try_rlock(&(rw)->rw_lock, f, l)
166242515Sattilio#define	_rw_runlock(rw, f, l)						\
167242515Sattilio	_rw_runlock_cookie(&(rw)->rw_lock, f, l)
168242515Sattilio#define	_rw_wlock_hard(rw, t, f, l)					\
169242515Sattilio	__rw_wlock_hard(&(rw)->rw_lock, t, f, l)
170242515Sattilio#define	_rw_wunlock_hard(rw, t, f, l)					\
171242515Sattilio	__rw_wunlock_hard(&(rw)->rw_lock, t, f, l)
172242515Sattilio#define	_rw_try_upgrade(rw, f, l)					\
173242515Sattilio	__rw_try_upgrade(&(rw)->rw_lock, f, l)
174242515Sattilio#define	_rw_downgrade(rw, f, l)						\
175242515Sattilio	__rw_downgrade(&(rw)->rw_lock, f, l)
176154941Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
177242515Sattilio#define	_rw_assert(rw, w, f, l)						\
178242515Sattilio	__rw_assert(&(rw)->rw_lock, w, f, l)
179154941Sjhb#endif
180154941Sjhb
181242515Sattilio
182154941Sjhb/*
183154941Sjhb * Public interface for lock operations.
184154941Sjhb */
185154941Sjhb
186154941Sjhb#ifndef LOCK_DEBUG
187154941Sjhb#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rwlock.h>
188154941Sjhb#endif
189154941Sjhb#if LOCK_DEBUG > 0 || defined(RWLOCK_NOINLINE)
190154941Sjhb#define	rw_wlock(rw)		_rw_wlock((rw), LOCK_FILE, LOCK_LINE)
191154941Sjhb#define	rw_wunlock(rw)		_rw_wunlock((rw), LOCK_FILE, LOCK_LINE)
192154941Sjhb#else
193154941Sjhb#define	rw_wlock(rw)							\
194154941Sjhb	__rw_wlock((rw), curthread, LOCK_FILE, LOCK_LINE)
195154941Sjhb#define	rw_wunlock(rw)							\
196154941Sjhb	__rw_wunlock((rw), curthread, LOCK_FILE, LOCK_LINE)
197154941Sjhb#endif
198154941Sjhb#define	rw_rlock(rw)		_rw_rlock((rw), LOCK_FILE, LOCK_LINE)
199154941Sjhb#define	rw_runlock(rw)		_rw_runlock((rw), LOCK_FILE, LOCK_LINE)
200177843Sattilio#define	rw_try_rlock(rw)	_rw_try_rlock((rw), LOCK_FILE, LOCK_LINE)
201157882Sjhb#define	rw_try_upgrade(rw)	_rw_try_upgrade((rw), LOCK_FILE, LOCK_LINE)
202177843Sattilio#define	rw_try_wlock(rw)	_rw_try_wlock((rw), LOCK_FILE, LOCK_LINE)
203157882Sjhb#define	rw_downgrade(rw)	_rw_downgrade((rw), LOCK_FILE, LOCK_LINE)
204177260Spjd#define	rw_unlock(rw)	do {						\
205177260Spjd	if (rw_wowned(rw))						\
206177260Spjd		rw_wunlock(rw);						\
207177260Spjd	else								\
208177260Spjd		rw_runlock(rw);						\
209177260Spjd} while (0)
210167387Sjhb#define	rw_sleep(chan, rw, pri, wmesg, timo)				\
211247787Sdavide	_sleep((chan), &(rw)->lock_object, (pri), (wmesg),		\
212247787Sdavide	    tick_sbt * (timo), 0, C_HARDCLOCK)
213154941Sjhb
214167787Sjhb#define	rw_initialized(rw)	lock_initalized(&(rw)->lock_object)
215154941Sjhb
216154941Sjhbstruct rw_args {
217242515Sattilio	void		*ra_rw;
218154941Sjhb	const char 	*ra_desc;
219154941Sjhb};
220154941Sjhb
221185778Skmacystruct rw_args_flags {
222242515Sattilio	void		*ra_rw;
223185778Skmacy	const char 	*ra_desc;
224185778Skmacy	int		ra_flags;
225185778Skmacy};
226185778Skmacy
227154941Sjhb#define	RW_SYSINIT(name, rw, desc)					\
228154941Sjhb	static struct rw_args name##_args = {				\
229154941Sjhb		(rw),							\
230154941Sjhb		(desc),							\
231154941Sjhb	};								\
232154941Sjhb	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
233154941Sjhb	    rw_sysinit, &name##_args);					\
234159897Sjhb	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
235242515Sattilio	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
236154941Sjhb
237185778Skmacy
238185778Skmacy#define	RW_SYSINIT_FLAGS(name, rw, desc, flags)				\
239185778Skmacy	static struct rw_args_flags name##_args = {			\
240185778Skmacy		(rw),							\
241185778Skmacy		(desc),							\
242185778Skmacy		(flags),						\
243185778Skmacy	};								\
244185778Skmacy	SYSINIT(name##_rw_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
245185778Skmacy	    rw_sysinit_flags, &name##_args);				\
246185778Skmacy	SYSUNINIT(name##_rw_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
247242515Sattilio	    _rw_destroy, __DEVOLATILE(void *, &(rw)->rw_lock))
248185778Skmacy
249154941Sjhb/*
250171052Sattilio * Options passed to rw_init_flags().
251171052Sattilio */
252171052Sattilio#define	RW_DUPOK	0x01
253171052Sattilio#define	RW_NOPROFILE	0x02
254171052Sattilio#define	RW_NOWITNESS	0x04
255171052Sattilio#define	RW_QUIET	0x08
256171052Sattilio#define	RW_RECURSE	0x10
257323870Smarius#define	RW_NEW		0x20
258171052Sattilio
259171052Sattilio/*
260154941Sjhb * The INVARIANTS-enabled rw_assert() functionality.
261154941Sjhb *
262154941Sjhb * The constants need to be defined for INVARIANT_SUPPORT infrastructure
263154941Sjhb * support as _rw_assert() itself uses them and the latter implies that
264154941Sjhb * _rw_assert() must build.
265154941Sjhb */
266154941Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
267154941Sjhb#define	RA_LOCKED		LA_LOCKED
268154941Sjhb#define	RA_RLOCKED		LA_SLOCKED
269154941Sjhb#define	RA_WLOCKED		LA_XLOCKED
270154941Sjhb#define	RA_UNLOCKED		LA_UNLOCKED
271171052Sattilio#define	RA_RECURSED		LA_RECURSED
272171052Sattilio#define	RA_NOTRECURSED		LA_NOTRECURSED
273154941Sjhb#endif
274154941Sjhb
275154941Sjhb#ifdef INVARIANTS
276155042Sglebius#define	rw_assert(rw, what)	_rw_assert((rw), (what), LOCK_FILE, LOCK_LINE)
277154941Sjhb#else
278154941Sjhb#define	rw_assert(rw, what)
279154941Sjhb#endif
280154941Sjhb
281154941Sjhb#endif /* _KERNEL */
282154941Sjhb#endif /* !_SYS_RWLOCK_H_ */
283