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 343420 2019-01-25 11:01:11Z kib $
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);
55173444Sups
56173444Supsvoid	_rm_wlock_debug(struct rmlock *rm, const char *file, int line);
57173444Supsvoid	_rm_wunlock_debug(struct rmlock *rm, const char *file, int line);
58212112Smlaierint	_rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
59212112Smlaier	    int trylock, const char *file, int line);
60173444Supsvoid	_rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
61193026Srwatson	    const char *file, int line);
62173444Sups
63173444Supsvoid	_rm_wlock(struct rmlock *rm);
64173444Supsvoid	_rm_wunlock(struct rmlock *rm);
65212112Smlaierint	_rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker,
66212112Smlaier	    int trylock);
67173444Supsvoid	_rm_runlock(struct rmlock *rm,  struct rm_priotracker *tracker);
68252209Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
69252209Sjhbvoid	_rm_assert(const struct rmlock *rm, int what, const char *file,
70252209Sjhb	    int line);
71252209Sjhb#endif
72173444Sups
73173444Sups/*
74173444Sups * Public interface for lock operations.
75173444Sups */
76173444Sups#ifndef LOCK_DEBUG
77173444Sups#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
78173444Sups#endif
79173444Sups
80173444Sups#if LOCK_DEBUG > 0
81173444Sups#define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
82173444Sups#define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
83173444Sups#define	rm_rlock(rm,tracker)  \
84212112Smlaier    ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE ))
85212112Smlaier#define	rm_try_rlock(rm,tracker)  \
86212112Smlaier    _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE )
87173444Sups#define	rm_runlock(rm,tracker)	\
88173444Sups    _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
89173444Sups#else
90212112Smlaier#define	rm_wlock(rm)			_rm_wlock((rm))
91212112Smlaier#define	rm_wunlock(rm)			_rm_wunlock((rm))
92212112Smlaier#define	rm_rlock(rm,tracker)		((void)_rm_rlock((rm),(tracker), 0))
93212112Smlaier#define	rm_try_rlock(rm,tracker)	_rm_rlock((rm),(tracker), 1)
94212112Smlaier#define	rm_runlock(rm,tracker)		_rm_runlock((rm), (tracker))
95173444Sups#endif
96252209Sjhb#define	rm_sleep(chan, rm, pri, wmesg, timo)				\
97252209Sjhb	_sleep((chan), &(rm)->lock_object, (pri), (wmesg),		\
98252209Sjhb	    tick_sbt * (timo), 0, C_HARDCLOCK)
99173444Sups
100173444Supsstruct rm_args {
101173444Sups	struct rmlock	*ra_rm;
102173444Sups	const char 	*ra_desc;
103326305Smarkj	int		ra_flags;
104193030Srwatson};
105193030Srwatson
106326305Smarkj#define	RM_SYSINIT_FLAGS(name, rm, desc, flags)				\
107173444Sups	static struct rm_args name##_args = {				\
108173444Sups		(rm),							\
109173444Sups		(desc),							\
110326305Smarkj		(flags),						\
111173444Sups	};								\
112173444Sups	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
113173444Sups	    rm_sysinit, &name##_args);					\
114173444Sups	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
115173444Sups	    rm_destroy, (rm))
116173444Sups
117326305Smarkj#define	RM_SYSINIT(name, rm, desc)	RM_SYSINIT_FLAGS(name, rm, desc, 0)
118193030Srwatson
119252209Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
120252209Sjhb#define	RA_LOCKED		LA_LOCKED
121252209Sjhb#define	RA_RLOCKED		LA_SLOCKED
122252209Sjhb#define	RA_WLOCKED		LA_XLOCKED
123252209Sjhb#define	RA_UNLOCKED		LA_UNLOCKED
124252209Sjhb#define	RA_RECURSED		LA_RECURSED
125252209Sjhb#define	RA_NOTRECURSED		LA_NOTRECURSED
126252209Sjhb#endif
127252209Sjhb
128252209Sjhb#ifdef INVARIANTS
129252209Sjhb#define	rm_assert(rm, what)	_rm_assert((rm), (what), LOCK_FILE, LOCK_LINE)
130252209Sjhb#else
131252209Sjhb#define	rm_assert(rm, what)
132252209Sjhb#endif
133252209Sjhb
134173444Sups#endif /* _KERNEL */
135173444Sups#endif /* !_SYS_RMLOCK_H_ */
136