]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
2 * Copyright (c) 2000-2006 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/locks.h>
58 #include <sys/systm.h> /* for unix_syscall_return() */
59 #include <libkern/OSAtomic.h>
61 extern void compute_averunnable(void *); /* XXX */
66 _sleep_continue( __unused
void *parameter
, wait_result_t wresult
)
68 struct proc
*p
= current_proc();
69 thread_t self
= current_thread();
73 int dropmutex
, spinmutex
;
75 ut
= get_bsdthread_info(self
);
76 catch = ut
->uu_pri
& PCATCH
;
77 dropmutex
= ut
->uu_pri
& PDROP
;
78 spinmutex
= ut
->uu_pri
& PSPIN
;
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
)) != 0) {
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
);
119 if (ut
->uu_mtx
&& !dropmutex
) {
121 lck_mtx_lock_spin(ut
->uu_mtx
);
123 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),
162 thread_t self
= current_thread();
165 int dropmutex
= pri
& PDROP
;
166 int spinmutex
= pri
& PSPIN
;
170 ut
= get_bsdthread_info(self
);
173 p
->p_priority
= pri
& PRIMASK
;
174 /* It can still block in proc_exit() after the teardown. */
175 if (p
->p_stats
!= NULL
)
176 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_nvcsw
);
179 catch = THREAD_ABORTSAFE
;
181 catch = THREAD_UNINT
;
183 /* set wait message & channel */
185 ut
->uu_wmesg
= wmsg
? wmsg
: "unknown";
187 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
191 flags
= LCK_SLEEP_UNLOCK
;
193 flags
= LCK_SLEEP_DEFAULT
;
196 flags
|= LCK_SLEEP_SPIN
;
199 wait_result
= lck_mtx_sleep_deadline(mtx
, flags
, chan
, catch, abstime
);
201 wait_result
= lck_mtx_sleep(mtx
, flags
, chan
, catch);
205 assert_wait_deadline(chan
, catch, abstime
);
209 if (catch == THREAD_ABORTSAFE
) {
210 if (SHOULDissignal(p
,ut
)) {
211 if ((sig
= CURSIG(p
)) != 0) {
212 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
214 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
218 if (mtx
&& !dropmutex
) {
220 lck_mtx_lock_spin(mtx
);
227 if (thread_should_abort(self
)) {
228 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
232 if (mtx
&& !dropmutex
) {
234 lck_mtx_lock_spin(mtx
);
244 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
245 ut
->uu_continuation
= continuation
;
247 ut
->uu_timo
= abstime
? 1: 0;
249 (void) thread_block(_sleep_continue
);
253 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
255 if (mtx
&& !dropmutex
) {
257 lck_mtx_lock_spin(mtx
);
263 switch (wait_result
) {
264 case THREAD_TIMED_OUT
:
267 case THREAD_AWAKENED
:
269 * Posix implies any signal should be delivered
270 * first, regardless of whether awakened due
271 * to receiving event.
273 if (catch != THREAD_ABORTSAFE
)
275 /* else fall through */
276 case THREAD_INTERRUPTED
:
277 if (catch == THREAD_ABORTSAFE
) {
278 if (thread_should_abort(self
)) {
280 } else if (SHOULDissignal(p
, ut
)) {
281 if ((sig
= CURSIG(p
)) != 0) {
282 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
287 if (thread_should_abort(self
)) {
290 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
291 /* due to thread cancel */
299 if (error
== EINTR
|| error
== ERESTART
)
300 act_set_astbsd(self
);
312 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
322 int (*continuation
)(int))
324 u_int64_t abstime
= 0;
327 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
329 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
340 u_int64_t abstime
= 0;
342 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
343 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
344 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
347 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
358 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
368 u_int64_t abstime
= 0;
371 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
372 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
381 int (*continuation
)(int))
383 u_int64_t abstime
= 0;
386 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
387 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
396 int (*continuation
)(int))
398 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
402 * Wake up all processes sleeping on chan.
407 thread_wakeup((caddr_t
)chan
);
411 * Wake up the first process sleeping on chan.
413 * Be very sure that the first process is really
414 * the right one to wakeup.
417 wakeup_one(caddr_t chan
)
419 thread_wakeup_one((caddr_t
)chan
);
423 * Compute the priority of a process when running in user mode.
424 * Arrange to reschedule if the resulting priority is better
425 * than that of the current process.
428 resetpriority(struct proc
*p
)
430 (void)task_importance(p
->task
, -p
->p_nice
);
433 struct loadavg averunnable
=
434 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
436 * Constants for averages over 1, 5, and 15 minutes
437 * when sampling at 5 second intervals.
439 static fixpt_t cexp
[3] = {
440 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
441 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
442 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
446 compute_averunnable(void *arg
)
448 unsigned int nrun
= *(unsigned int *)arg
;
449 struct loadavg
*avg
= &averunnable
;
452 for (i
= 0; i
< 3; i
++)
453 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
454 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;