]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_synch.c
d8afd780c650351a6b9df994ef040d5d9e776aec
2 * Copyright (c) 2000-2016 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 <kern/queue.h>
45 #include <kern/thread.h>
46 #include <kern/sched_prim.h>
49 #include <kern/cpu_number.h>
50 #include <vm/vm_kern.h>
52 #include <kern/task.h>
53 #include <mach/time_value.h>
54 #include <kern/locks.h>
55 #include <kern/policy_internal.h>
57 #include <sys/systm.h> /* for unix_syscall_return() */
58 #include <libkern/OSAtomic.h>
60 extern void compute_averunnable(void *); /* XXX */
62 __attribute__((noreturn
))
64 _sleep_continue( __unused
void *parameter
, wait_result_t wresult
)
66 struct proc
*p
= current_proc();
67 thread_t self
= current_thread();
71 int dropmutex
, spinmutex
;
73 ut
= get_bsdthread_info(self
);
74 catch = ut
->uu_pri
& PCATCH
;
75 dropmutex
= ut
->uu_pri
& PDROP
;
76 spinmutex
= ut
->uu_pri
& PSPIN
;
79 case THREAD_TIMED_OUT
:
84 * Posix implies any signal should be delivered
85 * first, regardless of whether awakened due
91 /* else fall through */
92 case THREAD_INTERRUPTED
:
94 if (thread_should_abort(self
)) {
96 } else if (SHOULDissignal(p
, ut
)) {
97 if ((sig
= CURSIG(p
)) != 0) {
98 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 */
117 if (error
== EINTR
|| error
== ERESTART
) {
118 act_set_astbsd(self
);
121 if (ut
->uu_mtx
&& !dropmutex
) {
123 lck_mtx_lock_spin(ut
->uu_mtx
);
125 lck_mtx_lock(ut
->uu_mtx
);
131 unix_syscall_return((*ut
->uu_continuation
)(error
));
135 * Give up the processor till a wakeup occurs
136 * on chan, at which time the process
137 * enters the scheduling queue at priority pri.
138 * The most important effect of pri is that when
139 * pri<=PZERO a signal cannot disturb the sleep;
140 * if pri>PZERO signals will be processed.
141 * If pri&PCATCH is set, signals will cause sleep
142 * to return 1, rather than longjmp.
143 * Callers of this routine must be prepared for
144 * premature return, and check that the reason for
145 * sleeping has gone away.
147 * if msleep was the entry point, than we have a mutex to deal with
149 * The mutex is unlocked before the caller is blocked, and
150 * relocked before msleep returns unless the priority includes the PDROP
151 * flag... if PDROP is specified, _sleep returns with the mutex unlocked
152 * regardless of whether it actually blocked or not.
161 int (*continuation
)(int),
165 thread_t self
= current_thread();
168 int dropmutex
= pri
& PDROP
;
169 int spinmutex
= pri
& PSPIN
;
173 ut
= get_bsdthread_info(self
);
176 p
->p_priority
= pri
& PRIMASK
;
177 /* It can still block in proc_exit() after the teardown. */
178 if (p
->p_stats
!= NULL
) {
179 OSIncrementAtomicLong(&p
->p_stats
->p_ru
.ru_nvcsw
);
183 catch = THREAD_ABORTSAFE
;
185 catch = THREAD_UNINT
;
188 /* set wait message & channel */
190 ut
->uu_wmesg
= wmsg
? wmsg
: "unknown";
192 if (mtx
!= NULL
&& chan
!= NULL
&& (thread_continue_t
)continuation
== THREAD_CONTINUE_NULL
) {
196 flags
= LCK_SLEEP_UNLOCK
;
198 flags
= LCK_SLEEP_DEFAULT
;
202 flags
|= LCK_SLEEP_SPIN
;
206 wait_result
= lck_mtx_sleep_deadline(mtx
, flags
, chan
, catch, abstime
);
208 wait_result
= lck_mtx_sleep(mtx
, flags
, chan
, catch);
212 assert_wait_deadline(chan
, catch, abstime
);
218 if (catch == THREAD_ABORTSAFE
) {
219 if (SHOULDissignal(p
, ut
)) {
220 if ((sig
= CURSIG(p
)) != 0) {
221 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
) {
224 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
)) {
229 if (mtx
&& !dropmutex
) {
231 lck_mtx_lock_spin(mtx
);
239 if (thread_should_abort(self
)) {
240 if (clear_wait(self
, THREAD_INTERRUPTED
) == KERN_FAILURE
) {
245 if (mtx
&& !dropmutex
) {
247 lck_mtx_lock_spin(mtx
);
258 if ((thread_continue_t
)continuation
!= THREAD_CONTINUE_NULL
) {
259 ut
->uu_continuation
= continuation
;
261 ut
->uu_timo
= abstime
? 1: 0;
263 (void) thread_block(_sleep_continue
);
267 wait_result
= thread_block(THREAD_CONTINUE_NULL
);
269 if (mtx
&& !dropmutex
) {
271 lck_mtx_lock_spin(mtx
);
278 switch (wait_result
) {
279 case THREAD_TIMED_OUT
:
282 case THREAD_AWAKENED
:
285 * Posix implies any signal should be delivered
286 * first, regardless of whether awakened due
287 * to receiving event.
289 if (catch != THREAD_ABORTSAFE
) {
292 /* else fall through */
293 case THREAD_INTERRUPTED
:
294 if (catch == THREAD_ABORTSAFE
) {
295 if (thread_should_abort(self
)) {
297 } else if (SHOULDissignal(p
, ut
)) {
298 if ((sig
= CURSIG(p
)) != 0) {
299 if (p
->p_sigacts
->ps_sigintr
& sigmask(sig
)) {
305 if (thread_should_abort(self
)) {
308 } else if ((ut
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
) {
309 /* due to thread cancel */
318 if (error
== EINTR
|| error
== ERESTART
) {
319 act_set_astbsd(self
);
332 return _sleep((caddr_t
)chan
, pri
, (char *)NULL
, 0, (int (*)(int))0, (lck_mtx_t
*)0);
342 int (*continuation
)(int))
344 u_int64_t abstime
= 0;
347 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
350 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, mtx
);
361 u_int64_t abstime
= 0;
363 if (ts
&& (ts
->tv_sec
|| ts
->tv_nsec
)) {
364 nanoseconds_to_absolutetime((uint64_t)ts
->tv_sec
* NSEC_PER_SEC
+ ts
->tv_nsec
, &abstime
);
365 clock_absolutetime_interval_to_deadline( abstime
, &abstime
);
368 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
379 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, mtx
);
389 u_int64_t abstime
= 0;
392 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
394 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, (int (*)(int))0, (lck_mtx_t
*)0);
403 int (*continuation
)(int))
405 u_int64_t abstime
= 0;
408 clock_interval_to_deadline(timo
, NSEC_PER_SEC
/ hz
, &abstime
);
410 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
419 int (*continuation
)(int))
421 return _sleep((caddr_t
)chan
, pri
, wmsg
, abstime
, continuation
, (lck_mtx_t
*)0);
425 * Wake up all processes sleeping on chan.
430 thread_wakeup((caddr_t
)chan
);
434 * Wake up the first process sleeping on chan.
436 * Be very sure that the first process is really
437 * the right one to wakeup.
440 wakeup_one(caddr_t chan
)
442 thread_wakeup_one((caddr_t
)chan
);
446 * Compute the priority of a process when running in user mode.
447 * Arrange to reschedule if the resulting priority is better
448 * than that of the current process.
451 resetpriority(struct proc
*p
)
453 (void)task_importance(p
->task
, -p
->p_nice
);
456 struct loadavg averunnable
=
457 { {0, 0, 0}, FSCALE
}; /* load average, of runnable procs */
459 * Constants for averages over 1, 5, and 15 minutes
460 * when sampling at 5 second intervals.
462 static fixpt_t cexp
[3] = {
463 (fixpt_t
)(0.9200444146293232 * FSCALE
), /* exp(-1/12) */
464 (fixpt_t
)(0.9834714538216174 * FSCALE
), /* exp(-1/60) */
465 (fixpt_t
)(0.9944598480048967 * FSCALE
), /* exp(-1/180) */
469 compute_averunnable(void *arg
)
471 unsigned int nrun
= *(unsigned int *)arg
;
472 struct loadavg
*avg
= &averunnable
;
475 for (i
= 0; i
< 3; i
++) {
476 avg
->ldavg
[i
] = (cexp
[i
] * avg
->ldavg
[i
] +
477 nrun
* FSCALE
* (FSCALE
- cexp
[i
])) >> FSHIFT
;