1*f6aac1c3SLionel Sambuc /* $NetBSD: mutex.h,v 1.6 2009/04/24 17:49:51 ad Exp $ */ 2*f6aac1c3SLionel Sambuc 3*f6aac1c3SLionel Sambuc /*- 4*f6aac1c3SLionel Sambuc * Copyright (c) 2002, 2006, 2009 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 and Andrew Doran. 9*f6aac1c3SLionel Sambuc * 10*f6aac1c3SLionel Sambuc * Redistribution and use in source and binary forms, with or without 11*f6aac1c3SLionel Sambuc * modification, are permitted provided that the following conditions 12*f6aac1c3SLionel Sambuc * are met: 13*f6aac1c3SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 14*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer. 15*f6aac1c3SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16*f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 17*f6aac1c3SLionel Sambuc * documentation and/or other materials provided with the distribution. 18*f6aac1c3SLionel Sambuc * 19*f6aac1c3SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20*f6aac1c3SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21*f6aac1c3SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22*f6aac1c3SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23*f6aac1c3SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24*f6aac1c3SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25*f6aac1c3SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26*f6aac1c3SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27*f6aac1c3SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28*f6aac1c3SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29*f6aac1c3SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE. 30*f6aac1c3SLionel Sambuc */ 31*f6aac1c3SLionel Sambuc 32*f6aac1c3SLionel Sambuc #ifndef _X86_MUTEX_H_ 33*f6aac1c3SLionel Sambuc #define _X86_MUTEX_H_ 34*f6aac1c3SLionel Sambuc 35*f6aac1c3SLionel Sambuc struct kmutex { 36*f6aac1c3SLionel Sambuc union { 37*f6aac1c3SLionel Sambuc volatile uintptr_t mtxa_owner; 38*f6aac1c3SLionel Sambuc #ifdef __MUTEX_PRIVATE 39*f6aac1c3SLionel Sambuc struct { 40*f6aac1c3SLionel Sambuc volatile uint8_t mtxs_dummy; 41*f6aac1c3SLionel Sambuc ipl_cookie_t mtxs_ipl; 42*f6aac1c3SLionel Sambuc __cpu_simple_lock_t mtxs_lock; 43*f6aac1c3SLionel Sambuc volatile uint8_t mtxs_unused; 44*f6aac1c3SLionel Sambuc } s; 45*f6aac1c3SLionel Sambuc #endif 46*f6aac1c3SLionel Sambuc } u; 47*f6aac1c3SLionel Sambuc }; 48*f6aac1c3SLionel Sambuc 49*f6aac1c3SLionel Sambuc #ifdef __MUTEX_PRIVATE 50*f6aac1c3SLionel Sambuc 51*f6aac1c3SLionel Sambuc #define mtx_owner u.mtxa_owner 52*f6aac1c3SLionel Sambuc #define mtx_ipl u.s.mtxs_ipl 53*f6aac1c3SLionel Sambuc #define mtx_lock u.s.mtxs_lock 54*f6aac1c3SLionel Sambuc 55*f6aac1c3SLionel Sambuc #define __HAVE_MUTEX_STUBS 1 56*f6aac1c3SLionel Sambuc #define __HAVE_SPIN_MUTEX_STUBS 1 57*f6aac1c3SLionel Sambuc #define __HAVE_SIMPLE_MUTEXES 1 58*f6aac1c3SLionel Sambuc 59*f6aac1c3SLionel Sambuc /* 60*f6aac1c3SLionel Sambuc * MUTEX_RECEIVE: technically, no memory barrier is required 61*f6aac1c3SLionel Sambuc * as 'ret' implies a load fence. However we need this to 62*f6aac1c3SLionel Sambuc * handle a bug with some Opteron revisions. See patch.c, 63*f6aac1c3SLionel Sambuc * lock_stubs.S. 64*f6aac1c3SLionel Sambuc */ 65*f6aac1c3SLionel Sambuc #define MUTEX_RECEIVE(mtx) membar_consumer() 66*f6aac1c3SLionel Sambuc 67*f6aac1c3SLionel Sambuc /* 68*f6aac1c3SLionel Sambuc * MUTEX_GIVE: no memory barrier required, as _lock_cas() will take care of it. 69*f6aac1c3SLionel Sambuc */ 70*f6aac1c3SLionel Sambuc #define MUTEX_GIVE(mtx) /* nothing */ 71*f6aac1c3SLionel Sambuc 72*f6aac1c3SLionel Sambuc #define MUTEX_CAS(p, o, n) \ 73*f6aac1c3SLionel Sambuc (_atomic_cas_ulong((volatile unsigned long *)(p), (o), (n)) == (o)) 74*f6aac1c3SLionel Sambuc 75*f6aac1c3SLionel Sambuc unsigned long _atomic_cas_ulong(volatile unsigned long *, 76*f6aac1c3SLionel Sambuc unsigned long, unsigned long); 77*f6aac1c3SLionel Sambuc 78*f6aac1c3SLionel Sambuc #endif /* __MUTEX_PRIVATE */ 79*f6aac1c3SLionel Sambuc 80*f6aac1c3SLionel Sambuc #endif /* _X86_MUTEX_H_ */ 81