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