]>
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>
31 #include <sys/proc_internal.h>
33 #include <sys/file_internal.h>
34 #include <sys/vnode.h>
35 #include <sys/kernel.h>
37 #include <machine/spl.h>
39 #include <kern/queue.h>
41 #include <kern/thread.h>
42 #include <kern/sched_prim.h>
45 #include <kern/cpu_number.h>
46 #include <vm/vm_kern.h>
48 #include <kern/task.h>
49 #include <mach/time_value.h>
50 #include <kern/lock.h>
55 #include <sys/ktrace.h>
61 wait_result_t wresult
)
63 register struct proc
*p
= current_proc();
64 register thread_t self
= current_thread();
70 ut
= get_bsdthread_info(self
);
71 catch = ut
->uu_pri
& PCATCH
;
72 dropmutex
= ut
->uu_pri
& PDROP
;
75 case THREAD_TIMED_OUT
:
80 * Posix implies any signal should be delivered
81 * first, regardless of whether awakened due
86 /* else fall through */
87 case THREAD_INTERRUPTED
:
89 if (thread_should_abort(self
)) {
91 } else if (SHOULDissignal(p
,ut
)) {
92 if (sig
= CURSIG(p
)) {
93 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
98 if (thread_should_abort(self
)) {
101 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
102 /* due to thread cancel */
110 if (error
== EINTR
|| error
== ERESTART
)
111 act_set_astbsd(self
);
114 if (KTRPOINT(p
, KTR_CSW
))
115 ktrcsw(p
->p_tracep
, 0, 0);
117 if (ut
->uu_mtx
&& !dropmutex
)
118 lck_mtx_lock(ut
->uu_mtx
);
120 unix_syscall_return((*ut
->uu_continuation
)(error
));
124 * Give up the processor till a wakeup occurs
125 * on chan, at which time the process
126 * enters the scheduling queue at priority pri.
127 * The most important effect of pri is that when
128 * pri<=PZERO a signal cannot disturb the sleep;
129 * if pri>PZERO signals will be processed.
130 * If pri&PCATCH is set, signals will cause sleep
131 * to return 1, rather than longjmp.
132 * Callers of this routine must be prepared for
133 * premature return, and check that the reason for
134 * sleeping has gone away.
136 * if msleep was the entry point, than we have a mutex to deal with
138 * The mutex is unlocked before the caller is blocked, and
139 * relocked before msleep returns unless the priority includes the PDROP
140 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
141 * regardless of whether it actually blocked or not.
150 int (*continuation
)(int),
153 register struct proc
*p
;
154 register thread_t self
= current_thread();
156 int sig
, catch = pri
& PCATCH
;
157 int dropmutex
= pri
& PDROP
;
161 ut
= get_bsdthread_info(self
);
165 if (KTRPOINT(p
, KTR_CSW
))
166 ktrcsw(p
->p_tracep
, 1, 0);
168 p
->p_priority
= pri
& PRIMASK
;
169 p
->p_stats
->p_ru
.ru_nvcsw
++;
171 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
174 wait_result
= lck_mtx_sleep_deadline(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
175 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
177 wait_result
= lck_mtx_sleep(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
178 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
182 assert_wait_deadline(chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
186 if (SHOULDissignal(p
,ut
)) {
187 if (sig
= CURSIG(p
)) {
188 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
190 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
191 if ((pri
& PTTYBLOCK
) && ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
192 p
->p_flag
|= P_TTYSLEEP
;
193 /* reset signal bits */
194 clear_procsiglist(p
, sig
);
195 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
196 /* assert wait can block and SIGCONT should be checked */
197 if (p
->p_flag
& P_TTYSLEEP
) {
198 thread_block(THREAD_CONTINUE_NULL
);
200 if (mtx
&& !dropmutex
)
204 /* return with success */
208 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
212 if (mtx
&& !dropmutex
)
217 if (thread_should_abort(self
)) {
218 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
222 if (mtx
&& !dropmutex
)
230 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
231 ut
->uu_continuation
= continuation
;
233 ut
->uu_timo
= abstime
? 1: 0;
235 (void) thread_block(_sleep_continue
);
239 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
241 if (mtx
&& !dropmutex
)
245 switch (wait_result
) {
246 case THREAD_TIMED_OUT
:
249 case THREAD_AWAKENED
:
251 * Posix implies any signal should be delivered
252 * first, regardless of whether awakened due
253 * to receiving event.
257 /* else fall through */
258 case THREAD_INTERRUPTED
:
260 if (thread_should_abort(self
)) {
262 } else if (SHOULDissignal(p
, ut
)) {
263 if (sig
= CURSIG(p
)) {
264 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
269 if (thread_should_abort(self
)) {
278 if (error
== EINTR
|| error
== ERESTART
)
279 act_set_astbsd(self
);
282 if (KTRPOINT(p
, KTR_CSW
))
283 ktrcsw(p
->p_tracep
, 0, 0);
293 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
303 int (*continuation
)(int))
305 u_int64_t abstime
= 0;
308 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
310 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
321 u_int64_t abstime
= 0;
323 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
324 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
325 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
328 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
339 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
349 u_int64_t abstime
= 0;
352 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
353 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
362 int (*continuation
)(int))
364 u_int64_t abstime
= 0;
367 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
368 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
377 int (*continuation
)(int))
379 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
383 * Wake up all processes sleeping on chan.
389 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
393 * Wake up the first process sleeping on chan.
395 * Be very sure that the first process is really
396 * the right one to wakeup.
400 register caddr_t chan
;
402 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
406 * Compute the priority of a process when running in user mode.
407 * Arrange to reschedule if the resulting priority is better
408 * than that of the current process.
412 register struct proc
*p
;
414 (void)task_importance(p
->task
, -p
->p_nice
);
417 struct loadavg averunnable
=
418 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
420 * Constants for averages over 1, 5, and 15 minutes
421 * when sampling at 5 second intervals.
423 static fixpt_t cexp
[3] = {
424 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
425 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
426 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
433 unsigned int nrun
= *(unsigned int *)arg
;
434 struct loadavg
*avg
= &averunnable
;
437 for (i
= 0; i
< 3; i
++)
438 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
439 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;