]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
06f1d4591d7f54d43fd11a040a98707d989283e4
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1987 Carnegie-Mellon University
28 * All rights reserved. The CMU software License Agreement specifies
29 * the terms and conditions for use and redistribution.
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/vnode.h>
38 #include <sys/kernel.h>
41 #include <machine/spl.h>
43 #include <kern/queue.h>
45 #include <kern/thread.h>
46 #include <kern/sched_prim.h>
49 #include <kern/cpu_number.h>
50 #include <vm/vm_kern.h>
52 #include <kern/task.h>
53 #include <mach/time_value.h>
57 #include <sys/ktrace.h>
63 register struct proc
*p
;
64 register thread_t thread
= current_thread();
70 th_act
= current_act();
71 ut
= get_bsdthread_info(th_act
);
72 catch = ut
->uu_pri
& PCATCH
;
76 thread
->wait_mesg
= NULL
;
78 switch (get_thread_waitresult(thread
)) {
79 case THREAD_TIMED_OUT
:
84 * Posix implies any signal should be delivered
85 * first, regardless of whether awakened due
90 /* else fall through */
91 case THREAD_INTERRUPTED
:
93 if (thread_should_abort(current_thread())) {
95 } else if (SHOULDissignal(p
,ut
)) {
96 if (sig
= CURSIG(p
)) {
97 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
102 if (thread_should_abort(current_thread())) {
111 if (error
== EINTR
|| error
== ERESTART
)
112 act_set_astbsd(th_act
);
115 thread_cancel_timer();
118 if (KTRPOINT(p
, KTR_CSW
))
119 ktrcsw(p
->p_tracep
, 0, 0, -1);
122 unix_syscall_return((*ut
->uu_continuation
)(error
));
126 * Give up the processor till a wakeup occurs
127 * on chan, at which time the process
128 * enters the scheduling queue at priority pri.
129 * The most important effect of pri is that when
130 * pri<=PZERO a signal cannot disturb the sleep;
131 * if pri>PZERO signals will be processed.
132 * If pri&PCATCH is set, signals will cause sleep
133 * to return 1, rather than longjmp.
134 * Callers of this routine must be prepared for
135 * premature return, and check that the reason for
136 * sleeping has gone away.
145 int (*continuation
)(int))
147 register struct proc
*p
;
148 register thread_t thread
= current_thread();
151 int sig
, catch = pri
& PCATCH
;
152 int sigttblock
= pri
& PTTYBLOCK
;
159 th_act
= current_act();
160 ut
= get_bsdthread_info(th_act
);
164 if (KTRPOINT(p
, KTR_CSW
))
165 ktrcsw(p
->p_tracep
, 1, 0, -1);
167 p
->p_priority
= pri
& PRIMASK
;
170 wait_result
= assert_wait(chan
,
171 (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
174 thread_set_timer_deadline(abstime
);
177 * We start our timeout
178 * before calling CURSIG, as we could stop there, and a wakeup
179 * or a SIGCONT (or both) could occur while we were stopped.
180 * A SIGCONT would cause us to be marked as SSLEEP
181 * without resuming us, thus we must be ready for sleep
182 * when CURSIG is called. If the wakeup happens while we're
183 * stopped, p->p_wchan will be 0 upon return from CURSIG.
186 if (SHOULDissignal(p
,ut
)) {
187 if (sig
= CURSIG(p
)) {
188 clear_wait(thread
, THREAD_INTERRUPTED
);
189 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
190 if (sigttblock
&& ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
191 p
->p_flag
|= P_TTYSLEEP
;
192 /* reset signal bits */
193 clear_procsiglist(p
, sig
);
194 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
195 /* assert wait can block and SIGCONT should be checked */
196 if (p
->p_flag
& P_TTYSLEEP
)
197 thread_block(THREAD_CONTINUE_NULL
);
198 /* return with success */
202 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
209 if (thread_should_abort(current_thread())) {
210 clear_wait(thread
, THREAD_INTERRUPTED
);
214 if (get_thread_waitresult(thread
) != THREAD_WAITING
) {
215 /*already happened */
221 thread
->wait_mesg
= wmsg
;
224 p
->p_stats
->p_ru
.ru_nvcsw
++;
226 if (continuation
!= THREAD_CONTINUE_NULL
) {
227 ut
->uu_continuation
= continuation
;
229 ut
->uu_timo
= abstime
? 1: 0;
230 (void) thread_block(_sleep_continue
);
234 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
237 thread
->wait_mesg
= NULL
;
239 switch (wait_result
) {
240 case THREAD_TIMED_OUT
:
243 case THREAD_AWAKENED
:
245 * Posix implies any signal should be delivered
246 * first, regardless of whether awakened due
247 * to receiving event.
251 /* else fall through */
252 case THREAD_INTERRUPTED
:
254 if (thread_should_abort(current_thread())) {
256 } else if (SHOULDissignal(p
,ut
)) {
257 if (sig
= CURSIG(p
)) {
258 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
263 if (thread_should_abort(current_thread())) {
272 if (error
== EINTR
|| error
== ERESTART
)
273 act_set_astbsd(th_act
);
275 thread_cancel_timer();
278 if (KTRPOINT(p
, KTR_CSW
))
279 ktrcsw(p
->p_tracep
, 0, 0, -1);
289 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0);
299 u_int64_t abstime
= 0;
302 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
303 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0);
312 int (*continuation
)(int))
314 u_int64_t abstime
= 0;
317 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
318 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
327 int (*continuation
)(int))
329 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
333 * Wake up all processes sleeping on chan.
339 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
343 * Wake up the first process sleeping on chan.
345 * Be very sure that the first process is really
346 * the right one to wakeup.
350 register caddr_t chan
;
352 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
356 * Compute the priority of a process when running in user mode.
357 * Arrange to reschedule if the resulting priority is better
358 * than that of the current process.
362 register struct proc
*p
;
364 (void)task_importance(p
->task
, -p
->p_nice
);
367 struct loadavg averunnable
=
368 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
370 * Constants for averages over 1, 5, and 15 minutes
371 * when sampling at 5 second intervals.
373 static fixpt_t cexp
[3] = {
374 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
375 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
376 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
384 struct loadavg
*avg
= &averunnable
;
386 for (i
= 0; i
< 3; i
++)
387 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
388 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;