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/lock.h>
58 #include <sys/systm.h> /* for unix_syscall_return() */
59 #include <libkern/OSAtomic.h>
61 extern boolean_t
thread_should_abort(thread_t
); /* XXX */
62 extern void compute_averunnable(void *); /* XXX */
67 _sleep_continue( __unused
void *parameter
, wait_result_t wresult
)
69 struct proc
*p
= current_proc();
70 thread_t self
= current_thread();
74 int dropmutex
, spinmutex
;
76 ut
= get_bsdthread_info(self
);
77 catch = ut
->uu_pri
& PCATCH
;
78 dropmutex
= ut
->uu_pri
& PDROP
;
79 spinmutex
= ut
->uu_pri
& PSPIN
;
82 case THREAD_TIMED_OUT
:
87 * Posix implies any signal should be delivered
88 * first, regardless of whether awakened due
93 /* else fall through */
94 case THREAD_INTERRUPTED
:
96 if (thread_should_abort(self
)) {
98 } else if (SHOULDissignal(p
,ut
)) {
99 if ((sig
= CURSIG(p
)) != 0) {
100 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
105 if (thread_should_abort(self
)) {
108 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
109 /* due to thread cancel */
117 if (error
== EINTR
|| error
== ERESTART
)
118 act_set_astbsd(self
);
120 if (ut
->uu_mtx
&& !dropmutex
) {
122 lck_mtx_lock_spin(ut
->uu_mtx
);
124 lck_mtx_lock(ut
->uu_mtx
);
129 unix_syscall_return((*ut
->uu_continuation
)(error
));
133 * Give up the processor till a wakeup occurs
134 * on chan, at which time the process
135 * enters the scheduling queue at priority pri.
136 * The most important effect of pri is that when
137 * pri<=PZERO a signal cannot disturb the sleep;
138 * if pri>PZERO signals will be processed.
139 * If pri&PCATCH is set, signals will cause sleep
140 * to return 1, rather than longjmp.
141 * Callers of this routine must be prepared for
142 * premature return, and check that the reason for
143 * sleeping has gone away.
145 * if msleep was the entry point, than we have a mutex to deal with
147 * The mutex is unlocked before the caller is blocked, and
148 * relocked before msleep returns unless the priority includes the PDROP
149 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
150 * regardless of whether it actually blocked or not.
159 int (*continuation
)(int),
163 thread_t self
= current_thread();
165 int sig
, catch = pri
& PCATCH
;
166 int dropmutex
= pri
& PDROP
;
167 int spinmutex
= pri
& PSPIN
;
171 ut
= get_bsdthread_info(self
);
174 p
->p_priority
= pri
& PRIMASK
;
175 /* It can still block in proc_exit() after the teardown. */
176 if (p
->p_stats
!= NULL
)
177 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_nvcsw
);
179 /* set wait message & channel */
181 ut
->uu_wmesg
= wmsg
? wmsg
: "unknown";
183 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
186 wait_result
= lck_mtx_sleep_deadline(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
187 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
189 wait_result
= lck_mtx_sleep(mtx
, (dropmutex
) ? LCK_SLEEP_UNLOCK
: 0,
190 chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
);
194 assert_wait_deadline(chan
, (catch) ? THREAD_ABORTSAFE
: THREAD_UNINT
, abstime
);
198 if (SHOULDissignal(p
,ut
)) {
199 if ((sig
= CURSIG(p
)) != 0) {
200 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
202 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
206 if (mtx
&& !dropmutex
) {
208 lck_mtx_lock_spin(mtx
);
215 if (thread_should_abort(self
)) {
216 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
)
220 if (mtx
&& !dropmutex
) {
222 lck_mtx_lock_spin(mtx
);
232 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
233 ut
->uu_continuation
= continuation
;
235 ut
->uu_timo
= abstime
? 1: 0;
237 (void) thread_block(_sleep_continue
);
241 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
243 if (mtx
&& !dropmutex
) {
245 lck_mtx_lock_spin(mtx
);
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
)) != 0) {
270 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
))
275 if (thread_should_abort(self
)) {
278 } else if( (ut
->uu_flag
& ( UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
279 /* due to thread cancel */
287 if (error
== EINTR
|| error
== ERESTART
)
288 act_set_astbsd(self
);
300 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
310 int (*continuation
)(int))
312 u_int64_t abstime
= 0;
315 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
317 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
328 u_int64_t abstime
= 0;
330 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
331 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
332 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
335 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
346 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
356 u_int64_t abstime
= 0;
359 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
360 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
369 int (*continuation
)(int))
371 u_int64_t abstime
= 0;
374 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
375 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
384 int (*continuation
)(int))
386 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
390 * 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.
405 wakeup_one(caddr_t chan
)
407 thread_wakeup_prim((caddr_t
)chan
, TRUE
, THREAD_AWAKENED
);
411 * Compute the priority of a process when running in user mode.
412 * Arrange to reschedule if the resulting priority is better
413 * than that of the current process.
416 resetpriority(struct proc
*p
)
418 (void)task_importance(p
->task
, -p
->p_nice
);
421 struct loadavg averunnable
=
422 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
424 * Constants for averages over 1, 5, and 15 minutes
425 * when sampling at 5 second intervals.
427 static fixpt_t cexp
[3] = {
428 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
429 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
430 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
434 compute_averunnable(void *arg
)
436 unsigned int nrun
= *(unsigned int *)arg
;
437 struct loadavg
*avg
= &averunnable
;
440 for (i
= 0; i
< 3; i
++)
441 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
442 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;