]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
fea018ff919dafc5d44a6e8182122f797becb005
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Mach Operating System
25 * Copyright (c) 1987 Carnegie-Mellon University
26 * All rights reserved. The CMU software License Agreement specifies
27 * the terms and conditions for use and redistribution.
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/proc_internal.h>
34 #include <sys/file_internal.h>
35 #include <sys/vnode.h>
36 #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>
51 #include <kern/lock.h>
56 #include <sys/ktrace.h>
62 wait_result_t wresult
)
64 register struct proc
*p
= current_proc();
65 register thread_t self
= current_thread();
71 ut
= get_bsdthread_info(self
);
72 catch = ut
->uu_pri
& PCATCH
;
73 dropmutex
= ut
->uu_pri
& PDROP
;
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(self
)) {
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(self
)) {
102 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
103 /* due to thread cancel */
111 if (error
== EINTR
|| error
== ERESTART
)
112 act_set_astbsd(self
);
115 if (KTRPOINT(p
, KTR_CSW
))
116 ktrcsw(p
->p_tracep
, 0, 0);
118 if (ut
->uu_mtx
&& !dropmutex
)
119 lck_mtx_lock(ut
->uu_mtx
);
121 unix_syscall_return((*ut
->uu_continuation
)(error
));
125 * Give up the processor till a wakeup occurs
126 * on chan, at which time the process
127 * enters the scheduling queue at priority pri.
128 * The most important effect of pri is that when
129 * pri<=PZERO a signal cannot disturb the sleep;
130 * if pri>PZERO signals will be processed.
131 * If pri&PCATCH is set, signals will cause sleep
132 * to return 1, rather than longjmp.
133 * Callers of this routine must be prepared for
134 * premature return, and check that the reason for
135 * sleeping has gone away.
137 * if msleep was the entry point, than we have a mutex to deal with
139 * The mutex is unlocked before the caller is blocked, and
140 * relocked before msleep returns unless the priority includes the PDROP
141 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
142 * regardless of whether it actually blocked or not.
151 int (*continuation
)(int),
154 register struct proc
*p
;
155 register thread_t self
= current_thread();
157 int sig
, catch = pri
& PCATCH
;
158 int dropmutex
= pri
& PDROP
;
162 ut
= get_bsdthread_info(self
);
166 if (KTRPOINT(p
, KTR_CSW
))
167 ktrcsw(p
->p_tracep
, 1, 0);
169 p
->p_priority
= pri
& PRIMASK
;
170 p
->p_stats
->p_ru
.ru_nvcsw
++;
172 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
175 wait_result
= lck_mtx_sleep_deadline(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
176 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
178 wait_result
= lck_mtx_sleep(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
179 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
183 assert_wait_deadline(chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
187 if (SHOULDissignal(p
,ut
)) {
188 if (sig
= CURSIG(p
)) {
189 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
191 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
192 if ((pri
& PTTYBLOCK
) && ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
193 p
->p_flag
|= P_TTYSLEEP
;
194 /* reset signal bits */
195 clear_procsiglist(p
, sig
);
196 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
197 /* assert wait can block and SIGCONT should be checked */
198 if (p
->p_flag
& P_TTYSLEEP
) {
199 thread_block(THREAD_CONTINUE_NULL
);
201 if (mtx
&& !dropmutex
)
205 /* return with success */
209 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
213 if (mtx
&& !dropmutex
)
218 if (thread_should_abort(self
)) {
219 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
223 if (mtx
&& !dropmutex
)
231 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
232 ut
->uu_continuation
= continuation
;
234 ut
->uu_timo
= abstime
? 1: 0;
236 (void) thread_block(_sleep_continue
);
240 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
242 if (mtx
&& !dropmutex
)
246 switch (wait_result
) {
247 case THREAD_TIMED_OUT
:
250 case THREAD_AWAKENED
:
252 * Posix implies any signal should be delivered
253 * first, regardless of whether awakened due
254 * to receiving event.
258 /* else fall through */
259 case THREAD_INTERRUPTED
:
261 if (thread_should_abort(self
)) {
263 } else if (SHOULDissignal(p
, ut
)) {
264 if (sig
= CURSIG(p
)) {
265 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
270 if (thread_should_abort(self
)) {
279 if (error
== EINTR
|| error
== ERESTART
)
280 act_set_astbsd(self
);
283 if (KTRPOINT(p
, KTR_CSW
))
284 ktrcsw(p
->p_tracep
, 0, 0);
294 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
304 int (*continuation
)(int))
306 u_int64_t abstime
= 0;
309 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
311 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
322 u_int64_t abstime
= 0;
324 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
325 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
326 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
329 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
340 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
350 u_int64_t abstime
= 0;
353 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
354 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
363 int (*continuation
)(int))
365 u_int64_t abstime
= 0;
368 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
369 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
378 int (*continuation
)(int))
380 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
384 * Wake up all processes sleeping on chan.
390 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
394 * Wake up the first process sleeping on chan.
396 * Be very sure that the first process is really
397 * the right one to wakeup.
401 register caddr_t chan
;
403 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
407 * Compute the priority of a process when running in user mode.
408 * Arrange to reschedule if the resulting priority is better
409 * than that of the current process.
413 register struct proc
*p
;
415 (void)task_importance(p
->task
, -p
->p_nice
);
418 struct loadavg averunnable
=
419 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
421 * Constants for averages over 1, 5, and 15 minutes
422 * when sampling at 5 second intervals.
424 static fixpt_t cexp
[3] = {
425 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
426 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
427 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
434 unsigned int nrun
= *(unsigned int *)arg
;
435 struct loadavg
*avg
= &averunnable
;
438 for (i
= 0; i
< 3; i
++)
439 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
440 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;