source: rtems-libbsd/freebsd/sys/mutex.h @ b55081a

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since b55081a was b55081a, checked in by Joel Sherrill <joel.sherrill@…>, on 03/08/12 at 21:54:05

kern/kern_subr.c added for hashinit

More undefined symbols resulted.

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