lockmgr.h revision 179025
1/*-
2 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice(s), this list of conditions and the following disclaimer as
10 *    the first lines of this file unmodified other than the possible
11 *    addition of one or more copyright notices.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice(s), this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 * DAMAGE.
27 *
28 * $FreeBSD: head/sys/sys/lockmgr.h 179025 2008-05-15 20:10:06Z attilio $
29 */
30
31#ifndef	_SYS_LOCKMGR_H_
32#define	_SYS_LOCKMGR_H_
33
34#include <sys/_lock.h>
35#include <sys/_lockmgr.h>
36#include <sys/_mutex.h>
37#include <sys/_rwlock.h>
38
39#define	LK_SHARE			0x01
40#define	LK_SHARED_WAITERS		0x02
41#define	LK_EXCLUSIVE_WAITERS		0x04
42#define	LK_ALL_WAITERS							\
43	(LK_SHARED_WAITERS | LK_EXCLUSIVE_WAITERS)
44#define	LK_FLAGMASK							\
45	(LK_SHARE | LK_ALL_WAITERS)
46
47#define	LK_HOLDER(x)			((x) & ~LK_FLAGMASK)
48#define	LK_SHARERS_SHIFT		3
49#define	LK_SHARERS(x)			(LK_HOLDER(x) >> LK_SHARERS_SHIFT)
50#define	LK_SHARERS_LOCK(x)		((x) << LK_SHARERS_SHIFT | LK_SHARE)
51#define	LK_ONE_SHARER			(1 << LK_SHARERS_SHIFT)
52#define	LK_UNLOCKED			LK_SHARERS_LOCK(0)
53#define	LK_KERNPROC			((uintptr_t)(-1) & ~LK_FLAGMASK)
54
55#ifdef _KERNEL
56
57#if !defined(LOCK_FILE) || !defined(LOCK_LINE)
58#error	"LOCK_FILE and LOCK_LINE not defined, include <sys/lock.h> before"
59#endif
60
61struct thread;
62#define	lk_recurse	lock_object.lo_data
63
64/*
65 * Function prototipes.  Routines that start with an underscore are not part
66 * of the public interface and might be wrappered with a macro.
67 */
68int	 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
69	    const char *wmesg, int prio, int timo, const char *file, int line);
70#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
71void	 _lockmgr_assert(struct lock *lk, int what, const char *file, int line);
72#endif
73void	 _lockmgr_disown(struct lock *lk, const char *file, int line);
74
75void	 lockdestroy(struct lock *lk);
76void	 lockinit(struct lock *lk, int prio, const char *wmesg, int timo,
77	    int flags);
78#ifdef DDB
79int	 lockmgr_chain(struct thread *td, struct thread **ownerp);
80#endif
81void	 lockmgr_printinfo(struct lock *lk);
82int	 lockstatus(struct lock *lk);
83
84/*
85 * As far as the ilk can be a static NULL pointer these functions need a
86 * strict prototype in order to safely use the lock_object member.
87 */
88static __inline int
89_lockmgr_args(struct lock *lk, u_int flags, struct mtx *ilk, const char *wmesg,
90    int prio, int timo, const char *file, int line)
91{
92
93	return (__lockmgr_args(lk, flags, (ilk != NULL) ? &ilk->lock_object :
94	    NULL, wmesg, prio, timo, file, line));
95}
96
97static __inline int
98_lockmgr_args_rw(struct lock *lk, u_int flags, struct rwlock *ilk,
99    const char *wmesg, int prio, int timo, const char *file, int line)
100{
101
102	return (__lockmgr_args(lk, flags, (ilk != NULL) ? &ilk->lock_object :
103	    NULL, wmesg, prio, timo, file, line));
104}
105
106/*
107 * Define aliases in order to complete lockmgr KPI.
108 */
109#define	lockmgr(lk, flags, ilk)						\
110	_lockmgr_args((lk), (flags), (ilk), LK_WMESG_DEFAULT,		\
111	    LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, LOCK_FILE, LOCK_LINE)
112#define	lockmgr_args(lk, flags, ilk, wmesg, prio, timo)			\
113	_lockmgr_args((lk), (flags), (ilk), (wmesg), (prio), (timo),	\
114	    LOCK_FILE, LOCK_LINE)
115#define	lockmgr_args_rw(lk, flags, ilk, wmesg, prio, timo)		\
116	_lockmgr_args_rw((lk), (flags), (ilk), (wmesg), (prio), (timo),	\
117	    LOCK_FILE, LOCK_LINE)
118#define	lockmgr_disown(lk)						\
119	_lockmgr_disown((lk), LOCK_FILE, LOCK_LINE)
120#define	lockmgr_recursed(lk)						\
121	((lk)->lk_recurse != 0)
122#define	lockmgr_rw(lk, flags, ilk)					\
123	_lockmgr_args_rw((lk), (flags), (ilk), LK_WMESG_DEFAULT,	\
124	    LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, LOCK_FILE, LOCK_LINE)
125#define	lockmgr_waiters(lk)						\
126	((lk)->lk_lock & LK_ALL_WAITERS)
127#ifdef INVARIANTS
128#define	lockmgr_assert(lk, what)					\
129	_lockmgr_assert((lk), (what), LOCK_FILE, LOCK_LINE)
130#else
131#define	lockmgr_assert(lk, what)
132#endif
133
134/*
135 * Flags for lockinit().
136 */
137#define	LK_INIT_MASK	0x0000FF
138#define	LK_CANRECURSE	0x000001
139#define	LK_NODUP	0x000002
140#define	LK_NOPROFILE	0x000004
141#define	LK_NOSHARE	0x000008
142#define	LK_NOWITNESS	0x000010
143#define	LK_QUIET	0x000020
144
145/*
146 * Additional attributes to be used in lockmgr().
147 */
148#define	LK_EATTR_MASK	0x00FF00
149#define	LK_INTERLOCK	0x000100
150#define	LK_NOWAIT	0x000200
151#define	LK_RETRY	0x000400
152#define	LK_SLEEPFAIL	0x000800
153#define	LK_TIMELOCK	0x001000
154
155/*
156 * Operations for lockmgr().
157 */
158#define	LK_TYPE_MASK	0xFF0000
159#define	LK_DOWNGRADE	0x010000
160#define	LK_DRAIN	0x020000
161#define	LK_EXCLOTHER	0x040000
162#define	LK_EXCLUSIVE	0x080000
163#define	LK_RELEASE	0x100000
164#define	LK_SHARED	0x200000
165#define	LK_UPGRADE	0x400000
166
167#define	LK_TOTAL_MASK	(LK_INIT_MASK | LK_EATTR_MASK | LK_TYPE_MASK)
168
169/*
170 * Default values for lockmgr_args().
171 */
172#define	LK_WMESG_DEFAULT	(NULL)
173#define	LK_PRIO_DEFAULT		(0)
174#define	LK_TIMO_DEFAULT		(0)
175
176/*
177 * Assertion flags.
178 */
179#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
180#define	KA_LOCKED	LA_LOCKED
181#define	KA_SLOCKED	LA_SLOCKED
182#define	KA_XLOCKED	LA_XLOCKED
183#define	KA_UNLOCKED	LA_UNLOCKED
184#define	KA_RECURSED	LA_RECURSED
185#define	KA_NOTRECURSED	LA_NOTRECURSED
186#define	KA_HELD
187#define	KA_UNHELD
188#endif
189
190#endif /* _KERNEL */
191
192#endif /* !_SYS_LOCKMGR_H_ */
193