sx.h revision 168191
1139825Simp/*-
2168191Sjhb * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
3168191Sjhb * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
4168191Sjhb * All rights reserved.
573782Sjasone *
673782Sjasone * Redistribution and use in source and binary forms, with or without
773782Sjasone * modification, are permitted provided that the following conditions
873782Sjasone * are met:
973782Sjasone * 1. Redistributions of source code must retain the above copyright
1073782Sjasone *    notice(s), this list of conditions and the following disclaimer as
1173782Sjasone *    the first lines of this file unmodified other than the possible
1273782Sjasone *    addition of one or more copyright notices.
1373782Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1473782Sjasone *    notice(s), this list of conditions and the following disclaimer in the
1573782Sjasone *    documentation and/or other materials provided with the distribution.
1673782Sjasone *
1773782Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
1873782Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1973782Sjasone * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2073782Sjasone * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
2173782Sjasone * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2273782Sjasone * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2373782Sjasone * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2473782Sjasone * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2573782Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2673782Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
2773782Sjasone * DAMAGE.
2873782Sjasone *
2973782Sjasone * $FreeBSD: head/sys/sys/sx.h 168191 2007-03-31 23:23:42Z jhb $
3073782Sjasone */
3173782Sjasone
3273782Sjasone#ifndef	_SYS_SX_H_
3373782Sjasone#define	_SYS_SX_H_
3473782Sjasone
3583103Sjhb#include <sys/_lock.h>
36168191Sjhb#include <sys/_sx.h>
3773782Sjasone
38168191Sjhb#ifdef	_KERNEL
39168191Sjhb#include <machine/atomic.h>
40168191Sjhb#endif
4173782Sjasone
42168191Sjhb/*
43168191Sjhb * In general, the sx locks and rwlocks use very similar algorithms.
44168191Sjhb * The main difference in the implementations is how threads are
45168191Sjhb * blocked when a lock is unavailable.  For this, sx locks use sleep
46168191Sjhb * queues which do not support priority propagation, and rwlocks use
47168191Sjhb * turnstiles which do.
48168191Sjhb *
49168191Sjhb * The sx_lock field consists of several fields.  The low bit
50168191Sjhb * indicates if the lock is locked with a shared or exclusive lock.  A
51168191Sjhb * value of 0 indicates an exclusive lock, and a value of 1 indicates
52168191Sjhb * a shared lock.  Bit 1 is a boolean indicating if there are any
53168191Sjhb * threads waiting for a shared lock.  Bit 2 is a boolean indicating
54168191Sjhb * if there are any threads waiting for an exclusive lock.  Bit 3 is a
55168191Sjhb * boolean indicating if an exclusive lock is recursively held.  The
56168191Sjhb * rest of the variable's definition is dependent on the value of the
57168191Sjhb * first bit.  For an exclusive lock, it is a pointer to the thread
58168191Sjhb * holding the lock, similar to the mtx_lock field of mutexes.  For
59168191Sjhb * shared locks, it is a count of read locks that are held.
60168191Sjhb *
61168191Sjhb * When the lock is not locked by any thread, it is encoded as a
62168191Sjhb * shared lock with zero waiters.
63168191Sjhb *
64168191Sjhb * A note about memory barriers.  Exclusive locks need to use the same
65168191Sjhb * memory barriers as mutexes: _acq when acquiring an exclusive lock
66168191Sjhb * and _rel when releasing an exclusive lock.  On the other side,
67168191Sjhb * shared lock needs to use an _acq barrier when acquiring the lock
68168191Sjhb * but, since they don't update any locked data, no memory barrier is
69168191Sjhb * needed when releasing a shared lock.
70168191Sjhb */
71168191Sjhb
72168191Sjhb#define	SX_LOCK_SHARED			0x01
73168191Sjhb#define	SX_LOCK_SHARED_WAITERS		0x02
74168191Sjhb#define	SX_LOCK_EXCLUSIVE_WAITERS	0x04
75168191Sjhb#define	SX_LOCK_RECURSED		0x08
76168191Sjhb#define	SX_LOCK_FLAGMASK						\
77168191Sjhb	(SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS |			\
78168191Sjhb	SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED)
79168191Sjhb
80168191Sjhb#define	SX_OWNER(x)			((x) & ~SX_LOCK_FLAGMASK)
81168191Sjhb#define	SX_SHARERS_SHIFT		4
82168191Sjhb#define	SX_SHARERS(x)			(SX_OWNER(x) >> SX_SHARERS_SHIFT)
83168191Sjhb#define	SX_SHARERS_LOCK(x)						\
84168191Sjhb	((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED)
85168191Sjhb#define	SX_ONE_SHARER			(1 << SX_SHARERS_SHIFT)
86168191Sjhb
87168191Sjhb#define	SX_LOCK_UNLOCKED		SX_SHARERS_LOCK(0)
88168191Sjhb
8973782Sjasone#ifdef _KERNEL
90168191Sjhb
91168191Sjhb/*
92168191Sjhb * Full lock operations that are suitable to be inlined in non-debug kernels.
93168191Sjhb * If the lock can't be acquired or released trivially then the work is
94168191Sjhb * deferred to 'tougher' functions.
95168191Sjhb */
96168191Sjhb
97168191Sjhb/* Acquire an exclusive lock. */
98168191Sjhb#define	__sx_xlock(sx, tid, file, line) do {				\
99168191Sjhb	uintptr_t _tid = (uintptr_t)(tid);				\
100168191Sjhb	int contested = 0;                                              \
101168191Sjhb        uint64_t waitstart = 0;                                         \
102168191Sjhb									\
103168191Sjhb	if (!atomic_cmpset_acq_ptr(&(sx)->sx_lock, SX_LOCK_UNLOCKED,	\
104168191Sjhb	    _tid)) {							\
105168191Sjhb		lock_profile_obtain_lock_failed(&(sx)->lock_object,	\
106168191Sjhb		    &contested, &waitstart);				\
107168191Sjhb		_sx_xlock_hard((sx), _tid, (file), (line));		\
108168191Sjhb	}								\
109168191Sjhb	lock_profile_obtain_lock_success(&(sx)->lock_object, contested,	\
110168191Sjhb	    waitstart, (file), (line));					\
111168191Sjhb} while (0)
112168191Sjhb
113168191Sjhb/* Release an exclusive lock. */
114168191Sjhb#define	__sx_xunlock(sx, tid, file, line) do {				\
115168191Sjhb	uintptr_t _tid = (uintptr_t)(tid);				\
116168191Sjhb									\
117168191Sjhb	if (!atomic_cmpset_rel_ptr(&(sx)->sx_lock, _tid,		\
118168191Sjhb	    SX_LOCK_UNLOCKED))						\
119168191Sjhb		_sx_xunlock_hard((sx), _tid, (file), (line));		\
120168191Sjhb} while (0)
121168191Sjhb
122168191Sjhb/* Acquire a shared lock. */
123168191Sjhb#define	__sx_slock(sx, file, line) do {					\
124168191Sjhb	uintptr_t x = (sx)->sx_lock;					\
125168191Sjhb	int contested = 0;                                              \
126168191Sjhb        uint64_t waitstart = 0;                                         \
127168191Sjhb									\
128168191Sjhb	if (!(x & SX_LOCK_SHARED) ||					\
129168191Sjhb	    !atomic_cmpset_acq_ptr(&(sx)->sx_lock, x,			\
130168191Sjhb	    x + SX_ONE_SHARER)) {					\
131168191Sjhb		lock_profile_obtain_lock_failed(&(sx)->lock_object,	\
132168191Sjhb		    &contested, &waitstart);				\
133168191Sjhb		_sx_slock_hard((sx), (file), (line));			\
134168191Sjhb	}								\
135168191Sjhb	lock_profile_obtain_lock_success(&(sx)->lock_object, contested,	\
136168191Sjhb	    waitstart, (file), (line));					\
137168191Sjhb} while (0)
138168191Sjhb
139168191Sjhb/*
140168191Sjhb * Release a shared lock.  We can just drop a single shared lock so
141168191Sjhb * long as we aren't trying to drop the last shared lock when other
142168191Sjhb * threads are waiting for an exclusive lock.  This takes advantage of
143168191Sjhb * the fact that an unlocked lock is encoded as a shared lock with a
144168191Sjhb * count of 0.
145168191Sjhb */
146168191Sjhb#define	__sx_sunlock(sx, file, line) do {				\
147168191Sjhb	uintptr_t x = (sx)->sx_lock;					\
148168191Sjhb									\
149168191Sjhb	if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) ||	\
150168191Sjhb	    !atomic_cmpset_ptr(&(sx)->sx_lock, x, x - SX_ONE_SHARER))	\
151168191Sjhb		_sx_sunlock_hard((sx), (file), (line));			\
152168191Sjhb} while (0)
153168191Sjhb
154168191Sjhb/*
155168191Sjhb * Function prototipes.  Routines that start with an underscore are not part
156168191Sjhb * of the public interface and are wrappered with a macro.
157168191Sjhb */
15893672Sarrvoid	sx_sysinit(void *arg);
159168191Sjhb#define	sx_init(sx, desc)	sx_init_flags((sx), (desc), 0)
160168191Sjhbvoid	sx_init_flags(struct sx *sx, const char *description, int opts);
16173782Sjasonevoid	sx_destroy(struct sx *sx);
16274912Sjhbvoid	_sx_slock(struct sx *sx, const char *file, int line);
16374912Sjhbvoid	_sx_xlock(struct sx *sx, const char *file, int line);
16478872Sjhbint	_sx_try_slock(struct sx *sx, const char *file, int line);
16578872Sjhbint	_sx_try_xlock(struct sx *sx, const char *file, int line);
16674912Sjhbvoid	_sx_sunlock(struct sx *sx, const char *file, int line);
16774912Sjhbvoid	_sx_xunlock(struct sx *sx, const char *file, int line);
16881599Sjasoneint	_sx_try_upgrade(struct sx *sx, const char *file, int line);
16981599Sjasonevoid	_sx_downgrade(struct sx *sx, const char *file, int line);
170168191Sjhbvoid	_sx_xlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
171168191Sjhb	    line);
172168191Sjhbvoid	_sx_slock_hard(struct sx *sx, const char *file, int line);
173168191Sjhbvoid	_sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
174168191Sjhb	    line);
175168191Sjhbvoid	_sx_sunlock_hard(struct sx *sx, const char *file, int line);
176161721Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
17785388Sjhbvoid	_sx_assert(struct sx *sx, int what, const char *file, int line);
17893688Sarr#endif
179161337Sjhb#ifdef DDB
180161337Sjhbint	sx_chain(struct thread *td, struct thread **ownerp);
181161337Sjhb#endif
18293672Sarr
18393672Sarrstruct sx_args {
18493672Sarr	struct sx 	*sa_sx;
18593672Sarr	const char	*sa_desc;
18693672Sarr};
18793672Sarr
18893672Sarr#define	SX_SYSINIT(name, sxa, desc)					\
18993672Sarr	static struct sx_args name##_args = {				\
190149739Sjhb		(sxa),							\
191149739Sjhb		(desc)							\
19293672Sarr	};								\
19393672Sarr	SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
194149739Sjhb	    sx_sysinit, &name##_args);					\
195149739Sjhb	SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
196149739Sjhb	    sx_destroy, (sxa))
19773782Sjasone
198168191Sjhb/*
199168191Sjhb * Public interface for lock operations.
200168191Sjhb */
201168191Sjhb#ifndef LOCK_DEBUG
202168191Sjhb#error	"LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>"
203168191Sjhb#endif
204168191Sjhb#if	(LOCK_DEBUG > 0) || defined(SX_NOINLINE)
205168191Sjhb#define	sx_xlock(sx)		_sx_xlock((sx), LOCK_FILE, LOCK_LINE)
206168191Sjhb#define	sx_xunlock(sx)		_sx_xunlock((sx), LOCK_FILE, LOCK_LINE)
20783593Sjhb#define	sx_slock(sx)		_sx_slock((sx), LOCK_FILE, LOCK_LINE)
208168191Sjhb#define	sx_sunlock(sx)		_sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
209168191Sjhb#else
210168191Sjhb#define	sx_xlock(sx)							\
211168191Sjhb	__sx_xlock((sx), curthread, LOCK_FILE, LOCK_LINE)
212168191Sjhb#define	sx_xunlock(sx)							\
213168191Sjhb	__sx_xunlock((sx), curthread, LOCK_FILE, LOCK_LINE)
214168191Sjhb#define	sx_slock(sx)		__sx_slock((sx), LOCK_FILE, LOCK_LINE)
215168191Sjhb#define	sx_sunlock(sx)		__sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
216168191Sjhb#endif	/* LOCK_DEBUG > 0 || SX_NOINLINE */
21783593Sjhb#define	sx_try_slock(sx)	_sx_try_slock((sx), LOCK_FILE, LOCK_LINE)
21883593Sjhb#define	sx_try_xlock(sx)	_sx_try_xlock((sx), LOCK_FILE, LOCK_LINE)
21983593Sjhb#define	sx_try_upgrade(sx)	_sx_try_upgrade((sx), LOCK_FILE, LOCK_LINE)
22083593Sjhb#define	sx_downgrade(sx)	_sx_downgrade((sx), LOCK_FILE, LOCK_LINE)
221168191Sjhb
222168191Sjhb#define	sx_xlocked(sx)							\
223168191Sjhb	(((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) ==	\
224168191Sjhb	    (uintptr_t)curthread)
225168191Sjhb
226157296Sjhb#define	sx_unlock(sx) do {						\
227159844Sjhb	if (sx_xlocked(sx))						\
228157296Sjhb		sx_xunlock(sx);						\
229157296Sjhb	else								\
230157296Sjhb		sx_sunlock(sx);						\
231157296Sjhb} while (0)
232168191Sjhb
233167387Sjhb#define	sx_sleep(chan, sx, pri, wmesg, timo)				\
234167787Sjhb	_sleep((chan), &(sx)->lock_object, (pri), (wmesg), (timo))
23574912Sjhb
236168191Sjhb/*
237168191Sjhb * Options passed to sx_init_flags().
238168191Sjhb */
239168191Sjhb#define	SX_DUPOK		0x01
240168191Sjhb#define	SX_NOPROFILE		0x02
241168191Sjhb#define	SX_NOWITNESS		0x04
242168191Sjhb#define	SX_QUIET		0x08
243168191Sjhb#define	SX_ADAPTIVESPIN		0x10
244168191Sjhb
245168191Sjhb/*
246168191Sjhb * XXX: These options should be renamed as SA_*
247168191Sjhb */
24885412Sjhb#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
24985388Sjhb#define	SX_LOCKED		LA_LOCKED
25085388Sjhb#define	SX_SLOCKED		LA_SLOCKED
25185388Sjhb#define	SX_XLOCKED		LA_XLOCKED
252125419Spjd#define	SX_UNLOCKED		LA_UNLOCKED
253168191Sjhb#define	SX_RECURSED		LA_RECURSED
254168191Sjhb#define	SX_NOTRECURSED		LA_NOTRECURSED
255125444Sbde#endif
25673863Sbmilekic
25785412Sjhb#ifdef INVARIANTS
25885388Sjhb#define	sx_assert(sx, what)	_sx_assert((sx), (what), LOCK_FILE, LOCK_LINE)
259125444Sbde#else
26085388Sjhb#define	sx_assert(sx, what)
261125444Sbde#endif
26273782Sjasone
263125444Sbde#endif /* _KERNEL */
264125444Sbde
265125444Sbde#endif /* !_SYS_SX_H_ */
266