sx.h revision 73782
156160Sru/*
2146515Sru * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
321495Sjmacd *
4146515Sru * Redistribution and use in source and binary forms, with or without
5116525Sru * modification, are permitted provided that the following conditions
621495Sjmacd * are met:
721495Sjmacd * 1. Redistributions of source code must retain the above copyright
821495Sjmacd *    notice(s), this list of conditions and the following disclaimer as
921495Sjmacd *    the first lines of this file unmodified other than the possible
1021495Sjmacd *    addition of one or more copyright notices.
1121495Sjmacd * 2. Redistributions in binary form must reproduce the above copyright
1221495Sjmacd *    notice(s), this list of conditions and the following disclaimer in the
1321495Sjmacd *    documentation and/or other materials provided with the distribution.
1421495Sjmacd *
1521495Sjmacd * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1621495Sjmacd * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1721495Sjmacd * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1821495Sjmacd * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
1921495Sjmacd * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2021495Sjmacd * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2121495Sjmacd * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2221495Sjmacd * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2356160Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2421495Sjmacd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2521495Sjmacd * DAMAGE.
2656160Sru *
2756160Sru * $FreeBSD: head/sys/sys/sx.h 73782 2001-03-05 19:59:41Z jasone $
2821495Sjmacd */
2956160Sru
3021495Sjmacd#ifndef	_SYS_SX_H_
3121495Sjmacd#define	_SYS_SX_H_
3256160Sru
3356160Sru#ifndef	LOCORE
3456160Sru#include <sys/mutex.h>
3556160Sru#include <sys/condvar.h>
3656160Sru
3721495Sjmacdstruct sx {
3856160Sru	struct mtx	sx_lock;	/* General protection lock and xlock. */
3956160Sru	int		sx_cnt;		/* -1: xlock, > 0: slock count. */
4056160Sru	struct cv	sx_shrd_cv;	/* slock waiters. */
4156160Sru	int		sx_shrd_wcnt;	/* Number of slock waiters. */
4221495Sjmacd	struct cv	sx_excl_cv;	/* xlock waiters. */
4356160Sru	int		sx_excl_wcnt;	/* Number of xlock waiters. */
4421495Sjmacd};
4556160Sru
4656160Sru#ifdef _KERNEL
4756160Sruvoid	sx_init(struct sx *sx, const char *description);
4856160Sruvoid	sx_destroy(struct sx *sx);
4956160Sruvoid	sx_slock(struct sx *sx);
5021495Sjmacdvoid	sx_xlock(struct sx *sx);
5156160Sruvoid	sx_sunlock(struct sx *sx);
5221495Sjmacdvoid	sx_xunlock(struct sx *sx);
5321495Sjmacd
5421495Sjmacd#ifdef INVARIANTS
5521495Sjmacd/*
5621495Sjmacd * SX_ASSERT_SLOCKED() can only detect that at least *some* thread owns an
5721495Sjmacd * slock, but it cannot guarantee that *this* thread owns an slock.
5821495Sjmacd */
5956160Sru#define	SX_ASSERT_SLOCKED(sx) do {					\
6056160Sru	mtx_lock(&(sx)->sx_lock);					\
6156160Sru	KASSERT(((sx)->sx_cnt > 0), ("%s: lacking slock\n",		\
62146515Sru	    __FUNCTION__));						\
63146515Sru	mtx_unlock(&(sx)->sx_lock);					\
64146515Sru} while (0)
65146515Sru/*
6656160Sru * SX_ASSERT_XLOCKED() can only detect that at least *some* thread owns an
6756160Sru * xlock, but it cannot guarantee that *this* thread owns an xlock.
6856160Sru */
6956160Sru#define	SX_ASSERT_XLOCKED(sx) do {					\
7056160Sru	mtx_lock(&(sx)->sx_lock);					\
7121495Sjmacd	KASSERT(((sx)->sx_cnt == -1), ("%s: lacking xlock\n",		\
7221495Sjmacd	    __FUNCTION__));						\
7356160Sru	mtx_unlock(&(sx)->sx_lock);					\
7456160Sru} while (0)
7556160Sru#else	/* INVARIANTS */
7656160Sru#define	SX_ASSERT_SLOCKED(sx)
7756160Sru#define	SX_ASSERT_XLOCKER(sx)
7856160Sru#endif	/* INVARIANTS */
7956160Sru
8056160Sru#endif	/* _KERNEL */
8156160Sru#endif	/* !LOCORE */
8256160Sru#endif	/* _SYS_SX_H_ */
8356160Sru