kern_condvar.c (134013) | kern_condvar.c (136445) |
---|---|
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 --- 11 unchanged lines hidden (view full) --- 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> | 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 --- 11 unchanged lines hidden (view full) --- 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: head/sys/kern/kern_condvar.c 134013 2004-08-19 11:31:42Z jhb $"); | 28__FBSDID("$FreeBSD: head/sys/kern/kern_condvar.c 136445 2004-10-12 18:36:20Z jhb $"); |
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> --- 34 unchanged lines hidden (view full) --- 71/* 72 * Destroy a condition variable. The condition variable must be re-initialized 73 * in order to be re-used. 74 */ 75void 76cv_destroy(struct cv *cvp) 77{ 78#ifdef INVARIANTS | 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> --- 34 unchanged lines hidden (view full) --- 71/* 72 * Destroy a condition variable. The condition variable must be re-initialized 73 * in order to be re-used. 74 */ 75void 76cv_destroy(struct cv *cvp) 77{ 78#ifdef INVARIANTS |
79 struct sleepqueue *sq; | 79 struct sleepqueue *sq; |
80 | 80 |
81 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 */ 94void 95cv_wait(struct cv *cvp, struct mtx *mp) 96{ | 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 */ 95void 96cv_wait(struct cv *cvp, struct mtx *mp) 97{ |
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 --- 7 unchanged lines hidden (view full) --- 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 | 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 --- 7 unchanged lines hidden (view full) --- 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); | 121 sleepq_lock(cvp); |
122 123 cvp->cv_waiters++; 124 DROP_GIANT(); 125 mtx_unlock(mp); 126 | 122 123 cvp->cv_waiters++; 124 DROP_GIANT(); 125 mtx_unlock(mp); 126 |
127 sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); | 127 sleepq_add(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); --- 4 unchanged lines hidden (view full) --- 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 */ 145int 146cv_wait_sig(struct cv *cvp, struct mtx *mp) 147{ | 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); --- 4 unchanged lines hidden (view full) --- 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 */ 145int 146cv_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#ifdef KTRACE --- 10 unchanged lines hidden (view full) --- 167 * After a panic, or during autoconfiguration, just give 168 * interrupts a chance, then just return; don't run any other 169 * procs or panic below, in case this is the idle process and 170 * already asleep. 171 */ 172 return (0); 173 } 174 | 148 struct thread *td; 149 struct proc *p; 150 int rval, sig; 151 WITNESS_SAVE_DECL(mp); 152 153 td = curthread; 154 p = td->td_proc; 155#ifdef KTRACE --- 10 unchanged lines hidden (view full) --- 166 * After a panic, or during autoconfiguration, just give 167 * interrupts a chance, then just return; don't run any other 168 * procs or panic below, in case this is the idle process and 169 * already asleep. 170 */ 171 return (0); 172 } 173 |
175 sq = sleepq_lookup(cvp); | 174 sleepq_lock(cvp); |
176 177 /* 178 * Don't bother sleeping if we are exiting and not the exiting 179 * thread or if our thread is marked as interrupted. 180 */ 181 mtx_lock_spin(&sched_lock); 182 rval = thread_sleep_check(td); 183 mtx_unlock_spin(&sched_lock); 184 if (rval != 0) { 185 sleepq_release(cvp); 186 return (rval); 187 } 188 189 cvp->cv_waiters++; 190 DROP_GIANT(); 191 mtx_unlock(mp); 192 | 175 176 /* 177 * Don't bother sleeping if we are exiting and not the exiting 178 * thread or if our thread is marked as interrupted. 179 */ 180 mtx_lock_spin(&sched_lock); 181 rval = thread_sleep_check(td); 182 mtx_unlock_spin(&sched_lock); 183 if (rval != 0) { 184 sleepq_release(cvp); 185 return (rval); 186 } 187 188 cvp->cv_waiters++; 189 DROP_GIANT(); 190 mtx_unlock(mp); 191 |
193 sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR | | 192 sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR | |
194 SLEEPQ_INTERRUPTIBLE); 195 sig = sleepq_catch_signals(cvp); 196 rval = sleepq_wait_sig(cvp); 197 if (rval == 0) 198 rval = sleepq_calc_signal_retval(sig); 199 200#ifdef KTRACE 201 if (KTRPOINT(td, KTR_CSW)) --- 9 unchanged lines hidden (view full) --- 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 */ 216int 217cv_timedwait(struct cv *cvp, struct mtx *mp, int timo) 218{ | 193 SLEEPQ_INTERRUPTIBLE); 194 sig = sleepq_catch_signals(cvp); 195 rval = sleepq_wait_sig(cvp); 196 if (rval == 0) 197 rval = sleepq_calc_signal_retval(sig); 198 199#ifdef KTRACE 200 if (KTRPOINT(td, KTR_CSW)) --- 9 unchanged lines hidden (view full) --- 210/* 211 * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the 212 * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout 213 * expires. 214 */ 215int 216cv_timedwait(struct cv *cvp, struct mtx *mp, int timo) 217{ |
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)) --- 9 unchanged lines hidden (view full) --- 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 | 218 struct thread *td; 219 int rval; 220 WITNESS_SAVE_DECL(mp); 221 222 td = curthread; 223 rval = 0; 224#ifdef KTRACE 225 if (KTRPOINT(td, KTR_CSW)) --- 9 unchanged lines hidden (view full) --- 235 * After a panic, or during autoconfiguration, just give 236 * interrupts a chance, then just return; don't run any other 237 * thread or panic below, in case this is the idle process and 238 * already asleep. 239 */ 240 return 0; 241 } 242 |
245 sq = sleepq_lookup(cvp); | 243 sleepq_lock(cvp); |
246 247 cvp->cv_waiters++; 248 DROP_GIANT(); 249 mtx_unlock(mp); 250 | 244 245 cvp->cv_waiters++; 246 DROP_GIANT(); 247 mtx_unlock(mp); 248 |
251 sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR); | 249 sleepq_add(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(); --- 7 unchanged lines hidden (view full) --- 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 */ 272int 273cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo) 274{ | 250 sleepq_set_timeout(cvp, timo); 251 rval = sleepq_timedwait(cvp); 252 253#ifdef KTRACE 254 if (KTRPOINT(td, KTR_CSW)) 255 ktrcsw(0, 0); 256#endif 257 PICKUP_GIANT(); --- 7 unchanged lines hidden (view full) --- 265 * Wait on a condition variable for at most timo/hz seconds, allowing 266 * interruption by signals. Returns 0 if the thread was resumed by cv_signal 267 * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if 268 * a signal was caught. 269 */ 270int 271cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo) 272{ |
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; --- 12 unchanged lines hidden (view full) --- 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 | 273 struct thread *td; 274 struct proc *p; 275 int rval; 276 int sig; 277 WITNESS_SAVE_DECL(mp); 278 279 td = curthread; 280 p = td->td_proc; --- 12 unchanged lines hidden (view full) --- 293 * After a panic, or during autoconfiguration, just give 294 * interrupts a chance, then just return; don't run any other 295 * thread or panic below, in case this is the idle process and 296 * already asleep. 297 */ 298 return 0; 299 } 300 |
304 sq = sleepq_lookup(cvp); | 301 sleepq_lock(cvp); |
305 306 /* 307 * Don't bother sleeping if we are exiting and not the exiting 308 * thread or if our thread is marked as interrupted. 309 */ 310 mtx_lock_spin(&sched_lock); 311 rval = thread_sleep_check(td); 312 mtx_unlock_spin(&sched_lock); 313 if (rval != 0) { 314 sleepq_release(cvp); 315 return (rval); 316 } 317 318 cvp->cv_waiters++; 319 DROP_GIANT(); 320 mtx_unlock(mp); 321 | 302 303 /* 304 * Don't bother sleeping if we are exiting and not the exiting 305 * thread or if our thread is marked as interrupted. 306 */ 307 mtx_lock_spin(&sched_lock); 308 rval = thread_sleep_check(td); 309 mtx_unlock_spin(&sched_lock); 310 if (rval != 0) { 311 sleepq_release(cvp); 312 return (rval); 313 } 314 315 cvp->cv_waiters++; 316 DROP_GIANT(); 317 mtx_unlock(mp); 318 |
322 sleepq_add(sq, cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR | | 319 sleepq_add(cvp, mp, cvp->cv_description, SLEEPQ_CONDVAR | |
323 SLEEPQ_INTERRUPTIBLE); 324 sleepq_set_timeout(cvp, timo); 325 sig = sleepq_catch_signals(cvp); 326 rval = sleepq_timedwait_sig(cvp, sig != 0); 327 if (rval == 0) 328 rval = sleepq_calc_signal_retval(sig); 329 330#ifdef KTRACE --- 13 unchanged lines hidden (view full) --- 344 * sleeping process in. Note that this may also result in additional threads 345 * being made runnable. Should be called with the same mutex as was passed to 346 * cv_wait held. 347 */ 348void 349cv_signal(struct cv *cvp) 350{ 351 | 320 SLEEPQ_INTERRUPTIBLE); 321 sleepq_set_timeout(cvp, timo); 322 sig = sleepq_catch_signals(cvp); 323 rval = sleepq_timedwait_sig(cvp, sig != 0); 324 if (rval == 0) 325 rval = sleepq_calc_signal_retval(sig); 326 327#ifdef KTRACE --- 13 unchanged lines hidden (view full) --- 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 */ 345void 346cv_signal(struct cv *cvp) 347{ 348 |
349 sleepq_lock(cvp); |
|
352 if (cvp->cv_waiters > 0) { 353 cvp->cv_waiters--; 354 sleepq_signal(cvp, SLEEPQ_CONDVAR, -1); | 350 if (cvp->cv_waiters > 0) { 351 cvp->cv_waiters--; 352 sleepq_signal(cvp, SLEEPQ_CONDVAR, -1); |
355 } | 353 } else 354 sleepq_release(cvp); |
356} 357 358/* 359 * Broadcast a signal to a condition variable. Wakes up all waiting threads. 360 * Should be called with the same mutex as was passed to cv_wait held. 361 */ 362void 363cv_broadcastpri(struct cv *cvp, int pri) 364{ 365 | 355} 356 357/* 358 * Broadcast a signal to a condition variable. Wakes up all waiting threads. 359 * Should be called with the same mutex as was passed to cv_wait held. 360 */ 361void 362cv_broadcastpri(struct cv *cvp, int pri) 363{ 364 |
365 sleepq_lock(cvp); |
|
366 if (cvp->cv_waiters > 0) { 367 cvp->cv_waiters = 0; 368 sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri); | 366 if (cvp->cv_waiters > 0) { 367 cvp->cv_waiters = 0; 368 sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri); |
369 } | 369 } else 370 sleepq_release(cvp); |
370} | 371} |