mutex.h revision 92373
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: head/sys/sys/mutex.h 92373 2002-03-15 22:10:50Z jhb $ 30 */ 31 32#ifndef _SYS_MUTEX_H_ 33#define _SYS_MUTEX_H_ 34 35#ifndef LOCORE 36#include <sys/queue.h> 37#include <sys/_lock.h> 38#include <sys/_mutex.h> 39 40#ifdef _KERNEL 41#include <sys/pcpu.h> 42#include <machine/atomic.h> 43#include <machine/cpufunc.h> 44#endif /* _KERNEL_ */ 45#endif /* !LOCORE */ 46 47#include <machine/mutex.h> 48 49#ifdef _KERNEL 50 51/* 52 * Mutex types and options passed to mtx_init(). MTX_QUIET can also be 53 * passed in. 54 */ 55#define MTX_DEF 0x00000000 /* DEFAULT (sleep) lock */ 56#define MTX_SPIN 0x00000001 /* Spin lock (disables interrupts) */ 57#define MTX_RECURSE 0x00000004 /* Option: lock allowed to recurse */ 58#define MTX_NOWITNESS 0x00000008 /* Don't do any witness checking. */ 59#define MTX_SLEEPABLE 0x00000010 /* We can sleep with this lock. */ 60 61/* 62 * Option flags passed to certain lock/unlock routines, through the use 63 * of corresponding mtx_{lock,unlock}_flags() interface macros. 64 */ 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 * XXX: Friendly reminder to fix things in MP code that is presently being 82 * XXX: worked on. 83 */ 84#define mp_fixme(string) 85 86#ifdef _KERNEL 87 88/* 89 * Prototypes 90 * 91 * NOTE: Functions prepended with `_' (underscore) are exported to other parts 92 * of the kernel via macros, thus allowing us to use the cpp LOCK_FILE 93 * and LOCK_LINE. These functions should not be called directly by any 94 * code using the API. Their macros cover their functionality. 95 * 96 * [See below for descriptions] 97 * 98 */ 99void mtx_init(struct mtx *m, const char *description, int opts); 100void mtx_destroy(struct mtx *m); 101void _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line); 102void _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line); 103void _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line); 104void _mtx_unlock_spin(struct mtx *m, int opts, const char *file, int line); 105int _mtx_trylock(struct mtx *m, int opts, const char *file, int line); 106void _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line); 107void _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line); 108void _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, 109 int line); 110void _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, 111 int line); 112#ifdef INVARIANT_SUPPORT 113void _mtx_assert(struct mtx *m, int what, const char *file, int line); 114#endif 115int mtx_lock_giant(int sysctlvar); 116void mtx_unlock_giant(int s); 117 118/* 119 * We define our machine-independent (unoptimized) mutex micro-operations 120 * here, if they are not already defined in the machine-dependent mutex.h 121 */ 122 123/* Actually obtain mtx_lock */ 124#ifndef _obtain_lock 125#define _obtain_lock(mp, tid) \ 126 atomic_cmpset_acq_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED, (tid)) 127#endif 128 129/* Actually release mtx_lock */ 130#ifndef _release_lock 131#define _release_lock(mp, tid) \ 132 atomic_cmpset_rel_ptr(&(mp)->mtx_lock, (tid), (void *)MTX_UNOWNED) 133#endif 134 135/* Actually release mtx_lock quickly, assuming we own it. */ 136#ifndef _release_lock_quick 137#define _release_lock_quick(mp) \ 138 atomic_store_rel_ptr(&(mp)->mtx_lock, (void *)MTX_UNOWNED) 139#endif 140 141/* 142 * Obtain a sleep lock inline, or call the "hard" function if we can't get it 143 * easy. 144 */ 145#ifndef _get_sleep_lock 146#define _get_sleep_lock(mp, tid, opts, file, line) do { \ 147 if (!_obtain_lock((mp), (tid))) \ 148 _mtx_lock_sleep((mp), (opts), (file), (line)); \ 149} while (0) 150#endif 151 152/* 153 * Obtain a spin lock inline, or call the "hard" function if we can't get it 154 * easy. For spinlocks, we handle recursion inline (it turns out that function 155 * calls can be significantly expensive on some architectures). 156 * Since spin locks are not _too_ common, inlining this code is not too big 157 * a deal. 158 */ 159#ifndef _get_spin_lock 160#define _get_spin_lock(mp, tid, opts, file, line) do { \ 161 critical_enter(); \ 162 if (!_obtain_lock((mp), (tid))) { \ 163 if ((mp)->mtx_lock == (uintptr_t)(tid)) \ 164 (mp)->mtx_recurse++; \ 165 else \ 166 _mtx_lock_spin((mp), (opts), (file), (line)); \ 167 } \ 168} while (0) 169#endif 170 171/* 172 * Release a sleep lock inline, or call the "hard" function if we can't do it 173 * easy. 174 */ 175#ifndef _rel_sleep_lock 176#define _rel_sleep_lock(mp, tid, opts, file, line) do { \ 177 if (!_release_lock((mp), (tid))) \ 178 _mtx_unlock_sleep((mp), (opts), (file), (line)); \ 179} while (0) 180#endif 181 182/* 183 * For spinlocks, we can handle everything inline, as it's pretty simple and 184 * a function call would be too expensive (at least on some architectures). 185 * Since spin locks are not _too_ common, inlining this code is not too big 186 * a deal. 187 * 188 * Since we always perform a critical_enter() when attempting to acquire a 189 * spin lock, we need to always perform a matching critical_exit() when 190 * releasing a spin lock. This includes the recursion cases. 191 */ 192#ifndef _rel_spin_lock 193#define _rel_spin_lock(mp) do { \ 194 if (mtx_recursed((mp))) \ 195 (mp)->mtx_recurse--; \ 196 else \ 197 _release_lock_quick((mp)); \ 198 critical_exit(); \ 199} while (0) 200#endif 201 202/* 203 * Exported lock manipulation interface. 204 * 205 * mtx_lock(m) locks MTX_DEF mutex `m' 206 * 207 * mtx_lock_spin(m) locks MTX_SPIN mutex `m' 208 * 209 * mtx_unlock(m) unlocks MTX_DEF mutex `m' 210 * 211 * mtx_unlock_spin(m) unlocks MTX_SPIN mutex `m' 212 * 213 * mtx_lock_spin_flags(m, opts) and mtx_lock_flags(m, opts) locks mutex `m' 214 * and passes option flags `opts' to the "hard" function, if required. 215 * With these routines, it is possible to pass flags such as MTX_QUIET 216 * to the appropriate lock manipulation routines. 217 * 218 * mtx_trylock(m) attempts to acquire MTX_DEF mutex `m' but doesn't sleep if 219 * it cannot. Rather, it returns 0 on failure and non-zero on success. 220 * It does NOT handle recursion as we assume that if a caller is properly 221 * using this part of the interface, he will know that the lock in question 222 * is _not_ recursed. 223 * 224 * mtx_trylock_flags(m, opts) is used the same way as mtx_trylock() but accepts 225 * relevant option flags `opts.' 226 * 227 * mtx_initialized(m) returns non-zero if the lock `m' has been initialized. 228 * 229 * mtx_owned(m) returns non-zero if the current thread owns the lock `m' 230 * 231 * mtx_recursed(m) returns non-zero if the lock `m' is presently recursed. 232 */ 233#define mtx_lock(m) mtx_lock_flags((m), 0) 234#define mtx_lock_spin(m) mtx_lock_spin_flags((m), 0) 235#define mtx_trylock(m) mtx_trylock_flags((m), 0) 236#define mtx_unlock(m) mtx_unlock_flags((m), 0) 237#define mtx_unlock_spin(m) mtx_unlock_spin_flags((m), 0) 238 239struct mtx *mtx_pool_find(void *ptr); 240struct mtx *mtx_pool_alloc(void); 241void mtx_pool_lock(void *ptr); 242void mtx_pool_unlock(void *ptr); 243 244extern int mtx_pool_valid; 245 246#ifndef LOCK_DEBUG 247#error LOCK_DEBUG not defined, include <sys/lock.h> before <sys/mutex.h> 248#endif 249#if LOCK_DEBUG > 0 250#define mtx_lock_flags(m, opts) \ 251 _mtx_lock_flags((m), (opts), LOCK_FILE, LOCK_LINE) 252#define mtx_unlock_flags(m, opts) \ 253 _mtx_unlock_flags((m), (opts), LOCK_FILE, LOCK_LINE) 254#define mtx_lock_spin_flags(m, opts) \ 255 _mtx_lock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE) 256#define mtx_unlock_spin_flags(m, opts) \ 257 _mtx_unlock_spin_flags((m), (opts), LOCK_FILE, LOCK_LINE) 258#else 259#define mtx_lock_flags(m, opts) \ 260 _get_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE) 261#define mtx_unlock_flags(m, opts) \ 262 _rel_sleep_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE) 263#define mtx_lock_spin_flags(m, opts) \ 264 _get_spin_lock((m), curthread, (opts), LOCK_FILE, LOCK_LINE) 265#define mtx_unlock_spin_flags(m, opts) \ 266 _rel_spin_lock((m)) 267#endif 268 269#define mtx_trylock_flags(m, opts) \ 270 _mtx_trylock((m), (opts), LOCK_FILE, LOCK_LINE) 271 272#define mtx_initialized(m) ((m)->mtx_object.lo_flags & LO_INITIALIZED) 273 274#define mtx_owned(m) (((m)->mtx_lock & MTX_FLAGMASK) == (uintptr_t)curthread) 275 276#define mtx_recursed(m) ((m)->mtx_recurse != 0) 277 278/* 279 * Global locks. 280 */ 281extern struct mtx sched_lock; 282extern struct mtx Giant; 283 284/* 285 * Giant lock sysctl variables used by other modules 286 */ 287extern int kern_giant_proc; 288extern int kern_giant_file; 289extern int kern_giant_ucred; 290 291/* 292 * Giant lock manipulation and clean exit macros. 293 * Used to replace return with an exit Giant and return. 294 * 295 * Note that DROP_GIANT*() needs to be paired with PICKUP_GIANT() 296 */ 297#define DROP_GIANT() \ 298do { \ 299 int _giantcnt; \ 300 WITNESS_SAVE_DECL(Giant); \ 301 \ 302 if (mtx_owned(&Giant)) \ 303 WITNESS_SAVE(&Giant.mtx_object, Giant); \ 304 for (_giantcnt = 0; mtx_owned(&Giant); _giantcnt++) \ 305 mtx_unlock(&Giant) 306 307#define PICKUP_GIANT() \ 308 mtx_assert(&Giant, MA_NOTOWNED); \ 309 while (_giantcnt--) \ 310 mtx_lock(&Giant); \ 311 if (mtx_owned(&Giant)) \ 312 WITNESS_RESTORE(&Giant.mtx_object, Giant); \ 313} while (0) 314 315#define PARTIAL_PICKUP_GIANT() \ 316 mtx_assert(&Giant, MA_NOTOWNED); \ 317 while (_giantcnt--) \ 318 mtx_lock(&Giant); \ 319 if (mtx_owned(&Giant)) \ 320 WITNESS_RESTORE(&Giant.mtx_object, Giant) 321 322#define UGAR(rval) do { \ 323 int _val = (rval); \ 324 mtx_unlock(&Giant); \ 325 return (_val); \ 326} while (0) 327 328/* 329 * The INVARIANTS-enabled mtx_assert() functionality. 330 * 331 * The constants need to be defined for INVARIANT_SUPPORT infrastructure 332 * support as _mtx_assert() itself uses them and the latter implies that 333 * _mtx_assert() must build. 334 */ 335#ifdef INVARIANT_SUPPORT 336#define MA_OWNED 0x01 337#define MA_NOTOWNED 0x02 338#define MA_RECURSED 0x04 339#define MA_NOTRECURSED 0x08 340#endif /* INVARIANT_SUPPORT */ 341 342#ifdef INVARIANTS 343#define mtx_assert(m, what) \ 344 _mtx_assert((m), (what), __FILE__, __LINE__) 345 346#define GIANT_REQUIRED mtx_assert(&Giant, MA_OWNED) 347 348#else /* INVARIANTS */ 349#define mtx_assert(m, what) 350#define GIANT_REQUIRED 351#endif /* INVARIANTS */ 352 353#endif /* _KERNEL */ 354#endif /* !LOCORE */ 355#endif /* _SYS_MUTEX_H_ */ 356