]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/mutex.h
Add ELF machine EM_IAMCU, 32-bit Intel MCU
[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 #include <sys/queue.h>
36 #include <sys/_lock.h>
37 #include <sys/_mutex.h>
38
39 #ifdef _KERNEL
40 #include <sys/pcpu.h>
41 #include <sys/lock_profile.h>
42 #include <sys/lockstat.h>
43 #include <machine/atomic.h>
44 #include <machine/cpufunc.h>
45
46 /*
47  * Mutex types and options passed to mtx_init().  MTX_QUIET and MTX_DUPOK
48  * can also be passed in.
49  */
50 #define MTX_DEF         0x00000000      /* DEFAULT (sleep) lock */ 
51 #define MTX_SPIN        0x00000001      /* Spin lock (disables interrupts) */
52 #define MTX_RECURSE     0x00000004      /* Option: lock allowed to recurse */
53 #define MTX_NOWITNESS   0x00000008      /* Don't do any witness checking. */
54 #define MTX_NOPROFILE   0x00000020      /* Don't profile this lock */
55 #define MTX_NEW         0x00000040      /* Don't check for double-init */
56
57 /*
58  * Option flags passed to certain lock/unlock routines, through the use
59  * of corresponding mtx_{lock,unlock}_flags() interface macros.
60  */
61 #define MTX_QUIET       LOP_QUIET       /* Don't log a mutex event */
62 #define MTX_DUPOK       LOP_DUPOK       /* Don't log a duplicate acquire */
63
64 /*
65  * State bits kept in mutex->mtx_lock, for the DEFAULT lock type. None of this,
66  * with the exception of MTX_UNOWNED, applies to spin locks.
67  */
68 #define MTX_RECURSED    0x00000001      /* lock recursed (for MTX_DEF only) */
69 #define MTX_CONTESTED   0x00000002      /* lock contested (for MTX_DEF only) */
70 #define MTX_UNOWNED     0x00000004      /* Cookie for free mutex */
71 #define MTX_FLAGMASK    (MTX_RECURSED | MTX_CONTESTED | MTX_UNOWNED)
72
73 /*
74  * Value stored in mutex->mtx_lock to denote a destroyed mutex.
75  */
76 #define MTX_DESTROYED   (MTX_CONTESTED | MTX_UNOWNED)
77
78 /*
79  * Prototypes
80  *
81  * NOTE: Functions prepended with `_' (underscore) are exported to other parts
82  *       of the kernel via macros, thus allowing us to use the cpp LOCK_FILE
83  *       and LOCK_LINE or for hiding the lock cookie crunching to the
84  *       consumers. These functions should not be called directly by any
85  *       code using the API. Their macros cover their functionality.
86  *       Functions with a `_' suffix are the entrypoint for the common
87  *       KPI covering both compat shims and fast path case.  These can be
88  *       used by consumers willing to pass options, file and line
89  *       informations, in an option-independent way.
90  *
91  * [See below for descriptions]
92  *
93  */
94 void    _mtx_init(volatile uintptr_t *c, const char *name, const char *type,
95             int opts);
96 void    _mtx_destroy(volatile uintptr_t *c);
97 void    mtx_sysinit(void *arg);
98 int     _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file,
99             int line);
100 void    mutex_init(void);
101 void    __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t tid, int opts,
102             const char *file, int line);
103 void    __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file,
104             int line);
105 #ifdef SMP
106 void    _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t tid, int opts,
107             const char *file, int line);
108 #endif
109 void    __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file,
110             int line);
111 void    __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file,
112             int line);
113 void    __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
114              int line);
115 void    __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts,
116             const char *file, int line);
117 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
118 void    __mtx_assert(const volatile uintptr_t *c, int what, const char *file,
119             int line);
120 #endif
121 void    thread_lock_flags_(struct thread *, int, const char *, int);
122
123 #define thread_lock(tdp)                                                \
124         thread_lock_flags_((tdp), 0, __FILE__, __LINE__)
125 #define thread_lock_flags(tdp, opt)                                     \
126         thread_lock_flags_((tdp), (opt), __FILE__, __LINE__)
127 #define thread_unlock(tdp)                                              \
128        mtx_unlock_spin((tdp)->td_lock)
129
130 /*
131  * Top-level macros to provide lock cookie once the actual mtx is passed.
132  * They will also prevent passing a malformed object to the mtx KPI by
133  * failing compilation as the mtx_lock reserved member will not be found.
134  */
135 #define mtx_init(m, n, t, o)                                            \
136         _mtx_init(&(m)->mtx_lock, n, t, o)
137 #define mtx_destroy(m)                                                  \
138         _mtx_destroy(&(m)->mtx_lock)
139 #define mtx_trylock_flags_(m, o, f, l)                                  \
140         _mtx_trylock_flags_(&(m)->mtx_lock, o, f, l)
141 #define _mtx_lock_sleep(m, t, o, f, l)                                  \
142         __mtx_lock_sleep(&(m)->mtx_lock, t, o, f, l)
143 #define _mtx_unlock_sleep(m, o, f, l)                                   \
144         __mtx_unlock_sleep(&(m)->mtx_lock, o, f, l)
145 #ifdef SMP
146 #define _mtx_lock_spin(m, t, o, f, l)                                   \
147         _mtx_lock_spin_cookie(&(m)->mtx_lock, t, o, f, l)
148 #endif
149 #define _mtx_lock_flags(m, o, f, l)                                     \
150         __mtx_lock_flags(&(m)->mtx_lock, o, f, l)
151 #define _mtx_unlock_flags(m, o, f, l)                                   \
152         __mtx_unlock_flags(&(m)->mtx_lock, o, f, l)
153 #define _mtx_lock_spin_flags(m, o, f, l)                                \
154         __mtx_lock_spin_flags(&(m)->mtx_lock, o, f, l)
155 #define _mtx_unlock_spin_flags(m, o, f, l)                              \
156         __mtx_unlock_spin_flags(&(m)->mtx_lock, o, f, l)
157 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
158 #define _mtx_assert(m, w, f, l)                                         \
159         __mtx_assert(&(m)->mtx_lock, w, f, l)
160 #endif
161
162 #define mtx_recurse     lock_object.lo_data
163
164 /* Very simple operations on mtx_lock. */
165
166 /* Try to obtain mtx_lock once. */
167 #define _mtx_obtain_lock(mp, tid)                                       \
168         atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid))
169
170 /* Try to release mtx_lock if it is unrecursed and uncontested. */
171 #define _mtx_release_lock(mp, tid)                                      \
172         atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), MTX_UNOWNED)
173
174 /* Release mtx_lock quickly, assuming we own it. */
175 #define _mtx_release_lock_quick(mp)                                     \
176         atomic_store_rel_ptr(&(mp)->mtx_lock, MTX_UNOWNED)
177
178 /*
179  * Full lock operations that are suitable to be inlined in non-debug
180  * kernels.  If the lock cannot be acquired or released trivially then
181  * the work is deferred to another function.
182  */
183
184 /* Lock a normal mutex. */
185 #define __mtx_lock(mp, tid, opts, file, line) do {                      \
186         uintptr_t _tid = (uintptr_t)(tid);                              \
187                                                                         \
188         if (!_mtx_obtain_lock((mp), _tid))                              \
189                 _mtx_lock_sleep((mp), _tid, (opts), (file), (line));    \
190         else                                                            \
191                 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, \
192                     mp, 0, 0, (file), (line));                          \
193 } while (0)
194
195 /*
196  * Lock a spin mutex.  For spinlocks, we handle recursion inline (it
197  * turns out that function calls can be significantly expensive on
198  * some architectures).  Since spin locks are not _too_ common,
199  * inlining this code is not too big a deal.
200  */
201 #ifdef SMP
202 #define __mtx_lock_spin(mp, tid, opts, file, line) do {                 \
203         uintptr_t _tid = (uintptr_t)(tid);                              \
204                                                                         \
205         spinlock_enter();                                               \
206         if (!_mtx_obtain_lock((mp), _tid)) {                            \
207                 if ((mp)->mtx_lock == _tid)                             \
208                         (mp)->mtx_recurse++;                            \
209                 else                                                    \
210                         _mtx_lock_spin((mp), _tid, (opts), (file), (line)); \
211         } else                                                          \
212                 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, \
213                     mp, 0, 0, (file), (line));                          \
214 } while (0)
215 #else /* SMP */
216 #define __mtx_lock_spin(mp, tid, opts, file, line) do {                 \
217         uintptr_t _tid = (uintptr_t)(tid);                              \
218                                                                         \
219         spinlock_enter();                                               \
220         if ((mp)->mtx_lock == _tid)                                     \
221                 (mp)->mtx_recurse++;                                    \
222         else {                                                          \
223                 KASSERT((mp)->mtx_lock == MTX_UNOWNED, ("corrupt spinlock")); \
224                 (mp)->mtx_lock = _tid;                                  \
225         }                                                               \
226 } while (0)
227 #endif /* SMP */
228
229 /* Unlock a normal mutex. */
230 #define __mtx_unlock(mp, tid, opts, file, line) do {                    \
231         uintptr_t _tid = (uintptr_t)(tid);                              \
232                                                                         \
233         if ((mp)->mtx_recurse == 0)                                     \
234                  LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_UNLOCK_RELEASE,   \
235                     (mp));                                              \
236         if (!_mtx_release_lock((mp), _tid))                             \
237                 _mtx_unlock_sleep((mp), (opts), (file), (line));        \
238 } while (0)
239
240 /*
241  * Unlock a spin mutex.  For spinlocks, we can handle everything
242  * inline, as it's pretty simple and a function call would be too
243  * expensive (at least on some architectures).  Since spin locks are
244  * not _too_ common, inlining this code is not too big a deal.
245  *
246  * Since we always perform a spinlock_enter() when attempting to acquire a
247  * spin lock, we need to always perform a matching spinlock_exit() when
248  * releasing a spin lock.  This includes the recursion cases.
249  */
250 #ifdef SMP
251 #define __mtx_unlock_spin(mp) do {                                      \
252         if (mtx_recursed((mp)))                                         \
253                 (mp)->mtx_recurse--;                                    \
254         else {                                                          \
255                 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_SPIN_UNLOCK_RELEASE, \
256                         mp);                                            \
257                 _mtx_release_lock_quick((mp));                          \
258         }                                                               \
259         spinlock_exit();                                                \
260 } while (0)
261 #else /* SMP */
262 #define __mtx_unlock_spin(mp) do {                                      \
263         if (mtx_recursed((mp)))                                         \
264                 (mp)->mtx_recurse--;                                    \
265         else {                                                          \
266                 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_SPIN_UNLOCK_RELEASE, \
267                         mp);                                            \
268                 (mp)->mtx_lock = MTX_UNOWNED;                           \
269         }                                                               \
270         spinlock_exit();                                                \
271 } while (0)
272 #endif /* SMP */
273
274 /*
275  * Exported lock manipulation interface.
276  *
277  * mtx_lock(m) locks MTX_DEF mutex `m'
278  *
279  * mtx_lock_spin(m) locks MTX_SPIN mutex `m'
280  *
281  * mtx_unlock(m) unlocks MTX_DEF mutex `m'
282  *
283  * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m'
284  *
285  * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m'
286  *     and passes option flags `opts' to the "hard" function, if required.
287  *     With these routines, it is possible to pass flags such as MTX_QUIET
288  *     to the appropriate lock manipulation routines.
289  *
290  * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if
291  *     it cannot. Rather, it returns 0 on failure and non-zero on success.
292  *     It does NOT handle recursion as we assume that if a caller is properly
293  *     using this part of the interface, he will know that the lock in question
294  *     is _not_ recursed.
295  *
296  * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts
297  *     relevant option flags `opts.'
298  *
299  * mtx_initialized(m) returns non-zero if the lock `m' has been initialized.
300  *
301  * mtx_owned(m) returns non-zero if the current thread owns the lock `m'
302  *
303  * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed.
304  */ 
305 #define mtx_lock(m)             mtx_lock_flags((m), 0)
306 #define mtx_lock_spin(m)        mtx_lock_spin_flags((m), 0)
307 #define mtx_trylock(m)          mtx_trylock_flags((m), 0)
308 #define mtx_unlock(m)           mtx_unlock_flags((m), 0)
309 #define mtx_unlock_spin(m)      mtx_unlock_spin_flags((m), 0)
310
311 struct mtx_pool;
312
313 struct mtx_pool *mtx_pool_create(const char *mtx_name, int pool_size, int opts);
314 void mtx_pool_destroy(struct mtx_pool **poolp);
315 struct mtx *mtx_pool_find(struct mtx_pool *pool, void *ptr);
316 struct mtx *mtx_pool_alloc(struct mtx_pool *pool);
317 #define mtx_pool_lock(pool, ptr)                                        \
318         mtx_lock(mtx_pool_find((pool), (ptr)))
319 #define mtx_pool_lock_spin(pool, ptr)                                   \
320         mtx_lock_spin(mtx_pool_find((pool), (ptr)))
321 #define mtx_pool_unlock(pool, ptr)                                      \
322         mtx_unlock(mtx_pool_find((pool), (ptr)))
323 #define mtx_pool_unlock_spin(pool, ptr)                                 \
324         mtx_unlock_spin(mtx_pool_find((pool), (ptr)))
325
326 /*
327  * mtxpool_sleep is a general purpose pool of sleep mutexes.
328  */
329 extern struct mtx_pool *mtxpool_sleep;
330
331 #ifndef LOCK_DEBUG
332 #error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/mutex.h>
333 #endif
334 #if LOCK_DEBUG > 0 || defined(MUTEX_NOINLINE)
335 #define mtx_lock_flags_(m, opts, file, line)                            \
336         _mtx_lock_flags((m), (opts), (file), (line))
337 #define mtx_unlock_flags_(m, opts, file, line)                          \
338         _mtx_unlock_flags((m), (opts), (file), (line))
339 #define mtx_lock_spin_flags_(m, opts, file, line)                       \
340         _mtx_lock_spin_flags((m), (opts), (file), (line))
341 #define mtx_unlock_spin_flags_(m, opts, file, line)                     \
342         _mtx_unlock_spin_flags((m), (opts), (file), (line))
343 #else   /* LOCK_DEBUG == 0 && !MUTEX_NOINLINE */
344 #define mtx_lock_flags_(m, opts, file, line)                            \
345         __mtx_lock((m), curthread, (opts), (file), (line))
346 #define mtx_unlock_flags_(m, opts, file, line)                          \
347         __mtx_unlock((m), curthread, (opts), (file), (line))
348 #define mtx_lock_spin_flags_(m, opts, file, line)                       \
349         __mtx_lock_spin((m), curthread, (opts), (file), (line))
350 #define mtx_unlock_spin_flags_(m, opts, file, line)                     \
351         __mtx_unlock_spin((m))
352 #endif  /* LOCK_DEBUG > 0 || MUTEX_NOINLINE */
353
354 #ifdef INVARIANTS
355 #define mtx_assert_(m, what, file, line)                                \
356         _mtx_assert((m), (what), (file), (line))
357
358 #define GIANT_REQUIRED  mtx_assert_(&Giant, MA_OWNED, __FILE__, __LINE__)
359
360 #else   /* INVARIANTS */
361 #define mtx_assert_(m, what, file, line)        (void)0
362 #define GIANT_REQUIRED
363 #endif  /* INVARIANTS */
364
365 #define mtx_lock_flags(m, opts)                                         \
366         mtx_lock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
367 #define mtx_unlock_flags(m, opts)                                       \
368         mtx_unlock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
369 #define mtx_lock_spin_flags(m, opts)                                    \
370         mtx_lock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
371 #define mtx_unlock_spin_flags(m, opts)                                  \
372         mtx_unlock_spin_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
373 #define mtx_trylock_flags(m, opts)                                      \
374         mtx_trylock_flags_((m), (opts), LOCK_FILE, LOCK_LINE)
375 #define mtx_assert(m, what)                                             \
376         mtx_assert_((m), (what), __FILE__, __LINE__)
377
378 #define mtx_sleep(chan, mtx, pri, wmesg, timo)                          \
379         _sleep((chan), &(mtx)->lock_object, (pri), (wmesg),             \
380             tick_sbt * (timo), 0, C_HARDCLOCK)
381
382 #define mtx_initialized(m)      lock_initialized(&(m)->lock_object)
383
384 #define mtx_owned(m)    (((m)->mtx_lock & ~MTX_FLAGMASK) == (uintptr_t)curthread)
385
386 #define mtx_recursed(m) ((m)->mtx_recurse != 0)
387
388 #define mtx_name(m)     ((m)->lock_object.lo_name)
389
390 /*
391  * Global locks.
392  */
393 extern struct mtx Giant;
394 extern struct mtx blocked_lock;
395
396 /*
397  * Giant lock manipulation and clean exit macros.
398  * Used to replace return with an exit Giant and return.
399  *
400  * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT() 
401  * The #ifndef is to allow lint-like tools to redefine DROP_GIANT.
402  */
403 #ifndef DROP_GIANT
404 #define DROP_GIANT()                                                    \
405 do {                                                                    \
406         int _giantcnt = 0;                                              \
407         WITNESS_SAVE_DECL(Giant);                                       \
408                                                                         \
409         if (mtx_owned(&Giant)) {                                        \
410                 WITNESS_SAVE(&Giant.lock_object, Giant);                \
411                 for (_giantcnt = 0; mtx_owned(&Giant) &&                \
412                     !SCHEDULER_STOPPED(); _giantcnt++)                  \
413                         mtx_unlock(&Giant);                             \
414         }
415
416 #define PICKUP_GIANT()                                                  \
417         PARTIAL_PICKUP_GIANT();                                         \
418 } while (0)
419
420 #define PARTIAL_PICKUP_GIANT()                                          \
421         mtx_assert(&Giant, MA_NOTOWNED);                                \
422         if (_giantcnt > 0) {                                            \
423                 while (_giantcnt--)                                     \
424                         mtx_lock(&Giant);                               \
425                 WITNESS_RESTORE(&Giant.lock_object, Giant);             \
426         }
427 #endif
428
429 struct mtx_args {
430         void            *ma_mtx;
431         const char      *ma_desc;
432         int              ma_opts;
433 };
434
435 #define MTX_SYSINIT(name, mtx, desc, opts)                              \
436         static struct mtx_args name##_args = {                          \
437                 (mtx),                                                  \
438                 (desc),                                                 \
439                 (opts)                                                  \
440         };                                                              \
441         SYSINIT(name##_mtx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,       \
442             mtx_sysinit, &name##_args);                                 \
443         SYSUNINIT(name##_mtx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,   \
444             _mtx_destroy, __DEVOLATILE(void *, &(mtx)->mtx_lock))
445
446 /*
447  * The INVARIANTS-enabled mtx_assert() functionality.
448  *
449  * The constants need to be defined for INVARIANT_SUPPORT infrastructure
450  * support as _mtx_assert() itself uses them and the latter implies that
451  * _mtx_assert() must build.
452  */
453 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
454 #define MA_OWNED        LA_XLOCKED
455 #define MA_NOTOWNED     LA_UNLOCKED
456 #define MA_RECURSED     LA_RECURSED
457 #define MA_NOTRECURSED  LA_NOTRECURSED
458 #endif
459
460 /*
461  * Common lock type names.
462  */
463 #define MTX_NETWORK_LOCK        "network driver"
464
465 #endif  /* _KERNEL */
466 #endif  /* _SYS_MUTEX_H_ */