1*f6aac1c3SLionel Sambuc /* $NetBSD: lock.h,v 1.86 2010/07/01 13:00:57 hannken Exp $ */ 2*f6aac1c3SLionel Sambuc 3*f6aac1c3SLionel Sambuc /*- 4*f6aac1c3SLionel Sambuc * Copyright (c) 1999, 2000, 2006, 2007 The NetBSD Foundation, Inc. 5*f6aac1c3SLionel Sambuc * All rights reserved. 6*f6aac1c3SLionel Sambuc * 7*f6aac1c3SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation 8*f6aac1c3SLionel Sambuc * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9*f6aac1c3SLionel Sambuc * NASA Ames Research Center, by Ross Harvey, and by Andrew Doran. 10*f6aac1c3SLionel Sambuc * 11*f6aac1c3SLionel Sambuc * Redistribution and use in source and binary forms, with or without 12*f6aac1c3SLionel Sambuc * modification, are permitted provided that the following conditions 13*f6aac1c3SLionel Sambuc * are met: 14*f6aac1c3SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 15*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer. 16*f6aac1c3SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 17*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 18*f6aac1c3SLionel Sambuc * documentation and/or other materials provided with the distribution. 19*f6aac1c3SLionel Sambuc * 20*f6aac1c3SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21*f6aac1c3SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22*f6aac1c3SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23*f6aac1c3SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24*f6aac1c3SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25*f6aac1c3SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26*f6aac1c3SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27*f6aac1c3SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28*f6aac1c3SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29*f6aac1c3SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30*f6aac1c3SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 31*f6aac1c3SLionel Sambuc */ 32*f6aac1c3SLionel Sambuc 33*f6aac1c3SLionel Sambuc /* 34*f6aac1c3SLionel Sambuc * Copyright (c) 1995 35*f6aac1c3SLionel Sambuc * The Regents of the University of California. All rights reserved. 36*f6aac1c3SLionel Sambuc * 37*f6aac1c3SLionel Sambuc * This code contains ideas from software contributed to Berkeley by 38*f6aac1c3SLionel Sambuc * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating 39*f6aac1c3SLionel Sambuc * System project at Carnegie-Mellon University. 40*f6aac1c3SLionel Sambuc * 41*f6aac1c3SLionel Sambuc * Redistribution and use in source and binary forms, with or without 42*f6aac1c3SLionel Sambuc * modification, are permitted provided that the following conditions 43*f6aac1c3SLionel Sambuc * are met: 44*f6aac1c3SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 45*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer. 46*f6aac1c3SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 47*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 48*f6aac1c3SLionel Sambuc * documentation and/or other materials provided with the distribution. 49*f6aac1c3SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors 50*f6aac1c3SLionel Sambuc * may be used to endorse or promote products derived from this software 51*f6aac1c3SLionel Sambuc * without specific prior written permission. 52*f6aac1c3SLionel Sambuc * 53*f6aac1c3SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54*f6aac1c3SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55*f6aac1c3SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56*f6aac1c3SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57*f6aac1c3SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58*f6aac1c3SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59*f6aac1c3SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60*f6aac1c3SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61*f6aac1c3SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62*f6aac1c3SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63*f6aac1c3SLionel Sambuc * SUCH DAMAGE. 64*f6aac1c3SLionel Sambuc * 65*f6aac1c3SLionel Sambuc * @(#)lock.h 8.12 (Berkeley) 5/19/95 66*f6aac1c3SLionel Sambuc */ 67*f6aac1c3SLionel Sambuc 68*f6aac1c3SLionel Sambuc #ifndef _SYS_LOCK_H_ 69*f6aac1c3SLionel Sambuc #define _SYS_LOCK_H_ 70*f6aac1c3SLionel Sambuc 71*f6aac1c3SLionel Sambuc #include <sys/stdint.h> 72*f6aac1c3SLionel Sambuc #include <sys/mutex.h> 73*f6aac1c3SLionel Sambuc 74*f6aac1c3SLionel Sambuc #include <machine/lock.h> 75*f6aac1c3SLionel Sambuc 76*f6aac1c3SLionel Sambuc #ifdef _KERNEL 77*f6aac1c3SLionel Sambuc 78*f6aac1c3SLionel Sambuc /* 79*f6aac1c3SLionel Sambuc * From <machine/lock.h>. 80*f6aac1c3SLionel Sambuc */ 81*f6aac1c3SLionel Sambuc #ifndef SPINLOCK_SPIN_HOOK 82*f6aac1c3SLionel Sambuc #define SPINLOCK_SPIN_HOOK 83*f6aac1c3SLionel Sambuc #endif 84*f6aac1c3SLionel Sambuc #ifndef SPINLOCK_BACKOFF_HOOK 85*f6aac1c3SLionel Sambuc #define SPINLOCK_BACKOFF_HOOK nullop(NULL) 86*f6aac1c3SLionel Sambuc #endif 87*f6aac1c3SLionel Sambuc #ifndef SPINLOCK_BACKOFF_MIN 88*f6aac1c3SLionel Sambuc #define SPINLOCK_BACKOFF_MIN 4 89*f6aac1c3SLionel Sambuc #endif 90*f6aac1c3SLionel Sambuc #ifndef SPINLOCK_BACKOFF_MAX 91*f6aac1c3SLionel Sambuc #define SPINLOCK_BACKOFF_MAX 128 92*f6aac1c3SLionel Sambuc #endif 93*f6aac1c3SLionel Sambuc 94*f6aac1c3SLionel Sambuc #define SPINLOCK_BACKOFF(count) \ 95*f6aac1c3SLionel Sambuc do { \ 96*f6aac1c3SLionel Sambuc int __i; \ 97*f6aac1c3SLionel Sambuc for (__i = (count); __i != 0; __i--) { \ 98*f6aac1c3SLionel Sambuc SPINLOCK_BACKOFF_HOOK; \ 99*f6aac1c3SLionel Sambuc } \ 100*f6aac1c3SLionel Sambuc if ((count) < SPINLOCK_BACKOFF_MAX) \ 101*f6aac1c3SLionel Sambuc (count) += (count); \ 102*f6aac1c3SLionel Sambuc } while (/* CONSTCOND */ 0); 103*f6aac1c3SLionel Sambuc 104*f6aac1c3SLionel Sambuc #define SPINLOCK_RUN_HOOK(count) ((count) >= SPINLOCK_BACKOFF_MAX) 105*f6aac1c3SLionel Sambuc 106*f6aac1c3SLionel Sambuc #ifdef LOCKDEBUG 107*f6aac1c3SLionel Sambuc #define SPINLOCK_SPINOUT(spins) ((spins)++ > 0x0fffffff) 108*f6aac1c3SLionel Sambuc #else 109*f6aac1c3SLionel Sambuc #define SPINLOCK_SPINOUT(spins) ((void)(spins), 0) 110*f6aac1c3SLionel Sambuc #endif 111*f6aac1c3SLionel Sambuc 112*f6aac1c3SLionel Sambuc extern __cpu_simple_lock_t kernel_lock[]; 113*f6aac1c3SLionel Sambuc 114*f6aac1c3SLionel Sambuc #endif /* _KERNEL */ 115*f6aac1c3SLionel Sambuc 116*f6aac1c3SLionel Sambuc #endif /* _SYS_LOCK_H_ */ 117