2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER 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
28 * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
36 * XXX - compatability until lockmgr() goes away or all the #includes are
39 #include <sys/lockmgr.h>
41 #include <sys/queue.h>
42 #include <sys/_lock.h>
45 * Lock classes. Each lock has a class which describes characteristics
46 * common to all types of locks of a given class.
48 * Spin locks in general must always protect against preemption, as it is
49 * an error to perform any type of context switch while holding a spin lock.
50 * Also, for an individual lock to be recursable, its class must allow
51 * recursion and the lock itself must explicitly allow recursion.
59 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
60 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
61 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
62 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
64 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
65 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
66 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
67 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
68 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
69 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
71 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
72 #define LI_SLEPT 0x00010000 /* Lock instance has been slept with. */
73 #define LI_EXCLUSIVE 0x00020000 /* Exclusive lock instance. */
76 * Option flags passed to lock operations that witness also needs to know
77 * about or that are generic across all locks.
79 #define LOP_NOSWITCH 0x00000001 /* Lock doesn't switch on release. */
80 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
81 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
82 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
86 * Lock instances. A lock instance is the data associated with a lock while
87 * it is held by witness. For example, a lock instance will hold the
88 * recursion count of a lock. Lock instances are held in lists. Spin locks
89 * are held in a per-cpu list while sleep locks are held in per-process list.
91 struct lock_instance {
92 struct lock_object *li_lock;
93 const char *li_file; /* File and line of last acquire. */
95 u_int li_flags; /* Recursion count and LI_* flags. */
99 * A simple list type used to build the list of locks held by a process
100 * or CPU. We can't simply embed the list in struct lock_object since a
101 * lock may be held by more than one process if it is a shared lock. Locks
102 * are added to the head of the list, so we fill up each list entry from
103 * "the back" logically. To ease some of the arithmetic, we actually fill
104 * in each list entry the normal way (childer[0] then children[1], etc.) but
105 * when we traverse the list we read children[count-1] as the first entry
106 * down to children[0] as the final entry.
108 #define LOCK_NCHILDREN 3
110 struct lock_list_entry {
111 struct lock_list_entry *ll_next;
112 struct lock_instance ll_children[LOCK_NCHILDREN];
117 * Macros for KTR_LOCK tracing.
119 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
120 * lo - struct lock_object * for this lock
121 * flags - flags passed to the lock operation
122 * recurse - this locks recursion level (or 0 if class is not recursable)
123 * result - result of a try lock operation
127 #define LOCK_LOG_TEST(lo, flags) \
128 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
130 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
131 if (LOCK_LOG_TEST((lo), (flags))) \
132 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \
133 (lo)->lo_class->lc_name, (lo)->lo_name, \
134 (u_int)(recurse), (file), (line)); \
137 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
138 if (LOCK_LOG_TEST((lo), (flags))) \
139 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
140 (lo)->lo_class->lc_name, (lo)->lo_name, \
141 (u_int)(result), (file), (line)); \
144 #define LOCK_LOG_INIT(lo, flags) do { \
145 if (LOCK_LOG_TEST((lo), (flags))) \
146 CTR3(KTR_LOCK, __func__ ": %p (%s) %s", (lo), \
147 (lo)->lo_class->lc_name, (lo)->lo_name); \
150 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
153 * Helpful macros for quickly coming up with assertions with informative
156 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
157 #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__)
158 #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line)
159 #define MPASS4(ex, what, file, line) \
160 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
162 extern struct lock_class lock_class_mtx_sleep;
163 extern struct lock_class lock_class_mtx_spin;
164 extern struct lock_class lock_class_sx;
166 void witness_init(struct lock_object *);
167 void witness_destroy(struct lock_object *);
168 void witness_lock(struct lock_object *, int, const char *, int);
169 void witness_unlock(struct lock_object *, int, const char *, int);
170 void witness_save(struct lock_object *, const char **, int *);
171 void witness_restore(struct lock_object *, const char *, int);
172 int witness_list_locks(struct lock_list_entry **);
173 int witness_list(struct proc *);
174 int witness_sleep(int, struct lock_object *, const char *, int);
177 #define WITNESS_INIT(lock) \
180 #define WITNESS_DESTROY(lock) \
181 witness_destroy(lock)
183 #define WITNESS_LOCK(lock, flags, file, line) \
184 witness_lock((lock), (flags), (file), (line))
186 #define WITNESS_UNLOCK(lock, flags, file, line) \
187 witness_unlock((lock), (flags), (file), (line))
189 #define WITNESS_SLEEP(check, lock) \
190 witness_sleep((check), (lock), __FILE__, __LINE__)
192 #define WITNESS_SAVE_DECL(n) \
193 const char * __CONCAT(n, __wf); \
194 int __CONCAT(n, __wl)
196 #define WITNESS_SAVE(lock, n) \
197 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
199 #define WITNESS_RESTORE(lock, n) \
200 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
203 #define WITNESS_INIT(lock) (lock)->lo_flags |= LO_INITIALIZED
204 #define WITNESS_DESTROY(lock) (lock)->lo_flags &= ~LO_INITIALIZED
205 #define WITNESS_LOCK(lock, flags, file, line)
206 #define WITNESS_UNLOCK(lock, flags, file, line)
207 #define WITNESS_SLEEP(check, lock)
208 #define WITNESS_SAVE_DECL(n)
209 #define WITNESS_SAVE(lock, n)
210 #define WITNESS_RESTORE(lock, n)
214 #endif /* _SYS_LOCK_H_ */