]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Mach Operating System
24 * Copyright (c) 1987 Carnegie-Mellon University
25 * All rights reserved. The CMU software License Agreement specifies
26 * the terms and conditions for use and redistribution.
29 #include <sys/param.h>
30 #include <sys/systm.h>
34 #include <sys/vnode.h>
35 #include <sys/kernel.h>
38 #include <machine/spl.h>
40 #include <kern/queue.h>
42 #include <kern/thread.h>
43 #include <kern/sched_prim.h>
46 #include <kern/cpu_number.h>
47 #include <vm/vm_kern.h>
49 #include <kern/task.h>
50 #include <mach/time_value.h>
54 #include <sys/ktrace.h>
60 register struct proc
*p
;
61 register thread_t thread
= current_thread();
67 th_act
= current_act();
68 ut
= get_bsdthread_info(th_act
);
69 catch = ut
->uu_pri
& PCATCH
;
73 thread
->wait_mesg
= NULL
;
75 switch (get_thread_waitresult(thread
)) {
76 case THREAD_TIMED_OUT
:
81 * Posix implies any signal should be delivered
82 * first, regardless of whether awakened due
87 /* else fall through */
88 case THREAD_INTERRUPTED
:
90 if (thread_should_abort(current_thread())) {
92 } else if (SHOULDissignal(p
,ut
)) {
93 if (sig
= CURSIG(p
)) {
94 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
99 if (thread_should_abort(current_thread())) {
108 if (error
== EINTR
|| error
== ERESTART
)
109 act_set_astbsd(th_act
);
112 thread_cancel_timer();
115 if (KTRPOINT(p
, KTR_CSW
))
116 ktrcsw(p
->p_tracep
, 0, 0, -1);
119 unix_syscall_return((*ut
->uu_continuation
)(error
));
123 * Give up the processor till a wakeup occurs
124 * on chan, at which time the process
125 * enters the scheduling queue at priority pri.
126 * The most important effect of pri is that when
127 * pri<=PZERO a signal cannot disturb the sleep;
128 * if pri>PZERO signals will be processed.
129 * If pri&PCATCH is set, signals will cause sleep
130 * to return 1, rather than longjmp.
131 * Callers of this routine must be prepared for
132 * premature return, and check that the reason for
133 * sleeping has gone away.
142 int (*continuation
)(int))
144 register struct proc
*p
;
145 register thread_t thread
= current_thread();
148 int sig
, catch = pri
& PCATCH
;
149 int sigttblock
= pri
& PTTYBLOCK
;
156 th_act
= current_act();
157 ut
= get_bsdthread_info(th_act
);
161 if (KTRPOINT(p
, KTR_CSW
))
162 ktrcsw(p
->p_tracep
, 1, 0, -1);
164 p
->p_priority
= pri
& PRIMASK
;
167 wait_result
= assert_wait(chan
,
168 (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
171 thread_set_timer_deadline(abstime
);
174 * We start our timeout
175 * before calling CURSIG, as we could stop there, and a wakeup
176 * or a SIGCONT (or both) could occur while we were stopped.
177 * A SIGCONT would cause us to be marked as SSLEEP
178 * without resuming us, thus we must be ready for sleep
179 * when CURSIG is called. If the wakeup happens while we're
180 * stopped, p->p_wchan will be 0 upon return from CURSIG.
183 if (SHOULDissignal(p
,ut
)) {
184 if (sig
= CURSIG(p
)) {
185 clear_wait(thread
, THREAD_INTERRUPTED
);
186 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
187 if (sigttblock
&& ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
188 p
->p_flag
|= P_TTYSLEEP
;
189 /* reset signal bits */
190 clear_procsiglist(p
, sig
);
191 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
192 /* assert wait can block and SIGCONT should be checked */
193 if (p
->p_flag
& P_TTYSLEEP
)
194 thread_block(THREAD_CONTINUE_NULL
);
195 /* return with success */
199 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
206 if (thread_should_abort(current_thread())) {
207 clear_wait(thread
, THREAD_INTERRUPTED
);
211 if (get_thread_waitresult(thread
) != THREAD_WAITING
) {
212 /*already happened */
218 thread
->wait_mesg
= wmsg
;
221 p
->p_stats
->p_ru
.ru_nvcsw
++;
223 if (continuation
!= THREAD_CONTINUE_NULL
) {
224 ut
->uu_continuation
= continuation
;
226 ut
->uu_timo
= abstime
? 1: 0;
227 (void) thread_block(_sleep_continue
);
231 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
234 thread
->wait_mesg
= NULL
;
236 switch (wait_result
) {
237 case THREAD_TIMED_OUT
:
240 case THREAD_AWAKENED
:
242 * Posix implies any signal should be delivered
243 * first, regardless of whether awakened due
244 * to receiving event.
248 /* else fall through */
249 case THREAD_INTERRUPTED
:
251 if (thread_should_abort(current_thread())) {
253 } else if (SHOULDissignal(p
,ut
)) {
254 if (sig
= CURSIG(p
)) {
255 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
260 if (thread_should_abort(current_thread())) {
269 if (error
== EINTR
|| error
== ERESTART
)
270 act_set_astbsd(th_act
);
272 thread_cancel_timer();
275 if (KTRPOINT(p
, KTR_CSW
))
276 ktrcsw(p
->p_tracep
, 0, 0, -1);
286 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0);
296 u_int64_t abstime
= 0;
299 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
300 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0);
309 int (*continuation
)(int))
311 u_int64_t abstime
= 0;
314 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
315 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
324 int (*continuation
)(int))
326 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
);
330 * Wake up all processes sleeping on chan.
336 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
340 * Wake up the first process sleeping on chan.
342 * Be very sure that the first process is really
343 * the right one to wakeup.
347 register caddr_t chan
;
349 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
353 * Compute the priority of a process when running in user mode.
354 * Arrange to reschedule if the resulting priority is better
355 * than that of the current process.
359 register struct proc
*p
;
361 (void)task_importance(p
->task
, -p
->p_nice
);
364 struct loadavg averunnable
=
365 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
367 * Constants for averages over 1, 5, and 15 minutes
368 * when sampling at 5 second intervals.
370 static fixpt_t cexp
[3] = {
371 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
372 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
373 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
381 struct loadavg
*avg
= &averunnable
;
383 for (i
= 0; i
< 3; i
++)
384 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
385 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;