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