xref: /freebsd/sys/kern/kern_sx.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
1 /*
2  * Copyright (C) 2001 Jason Evans <jasone@freebsd.org>.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice(s), this list of conditions and the following disclaimer as
9  *    the first lines of this file unmodified other than the possible
10  *    addition of one or more copyright notices.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice(s), this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25  * DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 /*
31  * Shared/exclusive locks.  This implementation assures deterministic lock
32  * granting behavior, so that slocks and xlocks are interleaved.
33  *
34  * Priority propagation will not generally raise the priority of lock holders,
35  * so should not be relied upon in combination with sx locks.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ktr.h>
41 #include <sys/condvar.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/sx.h>
45 
46 struct lock_class lock_class_sx = {
47 	"sx",
48 	LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE
49 };
50 
51 void
52 sx_init(struct sx *sx, const char *description)
53 {
54 	struct lock_object *lock;
55 
56 	bzero(sx, sizeof(*sx));
57 	lock = &sx->sx_object;
58 	lock->lo_class = &lock_class_sx;
59 	lock->lo_name = description;
60 	lock->lo_flags = LO_WITNESS | LO_RECURSABLE | LO_SLEEPABLE;
61 	mtx_init(&sx->sx_lock, "sx backing lock",
62 	    MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
63 	sx->sx_cnt = 0;
64 	cv_init(&sx->sx_shrd_cv, description);
65 	sx->sx_shrd_wcnt = 0;
66 	cv_init(&sx->sx_excl_cv, description);
67 	sx->sx_excl_wcnt = 0;
68 	sx->sx_xholder = NULL;
69 
70 	LOCK_LOG_INIT(lock, 0);
71 
72 	WITNESS_INIT(lock);
73 }
74 
75 void
76 sx_destroy(struct sx *sx)
77 {
78 
79 	LOCK_LOG_DESTROY(&sx->sx_object, 0);
80 
81 	KASSERT((sx->sx_cnt == 0 && sx->sx_shrd_wcnt == 0 && sx->sx_excl_wcnt ==
82 	    0), ("%s (%s): holders or waiters\n", __FUNCTION__,
83 	    sx->sx_object.lo_name));
84 
85 	mtx_destroy(&sx->sx_lock);
86 	cv_destroy(&sx->sx_shrd_cv);
87 	cv_destroy(&sx->sx_excl_cv);
88 
89 	WITNESS_DESTROY(&sx->sx_object);
90 }
91 
92 void
93 _sx_slock(struct sx *sx, const char *file, int line)
94 {
95 
96 	mtx_lock(&sx->sx_lock);
97 	KASSERT(sx->sx_xholder != curproc,
98 	    ("%s (%s): trying to get slock while xlock is held\n", __FUNCTION__,
99 	    sx->sx_object.lo_name));
100 
101 	/*
102 	 * Loop in case we lose the race for lock acquisition.
103 	 */
104 	while (sx->sx_cnt < 0) {
105 		sx->sx_shrd_wcnt++;
106 		cv_wait(&sx->sx_shrd_cv, &sx->sx_lock);
107 		sx->sx_shrd_wcnt--;
108 	}
109 
110 	/* Acquire a shared lock. */
111 	sx->sx_cnt++;
112 
113 	LOCK_LOG_LOCK("SLOCK", &sx->sx_object, 0, 0, file, line);
114 	WITNESS_LOCK(&sx->sx_object, 0, file, line);
115 
116 	mtx_unlock(&sx->sx_lock);
117 }
118 
119 void
120 _sx_xlock(struct sx *sx, const char *file, int line)
121 {
122 
123 	mtx_lock(&sx->sx_lock);
124 
125 	/*
126 	 * With sx locks, we're absolutely not permitted to recurse on
127 	 * xlocks, as it is fatal (deadlock). Normally, recursion is handled
128 	 * by WITNESS, but as it is not semantically correct to hold the
129 	 * xlock while in here, we consider it API abuse and put it under
130 	 * INVARIANTS.
131 	 */
132 	KASSERT(sx->sx_xholder != curproc,
133 	    ("%s (%s): xlock already held @ %s:%d", __FUNCTION__,
134 	    sx->sx_object.lo_name, file, line));
135 
136 	/* Loop in case we lose the race for lock acquisition. */
137 	while (sx->sx_cnt != 0) {
138 		sx->sx_excl_wcnt++;
139 		cv_wait(&sx->sx_excl_cv, &sx->sx_lock);
140 		sx->sx_excl_wcnt--;
141 	}
142 
143 	MPASS(sx->sx_cnt == 0);
144 
145 	/* Acquire an exclusive lock. */
146 	sx->sx_cnt--;
147 	sx->sx_xholder = curproc;
148 
149 	LOCK_LOG_LOCK("XLOCK", &sx->sx_object, 0, 0, file, line);
150 	WITNESS_LOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
151 
152 	mtx_unlock(&sx->sx_lock);
153 }
154 
155 void
156 _sx_sunlock(struct sx *sx, const char *file, int line)
157 {
158 
159 	mtx_lock(&sx->sx_lock);
160 	_SX_ASSERT_SLOCKED(sx);
161 
162 	WITNESS_UNLOCK(&sx->sx_object, 0, file, line);
163 
164 	/* Release. */
165 	sx->sx_cnt--;
166 
167 	/*
168 	 * If we just released the last shared lock, wake any waiters up, giving
169 	 * exclusive lockers precedence.  In order to make sure that exclusive
170 	 * lockers won't be blocked forever, don't wake shared lock waiters if
171 	 * there are exclusive lock waiters.
172 	 */
173 	if (sx->sx_excl_wcnt > 0) {
174 		if (sx->sx_cnt == 0)
175 			cv_signal(&sx->sx_excl_cv);
176 	} else if (sx->sx_shrd_wcnt > 0)
177 		cv_broadcast(&sx->sx_shrd_cv);
178 
179 	LOCK_LOG_LOCK("SUNLOCK", &sx->sx_object, 0, 0, file, line);
180 
181 	mtx_unlock(&sx->sx_lock);
182 }
183 
184 void
185 _sx_xunlock(struct sx *sx, const char *file, int line)
186 {
187 
188 	mtx_lock(&sx->sx_lock);
189 	_SX_ASSERT_XLOCKED(sx);
190 	MPASS(sx->sx_cnt == -1);
191 
192 	WITNESS_UNLOCK(&sx->sx_object, LOP_EXCLUSIVE, file, line);
193 
194 	/* Release. */
195 	sx->sx_cnt++;
196 	sx->sx_xholder = NULL;
197 
198 	/*
199 	 * Wake up waiters if there are any.  Give precedence to slock waiters.
200 	 */
201 	if (sx->sx_shrd_wcnt > 0)
202 		cv_broadcast(&sx->sx_shrd_cv);
203 	else if (sx->sx_excl_wcnt > 0)
204 		cv_signal(&sx->sx_excl_cv);
205 
206 	LOCK_LOG_LOCK("XUNLOCK", &sx->sx_object, 0, 0, file, line);
207 
208 	mtx_unlock(&sx->sx_lock);
209 }
210