2 * Copyright (c) 2000-2009 Apple 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 #include <mach/boolean.h>
60 #include <mach/thread_switch.h>
61 #include <ipc/ipc_port.h>
62 #include <ipc/ipc_space.h>
63 #include <kern/counters.h>
64 #include <kern/ipc_kobject.h>
65 #include <kern/processor.h>
66 #include <kern/sched.h>
67 #include <kern/sched_prim.h>
69 #include <kern/task.h>
70 #include <kern/thread.h>
71 #include <mach/policy.h>
73 #include <kern/syscall_subr.h>
74 #include <mach/mach_host_server.h>
75 #include <mach/mach_syscalls.h>
76 #include <sys/kdebug.h>
79 extern void workqueue_thread_yielded(void);
80 extern sched_call_t
workqueue_get_sched_callback(void);
84 /* Called from commpage to take a delayed preemption when exiting
85 * the "Preemption Free Zone" (PFZ).
89 __unused
struct pfz_exit_args
*args
)
91 /* For now, nothing special to do. We'll pick up the ASTs on kernel exit. */
93 return (KERN_SUCCESS
);
98 * swtch and swtch_pri both attempt to context switch (logic in
99 * thread_block no-ops the context switch if nothing would happen).
100 * A boolean is returned that indicates whether there is anything
101 * else runnable. That's no excuse to spin, though.
107 register processor_t myprocessor
;
110 disable_preemption();
111 myprocessor
= current_processor();
112 result
= !SCHED(processor_queue_empty
)(myprocessor
) || rt_runq
.count
> 0;
115 thread_syscall_return(result
);
121 __unused
struct swtch_args
*args
)
123 register processor_t myprocessor
;
126 disable_preemption();
127 myprocessor
= current_processor();
128 if (SCHED(processor_queue_empty
)(myprocessor
) && rt_runq
.count
== 0) {
129 mp_enable_preemption();
135 counter(c_swtch_block
++);
137 thread_block_reason((thread_continue_t
)swtch_continue
, NULL
, AST_YIELD
);
139 disable_preemption();
140 myprocessor
= current_processor();
141 result
= !SCHED(processor_queue_empty
)(myprocessor
) || rt_runq
.count
> 0;
148 swtch_pri_continue(void)
150 register processor_t myprocessor
;
153 thread_depress_abort_internal(current_thread());
155 disable_preemption();
156 myprocessor
= current_processor();
157 result
= !SCHED(processor_queue_empty
)(myprocessor
) || rt_runq
.count
> 0;
158 mp_enable_preemption();
160 thread_syscall_return(result
);
166 __unused
struct swtch_pri_args
*args
)
168 register processor_t myprocessor
;
171 disable_preemption();
172 myprocessor
= current_processor();
173 if (SCHED(processor_queue_empty
)(myprocessor
) && rt_runq
.count
== 0) {
174 mp_enable_preemption();
180 counter(c_swtch_pri_block
++);
182 thread_depress_abstime(thread_depress_time
);
184 thread_block_reason((thread_continue_t
)swtch_pri_continue
, NULL
, AST_YIELD
);
186 thread_depress_abort_internal(current_thread());
188 disable_preemption();
189 myprocessor
= current_processor();
190 result
= !SCHED(processor_queue_empty
)(myprocessor
) || rt_runq
.count
> 0;
197 thread_switch_disable_workqueue_sched_callback(void)
199 sched_call_t callback
= workqueue_get_sched_callback();
200 thread_t self
= current_thread();
201 if (!callback
|| self
->sched_call
!= callback
) {
204 spl_t s
= splsched();
206 thread_sched_call(self
, NULL
);
213 thread_switch_enable_workqueue_sched_callback(void)
215 sched_call_t callback
= workqueue_get_sched_callback();
216 thread_t self
= current_thread();
217 spl_t s
= splsched();
219 thread_sched_call(self
, callback
);
225 thread_switch_continue(void)
227 register thread_t self
= current_thread();
228 int option
= self
->saved
.swtch
.option
;
229 boolean_t reenable_workq_callback
= self
->saved
.swtch
.reenable_workq_callback
;
232 if (option
== SWITCH_OPTION_DEPRESS
|| option
== SWITCH_OPTION_OSLOCK_DEPRESS
)
233 thread_depress_abort_internal(self
);
235 if (reenable_workq_callback
)
236 thread_switch_enable_workqueue_sched_callback();
238 thread_syscall_return(KERN_SUCCESS
);
245 * Context switch. User may supply thread hint.
249 struct thread_switch_args
*args
)
251 thread_t thread
= THREAD_NULL
;
252 thread_t self
= current_thread();
253 mach_port_name_t thread_name
= args
->thread_name
;
254 int option
= args
->option
;
255 mach_msg_timeout_t option_time
= args
->option_time
;
256 uint32_t scale_factor
= NSEC_PER_MSEC
;
257 boolean_t reenable_workq_callback
= FALSE
;
258 boolean_t depress_option
= FALSE
;
259 boolean_t wait_option
= FALSE
;
262 * Validate and process option.
266 case SWITCH_OPTION_NONE
:
267 workqueue_thread_yielded();
269 case SWITCH_OPTION_WAIT
:
271 workqueue_thread_yielded();
273 case SWITCH_OPTION_DEPRESS
:
274 depress_option
= TRUE
;
275 workqueue_thread_yielded();
277 case SWITCH_OPTION_DISPATCH_CONTENTION
:
278 scale_factor
= NSEC_PER_USEC
;
280 if (thread_switch_disable_workqueue_sched_callback())
281 reenable_workq_callback
= TRUE
;
283 case SWITCH_OPTION_OSLOCK_DEPRESS
:
284 depress_option
= TRUE
;
285 if (thread_switch_disable_workqueue_sched_callback())
286 reenable_workq_callback
= TRUE
;
288 case SWITCH_OPTION_OSLOCK_WAIT
:
290 if (thread_switch_disable_workqueue_sched_callback())
291 reenable_workq_callback
= TRUE
;
294 return (KERN_INVALID_ARGUMENT
);
298 * Translate the port name if supplied.
300 if (thread_name
!= MACH_PORT_NULL
) {
303 if (ipc_port_translate_send(self
->task
->itk_space
,
304 thread_name
, &port
) == KERN_SUCCESS
) {
308 thread
= convert_port_to_thread(port
);
311 if (thread
== self
) {
312 thread_deallocate(thread
);
313 thread
= THREAD_NULL
;
318 if (option
== SWITCH_OPTION_OSLOCK_DEPRESS
|| option
== SWITCH_OPTION_OSLOCK_WAIT
) {
319 if (thread
!= THREAD_NULL
) {
321 if (thread
->task
!= self
->task
) {
323 * OSLock boosting only applies to other threads
324 * in your same task (even if you have a port for
325 * a thread in another task)
328 thread_deallocate(thread
);
329 thread
= THREAD_NULL
;
332 * Attempt to kick the lock owner up to our same IO throttling tier.
333 * If the thread is currently blocked in throttle_lowpri_io(),
334 * it will immediately break out.
336 * TODO: SFI break out?
338 int new_policy
= proc_get_effective_thread_policy(self
, TASK_POLICY_IO
);
340 set_thread_iotier_override(thread
, new_policy
);
346 * Try to handoff if supplied.
348 if (thread
!= THREAD_NULL
) {
349 spl_t s
= splsched();
351 /* This may return a different thread if the target is pushing on something */
352 thread_t pulled_thread
= thread_run_queue_remove_for_handoff(thread
);
354 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED_THREAD_SWITCH
)|DBG_FUNC_NONE
,
355 thread_tid(thread
), thread
->state
,
356 pulled_thread
? TRUE
: FALSE
, 0, 0);
358 if (pulled_thread
!= THREAD_NULL
) {
359 /* We can't be dropping the last ref here */
360 thread_deallocate_safe(thread
);
363 assert_wait_timeout((event_t
)assert_wait_timeout
, THREAD_ABORTSAFE
,
364 option_time
, scale_factor
);
365 else if (depress_option
)
366 thread_depress_ms(option_time
);
368 self
->saved
.swtch
.option
= option
;
369 self
->saved
.swtch
.reenable_workq_callback
= reenable_workq_callback
;
371 thread_run(self
, (thread_continue_t
)thread_switch_continue
, NULL
, pulled_thread
);
373 panic("returned from thread_run!");
378 thread_deallocate(thread
);
382 assert_wait_timeout((event_t
)assert_wait_timeout
, THREAD_ABORTSAFE
, option_time
, scale_factor
);
383 else if (depress_option
)
384 thread_depress_ms(option_time
);
386 self
->saved
.swtch
.option
= option
;
387 self
->saved
.swtch
.reenable_workq_callback
= reenable_workq_callback
;
389 thread_block_reason((thread_continue_t
)thread_switch_continue
, NULL
, AST_YIELD
);
392 thread_depress_abort_internal(self
);
394 if (reenable_workq_callback
)
395 thread_switch_enable_workqueue_sched_callback();
397 return (KERN_SUCCESS
);
401 * Depress thread's priority to lowest possible for the specified interval,
402 * with a value of zero resulting in no timeout being scheduled.
405 thread_depress_abstime(
408 register thread_t self
= current_thread();
414 if (!(self
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
)) {
415 processor_t myprocessor
= self
->last_processor
;
417 self
->sched_pri
= DEPRESSPRI
;
419 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHANGE_PRIORITY
),
420 (uintptr_t)thread_tid(self
),
423 0, /* eventually, 'reason' */
426 myprocessor
->current_pri
= self
->sched_pri
;
427 self
->sched_flags
|= TH_SFLAG_DEPRESS
;
430 clock_absolutetime_interval_to_deadline(interval
, &deadline
);
431 if (!timer_call_enter(&self
->depress_timer
, deadline
, TIMER_CALL_USER_CRITICAL
))
432 self
->depress_timer_active
++;
441 mach_msg_timeout_t interval
)
445 clock_interval_to_absolutetime_interval(
446 interval
, NSEC_PER_MSEC
, &abstime
);
447 thread_depress_abstime(abstime
);
451 * Priority depression expiration.
454 thread_depress_expire(
458 thread_t thread
= p0
;
463 if (--thread
->depress_timer_active
== 0) {
464 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
465 thread_recompute_sched_pri(thread
, FALSE
);
467 thread_unlock(thread
);
472 * Prematurely abort priority depression if there is one.
475 thread_depress_abort_internal(
478 kern_return_t result
= KERN_NOT_DEPRESSED
;
483 if (!(thread
->sched_flags
& TH_SFLAG_POLLDEPRESS
)) {
484 if (thread
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
) {
485 thread
->sched_flags
&= ~TH_SFLAG_DEPRESSED_MASK
;
486 thread_recompute_sched_pri(thread
, FALSE
);
487 result
= KERN_SUCCESS
;
490 if (timer_call_cancel(&thread
->depress_timer
))
491 thread
->depress_timer_active
--;
493 thread_unlock(thread
);
505 assert(self
== current_thread());
508 if (self
->sched_mode
== TH_MODE_FIXED
) {
509 uint64_t total_computation
, abstime
;
511 abstime
= mach_absolute_time();
512 total_computation
= abstime
- self
->computation_epoch
;
513 total_computation
+= self
->computation_metered
;
514 if (total_computation
>= max_poll_computation
) {
515 processor_t myprocessor
= current_processor();
519 if (!(self
->sched_flags
& TH_SFLAG_DEPRESSED_MASK
)) {
520 self
->sched_pri
= DEPRESSPRI
;
522 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_SCHED_CHANGE_PRIORITY
),
523 (uintptr_t)thread_tid(self
),
526 0, /* eventually, 'reason' */
529 myprocessor
->current_pri
= self
->sched_pri
;
531 self
->computation_epoch
= abstime
;
532 self
->computation_metered
= 0;
533 self
->sched_flags
|= TH_SFLAG_POLLDEPRESS
;
535 abstime
+= (total_computation
>> sched_poll_yield_shift
);
536 if (!timer_call_enter(&self
->depress_timer
, abstime
, TIMER_CALL_USER_CRITICAL
))
537 self
->depress_timer_active
++;
539 if ((preempt
= csw_check(myprocessor
, AST_NONE
)) != AST_NONE
)
550 thread_yield_internal(
551 mach_msg_timeout_t ms
)
553 processor_t myprocessor
;
555 disable_preemption();
556 myprocessor
= current_processor();
557 if (SCHED(processor_queue_empty
)(myprocessor
) && rt_runq
.count
== 0) {
558 mp_enable_preemption();
564 thread_depress_ms(ms
);
566 thread_block_reason(THREAD_CONTINUE_NULL
, NULL
, AST_YIELD
);
568 thread_depress_abort_internal(current_thread());