kern_synch.c (167327) | kern_synch.c (167387) |
---|---|
1/*- 2 * Copyright (c) 1982, 1986, 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 21 unchanged lines hidden (view full) --- 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95 35 */ 36 37#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1982, 1986, 1990, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 21 unchanged lines hidden (view full) --- 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_synch.c 8.9 (Berkeley) 5/19/95 35 */ 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 167327 2007-03-08 06:44:34Z julian $"); | 38__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 167387 2007-03-09 22:41:01Z jhb $"); |
39 40#include "opt_ktrace.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/condvar.h> 45#include <sys/kdb.h> 46#include <sys/kernel.h> --- 60 unchanged lines hidden (view full) --- 107 * runnable with the specified priority. Sleeps at most timo/hz seconds 108 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 109 * before and after sleeping, else signals are not checked. Returns 0 if 110 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 111 * signal needs to be delivered, ERESTART is returned if the current system 112 * call should be restarted if possible, and EINTR is returned if the system 113 * call should be interrupted by the signal (return EINTR). 114 * | 39 40#include "opt_ktrace.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/condvar.h> 45#include <sys/kdb.h> 46#include <sys/kernel.h> --- 60 unchanged lines hidden (view full) --- 107 * runnable with the specified priority. Sleeps at most timo/hz seconds 108 * (0 means no timeout). If pri includes PCATCH flag, signals are checked 109 * before and after sleeping, else signals are not checked. Returns 0 if 110 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a 111 * signal needs to be delivered, ERESTART is returned if the current system 112 * call should be restarted if possible, and EINTR is returned if the system 113 * call should be interrupted by the signal (return EINTR). 114 * |
115 * The mutex argument is unlocked before the caller is suspended, and 116 * re-locked before msleep returns. If priority includes the PDROP 117 * flag the mutex is not re-locked before returning. | 115 * The lock argument is unlocked before the caller is suspended, and 116 * re-locked before _sleep() returns. If priority includes the PDROP 117 * flag the lock is not re-locked before returning. |
118 */ 119int | 118 */ 119int |
120msleep(ident, mtx, priority, wmesg, timo) | 120_sleep(ident, lock, priority, wmesg, timo) |
121 void *ident; | 121 void *ident; |
122 struct mtx *mtx; | 122 struct lock_object *lock; |
123 int priority, timo; 124 const char *wmesg; 125{ 126 struct thread *td; 127 struct proc *p; | 123 int priority, timo; 124 const char *wmesg; 125{ 126 struct thread *td; 127 struct proc *p; |
128 int catch, rval, flags, pri; 129 WITNESS_SAVE_DECL(mtx); | 128 struct lock_class *class; 129 int catch, flags, lock_state, pri, rval; 130 WITNESS_SAVE_DECL(lock_witness); |
130 131 td = curthread; 132 p = td->td_proc; 133#ifdef KTRACE 134 if (KTRPOINT(td, KTR_CSW)) 135 ktrcsw(1, 0); 136#endif | 131 132 td = curthread; 133 p = td->td_proc; 134#ifdef KTRACE 135 if (KTRPOINT(td, KTR_CSW)) 136 ktrcsw(1, 0); 137#endif |
137 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, mtx == NULL ? NULL : 138 &mtx->mtx_object, "Sleeping on \"%s\"", wmesg); 139 KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL || 140 ident == &lbolt, ("sleeping without a mutex")); | 138 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock, 139 "Sleeping on \"%s\"", wmesg); 140 KASSERT(timo != 0 || mtx_owned(&Giant) || lock != NULL || 141 ident == &lbolt, ("sleeping without a lock")); |
141 KASSERT(p != NULL, ("msleep1")); 142 KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep")); | 142 KASSERT(p != NULL, ("msleep1")); 143 KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep")); |
144 if (lock != NULL) 145 class = LOCK_CLASS(lock); 146 else 147 class = NULL; |
|
143 144 if (cold) { 145 /* 146 * During autoconfiguration, just return; 147 * don't run any other threads or panic below, 148 * in case this is the idle thread and already asleep. 149 * XXX: this used to do "s = splhigh(); splx(safepri); 150 * splx(s);" to give interrupts a chance, but there is 151 * no way to give interrupts a chance now. 152 */ | 148 149 if (cold) { 150 /* 151 * During autoconfiguration, just return; 152 * don't run any other threads or panic below, 153 * in case this is the idle thread and already asleep. 154 * XXX: this used to do "s = splhigh(); splx(safepri); 155 * splx(s);" to give interrupts a chance, but there is 156 * no way to give interrupts a chance now. 157 */ |
153 if (mtx != NULL && priority & PDROP) 154 mtx_unlock(mtx); | 158 if (lock != NULL && priority & PDROP) 159 class->lc_unlock(lock); |
155 return (0); 156 } 157 catch = priority & PCATCH; 158 rval = 0; 159 160 /* 161 * If we are already on a sleep queue, then remove us from that 162 * sleep queue first. We have to do this to handle recursive 163 * sleeps. 164 */ 165 if (TD_ON_SLEEPQ(td)) 166 sleepq_remove(td, td->td_wchan); 167 168 if (ident == &pause_wchan) 169 flags = SLEEPQ_PAUSE; 170 else | 160 return (0); 161 } 162 catch = priority & PCATCH; 163 rval = 0; 164 165 /* 166 * If we are already on a sleep queue, then remove us from that 167 * sleep queue first. We have to do this to handle recursive 168 * sleeps. 169 */ 170 if (TD_ON_SLEEPQ(td)) 171 sleepq_remove(td, td->td_wchan); 172 173 if (ident == &pause_wchan) 174 flags = SLEEPQ_PAUSE; 175 else |
171 flags = SLEEPQ_MSLEEP; | 176 flags = SLEEPQ_SLEEP; |
172 if (catch) 173 flags |= SLEEPQ_INTERRUPTIBLE; 174 175 sleepq_lock(ident); | 177 if (catch) 178 flags |= SLEEPQ_INTERRUPTIBLE; 179 180 sleepq_lock(ident); |
176 CTR5(KTR_PROC, "msleep: thread %ld (pid %ld, %s) on %s (%p)", | 181 CTR5(KTR_PROC, "sleep: thread %ld (pid %ld, %s) on %s (%p)", |
177 td->td_tid, p->p_pid, p->p_comm, wmesg, ident); 178 179 DROP_GIANT(); | 182 td->td_tid, p->p_pid, p->p_comm, wmesg, ident); 183 184 DROP_GIANT(); |
180 if (mtx != NULL) { 181 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); 182 WITNESS_SAVE(&mtx->mtx_object, mtx); 183 mtx_unlock(mtx); 184 } | 185 if (lock != NULL) { 186 WITNESS_SAVE(lock, lock_witness); 187 lock_state = class->lc_unlock(lock); 188 } else 189 /* GCC needs to follow the Yellow Brick Road */ 190 lock_state = -1; |
185 186 /* 187 * We put ourselves on the sleep queue and start our timeout 188 * before calling thread_suspend_check, as we could stop there, 189 * and a wakeup or a SIGCONT (or both) could occur while we were 190 * stopped without resuming us. Thus, we must be ready for sleep 191 * when cursig() is called. If the wakeup happens while we're 192 * stopped, then td will no longer be on a sleep queue upon 193 * return from cursig(). 194 */ | 191 192 /* 193 * We put ourselves on the sleep queue and start our timeout 194 * before calling thread_suspend_check, as we could stop there, 195 * and a wakeup or a SIGCONT (or both) could occur while we were 196 * stopped without resuming us. Thus, we must be ready for sleep 197 * when cursig() is called. If the wakeup happens while we're 198 * stopped, then td will no longer be on a sleep queue upon 199 * return from cursig(). 200 */ |
195 sleepq_add(ident, ident == &lbolt ? NULL : &mtx->mtx_object, wmesg, 196 flags, 0); | 201 sleepq_add(ident, ident == &lbolt ? NULL : lock, wmesg, flags, 0); |
197 if (timo) 198 sleepq_set_timeout(ident, timo); 199 200 /* 201 * Adjust this thread's priority, if necessary. 202 */ 203 pri = priority & PRIMASK; 204 if (pri != 0 && pri != td->td_priority) { --- 12 unchanged lines hidden (view full) --- 217 sleepq_wait(ident); 218 rval = 0; 219 } 220#ifdef KTRACE 221 if (KTRPOINT(td, KTR_CSW)) 222 ktrcsw(0, 0); 223#endif 224 PICKUP_GIANT(); | 202 if (timo) 203 sleepq_set_timeout(ident, timo); 204 205 /* 206 * Adjust this thread's priority, if necessary. 207 */ 208 pri = priority & PRIMASK; 209 if (pri != 0 && pri != td->td_priority) { --- 12 unchanged lines hidden (view full) --- 222 sleepq_wait(ident); 223 rval = 0; 224 } 225#ifdef KTRACE 226 if (KTRPOINT(td, KTR_CSW)) 227 ktrcsw(0, 0); 228#endif 229 PICKUP_GIANT(); |
225 if (mtx != NULL && !(priority & PDROP)) { 226 mtx_lock(mtx); 227 WITNESS_RESTORE(&mtx->mtx_object, mtx); | 230 if (lock != NULL && !(priority & PDROP)) { 231 class->lc_lock(lock, lock_state); 232 WITNESS_RESTORE(lock, lock_witness); |
228 } 229 return (rval); 230} 231 232int 233msleep_spin(ident, mtx, wmesg, timo) 234 void *ident; 235 struct mtx *mtx; --- 30 unchanged lines hidden (view full) --- 266 DROP_GIANT(); 267 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); 268 WITNESS_SAVE(&mtx->mtx_object, mtx); 269 mtx_unlock_spin(mtx); 270 271 /* 272 * We put ourselves on the sleep queue and start our timeout. 273 */ | 233 } 234 return (rval); 235} 236 237int 238msleep_spin(ident, mtx, wmesg, timo) 239 void *ident; 240 struct mtx *mtx; --- 30 unchanged lines hidden (view full) --- 271 DROP_GIANT(); 272 mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED); 273 WITNESS_SAVE(&mtx->mtx_object, mtx); 274 mtx_unlock_spin(mtx); 275 276 /* 277 * We put ourselves on the sleep queue and start our timeout. 278 */ |
274 sleepq_add(ident, &mtx->mtx_object, wmesg, SLEEPQ_MSLEEP, 0); | 279 sleepq_add(ident, &mtx->mtx_object, wmesg, SLEEPQ_SLEEP, 0); |
275 if (timo) 276 sleepq_set_timeout(ident, timo); 277 278 /* 279 * Can't call ktrace with any spin locks held so it can lock the 280 * ktrace_mtx lock, and WITNESS_WARN considers it an error to hold 281 * any spin lock. Thus, we have to drop the sleepq spin lock while 282 * we handle those requests. This is safe since we have placed our --- 48 unchanged lines hidden (view full) --- 331 * Make all threads sleeping on the specified identifier runnable. 332 */ 333void 334wakeup(ident) 335 register void *ident; 336{ 337 338 sleepq_lock(ident); | 280 if (timo) 281 sleepq_set_timeout(ident, timo); 282 283 /* 284 * Can't call ktrace with any spin locks held so it can lock the 285 * ktrace_mtx lock, and WITNESS_WARN considers it an error to hold 286 * any spin lock. Thus, we have to drop the sleepq spin lock while 287 * we handle those requests. This is safe since we have placed our --- 48 unchanged lines hidden (view full) --- 336 * Make all threads sleeping on the specified identifier runnable. 337 */ 338void 339wakeup(ident) 340 register void *ident; 341{ 342 343 sleepq_lock(ident); |
339 sleepq_broadcast(ident, SLEEPQ_MSLEEP, -1, 0); | 344 sleepq_broadcast(ident, SLEEPQ_SLEEP, -1, 0); |
340} 341 342/* 343 * Make a thread sleeping on the specified identifier runnable. 344 * May wake more than one thread if a target thread is currently 345 * swapped out. 346 */ 347void 348wakeup_one(ident) 349 register void *ident; 350{ 351 352 sleepq_lock(ident); | 345} 346 347/* 348 * Make a thread sleeping on the specified identifier runnable. 349 * May wake more than one thread if a target thread is currently 350 * swapped out. 351 */ 352void 353wakeup_one(ident) 354 register void *ident; 355{ 356 357 sleepq_lock(ident); |
353 sleepq_signal(ident, SLEEPQ_MSLEEP, -1, 0); | 358 sleepq_signal(ident, SLEEPQ_SLEEP, -1, 0); |
354} 355 356/* 357 * The machine independent parts of context switching. 358 */ 359void 360mi_switch(int flags, struct thread *newtd) 361{ --- 216 unchanged lines hidden --- | 359} 360 361/* 362 * The machine independent parts of context switching. 363 */ 364void 365mi_switch(int flags, struct thread *newtd) 366{ --- 216 unchanged lines hidden --- |