xref: /freebsd/sys/kern/kern_condvar.c (revision 2bc6540439d0932b38067c9cc321fa0e2a61f264)
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 	sleepq_lock(cvp);
82 	sq = sleepq_lookup(cvp);
83 	sleepq_release(cvp);
84 	KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
85 #endif
86 }
87 
88 /*
89  * Wait on a condition variable.  The current thread is placed on the condition
90  * variable's wait queue and suspended.  A cv_signal or cv_broadcast on the same
91  * condition variable will resume the thread.  The mutex is released before
92  * sleeping and will be held on return.  It is recommended that the mutex be
93  * held when cv_signal or cv_broadcast are called.
94  */
95 void
96 cv_wait(struct cv *cvp, struct mtx *mp)
97 {
98 	WITNESS_SAVE_DECL(mp);
99 
100 	WITNESS_SAVE(&mp->mtx_object, mp);
101 
102 	if (cold || panicstr) {
103 		/*
104 		 * During autoconfiguration, just give interrupts
105 		 * a chance, then just return.  Don't run any other
106 		 * thread or panic below, in case this is the idle
107 		 * process and already asleep.
108 		 */
109 		return;
110 	}
111 
112 	cv_wait_unlock(cvp, mp);
113 	mtx_lock(mp);
114 	WITNESS_RESTORE(&mp->mtx_object, mp);
115 }
116 
117 /*
118  * Wait on a condition variable.  This function differs from cv_wait by
119  * not aquiring the mutex after condition variable was signaled.
120  */
121 void
122 cv_wait_unlock(struct cv *cvp, struct mtx *mp)
123 {
124 	struct thread *td;
125 
126 	td = curthread;
127 #ifdef KTRACE
128 	if (KTRPOINT(td, KTR_CSW))
129 		ktrcsw(1, 0);
130 #endif
131 	CV_ASSERT(cvp, mp, td);
132 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
133 	    "Waiting on \"%s\"", cvp->cv_description);
134 
135 	if (cold || panicstr) {
136 		/*
137 		 * During autoconfiguration, just give interrupts
138 		 * a chance, then just return.  Don't run any other
139 		 * thread or panic below, in case this is the idle
140 		 * process and already asleep.
141 		 */
142 		mtx_unlock(mp);
143 		return;
144 	}
145 
146 	sleepq_lock(cvp);
147 
148 	cvp->cv_waiters++;
149 	DROP_GIANT();
150 	mtx_unlock(mp);
151 
152 	sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
153 	sleepq_wait(cvp);
154 
155 	PICKUP_GIANT();
156 }
157 
158 /*
159  * Wait on a condition variable, allowing interruption by signals.  Return 0 if
160  * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
161  * a signal was caught.  If ERESTART is returned the system call should be
162  * restarted if possible.
163  */
164 int
165 cv_wait_sig(struct cv *cvp, struct mtx *mp)
166 {
167 	struct thread *td;
168 	struct proc *p;
169 	int rval, sig;
170 	WITNESS_SAVE_DECL(mp);
171 
172 	td = curthread;
173 	p = td->td_proc;
174 #ifdef KTRACE
175 	if (KTRPOINT(td, KTR_CSW))
176 		ktrcsw(1, 0);
177 #endif
178 	CV_ASSERT(cvp, mp, td);
179 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
180 	    "Waiting on \"%s\"", cvp->cv_description);
181 	WITNESS_SAVE(&mp->mtx_object, mp);
182 
183 	if (cold || panicstr) {
184 		/*
185 		 * After a panic, or during autoconfiguration, just give
186 		 * interrupts a chance, then just return; don't run any other
187 		 * procs or panic below, in case this is the idle process and
188 		 * already asleep.
189 		 */
190 		return (0);
191 	}
192 
193 	sleepq_lock(cvp);
194 
195 	/*
196 	 * Don't bother sleeping if we are exiting and not the exiting
197 	 * thread or if our thread is marked as interrupted.
198 	 */
199 	mtx_lock_spin(&sched_lock);
200 	rval = thread_sleep_check(td);
201 	mtx_unlock_spin(&sched_lock);
202 	if (rval != 0) {
203 		sleepq_release(cvp);
204 		return (rval);
205 	}
206 
207 	cvp->cv_waiters++;
208 	DROP_GIANT();
209 	mtx_unlock(mp);
210 
211 	sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR |
212 	    SLEEPQ_INTERRUPTIBLE);
213 	sig = sleepq_catch_signals(cvp);
214 	rval = sleepq_wait_sig(cvp);
215 	if (rval == 0)
216 		rval = sleepq_calc_signal_retval(sig);
217 
218 #ifdef KTRACE
219 	if (KTRPOINT(td, KTR_CSW))
220 		ktrcsw(0, 0);
221 #endif
222 	PICKUP_GIANT();
223 	mtx_lock(mp);
224 	WITNESS_RESTORE(&mp->mtx_object, mp);
225 
226 	return (rval);
227 }
228 
229 /*
230  * Wait on a condition variable for at most timo/hz seconds.  Returns 0 if the
231  * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
232  * expires.
233  */
234 int
235 cv_timedwait(struct cv *cvp, struct mtx *mp, int timo)
236 {
237 	struct thread *td;
238 	int rval;
239 	WITNESS_SAVE_DECL(mp);
240 
241 	td = curthread;
242 	rval = 0;
243 #ifdef KTRACE
244 	if (KTRPOINT(td, KTR_CSW))
245 		ktrcsw(1, 0);
246 #endif
247 	CV_ASSERT(cvp, mp, td);
248 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
249 	    "Waiting on \"%s\"", cvp->cv_description);
250 	WITNESS_SAVE(&mp->mtx_object, mp);
251 
252 	if (cold || panicstr) {
253 		/*
254 		 * After a panic, or during autoconfiguration, just give
255 		 * interrupts a chance, then just return; don't run any other
256 		 * thread or panic below, in case this is the idle process and
257 		 * already asleep.
258 		 */
259 		return 0;
260 	}
261 
262 	sleepq_lock(cvp);
263 
264 	cvp->cv_waiters++;
265 	DROP_GIANT();
266 	mtx_unlock(mp);
267 
268 	sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR);
269 	sleepq_set_timeout(cvp, timo);
270 	rval = sleepq_timedwait(cvp);
271 
272 #ifdef KTRACE
273 	if (KTRPOINT(td, KTR_CSW))
274 		ktrcsw(0, 0);
275 #endif
276 	PICKUP_GIANT();
277 	mtx_lock(mp);
278 	WITNESS_RESTORE(&mp->mtx_object, mp);
279 
280 	return (rval);
281 }
282 
283 /*
284  * Wait on a condition variable for at most timo/hz seconds, allowing
285  * interruption by signals.  Returns 0 if the thread was resumed by cv_signal
286  * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
287  * a signal was caught.
288  */
289 int
290 cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo)
291 {
292 	struct thread *td;
293 	struct proc *p;
294 	int rval;
295 	int sig;
296 	WITNESS_SAVE_DECL(mp);
297 
298 	td = curthread;
299 	p = td->td_proc;
300 	rval = 0;
301 #ifdef KTRACE
302 	if (KTRPOINT(td, KTR_CSW))
303 		ktrcsw(1, 0);
304 #endif
305 	CV_ASSERT(cvp, mp, td);
306 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
307 	    "Waiting on \"%s\"", cvp->cv_description);
308 	WITNESS_SAVE(&mp->mtx_object, mp);
309 
310 	if (cold || panicstr) {
311 		/*
312 		 * After a panic, or during autoconfiguration, just give
313 		 * interrupts a chance, then just return; don't run any other
314 		 * thread or panic below, in case this is the idle process and
315 		 * already asleep.
316 		 */
317 		return 0;
318 	}
319 
320 	sleepq_lock(cvp);
321 
322 	/*
323 	 * Don't bother sleeping if we are exiting and not the exiting
324 	 * thread or if our thread is marked as interrupted.
325 	 */
326 	mtx_lock_spin(&sched_lock);
327 	rval = thread_sleep_check(td);
328 	mtx_unlock_spin(&sched_lock);
329 	if (rval != 0) {
330 		sleepq_release(cvp);
331 		return (rval);
332 	}
333 
334 	cvp->cv_waiters++;
335 	DROP_GIANT();
336 	mtx_unlock(mp);
337 
338 	sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR |
339 	    SLEEPQ_INTERRUPTIBLE);
340 	sleepq_set_timeout(cvp, timo);
341 	sig = sleepq_catch_signals(cvp);
342 	rval = sleepq_timedwait_sig(cvp, sig != 0);
343 	if (rval == 0)
344 		rval = sleepq_calc_signal_retval(sig);
345 
346 #ifdef KTRACE
347 	if (KTRPOINT(td, KTR_CSW))
348 		ktrcsw(0, 0);
349 #endif
350 	PICKUP_GIANT();
351 	mtx_lock(mp);
352 	WITNESS_RESTORE(&mp->mtx_object, mp);
353 
354 	return (rval);
355 }
356 
357 /*
358  * Signal a condition variable, wakes up one waiting thread.  Will also wakeup
359  * the swapper if the process is not in memory, so that it can bring the
360  * sleeping process in.  Note that this may also result in additional threads
361  * being made runnable.  Should be called with the same mutex as was passed to
362  * cv_wait held.
363  */
364 void
365 cv_signal(struct cv *cvp)
366 {
367 
368 	sleepq_lock(cvp);
369 	if (cvp->cv_waiters > 0) {
370 		cvp->cv_waiters--;
371 		sleepq_signal(cvp, SLEEPQ_CONDVAR, -1);
372 	} else
373 		sleepq_release(cvp);
374 }
375 
376 /*
377  * Broadcast a signal to a condition variable.  Wakes up all waiting threads.
378  * Should be called with the same mutex as was passed to cv_wait held.
379  */
380 void
381 cv_broadcastpri(struct cv *cvp, int pri)
382 {
383 
384 	sleepq_lock(cvp);
385 	if (cvp->cv_waiters > 0) {
386 		cvp->cv_waiters = 0;
387 		sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri);
388 	} else
389 		sleepq_release(cvp);
390 }
391