rmlock.h revision 326305
1173444Sups/*-
2173444Sups * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
3173444Sups * All rights reserved.
4173444Sups *
5173444Sups * Redistribution and use in source and binary forms, with or without
6173444Sups * modification, are permitted provided that the following conditions
7173444Sups * are met:
8173444Sups * 1. Redistributions of source code must retain the above copyright
9173444Sups *    notice, this list of conditions and the following disclaimer.
10173444Sups * 2. Redistributions in binary form must reproduce the above copyright
11173444Sups *    notice, this list of conditions and the following disclaimer in the
12173444Sups *    documentation and/or other materials provided with the distribution.
13173444Sups * 3. Neither the name of the author nor the names of any co-contributors
14173444Sups *    may be used to endorse or promote products derived from this software
15173444Sups *    without specific prior written permission.
16173444Sups *
17173444Sups * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18173444Sups * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19173444Sups * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20173444Sups * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21173444Sups * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22173444Sups * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23173444Sups * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24173444Sups * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25173444Sups * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26173444Sups * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27173444Sups * SUCH DAMAGE.
28173444Sups *
29173444Sups * $FreeBSD: stable/11/sys/sys/rmlock.h 326305 2017-11-28 01:15:53Z markj $
30173444Sups */
31173444Sups
32173444Sups#ifndef _SYS_RMLOCK_H_
33173444Sups#define _SYS_RMLOCK_H_
34173444Sups
35173444Sups#include <sys/mutex.h>
36212112Smlaier#include <sys/sx.h>
37173444Sups#include <sys/_lock.h>
38173444Sups#include <sys/_rmlock.h>
39173444Sups
40173444Sups#ifdef _KERNEL
41173444Sups
42193030Srwatson/*
43247588Sjhb * Flags passed to rm_init_flags(9).
44193030Srwatson */
45193030Srwatson#define	RM_NOWITNESS	0x00000001
46193030Srwatson#define	RM_RECURSE	0x00000002
47212112Smlaier#define	RM_SLEEPABLE	0x00000004
48275751Sdchagin#define	RM_NEW		0x00000008
49193030Srwatson
50193030Srwatsonvoid	rm_init(struct rmlock *rm, const char *name);
51193030Srwatsonvoid	rm_init_flags(struct rmlock *rm, const char *name, int opts);
52173444Supsvoid	rm_destroy(struct rmlock *rm);
53227588Spjdint	rm_wowned(const struct rmlock *rm);
54173444Supsvoid	rm_sysinit(void *arg);
55193030Srwatsonvoid	rm_sysinit_flags(void *arg);
56173444Sups
57173444Supsvoid	_rm_wlock_debug(struct rmlock *rm, const char *file, int line);
58173444Supsvoid	_rm_wunlock_debug(struct rmlock *rm, const char *file, int line);
59212112Smlaierint	_rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
60212112Smlaier	    int trylock, const char *file, int line);
61173444Supsvoid	_rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
62193026Srwatson	    const char *file, int line);
63173444Sups
64173444Supsvoid	_rm_wlock(struct rmlock *rm);
65173444Supsvoid	_rm_wunlock(struct rmlock *rm);
66212112Smlaierint	_rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker,
67212112Smlaier	    int trylock);
68173444Supsvoid	_rm_runlock(struct rmlock *rm,  struct rm_priotracker *tracker);
69252209Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
70252209Sjhbvoid	_rm_assert(const struct rmlock *rm, int what, const char *file,
71252209Sjhb	    int line);
72252209Sjhb#endif
73173444Sups
74173444Sups/*
75173444Sups * Public interface for lock operations.
76173444Sups */
77173444Sups#ifndef LOCK_DEBUG
78173444Sups#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
79173444Sups#endif
80173444Sups
81173444Sups#if LOCK_DEBUG > 0
82173444Sups#define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
83173444Sups#define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
84173444Sups#define	rm_rlock(rm,tracker)  \
85212112Smlaier    ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE ))
86212112Smlaier#define	rm_try_rlock(rm,tracker)  \
87212112Smlaier    _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE )
88173444Sups#define	rm_runlock(rm,tracker)	\
89173444Sups    _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
90173444Sups#else
91212112Smlaier#define	rm_wlock(rm)			_rm_wlock((rm))
92212112Smlaier#define	rm_wunlock(rm)			_rm_wunlock((rm))
93212112Smlaier#define	rm_rlock(rm,tracker)		((void)_rm_rlock((rm),(tracker), 0))
94212112Smlaier#define	rm_try_rlock(rm,tracker)	_rm_rlock((rm),(tracker), 1)
95212112Smlaier#define	rm_runlock(rm,tracker)		_rm_runlock((rm), (tracker))
96173444Sups#endif
97252209Sjhb#define	rm_sleep(chan, rm, pri, wmesg, timo)				\
98252209Sjhb	_sleep((chan), &(rm)->lock_object, (pri), (wmesg),		\
99252209Sjhb	    tick_sbt * (timo), 0, C_HARDCLOCK)
100173444Sups
101173444Supsstruct rm_args {
102173444Sups	struct rmlock	*ra_rm;
103173444Sups	const char 	*ra_desc;
104326305Smarkj	int		ra_flags;
105193030Srwatson};
106193030Srwatson
107326305Smarkj#define	RM_SYSINIT_FLAGS(name, rm, desc, flags)				\
108173444Sups	static struct rm_args name##_args = {				\
109173444Sups		(rm),							\
110173444Sups		(desc),							\
111326305Smarkj		(flags),						\
112173444Sups	};								\
113173444Sups	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
114173444Sups	    rm_sysinit, &name##_args);					\
115173444Sups	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
116173444Sups	    rm_destroy, (rm))
117173444Sups
118326305Smarkj#define	RM_SYSINIT(name, rm, desc)	RM_SYSINIT_FLAGS(name, rm, desc, 0)
119193030Srwatson
120252209Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
121252209Sjhb#define	RA_LOCKED		LA_LOCKED
122252209Sjhb#define	RA_RLOCKED		LA_SLOCKED
123252209Sjhb#define	RA_WLOCKED		LA_XLOCKED
124252209Sjhb#define	RA_UNLOCKED		LA_UNLOCKED
125252209Sjhb#define	RA_RECURSED		LA_RECURSED
126252209Sjhb#define	RA_NOTRECURSED		LA_NOTRECURSED
127252209Sjhb#endif
128252209Sjhb
129252209Sjhb#ifdef INVARIANTS
130252209Sjhb#define	rm_assert(rm, what)	_rm_assert((rm), (what), LOCK_FILE, LOCK_LINE)
131252209Sjhb#else
132252209Sjhb#define	rm_assert(rm, what)
133252209Sjhb#endif
134252209Sjhb
135173444Sups#endif /* _KERNEL */
136173444Sups#endif /* !_SYS_RMLOCK_H_ */
137