11.6Sriastrad/*	$NetBSD: mutex.h,v 1.6 2023/07/12 12:50:13 riastradh Exp $	*/
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jason R. Thorpe and Andrew Doran.
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.2Sjmcneill#ifndef _ARCH_USERMODE_INCLUDE_MUTEX_H_
331.2Sjmcneill#define	_ARCH_USERMODE_INCLUDE_MUTEX_H_
341.1Sjmcneill
351.6Sriastrad#include <sys/types.h>
361.6Sriastrad
371.5Sriastrad#ifdef __MUTEX_PRIVATE
381.5Sriastrad#include <machine/intr.h>
391.5Sriastrad#endif
401.5Sriastrad
411.1Sjmcneillstruct kmutex {
421.1Sjmcneill	union {
431.1Sjmcneill		volatile uintptr_t	mtxa_owner;
441.1Sjmcneill#ifdef __MUTEX_PRIVATE
451.1Sjmcneill		struct {
461.1Sjmcneill			volatile uint8_t	mtxs_dummy;
471.1Sjmcneill			ipl_cookie_t		mtxs_ipl;
481.1Sjmcneill                        __cpu_simple_lock_t	mtxs_lock;
491.1Sjmcneill			volatile uint8_t	mtxs_unused;
501.1Sjmcneill		} s;
511.1Sjmcneill#endif
521.1Sjmcneill	} u;
531.1Sjmcneill};
541.1Sjmcneill
551.1Sjmcneill#ifdef __MUTEX_PRIVATE
561.1Sjmcneill
571.1Sjmcneill#define	mtx_owner 			u.mtxa_owner
581.1Sjmcneill#define	mtx_ipl 			u.s.mtxs_ipl
591.1Sjmcneill#define	mtx_lock			u.s.mtxs_lock
601.1Sjmcneill
611.1Sjmcneill#define	__HAVE_SIMPLE_MUTEXES		1
621.1Sjmcneill
631.1Sjmcneill#define	MUTEX_CAS(p, o, n)		\
641.1Sjmcneill    (_atomic_cas_ulong((volatile unsigned long *)(p), (o), (n)) == (o))
651.1Sjmcneill
661.1Sjmcneillunsigned long	_atomic_cas_ulong(volatile unsigned long *,
671.1Sjmcneill    unsigned long, unsigned long);
681.1Sjmcneill
691.1Sjmcneill#endif	/* __MUTEX_PRIVATE */
701.1Sjmcneill
711.2Sjmcneill#endif /* _ARCH_USERMODE_INCLUDE_MUTEX_H_ */
72