]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/mutex.h
This commit was generated by cvs2svn to compensate for changes in r75406,
[FreeBSD/FreeBSD.git] / sys / sys / mutex.h
1 /*-
2  * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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
14  *    written permission.
15  *
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
26  * SUCH DAMAGE.
27  *
28  *      from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
29  * $FreeBSD$
30  */
31
32 #ifndef _SYS_MUTEX_H_
33 #define _SYS_MUTEX_H_
34
35 #ifndef LOCORE
36 #include <sys/queue.h>
37
38 #ifdef _KERNEL
39 #include <sys/ktr.h>
40 #include <machine/atomic.h>
41 #include <machine/cpufunc.h>
42 #include <machine/globals.h>
43 #endif  /* _KERNEL_ */
44 #endif  /* !LOCORE */
45
46 #include <machine/mutex.h>
47
48 #ifdef _KERNEL
49
50 /*
51  * Mutex types and options passed to mtx_init().  MTX_QUIET can also be
52  * passed in.
53  */
54 #define MTX_DEF         0x00000000      /* DEFAULT (sleep) lock */ 
55 #define MTX_SPIN        0x00000001      /* Spin lock (disables interrupts) */
56 #define MTX_RECURSE     0x00000004      /* Option: lock allowed to recurse */
57 #define MTX_NOWITNESS   0x00000008      /* Don't do any witness checking. */
58 #define MTX_SLEEPABLE   0x00000010      /* We can sleep with this lock. */
59
60 /*
61  * Option flags passed to certain lock/unlock routines, through the use
62  * of corresponding mtx_{lock,unlock}_flags() interface macros.
63  */
64 #define MTX_NOSWITCH    LOP_NOSWITCH    /* Do not switch on release */
65 #define MTX_QUIET       LOP_QUIET       /* Don't log a mutex event */
66
67 /*
68  * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
69  * with the exception of MTX_UNOWNED, applies to spin locks.
70  */
71 #define MTX_RECURSED    0x00000001      /* lock recursed (for MTX_DEF only) */
72 #define MTX_CONTESTED   0x00000002      /* lock contested (for MTX_DEF only) */
73 #define MTX_UNOWNED     0x00000004      /* Cookie for free mutex */
74 #define MTX_FLAGMASK    ~(MTX_RECURSED | MTX_CONTESTED)
75
76 #endif  /* _KERNEL */
77
78 #ifndef LOCORE
79
80 /*
81  * Sleep/spin mutex
82  */
83
84 struct lock_object;
85
86 struct  mtx {
87         struct  lock_object mtx_object; /* Common lock properties. */
88         volatile uintptr_t mtx_lock;    /* owner (and state for sleep locks) */
89         volatile u_int mtx_recurse;     /* number of recursive holds */
90         critical_t mtx_savecrit;        /* saved flags (for spin locks) */
91         TAILQ_HEAD(, proc) mtx_blocked; /* threads blocked on this lock */
92         LIST_ENTRY(mtx) mtx_contested;  /* list of all contested locks */
93 };
94
95 /*
96  * XXX: Friendly reminder to fix things in MP code that is presently being
97  * XXX: worked on.
98  */
99 #define mp_fixme(string)
100
101 #ifdef _KERNEL
102
103 /*
104  * Prototypes
105  *
106  * NOTE: Functions prepended with `_' (underscore) are exported to other parts
107  *       of the kernel via macros, thus allowing us to use the cpp __FILE__
108  *       and __LINE__. These functions should not be called directly by any
109  *       code using the API. Their macros cover their functionality.
110  *
111  * [See below for descriptions]
112  *
113  */
114 void    mtx_init(struct mtx *m, const char *description, int opts);
115 void    mtx_destroy(struct mtx *m);
116 void    _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line);
117 void    _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line);
118 void    _mtx_lock_spin(struct mtx *m, int opts, critical_t mtx_crit,
119                        const char *file, int line);
120 void    _mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line);
121 int     _mtx_trylock(struct mtx *m, int opts, const char *file, int line);
122 void    _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line);
123 void    _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line);
124 void    _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file,
125                              int line);
126 void    _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file,
127                              int line);
128 #ifdef INVARIANT_SUPPORT
129 void    _mtx_assert(struct mtx *m, int what, const char *file, int line);
130 #endif
131 #ifdef WITNESS
132 void    _mtx_update_flags(struct mtx *m, int locking);
133 #endif
134
135 /*
136  * We define our machine-independent (unoptimized) mutex micro-operations
137  * here, if they are not already defined in the machine-dependent mutex.h 
138  */
139
140 /* Actually obtain mtx_lock */
141 #ifndef _obtain_lock
142 #define _obtain_lock(mp, tid)                                           \
143         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED, (tid))
144 #endif
145
146 /* Actually release mtx_lock */
147 #ifndef _release_lock
148 #define _release_lock(mp, tid)                                          \
149         atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), (void *)MTX_UNOWNED)
150 #endif
151
152 /* Actually release mtx_lock quickly, assuming we own it. */
153 #ifndef _release_lock_quick
154 #define _release_lock_quick(mp)                                         \
155         atomic_store_rel_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED)
156 #endif
157
158 /*
159  * Obtain a sleep lock inline, or call the "hard" function if we can't get it
160  * easy.
161  */
162 #ifndef _get_sleep_lock
163 #define _get_sleep_lock(mp, tid, opts, file, line) do {                 \
164         if (!_obtain_lock((mp), (tid)))                                 \
165                 _mtx_lock_sleep((mp), (opts), (file), (line));          \
166 } while (0)
167 #endif
168
169 /*
170  * Obtain a spin lock inline, or call the "hard" function if we can't get it
171  * easy. For spinlocks, we handle recursion inline (it turns out that function
172  * calls can be significantly expensive on some architectures).
173  * Since spin locks are not _too_ common, inlining this code is not too big 
174  * a deal.
175  */
176 #ifndef _get_spin_lock
177 #define _get_spin_lock(mp, tid, opts, file, line) do {                  \
178         critical_t _mtx_crit;                                           \
179         _mtx_crit = critical_enter();                                   \
180         if (!_obtain_lock((mp), (tid))) {                               \
181                 if ((mp)->mtx_lock == (uintptr_t)(tid))                 \
182                         (mp)->mtx_recurse++;                            \
183                 else                                                    \
184                         _mtx_lock_spin((mp), (opts), _mtx_crit, (file), \
185                             (line));                                    \
186         } else                                                          \
187                 (mp)->mtx_savecrit = _mtx_crit;                         \
188 } while (0)
189 #endif
190
191 /*
192  * Release a sleep lock inline, or call the "hard" function if we can't do it
193  * easy.
194  */
195 #ifndef _rel_sleep_lock
196 #define _rel_sleep_lock(mp, tid, opts, file, line) do {                 \
197         if (!_release_lock((mp), (tid)))                                \
198                 _mtx_unlock_sleep((mp), (opts), (file), (line));        \
199 } while (0)
200 #endif
201
202 /*
203  * For spinlocks, we can handle everything inline, as it's pretty simple and
204  * a function call would be too expensive (at least on some architectures).
205  * Since spin locks are not _too_ common, inlining this code is not too big 
206  * a deal.
207  */
208 #ifndef _rel_spin_lock
209 #define _rel_spin_lock(mp) do {                                         \
210         critical_t _mtx_crit = (mp)->mtx_savecrit;                      \
211         if (mtx_recursed((mp)))                                         \
212                 (mp)->mtx_recurse--;                                    \
213         else {                                                          \
214                 _release_lock_quick((mp));                              \
215                 critical_exit(_mtx_crit);                               \
216         }                                                               \
217 } while (0)
218 #endif
219
220 /*
221  * Update the lock object flags based on the current mutex state.
222  */
223 #ifdef WITNESS
224 #define mtx_update_flags(m, locking)    _mtx_update_flags((m), (locking))
225 #else
226 #define mtx_update_flags(m, locking)
227 #endif
228
229 /*
230  * Exported lock manipulation interface.
231  *
232  * mtx_lock(m) locks MTX_DEF mutex `m'
233  *
234  * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
235  *
236  * mtx_unlock(m) unlocks MTX_DEF mutex `m'
237  *
238  * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
239  *
240  * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
241  *     and passes option flags `opts' to the "hard" function, if required.
242  *     With these routines, it is possible to pass flags such as MTX_QUIET
243  *     and/or MTX_NOSWITCH to the appropriate lock manipulation routines.
244  *
245  * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
246  *     it cannot. Rather, it returns 0 on failure and non-zero on success.
247  *     It does NOT handle recursion as we assume that if a caller is properly
248  *     using this part of the interface, he will know that the lock in question
249  *     is _not_ recursed.
250  *
251  * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
252  *     relevant option flags `opts.'
253  *
254  * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
255  *
256  * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
257  *
258  * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
259  */ 
260 #define mtx_lock(m)             mtx_lock_flags((m), 0)
261 #define mtx_lock_spin(m)        mtx_lock_spin_flags((m), 0)
262 #define mtx_trylock(m)          mtx_trylock_flags((m), 0)
263 #define mtx_unlock(m)           mtx_unlock_flags((m), 0)
264 #define mtx_unlock_spin(m)      mtx_unlock_spin_flags((m), 0)
265
266 #ifdef KLD_MODULE
267 #define mtx_lock_flags(m, opts)                                         \
268         _mtx_lock_flags((m), (opts), __FILE__, __LINE__)
269 #define mtx_unlock_flags(m, opts)                                       \
270         _mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
271 #define mtx_lock_spin_flags(m, opts)                                    \
272         _mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
273 #define mtx_unlock_spin_flags(m, opts)                                  \
274         _mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
275 #else
276 #define mtx_lock_flags(m, opts)                                         \
277         __mtx_lock_flags((m), (opts), __FILE__, __LINE__)
278 #define mtx_unlock_flags(m, opts)                                       \
279         __mtx_unlock_flags((m), (opts), __FILE__, __LINE__)
280 #define mtx_lock_spin_flags(m, opts)                                    \
281         __mtx_lock_spin_flags((m), (opts), __FILE__, __LINE__)
282 #define mtx_unlock_spin_flags(m, opts)                                  \
283         __mtx_unlock_spin_flags((m), (opts), __FILE__, __LINE__)
284 #endif
285
286 #define __mtx_lock_flags(m, opts, file, line) do {                      \
287         MPASS(curproc != NULL);                                         \
288         KASSERT(((opts) & MTX_NOSWITCH) == 0,                           \
289             ("MTX_NOSWITCH used at %s:%d", (file), (line)));            \
290         _get_sleep_lock((m), curproc, (opts), (file), (line));          \
291         LOCK_LOG_LOCK("LOCK", &(m)->mtx_object, opts, m->mtx_recurse,   \
292             (file), (line));                                            \
293         mtx_update_flags((m), 1);                                       \
294         WITNESS_LOCK(&(m)->mtx_object, (opts), (file), (line));         \
295 } while (0)
296
297 #define __mtx_lock_spin_flags(m, opts, file, line) do {                 \
298         MPASS(curproc != NULL);                                         \
299         _get_spin_lock((m), curproc, (opts), (file), (line));           \
300         LOCK_LOG_LOCK("LOCK", &(m)->mtx_object, opts, m->mtx_recurse,   \
301             (file), (line));                                            \
302         mtx_update_flags((m), 1);                                       \
303         WITNESS_LOCK(&(m)->mtx_object, (opts), (file), (line));         \
304 } while (0)
305
306 #define __mtx_unlock_flags(m, opts, file, line) do {                    \
307         MPASS(curproc != NULL);                                         \
308         mtx_assert((m), MA_OWNED);                                      \
309         mtx_update_flags((m), 0);                                       \
310         WITNESS_UNLOCK(&(m)->mtx_object, (opts), (file), (line));       \
311         _rel_sleep_lock((m), curproc, (opts), (file), (line));          \
312         LOCK_LOG_LOCK("UNLOCK", &(m)->mtx_object, (opts),               \
313             (m)->mtx_recurse, (file), (line));                          \
314 } while (0)
315
316 #define __mtx_unlock_spin_flags(m, opts, file, line) do {               \
317         MPASS(curproc != NULL);                                         \
318         mtx_assert((m), MA_OWNED);                                      \
319         mtx_update_flags((m), 0);                                       \
320         WITNESS_UNLOCK(&(m)->mtx_object, (opts), (file), (line));       \
321         _rel_spin_lock((m));                                            \
322         LOCK_LOG_LOCK("UNLOCK", &(m)->mtx_object, (opts),               \
323             (m)->mtx_recurse, (file), (line));                          \
324 } while (0)
325
326 #define mtx_trylock_flags(m, opts)                                      \
327         _mtx_trylock((m), (opts), __FILE__, __LINE__)
328
329 #define mtx_initialized(m)      ((m)->mtx_object.lo_flags & LO_INITIALIZED)
330
331 #define mtx_owned(m)    (((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curproc)
332
333 #define mtx_recursed(m) ((m)->mtx_recurse != 0)
334
335 /*
336  * Global locks.
337  */
338 extern struct mtx       sched_lock;
339 extern struct mtx       Giant;
340
341 /*
342  * Giant lock manipulation and clean exit macros.
343  * Used to replace return with an exit Giant and return.
344  *
345  * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT() 
346  */
347 #define DROP_GIANT_NOSWITCH()                                           \
348 do {                                                                    \
349         int _giantcnt;                                                  \
350         WITNESS_SAVE_DECL(Giant);                                       \
351                                                                         \
352         if (mtx_owned(&Giant))                                          \
353                 WITNESS_SAVE(&Giant.mtx_object, Giant);                 \
354         for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++)             \
355                 mtx_unlock_flags(&Giant, MTX_NOSWITCH)
356
357 #define DROP_GIANT()                                                    \
358 do {                                                                    \
359         int _giantcnt;                                                  \
360         WITNESS_SAVE_DECL(Giant);                                       \
361                                                                         \
362         if (mtx_owned(&Giant))                                          \
363                 WITNESS_SAVE(&Giant.mtx_object, Giant);                 \
364         for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++)             \
365                 mtx_unlock(&Giant)
366
367 #define PICKUP_GIANT()                                                  \
368         mtx_assert(&Giant, MA_NOTOWNED);                                \
369         while (_giantcnt--)                                             \
370                 mtx_lock(&Giant);                                       \
371         if (mtx_owned(&Giant))                                          \
372                 WITNESS_RESTORE(&Giant.mtx_object, Giant);              \
373 } while (0)
374
375 #define PARTIAL_PICKUP_GIANT()                                          \
376         mtx_assert(&Giant, MA_NOTOWNED);                                \
377         while (_giantcnt--)                                             \
378                 mtx_lock(&Giant);                                       \
379         if (mtx_owned(&Giant))                                          \
380                 WITNESS_RESTORE(&Giant.mtx_object, Giant)
381
382 /*
383  * The INVARIANTS-enabled mtx_assert() functionality.
384  *
385  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
386  * support as _mtx_assert() itself uses them and the latter implies that
387  * _mtx_assert() must build.
388  */
389 #ifdef INVARIANT_SUPPORT
390 #define MA_OWNED        0x01
391 #define MA_NOTOWNED     0x02
392 #define MA_RECURSED     0x04
393 #define MA_NOTRECURSED  0x08
394 #endif /* INVARIANT_SUPPORT */
395
396 #ifdef INVARIANTS
397 #define mtx_assert(m, what)                                             \
398         _mtx_assert((m), (what), __FILE__, __LINE__)
399
400 #else   /* INVARIANTS */
401 #define mtx_assert(m, what)
402 #endif  /* INVARIANTS */
403
404 #endif  /* _KERNEL */
405 #endif  /* !LOCORE */
406 #endif  /* _SYS_MUTEX_H_ */