rmlock.h revision 247588
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: head/sys/sys/rmlock.h 247588 2013-03-01 22:03:31Z jhb $
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
48193030Srwatson
49193030Srwatsonvoid	rm_init(struct rmlock *rm, const char *name);
50193030Srwatsonvoid	rm_init_flags(struct rmlock *rm, const char *name, int opts);
51173444Supsvoid	rm_destroy(struct rmlock *rm);
52227588Spjdint	rm_wowned(const struct rmlock *rm);
53173444Supsvoid	rm_sysinit(void *arg);
54193030Srwatsonvoid	rm_sysinit_flags(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);
68173444Sups
69173444Sups/*
70173444Sups * Public interface for lock operations.
71173444Sups */
72173444Sups#ifndef LOCK_DEBUG
73173444Sups#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
74173444Sups#endif
75173444Sups
76173444Sups#if LOCK_DEBUG > 0
77173444Sups#define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
78173444Sups#define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
79173444Sups#define	rm_rlock(rm,tracker)  \
80212112Smlaier    ((void)_rm_rlock_debug((rm),(tracker), 0, LOCK_FILE, LOCK_LINE ))
81212112Smlaier#define	rm_try_rlock(rm,tracker)  \
82212112Smlaier    _rm_rlock_debug((rm),(tracker), 1, LOCK_FILE, LOCK_LINE )
83173444Sups#define	rm_runlock(rm,tracker)	\
84173444Sups    _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
85173444Sups#else
86212112Smlaier#define	rm_wlock(rm)			_rm_wlock((rm))
87212112Smlaier#define	rm_wunlock(rm)			_rm_wunlock((rm))
88212112Smlaier#define	rm_rlock(rm,tracker)		((void)_rm_rlock((rm),(tracker), 0))
89212112Smlaier#define	rm_try_rlock(rm,tracker)	_rm_rlock((rm),(tracker), 1)
90212112Smlaier#define	rm_runlock(rm,tracker)		_rm_runlock((rm), (tracker))
91173444Sups#endif
92173444Sups
93173444Supsstruct rm_args {
94173444Sups	struct rmlock	*ra_rm;
95173444Sups	const char 	*ra_desc;
96193030Srwatson};
97193030Srwatson
98193030Srwatsonstruct rm_args_flags {
99193030Srwatson	struct rmlock	*ra_rm;
100193030Srwatson	const char 	*ra_desc;
101173444Sups	int		ra_opts;
102173444Sups};
103173444Sups
104193030Srwatson#define	RM_SYSINIT(name, rm, desc)       				\
105173444Sups	static struct rm_args name##_args = {				\
106173444Sups		(rm),							\
107173444Sups		(desc),							\
108173444Sups	};								\
109173444Sups	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
110173444Sups	    rm_sysinit, &name##_args);					\
111173444Sups	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
112173444Sups	    rm_destroy, (rm))
113173444Sups
114193030Srwatson
115193030Srwatson#define	RM_SYSINIT_FLAGS(name, rm, desc, opts)       			\
116193030Srwatson	static struct rm_args name##_args = {				\
117193030Srwatson		(rm),							\
118193030Srwatson		(desc),							\
119193030Srwatson                (opts),							\
120193030Srwatson	};								\
121193030Srwatson	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
122193030Srwatson	    rm_sysinit_flags, &name##_args);				\
123193030Srwatson	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
124193030Srwatson	    rm_destroy, (rm))
125193030Srwatson
126173444Sups#endif /* _KERNEL */
127173444Sups#endif /* !_SYS_RMLOCK_H_ */
128