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: stable/9/sys/sys/sx.h 230799 2012-01-31 01:45:20Z attilio $
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 <sys/pcpu.h>
40 #include <sys/lock_profile.h>
41 #include <sys/lockstat.h>
42 #include <machine/atomic.h>
43 #endif
44 
45 /*
46  * In general, the sx locks and rwlocks use very similar algorithms.
47  * The main difference in the implementations is how threads are
48  * blocked when a lock is unavailable.  For this, sx locks use sleep
49  * queues which do not support priority propagation, and rwlocks use
50  * turnstiles which do.
51  *
52  * The sx_lock field consists of several fields.  The low bit
53  * indicates if the lock is locked with a shared or exclusive lock.  A
54  * value of 0 indicates an exclusive lock, and a value of 1 indicates
55  * a shared lock.  Bit 1 is a boolean indicating if there are any
56  * threads waiting for a shared lock.  Bit 2 is a boolean indicating
57  * if there are any threads waiting for an exclusive lock.  Bit 3 is a
58  * boolean indicating if an exclusive lock is recursively held.  The
59  * rest of the variable's definition is dependent on the value of the
60  * first bit.  For an exclusive lock, it is a pointer to the thread
61  * holding the lock, similar to the mtx_lock field of mutexes.  For
62  * shared locks, it is a count of read locks that are held.
63  *
64  * When the lock is not locked by any thread, it is encoded as a
65  * shared lock with zero waiters.
66  */
67 
68 #define	SX_LOCK_SHARED			0x01
69 #define	SX_LOCK_SHARED_WAITERS		0x02
70 #define	SX_LOCK_EXCLUSIVE_WAITERS	0x04
71 #define	SX_LOCK_RECURSED		0x08
72 #define	SX_LOCK_FLAGMASK						\
73 	(SX_LOCK_SHARED | SX_LOCK_SHARED_WAITERS |			\
74 	SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_RECURSED)
75 
76 #define	SX_OWNER(x)			((x) & ~SX_LOCK_FLAGMASK)
77 #define	SX_SHARERS_SHIFT		4
78 #define	SX_SHARERS(x)			(SX_OWNER(x) >> SX_SHARERS_SHIFT)
79 #define	SX_SHARERS_LOCK(x)						\
80 	((x) << SX_SHARERS_SHIFT | SX_LOCK_SHARED)
81 #define	SX_ONE_SHARER			(1 << SX_SHARERS_SHIFT)
82 
83 #define	SX_LOCK_UNLOCKED		SX_SHARERS_LOCK(0)
84 #define	SX_LOCK_DESTROYED						\
85 	(SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)
86 
87 #ifdef _KERNEL
88 
89 /*
90  * Function prototipes.  Routines that start with an underscore are not part
91  * of the public interface and are wrappered with a macro.
92  */
93 void	sx_sysinit(void *arg);
94 #define	sx_init(sx, desc)	sx_init_flags((sx), (desc), 0)
95 void	sx_init_flags(struct sx *sx, const char *description, int opts);
96 void	sx_destroy(struct sx *sx);
97 int	_sx_slock(struct sx *sx, int opts, const char *file, int line);
98 int	_sx_xlock(struct sx *sx, int opts, const char *file, int line);
99 int	_sx_try_slock(struct sx *sx, const char *file, int line);
100 int	_sx_try_xlock(struct sx *sx, const char *file, int line);
101 void	_sx_sunlock(struct sx *sx, const char *file, int line);
102 void	_sx_xunlock(struct sx *sx, const char *file, int line);
103 int	_sx_try_upgrade(struct sx *sx, const char *file, int line);
104 void	_sx_downgrade(struct sx *sx, const char *file, int line);
105 int	_sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts,
106 	    const char *file, int line);
107 int	_sx_slock_hard(struct sx *sx, int opts, const char *file, int line);
108 void	_sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int
109 	    line);
110 void	_sx_sunlock_hard(struct sx *sx, const char *file, int line);
111 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
112 void	_sx_assert(struct sx *sx, int what, const char *file, int line);
113 #endif
114 #ifdef DDB
115 int	sx_chain(struct thread *td, struct thread **ownerp);
116 #endif
117 
118 #define	sx_downgrade_(sx, file, line)					\
119 	_sx_downgrade((sx), (file), (line))
120 #define	sx_try_slock_(sx, file, line)					\
121 	_sx_try_slock((sx), (file), (line))
122 #define	sx_try_xlock_(sx, file, line)					\
123 	_sx_try_xlock((sx), (file), (line))
124 #define	sx_try_upgrade_(sx, file, line)					\
125 	_sx_try_upgrade((sx), (file), (line))
126 
127 struct sx_args {
128 	struct sx 	*sa_sx;
129 	const char	*sa_desc;
130 	int		sa_flags;
131 };
132 
133 #define	SX_SYSINIT_FLAGS(name, sxa, desc, flags)			\
134 	static struct sx_args name##_args = {				\
135 		(sxa),							\
136 		(desc),							\
137 		(flags)							\
138 	};								\
139 	SYSINIT(name##_sx_sysinit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
140 	    sx_sysinit, &name##_args);					\
141 	SYSUNINIT(name##_sx_sysuninit, SI_SUB_LOCK, SI_ORDER_MIDDLE,	\
142 	    sx_destroy, (sxa))
143 
144 #define	SX_SYSINIT(name, sxa, desc)	SX_SYSINIT_FLAGS(name, sxa, desc, 0)
145 
146 /*
147  * Full lock operations that are suitable to be inlined in non-debug kernels.
148  * If the lock can't be acquired or released trivially then the work is
149  * deferred to 'tougher' functions.
150  */
151 
152 /* Acquire an exclusive lock. */
153 static __inline int
__sx_xlock(struct sx * sx,struct thread * td,int opts,const char * file,int line)154 __sx_xlock(struct sx *sx, struct thread *td, int opts, const char *file,
155     int line)
156 {
157 	uintptr_t tid = (uintptr_t)td;
158 	int error = 0;
159 
160 	if (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid))
161 		error = _sx_xlock_hard(sx, tid, opts, file, line);
162 	else
163 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE,
164 		    sx, 0, 0, file, line);
165 
166 	return (error);
167 }
168 
169 /* Release an exclusive lock. */
170 static __inline void
__sx_xunlock(struct sx * sx,struct thread * td,const char * file,int line)171 __sx_xunlock(struct sx *sx, struct thread *td, const char *file, int line)
172 {
173 	uintptr_t tid = (uintptr_t)td;
174 
175 	if (!atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED))
176 		_sx_xunlock_hard(sx, tid, file, line);
177 }
178 
179 /* Acquire a shared lock. */
180 static __inline int
__sx_slock(struct sx * sx,int opts,const char * file,int line)181 __sx_slock(struct sx *sx, int opts, const char *file, int line)
182 {
183 	uintptr_t x = sx->sx_lock;
184 	int error = 0;
185 
186 	if (!(x & SX_LOCK_SHARED) ||
187 	    !atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER))
188 		error = _sx_slock_hard(sx, opts, file, line);
189 	else
190 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx, 0,
191 		    0, file, line);
192 
193 	return (error);
194 }
195 
196 /*
197  * Release a shared lock.  We can just drop a single shared lock so
198  * long as we aren't trying to drop the last shared lock when other
199  * threads are waiting for an exclusive lock.  This takes advantage of
200  * the fact that an unlocked lock is encoded as a shared lock with a
201  * count of 0.
202  */
203 static __inline void
__sx_sunlock(struct sx * sx,const char * file,int line)204 __sx_sunlock(struct sx *sx, const char *file, int line)
205 {
206 	uintptr_t x = sx->sx_lock;
207 
208 	if (x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS) ||
209 	    !atomic_cmpset_rel_ptr(&sx->sx_lock, x, x - SX_ONE_SHARER))
210 		_sx_sunlock_hard(sx, file, line);
211 }
212 
213 /*
214  * Public interface for lock operations.
215  */
216 #ifndef LOCK_DEBUG
217 #error	"LOCK_DEBUG not defined, include <sys/lock.h> before <sys/sx.h>"
218 #endif
219 #if	(LOCK_DEBUG > 0) || defined(SX_NOINLINE)
220 #define	sx_xlock_(sx, file, line)					\
221 	(void)_sx_xlock((sx), 0, (file), (line))
222 #define	sx_xlock_sig_(sx, file, line)					\
223 	_sx_xlock((sx), SX_INTERRUPTIBLE, (file), (line))
224 #define	sx_xunlock_(sx, file, line)					\
225 	_sx_xunlock((sx), (file), (line))
226 #define	sx_slock_(sx, file, line)					\
227 	(void)_sx_slock((sx), 0, (file), (line))
228 #define	sx_slock_sig_(sx, file, line)					\
229 	_sx_slock((sx), SX_INTERRUPTIBLE, (file) , (line))
230 #define	sx_sunlock_(sx, file, line)					\
231 	_sx_sunlock((sx), (file), (line))
232 #else
233 #define	sx_xlock_(sx, file, line)					\
234 	(void)__sx_xlock((sx), curthread, 0, (file), (line))
235 #define	sx_xlock_sig_(sx, file, line)					\
236 	__sx_xlock((sx), curthread, SX_INTERRUPTIBLE, (file), (line))
237 #define	sx_xunlock_(sx, file, line)					\
238 	__sx_xunlock((sx), curthread, (file), (line))
239 #define	sx_slock_(sx, file, line)					\
240 	(void)__sx_slock((sx), 0, (file), (line))
241 #define	sx_slock_sig_(sx, file, line)					\
242 	__sx_slock((sx), SX_INTERRUPTIBLE, (file), (line))
243 #define	sx_sunlock_(sx, file, line)					\
244 	__sx_sunlock((sx), (file), (line))
245 #endif	/* LOCK_DEBUG > 0 || SX_NOINLINE */
246 #define	sx_try_slock(sx)	sx_try_slock_((sx), LOCK_FILE, LOCK_LINE)
247 #define	sx_try_xlock(sx)	sx_try_xlock_((sx), LOCK_FILE, LOCK_LINE)
248 #define	sx_try_upgrade(sx)	sx_try_upgrade_((sx), LOCK_FILE, LOCK_LINE)
249 #define	sx_downgrade(sx)	sx_downgrade_((sx), LOCK_FILE, LOCK_LINE)
250 #ifdef INVARIANTS
251 #define	sx_assert_(sx, what, file, line)				\
252 	_sx_assert((sx), (what), (file), (line))
253 #else
254 #define	sx_assert_(sx, what, file, line)	(void)0
255 #endif
256 
257 #define	sx_xlock(sx)		sx_xlock_((sx), LOCK_FILE, LOCK_LINE)
258 #define	sx_xlock_sig(sx)	sx_xlock_sig_((sx), LOCK_FILE, LOCK_LINE)
259 #define	sx_xunlock(sx)		sx_xunlock_((sx), LOCK_FILE, LOCK_LINE)
260 #define	sx_slock(sx)		sx_slock_((sx), LOCK_FILE, LOCK_LINE)
261 #define	sx_slock_sig(sx)	sx_slock_sig_((sx), LOCK_FILE, LOCK_LINE)
262 #define	sx_sunlock(sx)		sx_sunlock_((sx), LOCK_FILE, LOCK_LINE)
263 #define	sx_assert(sx, what)	sx_assert_((sx), (what), __FILE__, __LINE__)
264 
265 /*
266  * Return a pointer to the owning thread if the lock is exclusively
267  * locked.
268  */
269 #define	sx_xholder(sx)							\
270 	((sx)->sx_lock & SX_LOCK_SHARED ? NULL :			\
271 	(struct thread *)SX_OWNER((sx)->sx_lock))
272 
273 #define	sx_xlocked(sx)							\
274 	(((sx)->sx_lock & ~(SX_LOCK_FLAGMASK & ~SX_LOCK_SHARED)) ==	\
275 	    (uintptr_t)curthread)
276 
277 #define	sx_unlock_(sx, file, line) do {					\
278 	if (sx_xlocked(sx))						\
279 		sx_xunlock_(sx, file, line);				\
280 	else								\
281 		sx_sunlock_(sx, file, line);				\
282 } while (0)
283 
284 #define	sx_unlock(sx)	sx_unlock_((sx), LOCK_FILE, LOCK_LINE)
285 
286 #define	sx_sleep(chan, sx, pri, wmesg, timo)				\
287 	_sleep((chan), &(sx)->lock_object, (pri), (wmesg), (timo))
288 
289 /*
290  * Options passed to sx_init_flags().
291  */
292 #define	SX_DUPOK		0x01
293 #define	SX_NOPROFILE		0x02
294 #define	SX_NOWITNESS		0x04
295 #define	SX_QUIET		0x08
296 #define	SX_NOADAPTIVE		0x10
297 #define	SX_RECURSE		0x20
298 
299 /*
300  * Options passed to sx_*lock_hard().
301  */
302 #define	SX_INTERRUPTIBLE	0x40
303 
304 #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT)
305 #define	SA_LOCKED		LA_LOCKED
306 #define	SA_SLOCKED		LA_SLOCKED
307 #define	SA_XLOCKED		LA_XLOCKED
308 #define	SA_UNLOCKED		LA_UNLOCKED
309 #define	SA_RECURSED		LA_RECURSED
310 #define	SA_NOTRECURSED		LA_NOTRECURSED
311 
312 /* Backwards compatability. */
313 #define	SX_LOCKED		LA_LOCKED
314 #define	SX_SLOCKED		LA_SLOCKED
315 #define	SX_XLOCKED		LA_XLOCKED
316 #define	SX_UNLOCKED		LA_UNLOCKED
317 #define	SX_RECURSED		LA_RECURSED
318 #define	SX_NOTRECURSED		LA_NOTRECURSED
319 #endif
320 
321 #endif /* _KERNEL */
322 
323 #endif /* !_SYS_SX_H_ */
324