xref: /freebsd/sys/kern/kern_condvar.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
1 /*-
2  * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ktrace.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/proc.h>
37 #include <sys/kernel.h>
38 #include <sys/ktr.h>
39 #include <sys/condvar.h>
40 #include <sys/sched.h>
41 #include <sys/signalvar.h>
42 #include <sys/sleepqueue.h>
43 #include <sys/resourcevar.h>
44 #ifdef KTRACE
45 #include <sys/uio.h>
46 #include <sys/ktrace.h>
47 #endif
48 
49 /*
50  * Common sanity checks for cv_wait* functions.
51  */
52 #define	CV_ASSERT(cvp, mp, td) do {					\
53 	KASSERT((td) != NULL, ("%s: curthread NULL", __func__));	\
54 	KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__));	\
55 	KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__));		\
56 	KASSERT((mp) != NULL, ("%s: mp NULL", __func__));		\
57 	mtx_assert((mp), MA_OWNED | MA_NOTRECURSED);			\
58 } while (0)
59 
60 /*
61  * Initialize a condition variable.  Must be called before use.
62  */
63 void
64 cv_init(struct cv *cvp, const char *desc)
65 {
66 
67 	cvp->cv_description = desc;
68 	cvp->cv_waiters = 0;
69 }
70 
71 /*
72  * Destroy a condition variable.  The condition variable must be re-initialized
73  * in order to be re-used.
74  */
75 void
76 cv_destroy(struct cv *cvp)
77 {
78 #ifdef INVARIANTS
79 	struct sleepqueue *sq;
80 
81 	sq = sleepq_lookup(cvp);
82 	sleepq_release(cvp);
83 	KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
84 #endif
85 }
86 
87 /*
88  * Wait on a condition variable.  The current thread is placed on the condition
89  * variable's wait queue and suspended.  A cv_signal or cv_broadcast on the same
90  * condition variable will resume the thread.  The mutex is released before
91  * sleeping and will be held on return.  It is recommended that the mutex be
92  * held when cv_signal or cv_broadcast are called.
93  */
94 void
95 cv_wait(struct cv *cvp, struct mtx *mp)
96 {
97 	struct sleepqueue *sq;
98 	struct thread *td;
99 	WITNESS_SAVE_DECL(mp);
100 
101 	td = curthread;
102 #ifdef KTRACE
103 	if (KTRPOINT(td, KTR_CSW))
104 		ktrcsw(1, 0);
105 #endif
106 	CV_ASSERT(cvp, mp, td);
107 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
108 	    "Waiting on \"%s\"", cvp->cv_description);
109 	WITNESS_SAVE(&mp->mtx_object, mp);
110 
111 	if (cold || panicstr) {
112 		/*
113 		 * During autoconfiguration, just give interrupts
114 		 * a chance, then just return.  Don't run any other
115 		 * thread or panic below, in case this is the idle
116 		 * process and already asleep.
117 		 */
118 		return;
119 	}
120 
121 	sq = sleepq_lookup(cvp);
122 
123 	cvp->cv_waiters++;
124 	DROP_GIANT();
125 	mtx_unlock(mp);
126 
127 	sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
128 	sleepq_wait(cvp);
129 
130 #ifdef KTRACE
131 	if (KTRPOINT(td, KTR_CSW))
132 		ktrcsw(0, 0);
133 #endif
134 	PICKUP_GIANT();
135 	mtx_lock(mp);
136 	WITNESS_RESTORE(&mp->mtx_object, mp);
137 }
138 
139 /*
140  * Wait on a condition variable, allowing interruption by signals.  Return 0 if
141  * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
142  * a signal was caught.  If ERESTART is returned the system call should be
143  * restarted if possible.
144  */
145 int
146 cv_wait_sig(struct cv *cvp, struct mtx *mp)
147 {
148 	struct sleepqueue *sq;
149 	struct thread *td;
150 	struct proc *p;
151 	int rval, sig;
152 	WITNESS_SAVE_DECL(mp);
153 
154 	td = curthread;
155 	p = td->td_proc;
156 	rval = 0;
157 #ifdef KTRACE
158 	if (KTRPOINT(td, KTR_CSW))
159 		ktrcsw(1, 0);
160 #endif
161 	CV_ASSERT(cvp, mp, td);
162 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
163 	    "Waiting on \"%s\"", cvp->cv_description);
164 	WITNESS_SAVE(&mp->mtx_object, mp);
165 
166 	if (cold || panicstr) {
167 		/*
168 		 * After a panic, or during autoconfiguration, just give
169 		 * interrupts a chance, then just return; don't run any other
170 		 * procs or panic below, in case this is the idle process and
171 		 * already asleep.
172 		 */
173 		return 0;
174 	}
175 
176 	sq = sleepq_lookup(cvp);
177 
178 	/* XXX: Missing the threading checks from msleep! */
179 
180 	cvp->cv_waiters++;
181 	DROP_GIANT();
182 	mtx_unlock(mp);
183 
184 	sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
185 	sig = sleepq_catch_signals(cvp);
186 	/*
187 	 * XXX: Missing magic return value handling for no signal
188 	 * caught but thread woken up during check.
189 	 */
190 	rval = sleepq_wait_sig(cvp);
191 	if (rval == 0)
192 		rval = sleepq_calc_signal_retval(sig);
193 
194 	/* XXX: Part of missing threading checks? */
195 	PROC_LOCK(p);
196 	if (p->p_flag & P_WEXIT)
197 		rval = EINTR;
198 	PROC_UNLOCK(p);
199 
200 #ifdef KTRACE
201 	if (KTRPOINT(td, KTR_CSW))
202 		ktrcsw(0, 0);
203 #endif
204 	PICKUP_GIANT();
205 	mtx_lock(mp);
206 	WITNESS_RESTORE(&mp->mtx_object, mp);
207 
208 	return (rval);
209 }
210 
211 /*
212  * Wait on a condition variable for at most timo/hz seconds.  Returns 0 if the
213  * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
214  * expires.
215  */
216 int
217 cv_timedwait(struct cv *cvp, struct mtx *mp, int timo)
218 {
219 	struct sleepqueue *sq;
220 	struct thread *td;
221 	int rval;
222 	WITNESS_SAVE_DECL(mp);
223 
224 	td = curthread;
225 	rval = 0;
226 #ifdef KTRACE
227 	if (KTRPOINT(td, KTR_CSW))
228 		ktrcsw(1, 0);
229 #endif
230 	CV_ASSERT(cvp, mp, td);
231 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
232 	    "Waiting on \"%s\"", cvp->cv_description);
233 	WITNESS_SAVE(&mp->mtx_object, mp);
234 
235 	if (cold || panicstr) {
236 		/*
237 		 * After a panic, or during autoconfiguration, just give
238 		 * interrupts a chance, then just return; don't run any other
239 		 * thread or panic below, in case this is the idle process and
240 		 * already asleep.
241 		 */
242 		return 0;
243 	}
244 
245 	sq = sleepq_lookup(cvp);
246 
247 	cvp->cv_waiters++;
248 	DROP_GIANT();
249 	mtx_unlock(mp);
250 
251 	sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
252 	sleepq_set_timeout(cvp, timo);
253 	rval = sleepq_timedwait(cvp);
254 
255 #ifdef KTRACE
256 	if (KTRPOINT(td, KTR_CSW))
257 		ktrcsw(0, 0);
258 #endif
259 	PICKUP_GIANT();
260 	mtx_lock(mp);
261 	WITNESS_RESTORE(&mp->mtx_object, mp);
262 
263 	return (rval);
264 }
265 
266 /*
267  * Wait on a condition variable for at most timo/hz seconds, allowing
268  * interruption by signals.  Returns 0 if the thread was resumed by cv_signal
269  * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
270  * a signal was caught.
271  */
272 int
273 cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo)
274 {
275 	struct sleepqueue *sq;
276 	struct thread *td;
277 	struct proc *p;
278 	int rval;
279 	int sig;
280 	WITNESS_SAVE_DECL(mp);
281 
282 	td = curthread;
283 	p = td->td_proc;
284 	rval = 0;
285 #ifdef KTRACE
286 	if (KTRPOINT(td, KTR_CSW))
287 		ktrcsw(1, 0);
288 #endif
289 	CV_ASSERT(cvp, mp, td);
290 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
291 	    "Waiting on \"%s\"", cvp->cv_description);
292 	WITNESS_SAVE(&mp->mtx_object, mp);
293 
294 	if (cold || panicstr) {
295 		/*
296 		 * After a panic, or during autoconfiguration, just give
297 		 * interrupts a chance, then just return; don't run any other
298 		 * thread or panic below, in case this is the idle process and
299 		 * already asleep.
300 		 */
301 		return 0;
302 	}
303 
304 	sq = sleepq_lookup(cvp);
305 
306 	cvp->cv_waiters++;
307 	DROP_GIANT();
308 	mtx_unlock(mp);
309 
310 	sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
311 	sleepq_set_timeout(cvp, timo);
312 	sig = sleepq_catch_signals(cvp);
313 	/*
314 	 * XXX: Missing magic return value handling for no signal
315 	 * caught but thread woken up during check.
316 	 */
317 	rval = sleepq_timedwait_sig(cvp, sig != 0);
318 	if (rval == 0)
319 		rval = sleepq_calc_signal_retval(sig);
320 
321 	/* XXX: Part of missing threading checks? */
322 	PROC_LOCK(p);
323 	if (p->p_flag & P_WEXIT)
324 		rval = EINTR;
325 	PROC_UNLOCK(p);
326 
327 #ifdef KTRACE
328 	if (KTRPOINT(td, KTR_CSW))
329 		ktrcsw(0, 0);
330 #endif
331 	PICKUP_GIANT();
332 	mtx_lock(mp);
333 	WITNESS_RESTORE(&mp->mtx_object, mp);
334 
335 	return (rval);
336 }
337 
338 /*
339  * Signal a condition variable, wakes up one waiting thread.  Will also wakeup
340  * the swapper if the process is not in memory, so that it can bring the
341  * sleeping process in.  Note that this may also result in additional threads
342  * being made runnable.  Should be called with the same mutex as was passed to
343  * cv_wait held.
344  */
345 void
346 cv_signal(struct cv *cvp)
347 {
348 
349 	if (cvp->cv_waiters > 0) {
350 		cvp->cv_waiters--;
351 		sleepq_signal(cvp, SLEEPQ_CONDVAR, -1);
352 	}
353 }
354 
355 /*
356  * Broadcast a signal to a condition variable.  Wakes up all waiting threads.
357  * Should be called with the same mutex as was passed to cv_wait held.
358  */
359 void
360 cv_broadcastpri(struct cv *cvp, int pri)
361 {
362 
363 	if (cvp->cv_waiters > 0) {
364 		cvp->cv_waiters = 0;
365 		sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri);
366 	}
367 }
368