sx.h revision 168191
1/*-
2 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
3 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice(s), this list of conditions and the following disclaimer as
11 *    the first lines of this file unmodified other than the possible
12 *    addition of one or more copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice(s), this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27 * DAMAGE.
28 *
29 * $FreeBSD: head/sys/sys/sx.h 168191 2007-03-31 23:23:42Z jhb $
30 */
31
32#ifndef	_SYS_SX_H_
33#define	_SYS_SX_H_
34
35#include <sys/_lock.h>
36#include <sys/_sx.h>
37
38#ifdef	_KERNEL
39#include <machine/atomic.h>
40#endif
41
42/*
43 * In general, the sx locks and rwlocks use very similar algorithms.
44 * The main difference in the implementations is how threads are
45 * blocked when a lock is unavailable.  For this, sx locks use sleep
46 * queues which do not support priority propagation, and rwlocks use
47 * turnstiles which do.
48 *
49 * The sx_lock field consists of several fields.  The low bit
50 * indicates if the lock is locked with a shared or exclusive lock.  A
51 * value of 0 indicates an exclusive lock, and a value of 1 indicates
52 * a shared lock.  Bit 1 is a boolean indicating if there are any
53 * threads waiting for a shared lock.  Bit 2 is a boolean indicating
54 * if there are any threads waiting for an exclusive lock.  Bit 3 is a
55 * boolean indicating if an exclusive lock is recursively held.  The
56 * rest of the variable's definition is dependent on the value of the
57 * first bit.  For an exclusive lock, it is a pointer to the thread
58 * holding the lock, similar to the mtx_lock field of mutexes.  For
59 * shared locks, it is a count of read locks that are held.
60 *
61 * When the lock is not locked by any thread, it is encoded as a
62 * shared lock with zero waiters.
63 *
64 * A note about memory barriers.  Exclusive locks need to use the same
65 * memory barriers as mutexes: _acq when acquiring an exclusive lock
66 * and _rel when releasing an exclusive lock.  On the other side,
67 * shared lock needs to use an _acq barrier when acquiring the lock
68 * but, since they don't update any locked data, no memory barrier is
69 * needed when releasing a shared lock.
70 */
71
72#define	SX_LOCK_SHARED			0x01
73#define	SX_LOCK_SHARED_WAITERS		0x02
74#define	SX_LOCK_EXCLUSIVE_WAITERS	0x04
75#define	SX_LOCK_RECURSED		0x08
76#define	SX_LOCK_FLAGMASK						\
77	(SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS |			\
78	SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED)
79
80#define	SX_OWNER(x)			((x) & ~SX_LOCK_FLAGMASK)
81#define	SX_SHARERS_SHIFT		4
82#define	SX_SHARERS(x)			(SX_OWNER(x) >> SX_SHARERS_SHIFT)
83#define	SX_SHARERS_LOCK(x)						\
84	((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED)
85#define	SX_ONE_SHARER			(1 << SX_SHARERS_SHIFT)
86
87#define	SX_LOCK_UNLOCKED		SX_SHARERS_LOCK(0)
88
89#ifdef _KERNEL
90
91/*
92 * Full lock operations that are suitable to be inlined in non-debug kernels.
93 * If the lock can't be acquired or released trivially then the work is
94 * deferred to 'tougher' functions.
95 */
96
97/* Acquire an exclusive lock. */
98#define	__sx_xlock(sx, tid, file, line) do {				\
99	uintptr_t _tid = (uintptr_t)(tid);				\
100	int contested = 0;                                              \
101        uint64_t waitstart = 0;                                         \
102									\
103	if (!atomic_cmpset_acq_ptr(&(sx)->sx_lock, SX_LOCK_UNLOCKED,	\
104	    _tid)) {							\
105		lock_profile_obtain_lock_failed(&(sx)->lock_object,	\
106		    &contested, &waitstart);				\
107		_sx_xlock_hard((sx), _tid, (file), (line));		\
108	}								\
109	lock_profile_obtain_lock_success(&(sx)->lock_object, contested,	\
110	    waitstart, (file), (line));					\
111} while (0)
112
113/* Release an exclusive lock. */
114#define	__sx_xunlock(sx, tid, file, line) do {				\
115	uintptr_t _tid = (uintptr_t)(tid);				\
116									\
117	if (!atomic_cmpset_rel_ptr(&(sx)->sx_lock, _tid,		\
118	    SX_LOCK_UNLOCKED))						\
119		_sx_xunlock_hard((sx), _tid, (file), (line));		\
120} while (0)
121
122/* Acquire a shared lock. */
123#define	__sx_slock(sx, file, line) do {					\
124	uintptr_t x = (sx)->sx_lock;					\
125	int contested = 0;                                              \
126        uint64_t waitstart = 0;                                         \
127									\
128	if (!(x & SX_LOCK_SHARED) ||					\
129	    !atomic_cmpset_acq_ptr(&(sx)->sx_lock, x,			\
130	    x + SX_ONE_SHARER)) {					\
131		lock_profile_obtain_lock_failed(&(sx)->lock_object,	\
132		    &contested, &waitstart);				\
133		_sx_slock_hard((sx), (file), (line));			\
134	}								\
135	lock_profile_obtain_lock_success(&(sx)->lock_object, contested,	\
136	    waitstart, (file), (line));					\
137} while (0)
138
139/*
140 * Release a shared lock.  We can just drop a single shared lock so
141 * long as we aren't trying to drop the last shared lock when other
142 * threads are waiting for an exclusive lock.  This takes advantage of
143 * the fact that an unlocked lock is encoded as a shared lock with a
144 * count of 0.
145 */
146#define	__sx_sunlock(sx, file, line) do {				\
147	uintptr_t x = (sx)->sx_lock;					\
148									\
149	if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) ||	\
150	    !atomic_cmpset_ptr(&(sx)->sx_lock, x, x - SX_ONE_SHARER))	\
151		_sx_sunlock_hard((sx), (file), (line));			\
152} while (0)
153
154/*
155 * Function prototipes.  Routines that start with an underscore are not part
156 * of the public interface and are wrappered with a macro.
157 */
158void	sx_sysinit(void *arg);
159#define	sx_init(sx, desc)	sx_init_flags((sx), (desc), 0)
160void	sx_init_flags(struct sx *sx, const char *description, int opts);
161void	sx_destroy(struct sx *sx);
162void	_sx_slock(struct sx *sx, const char *file, int line);
163void	_sx_xlock(struct sx *sx, const char *file, int line);
164int	_sx_try_slock(struct sx *sx, const char *file, int line);
165int	_sx_try_xlock(struct sx *sx, const char *file, int line);
166void	_sx_sunlock(struct sx *sx, const char *file, int line);
167void	_sx_xunlock(struct sx *sx, const char *file, int line);
168int	_sx_try_upgrade(struct sx *sx, const char *file, int line);
169void	_sx_downgrade(struct sx *sx, const char *file, int line);
170void	_sx_xlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
171	    line);
172void	_sx_slock_hard(struct sx *sx, const char *file, int line);
173void	_sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
174	    line);
175void	_sx_sunlock_hard(struct sx *sx, const char *file, int line);
176#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
177void	_sx_assert(struct sx *sx, int what, const char *file, int line);
178#endif
179#ifdef DDB
180int	sx_chain(struct thread *td, struct thread **ownerp);
181#endif
182
183struct sx_args {
184	struct sx 	*sa_sx;
185	const char	*sa_desc;
186};
187
188#define	SX_SYSINIT(name, sxa, desc)					\
189	static struct sx_args name##_args = {				\
190		(sxa),							\
191		(desc)							\
192	};								\
193	SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
194	    sx_sysinit, &name##_args);					\
195	SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
196	    sx_destroy, (sxa))
197
198/*
199 * Public interface for lock operations.
200 */
201#ifndef LOCK_DEBUG
202#error	"LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>"
203#endif
204#if	(LOCK_DEBUG > 0) || defined(SX_NOINLINE)
205#define	sx_xlock(sx)		_sx_xlock((sx), LOCK_FILE, LOCK_LINE)
206#define	sx_xunlock(sx)		_sx_xunlock((sx), LOCK_FILE, LOCK_LINE)
207#define	sx_slock(sx)		_sx_slock((sx), LOCK_FILE, LOCK_LINE)
208#define	sx_sunlock(sx)		_sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
209#else
210#define	sx_xlock(sx)							\
211	__sx_xlock((sx), curthread, LOCK_FILE, LOCK_LINE)
212#define	sx_xunlock(sx)							\
213	__sx_xunlock((sx), curthread, LOCK_FILE, LOCK_LINE)
214#define	sx_slock(sx)		__sx_slock((sx), LOCK_FILE, LOCK_LINE)
215#define	sx_sunlock(sx)		__sx_sunlock((sx), LOCK_FILE, LOCK_LINE)
216#endif	/* LOCK_DEBUG > 0 || SX_NOINLINE */
217#define	sx_try_slock(sx)	_sx_try_slock((sx), LOCK_FILE, LOCK_LINE)
218#define	sx_try_xlock(sx)	_sx_try_xlock((sx), LOCK_FILE, LOCK_LINE)
219#define	sx_try_upgrade(sx)	_sx_try_upgrade((sx), LOCK_FILE, LOCK_LINE)
220#define	sx_downgrade(sx)	_sx_downgrade((sx), LOCK_FILE, LOCK_LINE)
221
222#define	sx_xlocked(sx)							\
223	(((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) ==	\
224	    (uintptr_t)curthread)
225
226#define	sx_unlock(sx) do {						\
227	if (sx_xlocked(sx))						\
228		sx_xunlock(sx);						\
229	else								\
230		sx_sunlock(sx);						\
231} while (0)
232
233#define	sx_sleep(chan, sx, pri, wmesg, timo)				\
234	_sleep((chan), &(sx)->lock_object, (pri), (wmesg), (timo))
235
236/*
237 * Options passed to sx_init_flags().
238 */
239#define	SX_DUPOK		0x01
240#define	SX_NOPROFILE		0x02
241#define	SX_NOWITNESS		0x04
242#define	SX_QUIET		0x08
243#define	SX_ADAPTIVESPIN		0x10
244
245/*
246 * XXX: These options should be renamed as SA_*
247 */
248#if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
249#define	SX_LOCKED		LA_LOCKED
250#define	SX_SLOCKED		LA_SLOCKED
251#define	SX_XLOCKED		LA_XLOCKED
252#define	SX_UNLOCKED		LA_UNLOCKED
253#define	SX_RECURSED		LA_RECURSED
254#define	SX_NOTRECURSED		LA_NOTRECURSED
255#endif
256
257#ifdef INVARIANTS
258#define	sx_assert(sx, what)	_sx_assert((sx), (what), LOCK_FILE, LOCK_LINE)
259#else
260#define	sx_assert(sx, what)
261#endif
262
263#endif /* _KERNEL */
264
265#endif /* !_SYS_SX_H_ */
266