xref: /freebsd/sys/sys/rmlock.h (revision f53d15fe1b8a7a7df972f253b38c698f6ca45b71)
1f53d15feSStephan Uphoff /*-
2f53d15feSStephan Uphoff  * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
3f53d15feSStephan Uphoff  * All rights reserved.
4f53d15feSStephan Uphoff  *
5f53d15feSStephan Uphoff  * Redistribution and use in source and binary forms, with or without
6f53d15feSStephan Uphoff  * modification, are permitted provided that the following conditions
7f53d15feSStephan Uphoff  * are met:
8f53d15feSStephan Uphoff  * 1. Redistributions of source code must retain the above copyright
9f53d15feSStephan Uphoff  *    notice, this list of conditions and the following disclaimer.
10f53d15feSStephan Uphoff  * 2. Redistributions in binary form must reproduce the above copyright
11f53d15feSStephan Uphoff  *    notice, this list of conditions and the following disclaimer in the
12f53d15feSStephan Uphoff  *    documentation and/or other materials provided with the distribution.
13f53d15feSStephan Uphoff  * 3. Neither the name of the author nor the names of any co-contributors
14f53d15feSStephan Uphoff  *    may be used to endorse or promote products derived from this software
15f53d15feSStephan Uphoff  *    without specific prior written permission.
16f53d15feSStephan Uphoff  *
17f53d15feSStephan Uphoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18f53d15feSStephan Uphoff  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19f53d15feSStephan Uphoff  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20f53d15feSStephan Uphoff  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21f53d15feSStephan Uphoff  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22f53d15feSStephan Uphoff  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23f53d15feSStephan Uphoff  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24f53d15feSStephan Uphoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25f53d15feSStephan Uphoff  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26f53d15feSStephan Uphoff  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27f53d15feSStephan Uphoff  * SUCH DAMAGE.
28f53d15feSStephan Uphoff  *
29f53d15feSStephan Uphoff  * $FreeBSD$
30f53d15feSStephan Uphoff  */
31f53d15feSStephan Uphoff 
32f53d15feSStephan Uphoff #ifndef _SYS_RMLOCK_H_
33f53d15feSStephan Uphoff #define _SYS_RMLOCK_H_
34f53d15feSStephan Uphoff 
35f53d15feSStephan Uphoff #include <sys/mutex.h>
36f53d15feSStephan Uphoff 
37f53d15feSStephan Uphoff #include <sys/_lock.h>
38f53d15feSStephan Uphoff #include <sys/_rmlock.h>
39f53d15feSStephan Uphoff 
40f53d15feSStephan Uphoff #ifdef _KERNEL
41f53d15feSStephan Uphoff 
42f53d15feSStephan Uphoff 
43f53d15feSStephan Uphoff void	rm_init(struct rmlock *rm, const char *name, int opts);
44f53d15feSStephan Uphoff void	rm_destroy(struct rmlock *rm);
45f53d15feSStephan Uphoff void	rm_sysinit(void *arg);
46f53d15feSStephan Uphoff 
47f53d15feSStephan Uphoff 
48f53d15feSStephan Uphoff void	_rm_wlock_debug(struct rmlock *rm, const char *file, int line);
49f53d15feSStephan Uphoff void	_rm_wunlock_debug(struct rmlock *rm, const char *file, int line);
50f53d15feSStephan Uphoff void	_rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
51f53d15feSStephan Uphoff     const char *file, int line);
52f53d15feSStephan Uphoff void	_rm_runlock_debug(struct rmlock *rm,  struct rm_priotracker *tracker,
53f53d15feSStephan Uphoff     const char *file, int line);
54f53d15feSStephan Uphoff 
55f53d15feSStephan Uphoff 
56f53d15feSStephan Uphoff void	_rm_wlock(struct rmlock *rm);
57f53d15feSStephan Uphoff void	_rm_wunlock(struct rmlock *rm);
58f53d15feSStephan Uphoff void	_rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker);
59f53d15feSStephan Uphoff void	_rm_runlock(struct rmlock *rm,  struct rm_priotracker *tracker);
60f53d15feSStephan Uphoff 
61f53d15feSStephan Uphoff /*
62f53d15feSStephan Uphoff  * Public interface for lock operations.
63f53d15feSStephan Uphoff  *
64f53d15feSStephan Uphoff  */
65f53d15feSStephan Uphoff 
66f53d15feSStephan Uphoff #ifndef LOCK_DEBUG
67f53d15feSStephan Uphoff #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/rmlock.h>
68f53d15feSStephan Uphoff #endif
69f53d15feSStephan Uphoff 
70f53d15feSStephan Uphoff #if LOCK_DEBUG > 0
71f53d15feSStephan Uphoff 
72f53d15feSStephan Uphoff #define	rm_wlock(rm)	_rm_wlock_debug((rm), LOCK_FILE, LOCK_LINE)
73f53d15feSStephan Uphoff #define	rm_wunlock(rm)	_rm_wunlock_debug((rm), LOCK_FILE, LOCK_LINE)
74f53d15feSStephan Uphoff #define	rm_rlock(rm,tracker)  \
75f53d15feSStephan Uphoff     _rm_rlock_debug((rm),(tracker), LOCK_FILE, LOCK_LINE )
76f53d15feSStephan Uphoff #define	rm_runlock(rm,tracker)	\
77f53d15feSStephan Uphoff     _rm_runlock_debug((rm), (tracker), LOCK_FILE, LOCK_LINE )
78f53d15feSStephan Uphoff 
79f53d15feSStephan Uphoff #else
80f53d15feSStephan Uphoff 
81f53d15feSStephan Uphoff #define	rm_wlock(rm)		_rm_wlock((rm))
82f53d15feSStephan Uphoff #define	rm_wunlock(rm)		_rm_wunlock((rm))
83f53d15feSStephan Uphoff #define	rm_rlock(rm,tracker)   	_rm_rlock((rm),(tracker))
84f53d15feSStephan Uphoff #define	rm_runlock(rm,tracker)	_rm_runlock((rm), (tracker))
85f53d15feSStephan Uphoff 
86f53d15feSStephan Uphoff #endif
87f53d15feSStephan Uphoff 
88f53d15feSStephan Uphoff #define	rm_initialized(rm)	lock_initalized(&(rm)->lock_object)
89f53d15feSStephan Uphoff 
90f53d15feSStephan Uphoff struct rm_args {
91f53d15feSStephan Uphoff 	struct rmlock	*ra_rm;
92f53d15feSStephan Uphoff 	const char 	*ra_desc;
93f53d15feSStephan Uphoff 	int		ra_opts;
94f53d15feSStephan Uphoff };
95f53d15feSStephan Uphoff 
96f53d15feSStephan Uphoff #define	RM_SYSINIT(name, rm, desc, opts)       				\
97f53d15feSStephan Uphoff 	static struct rm_args name##_args = {				\
98f53d15feSStephan Uphoff 		(rm),							\
99f53d15feSStephan Uphoff 		(desc),							\
100f53d15feSStephan Uphoff                 (opts),							\
101f53d15feSStephan Uphoff 	};								\
102f53d15feSStephan Uphoff 	SYSINIT(name##_rm_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
103f53d15feSStephan Uphoff 	    rm_sysinit, &name##_args);					\
104f53d15feSStephan Uphoff 	SYSUNINIT(name##_rm_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
105f53d15feSStephan Uphoff 	    rm_destroy, (rm))
106f53d15feSStephan Uphoff 
107f53d15feSStephan Uphoff 
108f53d15feSStephan Uphoff #endif /* _KERNEL */
109f53d15feSStephan Uphoff #endif /* !_SYS_RMLOCK_H_ */
110