]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
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 self
= current_act();
69 ut
= get_bsdthread_info(self
);
70 catch = ut
->uu_pri
& PCATCH
;
73 switch (get_thread_waitresult(self
)) {
74 case THREAD_TIMED_OUT
:
79 * Posix implies any signal should be delivered
80 * first, regardless of whether awakened due
85 /* else fall through */
86 case THREAD_INTERRUPTED
:
88 if (thread_should_abort(self
)) {
90 } else if (SHOULDissignal(p
,ut
)) {
91 if (sig
= CURSIG(p
)) {
92 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
97 if (thread_should_abort(self
)) {
106 if (error
== EINTR
|| error
== ERESTART
)
107 act_set_astbsd(self
);
110 thread_cancel_timer();
113 if (KTRPOINT(p
, KTR_CSW
))
114 ktrcsw(p
->p_tracep
, 0, 0, -1);
117 unix_syscall_return((*ut
->uu_continuation
)(error
));
121 * Give up the processor till a wakeup occurs
122 * on chan, at which time the process
123 * enters the scheduling queue at priority pri.
124 * The most important effect of pri is that when
125 * pri<=PZERO a signal cannot disturb the sleep;
126 * if pri>PZERO signals will be processed.
127 * If pri&PCATCH is set, signals will cause sleep
128 * to return 1, rather than longjmp.
129 * Callers of this routine must be prepared for
130 * premature return, and check that the reason for
131 * sleeping has gone away.
140 int (*continuation
)(int))
142 register struct proc
*p
;
143 register thread_t self
= current_act();
145 int sig
, catch = pri
& PCATCH
;
146 int sigttblock
= pri
& PTTYBLOCK
;
153 ut
= get_bsdthread_info(self
);
157 if (KTRPOINT(p
, KTR_CSW
))
158 ktrcsw(p
->p_tracep
, 1, 0, -1);
160 p
->p_priority
= pri
& PRIMASK
;
163 assert_wait_prim(chan
, NULL
, abstime
,
164 (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
167 thread_set_timer_deadline(abstime
);
170 * We start our timeout
171 * before calling CURSIG, as we could stop there, and a wakeup
172 * or a SIGCONT (or both) could occur while we were stopped.
173 * A SIGCONT would cause us to be marked as SSLEEP
174 * without resuming us, thus we must be ready for sleep
175 * when CURSIG is called. If the wakeup happens while we're
176 * stopped, p->p_wchan will be 0 upon return from CURSIG.
179 if (SHOULDissignal(p
,ut
)) {
180 if (sig
= CURSIG(p
)) {
181 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
183 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
184 if (sigttblock
&& ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
185 p
->p_flag
|= P_TTYSLEEP
;
186 /* reset signal bits */
187 clear_procsiglist(p
, sig
);
188 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
189 /* assert wait can block and SIGCONT should be checked */
190 if (p
->p_flag
& P_TTYSLEEP
)
191 thread_block(THREAD_CONTINUE_NULL
);
192 /* return with success */
196 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
203 if (thread_should_abort(self
)) {
204 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
209 if (get_thread_waitresult(self
) != THREAD_WAITING
) {
210 /*already happened */
218 p
->p_stats
->p_ru
.ru_nvcsw
++;
220 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
221 ut
->uu_continuation
= continuation
;
223 ut
->uu_timo
= abstime
? 1: 0;
224 (void) thread_block(_sleep_continue
);
228 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
230 switch (wait_result
) {
231 case THREAD_TIMED_OUT
:
234 case THREAD_AWAKENED
:
236 * Posix implies any signal should be delivered
237 * first, regardless of whether awakened due
238 * to receiving event.
242 /* else fall through */
243 case THREAD_INTERRUPTED
:
245 if (thread_should_abort(self
)) {
247 } else if (SHOULDissignal(p
,ut
)) {
248 if (sig
= CURSIG(p
)) {
249 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
254 if (thread_should_abort(self
)) {
263 if (error
== EINTR
|| error
== ERESTART
)
264 act_set_astbsd(self
);
266 thread_cancel_timer();
269 if (KTRPOINT(p
, KTR_CSW
))
270 ktrcsw(p
->p_tracep
, 0, 0, -1);
280 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0);
290 u_int64_t abstime
= 0;
293 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
294 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0);
303 int (*continuation
)(int))
305 u_int64_t abstime
= 0;
308 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
309 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
318 int (*continuation
)(int))
320 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
324 * Wake up all processes sleeping on chan.
330 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
334 * Wake up the first process sleeping on chan.
336 * Be very sure that the first process is really
337 * the right one to wakeup.
341 register caddr_t chan
;
343 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
347 * Compute the priority of a process when running in user mode.
348 * Arrange to reschedule if the resulting priority is better
349 * than that of the current process.
353 register struct proc
*p
;
355 (void)task_importance(p
->task
, -p
->p_nice
);
358 struct loadavg averunnable
=
359 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
361 * Constants for averages over 1, 5, and 15 minutes
362 * when sampling at 5 second intervals.
364 static fixpt_t cexp
[3] = {
365 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
366 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
367 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
375 struct loadavg
*avg
= &averunnable
;
377 for (i
= 0; i
< 3; i
++)
378 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
379 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;