]>
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_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc_internal.h>
39 #include <sys/file_internal.h>
40 #include <sys/vnode.h>
41 #include <sys/kernel.h>
43 #include <machine/spl.h>
45 #include <kern/queue.h>
47 #include <kern/thread.h>
48 #include <kern/sched_prim.h>
51 #include <kern/cpu_number.h>
52 #include <vm/vm_kern.h>
54 #include <kern/task.h>
55 #include <mach/time_value.h>
56 #include <kern/lock.h>
61 #include <sys/ktrace.h>
67 wait_result_t wresult
)
69 register struct proc
*p
= current_proc();
70 register thread_t self
= current_thread();
76 ut
= get_bsdthread_info(self
);
77 catch = ut
->uu_pri
& PCATCH
;
78 dropmutex
= ut
->uu_pri
& PDROP
;
81 case THREAD_TIMED_OUT
:
86 * Posix implies any signal should be delivered
87 * first, regardless of whether awakened due
92 /* else fall through */
93 case THREAD_INTERRUPTED
:
95 if (thread_should_abort(self
)) {
97 } else if (SHOULDissignal(p
,ut
)) {
98 if (sig
= CURSIG(p
)) {
99 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
104 if (thread_should_abort(self
)) {
107 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
108 /* due to thread cancel */
116 if (error
== EINTR
|| error
== ERESTART
)
117 act_set_astbsd(self
);
120 if (KTRPOINT(p
, KTR_CSW
))
121 ktrcsw(p
->p_tracep
, 0, 0);
123 if (ut
->uu_mtx
&& !dropmutex
)
124 lck_mtx_lock(ut
->uu_mtx
);
126 unix_syscall_return((*ut
->uu_continuation
)(error
));
130 * Give up the processor till a wakeup occurs
131 * on chan, at which time the process
132 * enters the scheduling queue at priority pri.
133 * The most important effect of pri is that when
134 * pri<=PZERO a signal cannot disturb the sleep;
135 * if pri>PZERO signals will be processed.
136 * If pri&PCATCH is set, signals will cause sleep
137 * to return 1, rather than longjmp.
138 * Callers of this routine must be prepared for
139 * premature return, and check that the reason for
140 * sleeping has gone away.
142 * if msleep was the entry point, than we have a mutex to deal with
144 * The mutex is unlocked before the caller is blocked, and
145 * relocked before msleep returns unless the priority includes the PDROP
146 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
147 * regardless of whether it actually blocked or not.
156 int (*continuation
)(int),
159 register struct proc
*p
;
160 register thread_t self
= current_thread();
162 int sig
, catch = pri
& PCATCH
;
163 int dropmutex
= pri
& PDROP
;
167 ut
= get_bsdthread_info(self
);
171 if (KTRPOINT(p
, KTR_CSW
))
172 ktrcsw(p
->p_tracep
, 1, 0);
174 p
->p_priority
= pri
& PRIMASK
;
175 p
->p_stats
->p_ru
.ru_nvcsw
++;
177 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
180 wait_result
= lck_mtx_sleep_deadline(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
181 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
183 wait_result
= lck_mtx_sleep(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
184 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
188 assert_wait_deadline(chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
192 if (SHOULDissignal(p
,ut
)) {
193 if (sig
= CURSIG(p
)) {
194 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
196 /* if SIGTTOU or SIGTTIN then block till SIGCONT */
197 if ((pri
& PTTYBLOCK
) && ((sig
== SIGTTOU
) || (sig
== SIGTTIN
))) {
198 p
->p_flag
|= P_TTYSLEEP
;
199 /* reset signal bits */
200 clear_procsiglist(p
, sig
);
201 assert_wait(&p
->p_siglist
, THREAD_ABORTSAFE
);
202 /* assert wait can block and SIGCONT should be checked */
203 if (p
->p_flag
& P_TTYSLEEP
) {
204 thread_block(THREAD_CONTINUE_NULL
);
206 if (mtx
&& !dropmutex
)
210 /* return with success */
214 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
218 if (mtx
&& !dropmutex
)
223 if (thread_should_abort(self
)) {
224 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
228 if (mtx
&& !dropmutex
)
236 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
237 ut
->uu_continuation
= continuation
;
239 ut
->uu_timo
= abstime
? 1: 0;
241 (void) thread_block(_sleep_continue
);
245 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
247 if (mtx
&& !dropmutex
)
251 switch (wait_result
) {
252 case THREAD_TIMED_OUT
:
255 case THREAD_AWAKENED
:
257 * Posix implies any signal should be delivered
258 * first, regardless of whether awakened due
259 * to receiving event.
263 /* else fall through */
264 case THREAD_INTERRUPTED
:
266 if (thread_should_abort(self
)) {
268 } else if (SHOULDissignal(p
, ut
)) {
269 if (sig
= CURSIG(p
)) {
270 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
275 if (thread_should_abort(self
)) {
284 if (error
== EINTR
|| error
== ERESTART
)
285 act_set_astbsd(self
);
288 if (KTRPOINT(p
, KTR_CSW
))
289 ktrcsw(p
->p_tracep
, 0, 0);
299 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
309 int (*continuation
)(int))
311 u_int64_t abstime
= 0;
314 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
316 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
327 u_int64_t abstime
= 0;
329 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
330 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
331 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
334 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
345 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
355 u_int64_t abstime
= 0;
358 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
359 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
368 int (*continuation
)(int))
370 u_int64_t abstime
= 0;
373 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
374 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
383 int (*continuation
)(int))
385 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
389 * Wake up all processes sleeping on chan.
395 thread_wakeup_prim((caddr_t
)chan
, FALSE
, THREAD_AWAKENED
);
399 * Wake up the first process sleeping on chan.
401 * Be very sure that the first process is really
402 * the right one to wakeup.
406 register caddr_t chan
;
408 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
412 * Compute the priority of a process when running in user mode.
413 * Arrange to reschedule if the resulting priority is better
414 * than that of the current process.
418 register struct proc
*p
;
420 (void)task_importance(p
->task
, -p
->p_nice
);
423 struct loadavg averunnable
=
424 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
426 * Constants for averages over 1, 5, and 15 minutes
427 * when sampling at 5 second intervals.
429 static fixpt_t cexp
[3] = {
430 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
431 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
432 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
439 unsigned int nrun
= *(unsigned int *)arg
;
440 struct loadavg
*avg
= &averunnable
;
443 for (i
= 0; i
< 3; i
++)
444 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
445 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;