]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
156d25a1e643aa22a3637822d335f0a1172eff06
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Mach Operating System
32 * Copyright (c) 1987 Carnegie-Mellon University
33 * All rights reserved. The CMU software License Agreement specifies
34 * the terms and conditions for use and redistribution.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/proc_internal.h>
41 #include <sys/file_internal.h>
42 #include <sys/vnode.h>
43 #include <sys/kernel.h>
45 #include <machine/spl.h>
47 #include <kern/queue.h>
49 #include <kern/thread.h>
50 #include <kern/sched_prim.h>
53 #include <kern/cpu_number.h>
54 #include <vm/vm_kern.h>
56 #include <kern/task.h>
57 #include <mach/time_value.h>
58 #include <kern/lock.h>
63 #include <sys/ktrace.h>
69 wait_result_t wresult
)
71 register struct proc
*p
= current_proc();
72 register thread_t self
= current_thread();
78 ut
= get_bsdthread_info(self
);
79 catch = ut
->uu_pri
& PCATCH
;
80 dropmutex
= ut
->uu_pri
& PDROP
;
83 case THREAD_TIMED_OUT
:
88 * Posix implies any signal should be delivered
89 * first, regardless of whether awakened due
94 /* else fall through */
95 case THREAD_INTERRUPTED
:
97 if (thread_should_abort(self
)) {
99 } else if (SHOULDissignal(p
,ut
)) {
100 if (sig
= CURSIG(p
)) {
101 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
106 if (thread_should_abort(self
)) {
109 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
110 /* due to thread cancel */
118 if (error
== EINTR
|| error
== ERESTART
)
119 act_set_astbsd(self
);
122 if (KTRPOINT(p
, KTR_CSW
))
123 ktrcsw(p
->p_tracep
, 0, 0);
125 if (ut
->uu_mtx
&& !dropmutex
)
126 lck_mtx_lock(ut
->uu_mtx
);
128 unix_syscall_return((*ut
->uu_continuation
)(error
));
132 * Give up the processor till a wakeup occurs
133 * on chan, at which time the process
134 * enters the scheduling queue at priority pri.
135 * The most important effect of pri is that when
136 * pri<=PZERO a signal cannot disturb the sleep;
137 * if pri>PZERO signals will be processed.
138 * If pri&PCATCH is set, signals will cause sleep
139 * to return 1, rather than longjmp.
140 * Callers of this routine must be prepared for
141 * premature return, and check that the reason for
142 * sleeping has gone away.
144 * if msleep was the entry point, than we have a mutex to deal with
146 * The mutex is unlocked before the caller is blocked, and
147 * relocked before msleep returns unless the priority includes the PDROP
148 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
149 * regardless of whether it actually blocked or not.
158 int (*continuation
)(int),
161 register struct proc
*p
;
162 register thread_t self
= current_thread();
164 int sig
, catch = pri
& PCATCH
;
165 int dropmutex
= pri
& PDROP
;
169 ut
= get_bsdthread_info(self
);
173 if (KTRPOINT(p
, KTR_CSW
))
174 ktrcsw(p
->p_tracep
, 1, 0);
176 p
->p_priority
= pri
& PRIMASK
;
177 p
->p_stats
->p_ru
.ru_nvcsw
++;
179 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
182 wait_result
= lck_mtx_sleep_deadline(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
183 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
185 wait_result
= lck_mtx_sleep(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
186 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
190 assert_wait_deadline(chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
194 if (SHOULDissignal(p
,ut
)) {
195 if (sig
= CURSIG(p
)) {
196 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
198 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
199 if ((pri
& PTTYBLOCK
) && ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
200 p
->p_flag
|= P_TTYSLEEP
;
201 /* reset signal bits */
202 clear_procsiglist(p
, sig
);
203 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
204 /* assert wait can block and SIGCONT should be checked */
205 if (p
->p_flag
& P_TTYSLEEP
) {
206 thread_block(THREAD_CONTINUE_NULL
);
208 if (mtx
&& !dropmutex
)
212 /* return with success */
216 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
220 if (mtx
&& !dropmutex
)
225 if (thread_should_abort(self
)) {
226 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
230 if (mtx
&& !dropmutex
)
238 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
239 ut
->uu_continuation
= continuation
;
241 ut
->uu_timo
= abstime
? 1: 0;
243 (void) thread_block(_sleep_continue
);
247 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
249 if (mtx
&& !dropmutex
)
253 switch (wait_result
) {
254 case THREAD_TIMED_OUT
:
257 case THREAD_AWAKENED
:
259 * Posix implies any signal should be delivered
260 * first, regardless of whether awakened due
261 * to receiving event.
265 /* else fall through */
266 case THREAD_INTERRUPTED
:
268 if (thread_should_abort(self
)) {
270 } else if (SHOULDissignal(p
, ut
)) {
271 if (sig
= CURSIG(p
)) {
272 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
277 if (thread_should_abort(self
)) {
286 if (error
== EINTR
|| error
== ERESTART
)
287 act_set_astbsd(self
);
290 if (KTRPOINT(p
, KTR_CSW
))
291 ktrcsw(p
->p_tracep
, 0, 0);
301 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
311 int (*continuation
)(int))
313 u_int64_t abstime
= 0;
316 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
318 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
329 u_int64_t abstime
= 0;
331 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
332 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
333 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
336 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
347 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
357 u_int64_t abstime
= 0;
360 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
361 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
370 int (*continuation
)(int))
372 u_int64_t abstime
= 0;
375 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
376 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
385 int (*continuation
)(int))
387 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
391 * Wake up all processes sleeping on chan.
397 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
401 * Wake up the first process sleeping on chan.
403 * Be very sure that the first process is really
404 * the right one to wakeup.
408 register caddr_t chan
;
410 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
414 * Compute the priority of a process when running in user mode.
415 * Arrange to reschedule if the resulting priority is better
416 * than that of the current process.
420 register struct proc
*p
;
422 (void)task_importance(p
->task
, -p
->p_nice
);
425 struct loadavg averunnable
=
426 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
428 * Constants for averages over 1, 5, and 15 minutes
429 * when sampling at 5 second intervals.
431 static fixpt_t cexp
[3] = {
432 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
433 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
434 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
441 unsigned int nrun
= *(unsigned int *)arg
;
442 struct loadavg
*avg
= &averunnable
;
445 for (i
= 0; i
< 3; i
++)
446 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
447 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;