sx.h revision 83103
173782Sjasone/*
273782Sjasone * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
373782Sjasone *
473782Sjasone * Redistribution and use in source and binary forms, with or without
573782Sjasone * modification, are permitted provided that the following conditions
673782Sjasone * are met:
773782Sjasone * 1. Redistributions of source code must retain the above copyright
873782Sjasone *    notice(s), this list of conditions and the following disclaimer as
973782Sjasone *    the first lines of this file unmodified other than the possible
1073782Sjasone *    addition of one or more copyright notices.
1173782Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1273782Sjasone *    notice(s), this list of conditions and the following disclaimer in the
1373782Sjasone *    documentation and/or other materials provided with the distribution.
1473782Sjasone *
1573782Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1673782Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1773782Sjasone * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1873782Sjasone * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
1973782Sjasone * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2073782Sjasone * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2173782Sjasone * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2273782Sjasone * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2373782Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2473782Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2573782Sjasone * DAMAGE.
2673782Sjasone *
2773782Sjasone * $FreeBSD: head/sys/sys/sx.h 83103 2001-09-05 18:19:09Z jhb $
2873782Sjasone */
2973782Sjasone
3073782Sjasone#ifndef	_SYS_SX_H_
3173782Sjasone#define	_SYS_SX_H_
3273782Sjasone
3373782Sjasone#ifndef	LOCORE
3483103Sjhb#include <sys/_lock.h>
3576166Smarkm#include <sys/_mutex.h>
3674912Sjhb#include <sys/condvar.h>	/* XXX */
3773782Sjasone
3873782Sjasonestruct sx {
3974912Sjhb	struct lock_object sx_object;	/* Common lock properties. */
4073863Sbmilekic	struct mtx	sx_lock;	/* General protection lock. */
4173782Sjasone	int		sx_cnt;		/* -1: xlock, > 0: slock count. */
4273782Sjasone	struct cv	sx_shrd_cv;	/* slock waiters. */
4373782Sjasone	int		sx_shrd_wcnt;	/* Number of slock waiters. */
4473782Sjasone	struct cv	sx_excl_cv;	/* xlock waiters. */
4573782Sjasone	int		sx_excl_wcnt;	/* Number of xlock waiters. */
4673863Sbmilekic	struct proc	*sx_xholder;	/* Thread presently holding xlock. */
4773782Sjasone};
4873782Sjasone
4973782Sjasone#ifdef _KERNEL
5073782Sjasonevoid	sx_init(struct sx *sx, const char *description);
5173782Sjasonevoid	sx_destroy(struct sx *sx);
5274912Sjhbvoid	_sx_slock(struct sx *sx, const char *file, int line);
5374912Sjhbvoid	_sx_xlock(struct sx *sx, const char *file, int line);
5478872Sjhbint	_sx_try_slock(struct sx *sx, const char *file, int line);
5578872Sjhbint	_sx_try_xlock(struct sx *sx, const char *file, int line);
5674912Sjhbvoid	_sx_sunlock(struct sx *sx, const char *file, int line);
5774912Sjhbvoid	_sx_xunlock(struct sx *sx, const char *file, int line);
5881599Sjasoneint	_sx_try_upgrade(struct sx *sx, const char *file, int line);
5981599Sjasonevoid	_sx_downgrade(struct sx *sx, const char *file, int line);
6073782Sjasone
6178872Sjhb#define	sx_slock(sx)		_sx_slock((sx), __FILE__, __LINE__)
6278872Sjhb#define	sx_xlock(sx)		_sx_xlock((sx), __FILE__, __LINE__)
6378872Sjhb#define	sx_try_slock(sx)	_sx_try_slock((sx), __FILE__, __LINE__)
6478872Sjhb#define	sx_try_xlock(sx)	_sx_try_xlock((sx), __FILE__, __LINE__)
6578872Sjhb#define	sx_sunlock(sx)		_sx_sunlock((sx), __FILE__, __LINE__)
6678872Sjhb#define	sx_xunlock(sx)		_sx_xunlock((sx), __FILE__, __LINE__)
6781599Sjasone#define	sx_try_upgrade(sx)	_sx_try_upgrade((sx), __FILE__, __LINE__)
6881599Sjasone#define	sx_downgrade(sx)	_sx_downgrade((sx), __FILE__, __LINE__)
6974912Sjhb
7073782Sjasone#ifdef INVARIANTS
7173782Sjasone/*
7278872Sjhb * In the non-WITNESS case, SX_ASSERT_LOCKED() and SX_ASSERT_SLOCKED()
7378872Sjhb * can only detect that at least *some* thread owns an slock, but it cannot
7478872Sjhb * guarantee that *this* thread owns an slock.
7573782Sjasone */
7678872Sjhb#ifdef WITNESS
7778872Sjhb#define	_SX_ASSERT_LOCKED(sx, file, line)				\
7878872Sjhb	witness_assert(&(sx)->sx_object, LA_LOCKED, file, line)
7978872Sjhb#define _SX_ASSERT_SLOCKED(sx, file, line)				\
8078872Sjhb	witness_assert(&(sx)->sx_object, LA_SLOCKED, file, line)
8178872Sjhb#else
8278872Sjhb#define	_SX_ASSERT_LOCKED(sx, file, line) do {				\
8378872Sjhb	KASSERT(((sx)->sx_cnt > 0 || (sx)->sx_xholder == curproc),	\
8478872Sjhb	    ("Lock %s not locked @ %s:%d", (sx)->sx_object.lo_name,	\
8578872Sjhb	    file, line));						\
8673901Sjhb} while (0)
8778872Sjhb#define	_SX_ASSERT_SLOCKED(sx, file, line) do {				\
8878872Sjhb	KASSERT(((sx)->sx_cnt > 0), ("Lock %s not share locked @ %s:%d",\
8978872Sjhb	    (sx)->sx_object.lo_name, file, line));			\
9073782Sjasone} while (0)
9178872Sjhb#endif
9278872Sjhb#define	SX_ASSERT_LOCKED(sx)	_SX_ASSERT_LOCKED((sx), __FILE__, __LINE__)
9378872Sjhb#define	SX_ASSERT_SLOCKED(sx)	_SX_ASSERT_SLOCKED((sx), __FILE__, __LINE__)
9473863Sbmilekic
9573782Sjasone/*
9673863Sbmilekic * SX_ASSERT_XLOCKED() detects and guarantees that *we* own the xlock.
9773782Sjasone */
9878872Sjhb#define	_SX_ASSERT_XLOCKED(sx, file, line) do {				\
9973863Sbmilekic	KASSERT(((sx)->sx_xholder == curproc),				\
10078872Sjhb	    ("Lock %s not exclusively locked @ %s:%d",			\
10178872Sjhb	    (sx)->sx_object.lo_name, file, line));			\
10273782Sjasone} while (0)
10378872Sjhb#define SX_ASSERT_XLOCKED(sx)	_SX_ASSERT_XLOCKED((sx), __FILE__, __LINE__)
10473863Sbmilekic
10573782Sjasone#else	/* INVARIANTS */
10673782Sjasone#define	SX_ASSERT_SLOCKED(sx)
10773874Sdwmalone#define	SX_ASSERT_XLOCKED(sx)
10878872Sjhb#define	_SX_ASSERT_SLOCKED(sx, file, line)
10978872Sjhb#define	_SX_ASSERT_XLOCKED(sx, file, line)
11073782Sjasone#endif	/* INVARIANTS */
11173782Sjasone
11273782Sjasone#endif	/* _KERNEL */
11373782Sjasone#endif	/* !LOCORE */
11473782Sjasone#endif	/* _SYS_SX_H_ */
115