xref: /freebsd/sys/sys/rmlock.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1f53d15feSStephan Uphoff /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4f53d15feSStephan Uphoff  * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
5f53d15feSStephan Uphoff  * All rights reserved.
6f53d15feSStephan Uphoff  *
7f53d15feSStephan Uphoff  * Redistribution and use in source and binary forms, with or without
8f53d15feSStephan Uphoff  * modification, are permitted provided that the following conditions
9f53d15feSStephan Uphoff  * are met:
10f53d15feSStephan Uphoff  * 1. Redistributions of source code must retain the above copyright
11f53d15feSStephan Uphoff  *    notice, this list of conditions and the following disclaimer.
12f53d15feSStephan Uphoff  * 2. Redistributions in binary form must reproduce the above copyright
13f53d15feSStephan Uphoff  *    notice, this list of conditions and the following disclaimer in the
14f53d15feSStephan Uphoff  *    documentation and/or other materials provided with the distribution.
15f53d15feSStephan Uphoff  * 3. Neither the name of the author nor the names of any co-contributors
16f53d15feSStephan Uphoff  *    may be used to endorse or promote products derived from this software
17f53d15feSStephan Uphoff  *    without specific prior written permission.
18f53d15feSStephan Uphoff  *
19f53d15feSStephan Uphoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20f53d15feSStephan Uphoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21f53d15feSStephan Uphoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22f53d15feSStephan Uphoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23f53d15feSStephan Uphoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24f53d15feSStephan Uphoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25f53d15feSStephan Uphoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26f53d15feSStephan Uphoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27f53d15feSStephan Uphoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28f53d15feSStephan Uphoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29f53d15feSStephan Uphoff  * SUCH DAMAGE.
30f53d15feSStephan Uphoff  */
31f53d15feSStephan Uphoff 
32f53d15feSStephan Uphoff #ifndef _SYS_RMLOCK_H_
33f53d15feSStephan Uphoff #define _SYS_RMLOCK_H_
34f53d15feSStephan Uphoff 
35f53d15feSStephan Uphoff #include <sys/mutex.h>
3636058c09SMax Laier #include <sys/sx.h>
37f53d15feSStephan Uphoff #include <sys/_lock.h>
38f53d15feSStephan Uphoff #include <sys/_rmlock.h>
39f53d15feSStephan Uphoff 
40f53d15feSStephan Uphoff #ifdef _KERNEL
41f53d15feSStephan Uphoff 
421a109c1cSRobert Watson /*
43f9379dc4SJohn Baldwin  * Flags passed to rm_init_flags(9).
441a109c1cSRobert Watson  */
451a109c1cSRobert Watson #define	RM_NOWITNESS	0x00000001
461a109c1cSRobert Watson #define	RM_RECURSE	0x00000002
4736058c09SMax Laier #define	RM_SLEEPABLE	0x00000004
48fd07ddcfSDmitry Chagin #define	RM_NEW		0x00000008
492816bd84SMitchell Horne #define	RM_DUPOK	0x00000010
501a109c1cSRobert Watson 
511a109c1cSRobert Watson void	rm_init(struct rmlock *rm, const char *name);
521a109c1cSRobert Watson void	rm_init_flags(struct rmlock *rm, const char *name, int opts);
53f53d15feSStephan Uphoff void	rm_destroy(struct rmlock *rm);
54d576deedSPawel Jakub Dawidek int	rm_wowned(const struct rmlock *rm);
55f53d15feSStephan Uphoff void	rm_sysinit(void *arg);
56f53d15feSStephan Uphoff 
57f53d15feSStephan Uphoff void	_rm_wlock_debug(struct rmlock *rm, const char *file, int line);
58f53d15feSStephan Uphoff void	_rm_wunlock_debug(struct rmlock *rm, const char *file, int line);
5936058c09SMax Laier int	_rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
6036058c09SMax Laier 	    int trylock, const char *file, int line);
61f53d15feSStephan Uphoff void	_rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
62f53d15feSStephan Uphoff 	    const char *file, int line);
63f53d15feSStephan Uphoff 
64f53d15feSStephan Uphoff void	_rm_wlock(struct rmlock *rm);
65f53d15feSStephan Uphoff void	_rm_wunlock(struct rmlock *rm);
6636058c09SMax Laier int	_rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker,
6736058c09SMax Laier 	    int trylock);
68f53d15feSStephan Uphoff void	_rm_runlock(struct rmlock *rm,  struct rm_priotracker *tracker);
69cd32bd7aSJohn Baldwin #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
70cd32bd7aSJohn Baldwin void	_rm_assert(const struct rmlock *rm, int what, const char *file,
71cd32bd7aSJohn Baldwin 	    int line);
72cd32bd7aSJohn Baldwin #endif
73f53d15feSStephan Uphoff 
74f53d15feSStephan Uphoff /*
75f53d15feSStephan Uphoff  * Public interface for lock operations.
76f53d15feSStephan Uphoff  */
77f53d15feSStephan Uphoff #ifndef LOCK_DEBUG
78f53d15feSStephan Uphoff #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
79f53d15feSStephan Uphoff #endif
80f53d15feSStephan Uphoff 
81f53d15feSStephan Uphoff #if LOCK_DEBUG > 0
82f53d15feSStephan Uphoff #define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
83f53d15feSStephan Uphoff #define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
84f53d15feSStephan Uphoff #define	rm_rlock(rm,tracker)  \
8536058c09SMax Laier     ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE ))
8636058c09SMax Laier #define	rm_try_rlock(rm,tracker)  \
8736058c09SMax Laier     _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE )
88f53d15feSStephan Uphoff #define	rm_runlock(rm,tracker)	\
89f53d15feSStephan Uphoff     _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
90f53d15feSStephan Uphoff #else
91f53d15feSStephan Uphoff #define	rm_wlock(rm)			_rm_wlock((rm))
92f53d15feSStephan Uphoff #define	rm_wunlock(rm)			_rm_wunlock((rm))
9336058c09SMax Laier #define	rm_rlock(rm,tracker)		((void)_rm_rlock((rm),(tracker), 0))
9436058c09SMax Laier #define	rm_try_rlock(rm,tracker)	_rm_rlock((rm),(tracker), 1)
95f53d15feSStephan Uphoff #define	rm_runlock(rm,tracker)		_rm_runlock((rm), (tracker))
96f53d15feSStephan Uphoff #endif
97cd32bd7aSJohn Baldwin #define	rm_sleep(chan, rm, pri, wmesg, timo)				\
98cd32bd7aSJohn Baldwin 	_sleep((chan), &(rm)->lock_object, (pri), (wmesg),		\
99cd32bd7aSJohn Baldwin 	    tick_sbt * (timo), 0, C_HARDCLOCK)
100f53d15feSStephan Uphoff 
101f53d15feSStephan Uphoff struct rm_args {
102f53d15feSStephan Uphoff 	struct rmlock	*ra_rm;
103f53d15feSStephan Uphoff 	const char 	*ra_desc;
104755230ebSMark Johnston 	int		ra_flags;
1051a109c1cSRobert Watson };
1061a109c1cSRobert Watson 
107755230ebSMark Johnston #define	RM_SYSINIT_FLAGS(name, rm, desc, flags)				\
1081a109c1cSRobert Watson 	static struct rm_args name##_args = {				\
1091a109c1cSRobert Watson 		(rm),							\
1101a109c1cSRobert Watson 		(desc),							\
111755230ebSMark Johnston 		(flags),						\
1121a109c1cSRobert Watson 	};								\
1131a109c1cSRobert Watson 	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
1141a109c1cSRobert Watson 	    rm_sysinit, &name##_args);					\
1151a109c1cSRobert Watson 	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
1161a109c1cSRobert Watson 	    rm_destroy, (rm))
1171a109c1cSRobert Watson 
118755230ebSMark Johnston #define	RM_SYSINIT(name, rm, desc)	RM_SYSINIT_FLAGS(name, rm, desc, 0)
119f53d15feSStephan Uphoff 
120cd32bd7aSJohn Baldwin #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
121cd32bd7aSJohn Baldwin #define	RA_LOCKED		LA_LOCKED
122cd32bd7aSJohn Baldwin #define	RA_RLOCKED		LA_SLOCKED
123cd32bd7aSJohn Baldwin #define	RA_WLOCKED		LA_XLOCKED
124cd32bd7aSJohn Baldwin #define	RA_UNLOCKED		LA_UNLOCKED
125cd32bd7aSJohn Baldwin #define	RA_RECURSED		LA_RECURSED
126cd32bd7aSJohn Baldwin #define	RA_NOTRECURSED		LA_NOTRECURSED
127cd32bd7aSJohn Baldwin #endif
128cd32bd7aSJohn Baldwin 
129cd32bd7aSJohn Baldwin #ifdef INVARIANTS
130cd32bd7aSJohn Baldwin #define	rm_assert(rm, what)	_rm_assert((rm), (what), LOCK_FILE, LOCK_LINE)
131cd32bd7aSJohn Baldwin #else
132cd32bd7aSJohn Baldwin #define	rm_assert(rm, what)
133cd32bd7aSJohn Baldwin #endif
134cd32bd7aSJohn Baldwin 
1351f162fefSMateusz Guzik void	rms_init(struct rmslock *rms, const char *name);
1361f162fefSMateusz Guzik void	rms_destroy(struct rmslock *rms);
1371f162fefSMateusz Guzik void	rms_rlock(struct rmslock *rms);
1381a78ac24SMateusz Guzik int	rms_try_rlock(struct rmslock *rms);
1391f162fefSMateusz Guzik void	rms_runlock(struct rmslock *rms);
1401f162fefSMateusz Guzik void	rms_wlock(struct rmslock *rms);
1411f162fefSMateusz Guzik void	rms_wunlock(struct rmslock *rms);
1426fc2b069SMateusz Guzik void	rms_unlock(struct rmslock *rms);
1431f162fefSMateusz Guzik 
1441a78ac24SMateusz Guzik static inline int
rms_wowned(struct rmslock * rms)1451a78ac24SMateusz Guzik rms_wowned(struct rmslock *rms)
1461a78ac24SMateusz Guzik {
1471a78ac24SMateusz Guzik 
1486fc2b069SMateusz Guzik 	return (rms->owner == curthread);
1496fc2b069SMateusz Guzik }
1506fc2b069SMateusz Guzik 
15142e7abd5SMateusz Guzik #ifdef INVARIANTS
152*94882626SMateusz Guzik #define rms_assert_rlock_ok(x)	\
153*94882626SMateusz Guzik 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
154*94882626SMateusz Guzik #else
155*94882626SMateusz Guzik #define rms_assert_rlock_ok(x)
156*94882626SMateusz Guzik #endif
157*94882626SMateusz Guzik 
158*94882626SMateusz Guzik #ifdef INVARIANTS
1596fc2b069SMateusz Guzik /*
16042e7abd5SMateusz Guzik  * For assertion purposes.
16142e7abd5SMateusz Guzik  *
16242e7abd5SMateusz Guzik  * Main limitation is that we at best can tell there are readers, but not
16342e7abd5SMateusz Guzik  * whether curthread is one of them.
1646fc2b069SMateusz Guzik  */
1656fc2b069SMateusz Guzik static inline int
rms_rowned(struct rmslock * rms)1666fc2b069SMateusz Guzik rms_rowned(struct rmslock *rms)
1676fc2b069SMateusz Guzik {
1686fc2b069SMateusz Guzik 
16942e7abd5SMateusz Guzik 	return (rms->debug_readers > 0);
1706fc2b069SMateusz Guzik }
1716fc2b069SMateusz Guzik 
1726fc2b069SMateusz Guzik static inline int
rms_owned_any(struct rmslock * rms)1736fc2b069SMateusz Guzik rms_owned_any(struct rmslock *rms)
1746fc2b069SMateusz Guzik {
1756fc2b069SMateusz Guzik 
1766fc2b069SMateusz Guzik 	if (rms_wowned(rms))
1776fc2b069SMateusz Guzik 		return (1);
1786fc2b069SMateusz Guzik 
1796fc2b069SMateusz Guzik 	return (rms_rowned(rms));
1801a78ac24SMateusz Guzik }
18142e7abd5SMateusz Guzik #endif
1821a78ac24SMateusz Guzik 
183f53d15feSStephan Uphoff #endif /* _KERNEL */
184f53d15feSStephan Uphoff #endif /* !_SYS_RMLOCK_H_ */
185