xref: /freebsd/sys/kern/kern_condvar.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
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, lock, td) do {					\
53 	KASSERT((td) != NULL, ("%s: td NULL", __func__));		\
54 	KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__));	\
55 	KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__));		\
56 	KASSERT((lock) != NULL, ("%s: lock NULL", __func__));		\
57 } while (0)
58 
59 /*
60  * Initialize a condition variable.  Must be called before use.
61  */
62 void
63 cv_init(struct cv *cvp, const char *desc)
64 {
65 
66 	cvp->cv_description = desc;
67 	cvp->cv_waiters = 0;
68 }
69 
70 /*
71  * Destroy a condition variable.  The condition variable must be re-initialized
72  * in order to be re-used.
73  */
74 void
75 cv_destroy(struct cv *cvp)
76 {
77 #ifdef INVARIANTS
78 	struct sleepqueue *sq;
79 
80 	sleepq_lock(cvp);
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 lock_object *lock)
96 {
97 	WITNESS_SAVE_DECL(lock_witness);
98 	struct lock_class *class;
99 	struct thread *td;
100 	int lock_state;
101 
102 	td = curthread;
103 	lock_state = 0;
104 #ifdef KTRACE
105 	if (KTRPOINT(td, KTR_CSW))
106 		ktrcsw(1, 0, cv_wmesg(cvp));
107 #endif
108 	CV_ASSERT(cvp, lock, td);
109 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
110 	    "Waiting on \"%s\"", cvp->cv_description);
111 	class = LOCK_CLASS(lock);
112 
113 	if (cold || panicstr) {
114 		/*
115 		 * During autoconfiguration, just give interrupts
116 		 * a chance, then just return.  Don't run any other
117 		 * thread or panic below, in case this is the idle
118 		 * process and already asleep.
119 		 */
120 		return;
121 	}
122 
123 	sleepq_lock(cvp);
124 
125 	cvp->cv_waiters++;
126 	if (lock == &Giant.lock_object)
127 		mtx_assert(&Giant, MA_OWNED);
128 	DROP_GIANT();
129 
130 	sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
131 	if (lock != &Giant.lock_object) {
132 		if (class->lc_flags & LC_SLEEPABLE)
133 			sleepq_release(cvp);
134 		WITNESS_SAVE(lock, lock_witness);
135 		lock_state = class->lc_unlock(lock);
136 		if (class->lc_flags & LC_SLEEPABLE)
137 			sleepq_lock(cvp);
138 	}
139 	sleepq_wait(cvp, 0);
140 
141 #ifdef KTRACE
142 	if (KTRPOINT(td, KTR_CSW))
143 		ktrcsw(0, 0, cv_wmesg(cvp));
144 #endif
145 	PICKUP_GIANT();
146 	if (lock != &Giant.lock_object) {
147 		class->lc_lock(lock, lock_state);
148 		WITNESS_RESTORE(lock, lock_witness);
149 	}
150 }
151 
152 /*
153  * Wait on a condition variable.  This function differs from cv_wait by
154  * not aquiring the mutex after condition variable was signaled.
155  */
156 void
157 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
158 {
159 	struct lock_class *class;
160 	struct thread *td;
161 
162 	td = curthread;
163 #ifdef KTRACE
164 	if (KTRPOINT(td, KTR_CSW))
165 		ktrcsw(1, 0, cv_wmesg(cvp));
166 #endif
167 	CV_ASSERT(cvp, lock, td);
168 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
169 	    "Waiting on \"%s\"", cvp->cv_description);
170 	KASSERT(lock != &Giant.lock_object,
171 	    ("cv_wait_unlock cannot be used with Giant"));
172 	class = LOCK_CLASS(lock);
173 
174 	if (cold || panicstr) {
175 		/*
176 		 * During autoconfiguration, just give interrupts
177 		 * a chance, then just return.  Don't run any other
178 		 * thread or panic below, in case this is the idle
179 		 * process and already asleep.
180 		 */
181 		class->lc_unlock(lock);
182 		return;
183 	}
184 
185 	sleepq_lock(cvp);
186 
187 	cvp->cv_waiters++;
188 	DROP_GIANT();
189 
190 	sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
191 	if (class->lc_flags & LC_SLEEPABLE)
192 		sleepq_release(cvp);
193 	class->lc_unlock(lock);
194 	if (class->lc_flags & LC_SLEEPABLE)
195 		sleepq_lock(cvp);
196 	sleepq_wait(cvp, 0);
197 
198 #ifdef KTRACE
199 	if (KTRPOINT(td, KTR_CSW))
200 		ktrcsw(0, 0, cv_wmesg(cvp));
201 #endif
202 	PICKUP_GIANT();
203 }
204 
205 /*
206  * Wait on a condition variable, allowing interruption by signals.  Return 0 if
207  * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
208  * a signal was caught.  If ERESTART is returned the system call should be
209  * restarted if possible.
210  */
211 int
212 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
213 {
214 	WITNESS_SAVE_DECL(lock_witness);
215 	struct lock_class *class;
216 	struct thread *td;
217 	int lock_state, rval;
218 
219 	td = curthread;
220 	lock_state = 0;
221 #ifdef KTRACE
222 	if (KTRPOINT(td, KTR_CSW))
223 		ktrcsw(1, 0, cv_wmesg(cvp));
224 #endif
225 	CV_ASSERT(cvp, lock, td);
226 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
227 	    "Waiting on \"%s\"", cvp->cv_description);
228 	class = LOCK_CLASS(lock);
229 
230 	if (cold || panicstr) {
231 		/*
232 		 * After a panic, or during autoconfiguration, just give
233 		 * interrupts a chance, then just return; don't run any other
234 		 * procs or panic below, in case this is the idle process and
235 		 * already asleep.
236 		 */
237 		return (0);
238 	}
239 
240 	sleepq_lock(cvp);
241 
242 	cvp->cv_waiters++;
243 	if (lock == &Giant.lock_object)
244 		mtx_assert(&Giant, MA_OWNED);
245 	DROP_GIANT();
246 
247 	sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
248 	    SLEEPQ_INTERRUPTIBLE, 0);
249 	if (lock != &Giant.lock_object) {
250 		if (class->lc_flags & LC_SLEEPABLE)
251 			sleepq_release(cvp);
252 		WITNESS_SAVE(lock, lock_witness);
253 		lock_state = class->lc_unlock(lock);
254 		if (class->lc_flags & LC_SLEEPABLE)
255 			sleepq_lock(cvp);
256 	}
257 	rval = sleepq_wait_sig(cvp, 0);
258 
259 #ifdef KTRACE
260 	if (KTRPOINT(td, KTR_CSW))
261 		ktrcsw(0, 0, cv_wmesg(cvp));
262 #endif
263 	PICKUP_GIANT();
264 	if (lock != &Giant.lock_object) {
265 		class->lc_lock(lock, lock_state);
266 		WITNESS_RESTORE(lock, lock_witness);
267 	}
268 
269 	return (rval);
270 }
271 
272 /*
273  * Wait on a condition variable for (at most) the value specified in sbt
274  * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
275  * EWOULDBLOCK if the timeout expires.
276  */
277 int
278 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
279     sbintime_t pr, int flags)
280 {
281 	WITNESS_SAVE_DECL(lock_witness);
282 	struct lock_class *class;
283 	struct thread *td;
284 	int lock_state, rval;
285 
286 	td = curthread;
287 	lock_state = 0;
288 #ifdef KTRACE
289 	if (KTRPOINT(td, KTR_CSW))
290 		ktrcsw(1, 0, cv_wmesg(cvp));
291 #endif
292 	CV_ASSERT(cvp, lock, td);
293 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
294 	    "Waiting on \"%s\"", cvp->cv_description);
295 	class = LOCK_CLASS(lock);
296 
297 	if (cold || panicstr) {
298 		/*
299 		 * After a panic, or during autoconfiguration, just give
300 		 * interrupts a chance, then just return; don't run any other
301 		 * thread or panic below, in case this is the idle process and
302 		 * already asleep.
303 		 */
304 		return 0;
305 	}
306 
307 	sleepq_lock(cvp);
308 
309 	cvp->cv_waiters++;
310 	if (lock == &Giant.lock_object)
311 		mtx_assert(&Giant, MA_OWNED);
312 	DROP_GIANT();
313 
314 	sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
315 	sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
316 	if (lock != &Giant.lock_object) {
317 		if (class->lc_flags & LC_SLEEPABLE)
318 			sleepq_release(cvp);
319 		WITNESS_SAVE(lock, lock_witness);
320 		lock_state = class->lc_unlock(lock);
321 		if (class->lc_flags & LC_SLEEPABLE)
322 			sleepq_lock(cvp);
323 	}
324 	rval = sleepq_timedwait(cvp, 0);
325 
326 #ifdef KTRACE
327 	if (KTRPOINT(td, KTR_CSW))
328 		ktrcsw(0, 0, cv_wmesg(cvp));
329 #endif
330 	PICKUP_GIANT();
331 	if (lock != &Giant.lock_object) {
332 		class->lc_lock(lock, lock_state);
333 		WITNESS_RESTORE(lock, lock_witness);
334 	}
335 
336 	return (rval);
337 }
338 
339 /*
340  * Wait on a condition variable for (at most) the value specified in sbt
341  * argument, allowing interruption by signals.
342  * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
343  * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
344  * was caught.
345  */
346 int
347 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
348     sbintime_t sbt, sbintime_t pr, int flags)
349 {
350 	WITNESS_SAVE_DECL(lock_witness);
351 	struct lock_class *class;
352 	struct thread *td;
353 	int lock_state, rval;
354 
355 	td = curthread;
356 	lock_state = 0;
357 #ifdef KTRACE
358 	if (KTRPOINT(td, KTR_CSW))
359 		ktrcsw(1, 0, cv_wmesg(cvp));
360 #endif
361 	CV_ASSERT(cvp, lock, td);
362 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
363 	    "Waiting on \"%s\"", cvp->cv_description);
364 	class = LOCK_CLASS(lock);
365 
366 	if (cold || panicstr) {
367 		/*
368 		 * After a panic, or during autoconfiguration, just give
369 		 * interrupts a chance, then just return; don't run any other
370 		 * thread or panic below, in case this is the idle process and
371 		 * already asleep.
372 		 */
373 		return 0;
374 	}
375 
376 	sleepq_lock(cvp);
377 
378 	cvp->cv_waiters++;
379 	if (lock == &Giant.lock_object)
380 		mtx_assert(&Giant, MA_OWNED);
381 	DROP_GIANT();
382 
383 	sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
384 	    SLEEPQ_INTERRUPTIBLE, 0);
385 	sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
386 	if (lock != &Giant.lock_object) {
387 		if (class->lc_flags & LC_SLEEPABLE)
388 			sleepq_release(cvp);
389 		WITNESS_SAVE(lock, lock_witness);
390 		lock_state = class->lc_unlock(lock);
391 		if (class->lc_flags & LC_SLEEPABLE)
392 			sleepq_lock(cvp);
393 	}
394 	rval = sleepq_timedwait_sig(cvp, 0);
395 
396 #ifdef KTRACE
397 	if (KTRPOINT(td, KTR_CSW))
398 		ktrcsw(0, 0, cv_wmesg(cvp));
399 #endif
400 	PICKUP_GIANT();
401 	if (lock != &Giant.lock_object) {
402 		class->lc_lock(lock, lock_state);
403 		WITNESS_RESTORE(lock, lock_witness);
404 	}
405 
406 	return (rval);
407 }
408 
409 /*
410  * Signal a condition variable, wakes up one waiting thread.  Will also wakeup
411  * the swapper if the process is not in memory, so that it can bring the
412  * sleeping process in.  Note that this may also result in additional threads
413  * being made runnable.  Should be called with the same mutex as was passed to
414  * cv_wait held.
415  */
416 void
417 cv_signal(struct cv *cvp)
418 {
419 	int wakeup_swapper;
420 
421 	wakeup_swapper = 0;
422 	sleepq_lock(cvp);
423 	if (cvp->cv_waiters > 0) {
424 		cvp->cv_waiters--;
425 		wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0, 0);
426 	}
427 	sleepq_release(cvp);
428 	if (wakeup_swapper)
429 		kick_proc0();
430 }
431 
432 /*
433  * Broadcast a signal to a condition variable.  Wakes up all waiting threads.
434  * Should be called with the same mutex as was passed to cv_wait held.
435  */
436 void
437 cv_broadcastpri(struct cv *cvp, int pri)
438 {
439 	int wakeup_swapper;
440 
441 	/*
442 	 * XXX sleepq_broadcast pri argument changed from -1 meaning
443 	 * no pri to 0 meaning no pri.
444 	 */
445 	wakeup_swapper = 0;
446 	if (pri == -1)
447 		pri = 0;
448 	sleepq_lock(cvp);
449 	if (cvp->cv_waiters > 0) {
450 		cvp->cv_waiters = 0;
451 		wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);
452 	}
453 	sleepq_release(cvp);
454 	if (wakeup_swapper)
455 		kick_proc0();
456 }
457