sx.h revision 76166
1295367Sdes/*
2261287Sdes * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
3261287Sdes *
4261287Sdes * Redistribution and use in source and binary forms, with or without
5261287Sdes * modification, are permitted provided that the following conditions
6261287Sdes * are met:
7261287Sdes * 1. Redistributions of source code must retain the above copyright
8261287Sdes *    notice(s), this list of conditions and the following disclaimer as
9261287Sdes *    the first lines of this file unmodified other than the possible
10261287Sdes *    addition of one or more copyright notices.
11261287Sdes * 2. Redistributions in binary form must reproduce the above copyright
12261287Sdes *    notice(s), this list of conditions and the following disclaimer in the
13261287Sdes *    documentation and/or other materials provided with the distribution.
14261287Sdes *
15261287Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16261287Sdes * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17261287Sdes * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18261287Sdes * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19261287Sdes * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20261287Sdes * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21261287Sdes * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22261287Sdes * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23261287Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24261287Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25261287Sdes * DAMAGE.
26261287Sdes *
27261287Sdes * $FreeBSD: head/sys/sys/sx.h 76166 2001-05-01 08:13:21Z markm $
28261287Sdes */
29261287Sdes
30261287Sdes#ifndef	_SYS_SX_H_
31295367Sdes#define	_SYS_SX_H_
32261287Sdes
33261287Sdes#ifndef	LOCORE
34261287Sdes#include <sys/_mutex.h>
35261287Sdes#include <sys/condvar.h>	/* XXX */
36261287Sdes
37261287Sdesstruct sx {
38261287Sdes	struct lock_object sx_object;	/* Common lock properties. */
39261287Sdes	struct mtx	sx_lock;	/* General protection lock. */
40261287Sdes	int		sx_cnt;		/* -1: xlock, > 0: slock count. */
41261287Sdes	struct cv	sx_shrd_cv;	/* slock waiters. */
42261287Sdes	int		sx_shrd_wcnt;	/* Number of slock waiters. */
43261287Sdes	struct cv	sx_excl_cv;	/* xlock waiters. */
44	int		sx_excl_wcnt;	/* Number of xlock waiters. */
45	struct proc	*sx_xholder;	/* Thread presently holding xlock. */
46};
47
48#ifdef _KERNEL
49void	sx_init(struct sx *sx, const char *description);
50void	sx_destroy(struct sx *sx);
51void	_sx_slock(struct sx *sx, const char *file, int line);
52void	_sx_xlock(struct sx *sx, const char *file, int line);
53void	_sx_sunlock(struct sx *sx, const char *file, int line);
54void	_sx_xunlock(struct sx *sx, const char *file, int line);
55
56#define	sx_slock(sx)	_sx_slock((sx), __FILE__, __LINE__)
57#define	sx_xlock(sx)	_sx_xlock((sx), __FILE__, __LINE__)
58#define	sx_sunlock(sx)	_sx_sunlock((sx), __FILE__, __LINE__)
59#define	sx_xunlock(sx)	_sx_xunlock((sx), __FILE__, __LINE__)
60
61#ifdef INVARIANTS
62/*
63 * SX_ASSERT_SLOCKED() can only detect that at least *some* thread owns an
64 * slock, but it cannot guarantee that *this* thread owns an slock.
65 */
66#define	SX_ASSERT_SLOCKED(sx) do {					\
67	mtx_lock(&(sx)->sx_lock);					\
68	_SX_ASSERT_SLOCKED((sx));					\
69	mtx_unlock(&(sx)->sx_lock);					\
70} while (0)
71#define	_SX_ASSERT_SLOCKED(sx) do {					\
72	KASSERT(((sx)->sx_cnt > 0), ("%s: lacking slock %s\n",		\
73	    __FUNCTION__, (sx)->sx_object.lo_name));			\
74} while (0)
75
76/*
77 * SX_ASSERT_XLOCKED() detects and guarantees that *we* own the xlock.
78 */
79#define	SX_ASSERT_XLOCKED(sx) do {					\
80	mtx_lock(&(sx)->sx_lock);					\
81	_SX_ASSERT_XLOCKED((sx));					\
82	mtx_unlock(&(sx)->sx_lock);					\
83} while (0)
84#define	_SX_ASSERT_XLOCKED(sx) do {					\
85	KASSERT(((sx)->sx_xholder == curproc),				\
86	    ("%s: thread %p lacking xlock %s\n", __FUNCTION__,		\
87	    curproc, (sx)->sx_object.lo_name));				\
88} while (0)
89
90#else	/* INVARIANTS */
91#define	SX_ASSERT_SLOCKED(sx)
92#define	SX_ASSERT_XLOCKED(sx)
93#define	_SX_ASSERT_SLOCKED(sx)
94#define	_SX_ASSERT_XLOCKED(sx)
95#endif	/* INVARIANTS */
96
97#endif	/* _KERNEL */
98#endif	/* !LOCORE */
99#endif	/* _SYS_SX_H_ */
100