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@
29 * @OSF_FREE_COPYRIGHT@
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.
60 * Author: Avadis Tevanian, Jr.
63 * Scheduling primitives
70 #include <ddb/db_output.h>
72 #include <mach/mach_types.h>
73 #include <mach/machine.h>
74 #include <mach/policy.h>
75 #include <mach/sync_policy.h>
77 #include <machine/machine_routines.h>
78 #include <machine/sched_param.h>
79 #include <machine/machine_cpu.h>
81 #include <kern/kern_types.h>
82 #include <kern/clock.h>
83 #include <kern/counters.h>
84 #include <kern/cpu_number.h>
85 #include <kern/cpu_data.h>
86 #include <kern/debug.h>
87 #include <kern/lock.h>
88 #include <kern/macro_help.h>
89 #include <kern/machine.h>
90 #include <kern/misc_protos.h>
91 #include <kern/processor.h>
92 #include <kern/queue.h>
93 #include <kern/sched.h>
94 #include <kern/sched_prim.h>
95 #include <kern/syscall_subr.h>
96 #include <kern/task.h>
97 #include <kern/thread.h>
98 #include <kern/wait_queue.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_map.h>
104 #include <mach/sdt.h>
106 #include <sys/kdebug.h>
108 #include <kern/pms.h>
110 struct run_queue rt_runq
;
111 #define RT_RUNQ ((processor_t)-1)
112 decl_simple_lock_data(static,rt_lock
);
114 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
115 int default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
117 #define MAX_UNSAFE_QUANTA 800
118 int max_unsafe_quanta
= MAX_UNSAFE_QUANTA
;
120 #define MAX_POLL_QUANTA 2
121 int max_poll_quanta
= MAX_POLL_QUANTA
;
123 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
124 int sched_poll_yield_shift
= SCHED_POLL_YIELD_SHIFT
;
126 uint64_t max_unsafe_computation
;
127 uint32_t sched_safe_duration
;
128 uint64_t max_poll_computation
;
130 uint32_t std_quantum
;
131 uint32_t min_std_quantum
;
133 uint32_t std_quantum_us
;
135 uint32_t max_rt_quantum
;
136 uint32_t min_rt_quantum
;
138 uint32_t sched_cswtime
;
141 uint32_t sched_tick_interval
;
143 uint32_t sched_pri_shift
= INT8_MAX
;
144 uint32_t sched_fixed_shift
;
146 uint32_t sched_run_count
, sched_share_count
;
147 uint32_t sched_load_average
, sched_mach_factor
;
150 static void load_shift_init(void) __attribute__((section("__TEXT, initcode")));
151 static void preempt_pri_init(void) __attribute__((section("__TEXT, initcode")));
153 static thread_t
run_queue_dequeue(
157 static thread_t
choose_thread(
158 processor_t processor
,
161 static thread_t
thread_select_idle(
163 processor_t processor
);
165 static thread_t
processor_idle(
167 processor_t processor
);
169 static thread_t
steal_thread(
170 processor_set_t pset
);
172 static thread_t
steal_processor_thread(
173 processor_t processor
);
175 static void thread_update_scan(void);
178 extern int debug_task
;
179 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
181 #define TLOG(a, fmt, args...) do {} while (0)
186 boolean_t
thread_runnable(
194 * states are combinations of:
196 * W waiting (or on wait queue)
197 * N non-interruptible
202 * assert_wait thread_block clear_wait swapout swapin
204 * R RW, RWN R; setrun - -
205 * RN RWN RN; setrun - -
217 int8_t sched_load_shifts
[NRQS
];
218 int sched_preempt_pri
[NRQBM
];
224 * Calculate the timeslicing quantum
227 if (default_preemption_rate
< 1)
228 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
229 std_quantum_us
= (1000 * 1000) / default_preemption_rate
;
231 printf("standard timeslicing quantum is %d us\n", std_quantum_us
);
233 sched_safe_duration
= (2 * max_unsafe_quanta
/ default_preemption_rate
) *
234 (1 << SCHED_TICK_SHIFT
);
238 simple_lock_init(&rt_lock
, 0);
239 run_queue_init(&rt_runq
);
245 sched_timebase_init(void)
250 /* standard timeslicing quantum */
251 clock_interval_to_absolutetime_interval(
252 std_quantum_us
, NSEC_PER_USEC
, &abstime
);
253 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
254 std_quantum
= (uint32_t)abstime
;
256 /* smallest remaining quantum (250 us) */
257 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC
, &abstime
);
258 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
259 min_std_quantum
= (uint32_t)abstime
;
261 /* smallest rt computaton (50 us) */
262 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC
, &abstime
);
263 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
264 min_rt_quantum
= (uint32_t)abstime
;
266 /* maximum rt computation (50 ms) */
267 clock_interval_to_absolutetime_interval(
268 50, 1000*NSEC_PER_USEC
, &abstime
);
269 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
270 max_rt_quantum
= (uint32_t)abstime
;
272 /* scheduler tick interval */
273 clock_interval_to_absolutetime_interval(USEC_PER_SEC
>> SCHED_TICK_SHIFT
,
274 NSEC_PER_USEC
, &abstime
);
275 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
276 sched_tick_interval
= (uint32_t)abstime
;
279 * Compute conversion factor from usage to
280 * timesharing priorities with 5/8 ** n aging.
282 abstime
= (abstime
* 5) / 3;
283 for (shift
= 0; abstime
> BASEPRI_DEFAULT
; ++shift
)
285 sched_fixed_shift
= shift
;
287 max_unsafe_computation
= max_unsafe_quanta
* std_quantum
;
288 max_poll_computation
= max_poll_quanta
* std_quantum
;
292 * Set up values for timeshare
296 load_shift_init(void)
298 int8_t k
, *p
= sched_load_shifts
;
301 *p
++ = INT8_MIN
; *p
++ = 0;
303 for (i
= j
= 2, k
= 1; i
< NRQS
; ++k
) {
304 for (j
<<= 1; i
< j
; ++i
)
310 preempt_pri_init(void)
312 int i
, *p
= sched_preempt_pri
;
314 for (i
= BASEPRI_FOREGROUND
+ 1; i
< MINPRI_KERNEL
; ++i
)
317 for (i
= BASEPRI_PREEMPT
; i
<= MAXPRI
; ++i
)
322 * Thread wait timer expiration.
329 thread_t thread
= p0
;
334 if (--thread
->wait_timer_active
== 0) {
335 if (thread
->wait_timer_is_set
) {
336 thread
->wait_timer_is_set
= FALSE
;
337 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
340 thread_unlock(thread
);
349 * Set a timer for the current thread, if the thread
350 * is ready to wait. Must be called between assert_wait()
351 * and thread_block().
356 uint32_t scale_factor
)
358 thread_t thread
= current_thread();
364 if ((thread
->state
& TH_WAIT
) != 0) {
365 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
366 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
367 thread
->wait_timer_active
++;
368 thread
->wait_timer_is_set
= TRUE
;
370 thread_unlock(thread
);
375 thread_set_timer_deadline(
378 thread_t thread
= current_thread();
383 if ((thread
->state
& TH_WAIT
) != 0) {
384 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
385 thread
->wait_timer_active
++;
386 thread
->wait_timer_is_set
= TRUE
;
388 thread_unlock(thread
);
393 thread_cancel_timer(void)
395 thread_t thread
= current_thread();
400 if (thread
->wait_timer_is_set
) {
401 if (timer_call_cancel(&thread
->wait_timer
))
402 thread
->wait_timer_active
--;
403 thread
->wait_timer_is_set
= FALSE
;
405 thread_unlock(thread
);
409 #endif /* __LP64__ */
414 * Unblock thread on wake up.
416 * Returns TRUE if the thread is still running.
418 * Thread must be locked.
423 wait_result_t wresult
)
425 boolean_t result
= FALSE
;
430 thread
->wait_result
= wresult
;
433 * Cancel pending wait timer.
435 if (thread
->wait_timer_is_set
) {
436 if (timer_call_cancel(&thread
->wait_timer
))
437 thread
->wait_timer_active
--;
438 thread
->wait_timer_is_set
= FALSE
;
442 * Update scheduling state: not waiting,
445 thread
->state
&= ~(TH_WAIT
|TH_UNINT
);
447 if (!(thread
->state
& TH_RUN
)) {
448 thread
->state
|= TH_RUN
;
450 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
456 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
461 * Signal if idling on another processor.
463 if (thread
->state
& TH_IDLE
) {
464 processor_t processor
= thread
->last_processor
;
466 if (processor
!= current_processor())
467 machine_signal_idle(processor
);
474 * Calculate deadline for real-time threads.
476 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
477 thread
->realtime
.deadline
= mach_absolute_time();
478 thread
->realtime
.deadline
+= thread
->realtime
.constraint
;
482 * Clear old quantum, fail-safe computation, etc.
484 thread
->current_quantum
= 0;
485 thread
->computation_metered
= 0;
486 thread
->reason
= AST_NONE
;
488 KERNEL_DEBUG_CONSTANT(
489 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_MAKE_RUNNABLE
) | DBG_FUNC_NONE
,
490 (uintptr_t)thread_tid(thread
), thread
->sched_pri
, 0, 0, 0);
492 DTRACE_SCHED2(wakeup
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
500 * Unblock and dispatch thread.
502 * thread lock held, IPC locks may be held.
503 * thread must have been pulled from wait queue under same lock hold.
505 * KERN_SUCCESS - Thread was set running
506 * KERN_NOT_WAITING - Thread was not waiting
511 wait_result_t wresult
)
513 assert(thread
->at_safe_point
== FALSE
);
514 assert(thread
->wait_event
== NO_EVENT64
);
515 assert(thread
->wait_queue
== WAIT_QUEUE_NULL
);
517 if ((thread
->state
& (TH_WAIT
|TH_TERMINATE
)) == TH_WAIT
) {
518 if (!thread_unblock(thread
, wresult
))
519 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
521 return (KERN_SUCCESS
);
524 return (KERN_NOT_WAITING
);
528 * Routine: thread_mark_wait_locked
530 * Mark a thread as waiting. If, given the circumstances,
531 * it doesn't want to wait (i.e. already aborted), then
532 * indicate that in the return value.
534 * at splsched() and thread is locked.
538 thread_mark_wait_locked(
540 wait_interrupt_t interruptible
)
542 boolean_t at_safe_point
;
544 assert(thread
== current_thread());
547 * The thread may have certain types of interrupts/aborts masked
548 * off. Even if the wait location says these types of interrupts
549 * are OK, we have to honor mask settings (outer-scoped code may
550 * not be able to handle aborts at the moment).
552 if (interruptible
> (thread
->options
& TH_OPT_INTMASK
))
553 interruptible
= thread
->options
& TH_OPT_INTMASK
;
555 at_safe_point
= (interruptible
== THREAD_ABORTSAFE
);
557 if ( interruptible
== THREAD_UNINT
||
558 !(thread
->sched_mode
& TH_MODE_ABORT
) ||
560 (thread
->sched_mode
& TH_MODE_ABORTSAFELY
))) {
564 thread
->state
|= (interruptible
) ? TH_WAIT
: (TH_WAIT
| TH_UNINT
);
565 thread
->at_safe_point
= at_safe_point
;
566 return (thread
->wait_result
= THREAD_WAITING
);
569 if (thread
->sched_mode
& TH_MODE_ABORTSAFELY
)
570 thread
->sched_mode
&= ~TH_MODE_ISABORTED
;
572 return (thread
->wait_result
= THREAD_INTERRUPTED
);
576 * Routine: thread_interrupt_level
578 * Set the maximum interruptible state for the
579 * current thread. The effective value of any
580 * interruptible flag passed into assert_wait
581 * will never exceed this.
583 * Useful for code that must not be interrupted,
584 * but which calls code that doesn't know that.
586 * The old interrupt level for the thread.
590 thread_interrupt_level(
591 wait_interrupt_t new_level
)
593 thread_t thread
= current_thread();
594 wait_interrupt_t result
= thread
->options
& TH_OPT_INTMASK
;
596 thread
->options
= (thread
->options
& ~TH_OPT_INTMASK
) | (new_level
& TH_OPT_INTMASK
);
602 * Check to see if an assert wait is possible, without actually doing one.
603 * This is used by debug code in locks and elsewhere to verify that it is
604 * always OK to block when trying to take a blocking lock (since waiting
605 * for the actual assert_wait to catch the case may make it hard to detect
609 assert_wait_possible(void)
615 if(debug_mode
) return TRUE
; /* Always succeed in debug mode */
618 thread
= current_thread();
620 return (thread
== NULL
|| wait_queue_assert_possible(thread
));
626 * Assert that the current thread is about to go to
627 * sleep until the specified event occurs.
632 wait_interrupt_t interruptible
)
634 register wait_queue_t wq
;
637 assert(event
!= NO_EVENT
);
639 index
= wait_hash(event
);
640 wq
= &wait_queues
[index
];
641 return wait_queue_assert_wait(wq
, event
, interruptible
, 0);
647 wait_interrupt_t interruptible
,
649 uint32_t scale_factor
)
651 thread_t thread
= current_thread();
652 wait_result_t wresult
;
657 assert(event
!= NO_EVENT
);
658 wqueue
= &wait_queues
[wait_hash(event
)];
661 wait_queue_lock(wqueue
);
664 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
665 wresult
= wait_queue_assert_wait64_locked(wqueue
, CAST_DOWN(event64_t
, event
),
666 interruptible
, deadline
, thread
);
668 thread_unlock(thread
);
669 wait_queue_unlock(wqueue
);
676 assert_wait_deadline(
678 wait_interrupt_t interruptible
,
681 thread_t thread
= current_thread();
682 wait_result_t wresult
;
686 assert(event
!= NO_EVENT
);
687 wqueue
= &wait_queues
[wait_hash(event
)];
690 wait_queue_lock(wqueue
);
693 wresult
= wait_queue_assert_wait64_locked(wqueue
, CAST_DOWN(event64_t
,event
),
694 interruptible
, deadline
, thread
);
696 thread_unlock(thread
);
697 wait_queue_unlock(wqueue
);
704 * thread_sleep_fast_usimple_lock:
706 * Cause the current thread to wait until the specified event
707 * occurs. The specified simple_lock is unlocked before releasing
708 * the cpu and re-acquired as part of waking up.
710 * This is the simple lock sleep interface for components that use a
711 * faster version of simple_lock() than is provided by usimple_lock().
713 __private_extern__ wait_result_t
714 thread_sleep_fast_usimple_lock(
717 wait_interrupt_t interruptible
)
721 res
= assert_wait(event
, interruptible
);
722 if (res
== THREAD_WAITING
) {
724 res
= thread_block(THREAD_CONTINUE_NULL
);
732 * thread_sleep_usimple_lock:
734 * Cause the current thread to wait until the specified event
735 * occurs. The specified usimple_lock is unlocked before releasing
736 * the cpu and re-acquired as part of waking up.
738 * This is the simple lock sleep interface for components where
739 * simple_lock() is defined in terms of usimple_lock().
742 thread_sleep_usimple_lock(
745 wait_interrupt_t interruptible
)
749 res
= assert_wait(event
, interruptible
);
750 if (res
== THREAD_WAITING
) {
751 usimple_unlock(lock
);
752 res
= thread_block(THREAD_CONTINUE_NULL
);
759 * thread_sleep_lock_write:
761 * Cause the current thread to wait until the specified event
762 * occurs. The specified (write) lock is unlocked before releasing
763 * the cpu. The (write) lock will be re-acquired before returning.
766 thread_sleep_lock_write(
769 wait_interrupt_t interruptible
)
773 res
= assert_wait(event
, interruptible
);
774 if (res
== THREAD_WAITING
) {
775 lock_write_done(lock
);
776 res
= thread_block(THREAD_CONTINUE_NULL
);
785 * Force a preemption point for a thread and wait
786 * for it to stop running. Arbitrates access among
787 * multiple stop requests. (released by unstop)
789 * The thread must enter a wait state and stop via a
792 * Returns FALSE if interrupted.
798 wait_result_t wresult
;
799 spl_t s
= splsched();
804 while (thread
->state
& TH_SUSP
) {
805 thread
->wake_active
= TRUE
;
806 thread_unlock(thread
);
808 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
812 if (wresult
== THREAD_WAITING
)
813 wresult
= thread_block(THREAD_CONTINUE_NULL
);
815 if (wresult
!= THREAD_AWAKENED
)
823 thread
->state
|= TH_SUSP
;
825 while (thread
->state
& TH_RUN
) {
826 processor_t processor
= thread
->last_processor
;
828 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
829 cause_ast_check(processor
);
831 thread
->wake_active
= TRUE
;
832 thread_unlock(thread
);
834 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
838 if (wresult
== THREAD_WAITING
)
839 wresult
= thread_block(THREAD_CONTINUE_NULL
);
841 if (wresult
!= THREAD_AWAKENED
) {
842 thread_unstop(thread
);
851 thread_unlock(thread
);
861 * Release a previous stop request and set
862 * the thread running if appropriate.
864 * Use only after a successful stop operation.
870 spl_t s
= splsched();
875 if ((thread
->state
& (TH_RUN
|TH_WAIT
|TH_SUSP
)) == TH_SUSP
) {
876 thread
->state
&= ~TH_SUSP
;
877 thread_unblock(thread
, THREAD_AWAKENED
);
879 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
882 if (thread
->state
& TH_SUSP
) {
883 thread
->state
&= ~TH_SUSP
;
885 if (thread
->wake_active
) {
886 thread
->wake_active
= FALSE
;
887 thread_unlock(thread
);
889 thread_wakeup(&thread
->wake_active
);
897 thread_unlock(thread
);
905 * Wait for a thread to stop running. (non-interruptible)
912 wait_result_t wresult
;
913 spl_t s
= splsched();
918 while (thread
->state
& TH_RUN
) {
919 processor_t processor
= thread
->last_processor
;
921 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
922 cause_ast_check(processor
);
924 thread
->wake_active
= TRUE
;
925 thread_unlock(thread
);
927 wresult
= assert_wait(&thread
->wake_active
, THREAD_UNINT
);
931 if (wresult
== THREAD_WAITING
)
932 thread_block(THREAD_CONTINUE_NULL
);
939 thread_unlock(thread
);
945 * Routine: clear_wait_internal
947 * Clear the wait condition for the specified thread.
948 * Start the thread executing if that is appropriate.
950 * thread thread to awaken
951 * result Wakeup result the thread should see
954 * the thread is locked.
956 * KERN_SUCCESS thread was rousted out a wait
957 * KERN_FAILURE thread was waiting but could not be rousted
958 * KERN_NOT_WAITING thread was not waiting
960 __private_extern__ kern_return_t
963 wait_result_t wresult
)
965 wait_queue_t wq
= thread
->wait_queue
;
969 if (wresult
== THREAD_INTERRUPTED
&& (thread
->state
& TH_UNINT
))
970 return (KERN_FAILURE
);
972 if (wq
!= WAIT_QUEUE_NULL
) {
973 if (wait_queue_lock_try(wq
)) {
974 wait_queue_pull_thread_locked(wq
, thread
, TRUE
);
975 /* wait queue unlocked, thread still locked */
978 thread_unlock(thread
);
982 if (wq
!= thread
->wait_queue
)
983 return (KERN_NOT_WAITING
);
989 return (thread_go(thread
, wresult
));
992 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
993 thread
, wq
, cpu_number());
995 return (KERN_FAILURE
);
1002 * Clear the wait condition for the specified thread. Start the thread
1003 * executing if that is appropriate.
1006 * thread thread to awaken
1007 * result Wakeup result the thread should see
1012 wait_result_t result
)
1018 thread_lock(thread
);
1019 ret
= clear_wait_internal(thread
, result
);
1020 thread_unlock(thread
);
1027 * thread_wakeup_prim:
1029 * Common routine for thread_wakeup, thread_wakeup_with_result,
1030 * and thread_wakeup_one.
1036 boolean_t one_thread
,
1037 wait_result_t result
)
1039 register wait_queue_t wq
;
1042 index
= wait_hash(event
);
1043 wq
= &wait_queues
[index
];
1045 return (wait_queue_wakeup_one(wq
, event
, result
));
1047 return (wait_queue_wakeup_all(wq
, event
, result
));
1053 * Force the current thread to execute on the specified processor.
1055 * Returns the previous binding. PROCESSOR_NULL means
1058 * XXX - DO NOT export this to users - XXX
1062 processor_t processor
)
1064 thread_t self
= current_thread();
1071 prev
= self
->bound_processor
;
1072 self
->bound_processor
= processor
;
1074 thread_unlock(self
);
1083 * Select a new thread for the current processor to execute.
1085 * May select the current thread, which must be locked.
1090 processor_t processor
)
1092 processor_set_t pset
= processor
->processor_set
;
1093 thread_t new_thread
= THREAD_NULL
;
1094 boolean_t inactive_state
;
1098 * Update the priority.
1100 if (thread
->sched_stamp
!= sched_tick
)
1101 update_priority(thread
);
1103 processor
->current_pri
= thread
->sched_pri
;
1107 inactive_state
= processor
->state
!= PROCESSOR_SHUTDOWN
&& machine_processor_is_inactive(processor
);
1109 simple_lock(&rt_lock
);
1112 * Test to see if the current thread should continue
1113 * to run on this processor. Must be runnable, and not
1114 * bound to a different processor, nor be in the wrong
1117 if ( thread
->state
== TH_RUN
&&
1118 (thread
->sched_pri
>= BASEPRI_RTQUEUES
||
1119 processor
->processor_meta
== PROCESSOR_META_NULL
||
1120 processor
->processor_meta
->primary
== processor
) &&
1121 (thread
->bound_processor
== PROCESSOR_NULL
||
1122 thread
->bound_processor
== processor
) &&
1123 (thread
->affinity_set
== AFFINITY_SET_NULL
||
1124 thread
->affinity_set
->aset_pset
== pset
) ) {
1125 if ( thread
->sched_pri
>= BASEPRI_RTQUEUES
&&
1126 first_timeslice(processor
) ) {
1127 if (rt_runq
.highq
>= BASEPRI_RTQUEUES
) {
1128 register run_queue_t runq
= &rt_runq
;
1131 q
= runq
->queues
+ runq
->highq
;
1132 if (((thread_t
)q
->next
)->realtime
.deadline
<
1133 processor
->deadline
) {
1134 thread
= (thread_t
)q
->next
;
1135 ((queue_entry_t
)thread
)->next
->prev
= q
;
1136 q
->next
= ((queue_entry_t
)thread
)->next
;
1137 thread
->runq
= PROCESSOR_NULL
;
1138 runq
->count
--; runq
->urgency
--;
1139 assert(runq
->urgency
>= 0);
1140 if (queue_empty(q
)) {
1141 if (runq
->highq
!= IDLEPRI
)
1142 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
1143 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
1148 simple_unlock(&rt_lock
);
1150 processor
->deadline
= thread
->realtime
.deadline
;
1157 if (!inactive_state
&& rt_runq
.highq
< thread
->sched_pri
&&
1158 (new_thread
= choose_thread(processor
, thread
->sched_pri
)) == THREAD_NULL
) {
1160 simple_unlock(&rt_lock
);
1162 /* I am the highest priority runnable (non-idle) thread */
1164 pset_pri_hint(pset
, processor
, processor
->current_pri
);
1166 pset_count_hint(pset
, processor
, processor
->runq
.count
);
1168 processor
->deadline
= UINT64_MAX
;
1176 if (new_thread
!= THREAD_NULL
||
1177 (processor
->runq
.highq
>= rt_runq
.highq
&&
1178 (new_thread
= choose_thread(processor
, MINPRI
)) != THREAD_NULL
)) {
1179 simple_unlock(&rt_lock
);
1181 if (!inactive_state
) {
1182 pset_pri_hint(pset
, processor
, new_thread
->sched_pri
);
1184 pset_count_hint(pset
, processor
, processor
->runq
.count
);
1187 processor
->deadline
= UINT64_MAX
;
1190 return (new_thread
);
1193 if (rt_runq
.count
> 0) {
1194 thread
= run_queue_dequeue(&rt_runq
, SCHED_HEADQ
);
1195 simple_unlock(&rt_lock
);
1197 processor
->deadline
= thread
->realtime
.deadline
;
1203 simple_unlock(&rt_lock
);
1205 processor
->deadline
= UINT64_MAX
;
1208 * Set processor inactive based on
1209 * indication from the platform code.
1211 if (inactive_state
) {
1212 if (processor
->state
== PROCESSOR_RUNNING
)
1213 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
1215 if (processor
->state
== PROCESSOR_IDLE
)
1216 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
1218 processor
->state
= PROCESSOR_INACTIVE
;
1222 return (processor
->idle_thread
);
1226 * No runnable threads, attempt to steal
1227 * from other processors.
1229 new_thread
= steal_thread(pset
);
1230 if (new_thread
!= THREAD_NULL
)
1231 return (new_thread
);
1234 * If other threads have appeared, shortcut
1237 if (processor
->runq
.count
> 0 || rt_runq
.count
> 0)
1243 * Nothing is runnable, so set this processor idle if it
1246 if (processor
->state
== PROCESSOR_RUNNING
) {
1247 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
1248 processor
->state
= PROCESSOR_IDLE
;
1250 if (processor
->processor_meta
== PROCESSOR_META_NULL
|| processor
->processor_meta
->primary
== processor
) {
1251 enqueue_head(&pset
->idle_queue
, (queue_entry_t
)processor
);
1252 pset
->low_pri
= pset
->low_count
= processor
;
1255 enqueue_head(&processor
->processor_meta
->idle_queue
, (queue_entry_t
)processor
);
1257 if (thread
->sched_pri
< BASEPRI_RTQUEUES
) {
1260 return (processor
->idle_thread
);
1268 * Choose idle thread if fast idle is not possible.
1270 if ((thread
->state
& (TH_IDLE
|TH_TERMINATE
|TH_SUSP
)) || !(thread
->state
& TH_WAIT
) || thread
->wake_active
)
1271 return (processor
->idle_thread
);
1274 * Perform idling activities directly without a
1275 * context switch. Return dispatched thread,
1276 * else check again for a runnable thread.
1278 new_thread
= thread_select_idle(thread
, processor
);
1280 } while (new_thread
== THREAD_NULL
);
1282 return (new_thread
);
1286 * thread_select_idle:
1288 * Idle the processor using the current thread context.
1290 * Called with thread locked, then dropped and relocked.
1295 processor_t processor
)
1297 thread_t new_thread
;
1299 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1303 thread
->state
|= TH_IDLE
;
1304 processor
->current_pri
= IDLEPRI
;
1306 thread_unlock(thread
);
1309 * Switch execution timing to processor idle thread.
1311 processor
->last_dispatch
= mach_absolute_time();
1312 thread_timer_event(processor
->last_dispatch
, &processor
->idle_thread
->system_timer
);
1313 PROCESSOR_DATA(processor
, kernel_timer
) = &processor
->idle_thread
->system_timer
;
1316 * Cancel the quantum timer while idling.
1318 timer_call_cancel(&processor
->quantum_timer
);
1319 processor
->timeslice
= 0;
1321 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1324 * Enable interrupts and perform idling activities. No
1325 * preemption due to TH_IDLE being set.
1327 spllo(); new_thread
= processor_idle(thread
, processor
);
1330 * Return at splsched.
1332 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
1334 thread_lock(thread
);
1337 * If awakened, switch to thread timer and start a new quantum.
1338 * Otherwise skip; we will context switch to another thread or return here.
1340 if (!(thread
->state
& TH_WAIT
)) {
1341 processor
->last_dispatch
= mach_absolute_time();
1342 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1343 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1345 thread_quantum_init(thread
);
1347 processor
->quantum_end
= processor
->last_dispatch
+ thread
->current_quantum
;
1348 timer_call_enter1(&processor
->quantum_timer
, thread
, processor
->quantum_end
);
1349 processor
->timeslice
= 1;
1351 thread
->computation_epoch
= processor
->last_dispatch
;
1354 thread
->state
&= ~TH_IDLE
;
1357 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1360 return (new_thread
);
1366 * Locate a thread to execute from the processor run queue
1367 * and return it. Only choose a thread with greater or equal
1370 * Associated pset must be locked. Returns THREAD_NULL
1375 processor_t processor
,
1378 run_queue_t rq
= &processor
->runq
;
1379 queue_t queue
= rq
->queues
+ rq
->highq
;
1380 int pri
= rq
->highq
, count
= rq
->count
;
1383 while (count
> 0 && pri
>= priority
) {
1384 thread
= (thread_t
)queue_first(queue
);
1385 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
1386 if (thread
->bound_processor
== PROCESSOR_NULL
||
1387 thread
->bound_processor
== processor
) {
1388 remqueue(queue
, (queue_entry_t
)thread
);
1390 thread
->runq
= PROCESSOR_NULL
;
1392 if (testbit(pri
, sched_preempt_pri
)) {
1393 rq
->urgency
--; assert(rq
->urgency
>= 0);
1395 if (queue_empty(queue
)) {
1397 clrbit(MAXPRI
- pri
, rq
->bitmap
);
1398 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
1405 thread
= (thread_t
)queue_next((queue_entry_t
)thread
);
1411 return (THREAD_NULL
);
1415 * Perform a context switch and start executing the new thread.
1417 * Returns FALSE on failure, and the thread is re-dispatched.
1419 * Called at splsched.
1422 #define funnel_release_check(thread, debug) \
1424 if ((thread)->funnel_state & TH_FN_OWNED) { \
1425 (thread)->funnel_state = TH_FN_REFUNNEL; \
1426 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE, \
1427 (thread)->funnel_lock, (debug), 0, 0, 0); \
1428 funnel_unlock((thread)->funnel_lock); \
1432 #define funnel_refunnel_check(thread, debug) \
1434 if ((thread)->funnel_state & TH_FN_REFUNNEL) { \
1435 kern_return_t result = (thread)->wait_result; \
1437 (thread)->funnel_state = 0; \
1438 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, \
1439 (thread)->funnel_lock, (debug), 0, 0, 0); \
1440 funnel_lock((thread)->funnel_lock); \
1441 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE, \
1442 (thread)->funnel_lock, (debug), 0, 0, 0); \
1443 (thread)->funnel_state = TH_FN_OWNED; \
1444 (thread)->wait_result = result; \
1450 register thread_t self
,
1451 register thread_t thread
,
1454 thread_continue_t continuation
= self
->continuation
;
1455 void *parameter
= self
->parameter
;
1456 processor_t processor
;
1458 if (get_preemption_level() != 0) {
1459 int pl
= get_preemption_level();
1460 panic("thread_invoke: preemption_level %d, possible cause: %s",
1461 pl
, (pl
< 0 ? "unlocking an unlocked mutex or spinlock" :
1462 "blocking while holding a spinlock, or within interrupt context"));
1465 assert(self
== current_thread());
1468 * Mark thread interruptible.
1470 thread_lock(thread
);
1471 thread
->state
&= ~TH_UNINT
;
1474 assert(thread_runnable(thread
));
1478 * Allow time constraint threads to hang onto
1481 if ((self
->sched_mode
& TH_MODE_REALTIME
) && !self
->reserved_stack
)
1482 self
->reserved_stack
= self
->kernel_stack
;
1484 if (continuation
!= NULL
) {
1485 if (!thread
->kernel_stack
) {
1487 * If we are using a privileged stack,
1488 * check to see whether we can exchange it with
1489 * that of the other thread.
1491 if (self
->kernel_stack
== self
->reserved_stack
&& !thread
->reserved_stack
)
1495 * Context switch by performing a stack handoff.
1497 continuation
= thread
->continuation
;
1498 parameter
= thread
->parameter
;
1500 processor
= current_processor();
1501 processor
->active_thread
= thread
;
1502 processor
->current_pri
= thread
->sched_pri
;
1503 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1504 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1505 thread
->ps_switch
++;
1508 thread
->last_processor
= processor
;
1510 ast_context(thread
);
1511 thread_unlock(thread
);
1513 self
->reason
= reason
;
1515 processor
->last_dispatch
= mach_absolute_time();
1516 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1517 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1519 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_HANDOFF
)|DBG_FUNC_NONE
,
1520 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
1522 DTRACE_SCHED2(off__cpu
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
1524 TLOG(1, "thread_invoke: calling machine_stack_handoff\n");
1525 machine_stack_handoff(self
, thread
);
1527 DTRACE_SCHED(on__cpu
);
1529 thread_dispatch(self
, thread
);
1531 thread
->continuation
= thread
->parameter
= NULL
;
1533 counter(c_thread_invoke_hits
++);
1535 funnel_refunnel_check(thread
, 2);
1538 assert(continuation
);
1539 call_continuation(continuation
, parameter
, thread
->wait_result
);
1542 else if (thread
== self
) {
1543 /* same thread but with continuation */
1545 counter(++c_thread_invoke_same
);
1546 thread_unlock(self
);
1548 self
->continuation
= self
->parameter
= NULL
;
1550 funnel_refunnel_check(self
, 3);
1553 call_continuation(continuation
, parameter
, self
->wait_result
);
1559 * Check that the other thread has a stack
1561 if (!thread
->kernel_stack
) {
1563 if (!stack_alloc_try(thread
)) {
1564 counter(c_thread_invoke_misses
++);
1565 thread_unlock(thread
);
1566 thread_stack_enqueue(thread
);
1570 else if (thread
== self
) {
1572 counter(++c_thread_invoke_same
);
1573 thread_unlock(self
);
1579 * Context switch by full context save.
1581 processor
= current_processor();
1582 processor
->active_thread
= thread
;
1583 processor
->current_pri
= thread
->sched_pri
;
1584 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1585 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1586 thread
->ps_switch
++;
1589 thread
->last_processor
= processor
;
1591 ast_context(thread
);
1592 thread_unlock(thread
);
1594 counter(c_thread_invoke_csw
++);
1596 assert(self
->runq
== PROCESSOR_NULL
);
1597 self
->reason
= reason
;
1599 processor
->last_dispatch
= mach_absolute_time();
1600 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1601 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1603 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED
) | DBG_FUNC_NONE
,
1604 self
->reason
, (uintptr_t)thread_tid(thread
), self
->sched_pri
, thread
->sched_pri
, 0);
1606 DTRACE_SCHED2(off__cpu
, struct thread
*, thread
, struct proc
*, thread
->task
->bsd_info
);
1609 * This is where we actually switch register context,
1610 * and address space if required. We will next run
1611 * as a result of a subsequent context switch.
1613 thread
= machine_switch_context(self
, continuation
, thread
);
1614 TLOG(1,"thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self
, continuation
, thread
);
1616 DTRACE_SCHED(on__cpu
);
1619 * We have been resumed and are set to run.
1621 thread_dispatch(thread
, self
);
1624 self
->continuation
= self
->parameter
= NULL
;
1626 funnel_refunnel_check(self
, 3);
1629 call_continuation(continuation
, parameter
, self
->wait_result
);
1639 * Handle threads at context switch. Re-dispatch other thread
1640 * if still running, otherwise update run state and perform
1641 * special actions. Update quantum for other thread and begin
1642 * the quantum for ourselves.
1644 * Called at splsched.
1651 processor_t processor
= self
->last_processor
;
1653 if (thread
!= THREAD_NULL
) {
1655 * If blocked at a continuation, discard
1658 if (thread
->continuation
!= NULL
&& thread
->kernel_stack
!= 0)
1661 if (!(thread
->state
& TH_IDLE
)) {
1663 thread_lock(thread
);
1666 * Compute remainder of current quantum.
1668 if ( first_timeslice(processor
) &&
1669 processor
->quantum_end
> processor
->last_dispatch
)
1670 thread
->current_quantum
= (uint32_t)(processor
->quantum_end
- processor
->last_dispatch
);
1672 thread
->current_quantum
= 0;
1674 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
1676 * Cancel the deadline if the thread has
1677 * consumed the entire quantum.
1679 if (thread
->current_quantum
== 0) {
1680 thread
->realtime
.deadline
= UINT64_MAX
;
1681 thread
->reason
|= AST_QUANTUM
;
1685 * For non-realtime threads treat a tiny
1686 * remaining quantum as an expired quantum
1687 * but include what's left next time.
1689 if (thread
->current_quantum
< min_std_quantum
) {
1690 thread
->reason
|= AST_QUANTUM
;
1691 thread
->current_quantum
+= std_quantum
;
1696 * If we are doing a direct handoff then
1697 * take the remainder of the quantum.
1699 if ((thread
->reason
& (AST_HANDOFF
|AST_QUANTUM
)) == AST_HANDOFF
) {
1700 self
->current_quantum
= thread
->current_quantum
;
1701 thread
->reason
|= AST_QUANTUM
;
1702 thread
->current_quantum
= 0;
1705 thread
->computation_metered
+= (processor
->last_dispatch
- thread
->computation_epoch
);
1707 if (!(thread
->state
& TH_WAIT
)) {
1711 if (thread
->reason
& AST_QUANTUM
)
1712 thread_setrun(thread
, SCHED_TAILQ
);
1714 if (thread
->reason
& AST_PREEMPT
)
1715 thread_setrun(thread
, SCHED_HEADQ
);
1717 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
1719 thread
->reason
= AST_NONE
;
1721 thread_unlock(thread
);
1722 wake_unlock(thread
);
1728 boolean_t should_terminate
= FALSE
;
1730 /* Only the first call to thread_dispatch
1731 * after explicit termination should add
1732 * the thread to the termination queue
1734 if ((thread
->state
& (TH_TERMINATE
|TH_TERMINATE2
)) == TH_TERMINATE
) {
1735 should_terminate
= TRUE
;
1736 thread
->state
|= TH_TERMINATE2
;
1739 thread
->state
&= ~TH_RUN
;
1741 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1745 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1747 if (thread
->wake_active
) {
1748 thread
->wake_active
= FALSE
;
1749 thread_unlock(thread
);
1751 thread_wakeup(&thread
->wake_active
);
1754 thread_unlock(thread
);
1756 wake_unlock(thread
);
1758 if (should_terminate
)
1759 thread_terminate_enqueue(thread
);
1764 if (!(self
->state
& TH_IDLE
)) {
1766 * Get a new quantum if none remaining.
1768 if (self
->current_quantum
== 0)
1769 thread_quantum_init(self
);
1772 * Set up quantum timer and timeslice.
1774 processor
->quantum_end
= (processor
->last_dispatch
+ self
->current_quantum
);
1775 timer_call_enter1(&processor
->quantum_timer
, self
, processor
->quantum_end
);
1777 processor
->timeslice
= 1;
1779 self
->computation_epoch
= processor
->last_dispatch
;
1782 timer_call_cancel(&processor
->quantum_timer
);
1783 processor
->timeslice
= 0;
1787 #include <libkern/OSDebug.h>
1789 uint32_t kdebug_thread_block
= 0;
1793 * thread_block_reason:
1795 * Forces a reschedule, blocking the caller if a wait
1796 * has been asserted.
1798 * If a continuation is specified, then thread_invoke will
1799 * attempt to discard the thread's kernel stack. When the
1800 * thread resumes, it will execute the continuation function
1801 * on a new kernel stack.
1803 counter(mach_counter_t c_thread_block_calls
= 0;)
1806 thread_block_reason(
1807 thread_continue_t continuation
,
1811 register thread_t self
= current_thread();
1812 register processor_t processor
;
1813 register thread_t new_thread
;
1816 counter(++c_thread_block_calls
);
1820 if (!(reason
& AST_PREEMPT
))
1821 funnel_release_check(self
, 2);
1823 processor
= current_processor();
1825 /* If we're explicitly yielding, force a subsequent quantum */
1826 if (reason
& AST_YIELD
)
1827 processor
->timeslice
= 0;
1829 /* We're handling all scheduling AST's */
1830 ast_off(AST_SCHEDULING
);
1832 self
->continuation
= continuation
;
1833 self
->parameter
= parameter
;
1835 if (kdebug_thread_block
&& kdebug_enable
&& self
->state
!= TH_RUN
) {
1838 OSBacktrace((void **)&bt
[0], 8);
1840 KERNEL_DEBUG_CONSTANT(0x140004c | DBG_FUNC_START
, bt
[0], bt
[1], bt
[2], bt
[3], 0);
1841 KERNEL_DEBUG_CONSTANT(0x140004c | DBG_FUNC_END
, bt
[4], bt
[5], bt
[6], bt
[7], 0);
1846 new_thread
= thread_select(self
, processor
);
1847 thread_unlock(self
);
1848 } while (!thread_invoke(self
, new_thread
, reason
));
1850 funnel_refunnel_check(self
, 5);
1853 return (self
->wait_result
);
1859 * Block the current thread if a wait has been asserted.
1863 thread_continue_t continuation
)
1865 return thread_block_reason(continuation
, NULL
, AST_NONE
);
1869 thread_block_parameter(
1870 thread_continue_t continuation
,
1873 return thread_block_reason(continuation
, parameter
, AST_NONE
);
1879 * Switch directly from the current thread to the
1880 * new thread, handing off our quantum if appropriate.
1882 * New thread must be runnable, and not on a run queue.
1884 * Called at splsched.
1889 thread_continue_t continuation
,
1891 thread_t new_thread
)
1893 ast_t handoff
= AST_HANDOFF
;
1895 funnel_release_check(self
, 3);
1897 self
->continuation
= continuation
;
1898 self
->parameter
= parameter
;
1900 while (!thread_invoke(self
, new_thread
, handoff
)) {
1901 processor_t processor
= current_processor();
1904 new_thread
= thread_select(self
, processor
);
1905 thread_unlock(self
);
1909 funnel_refunnel_check(self
, 6);
1911 return (self
->wait_result
);
1917 * Called at splsched when a thread first receives
1918 * a new stack after a continuation.
1922 register thread_t thread
)
1924 register thread_t self
= current_thread();
1925 register thread_continue_t continuation
;
1926 register void *parameter
;
1928 DTRACE_SCHED(on__cpu
);
1930 continuation
= self
->continuation
;
1931 parameter
= self
->parameter
;
1933 thread_dispatch(thread
, self
);
1935 self
->continuation
= self
->parameter
= NULL
;
1937 funnel_refunnel_check(self
, 4);
1939 if (thread
!= THREAD_NULL
)
1942 TLOG(1, "thread_continue: calling call_continuation \n");
1943 call_continuation(continuation
, parameter
, self
->wait_result
);
1950 * Initialize a run queue before first use.
1958 rq
->highq
= IDLEPRI
;
1959 for (i
= 0; i
< NRQBM
; i
++)
1961 setbit(MAXPRI
- IDLEPRI
, rq
->bitmap
);
1962 rq
->urgency
= rq
->count
= 0;
1963 for (i
= 0; i
< NRQS
; i
++)
1964 queue_init(&rq
->queues
[i
]);
1968 * run_queue_dequeue:
1970 * Perform a dequeue operation on a run queue,
1971 * and return the resulting thread.
1973 * The run queue must be locked (see run_queue_remove()
1974 * for more info), and not empty.
1982 queue_t queue
= rq
->queues
+ rq
->highq
;
1984 if (options
& SCHED_HEADQ
) {
1985 thread
= (thread_t
)queue
->next
;
1986 ((queue_entry_t
)thread
)->next
->prev
= queue
;
1987 queue
->next
= ((queue_entry_t
)thread
)->next
;
1990 thread
= (thread_t
)queue
->prev
;
1991 ((queue_entry_t
)thread
)->prev
->next
= queue
;
1992 queue
->prev
= ((queue_entry_t
)thread
)->prev
;
1995 thread
->runq
= PROCESSOR_NULL
;
1997 if (testbit(rq
->highq
, sched_preempt_pri
)) {
1998 rq
->urgency
--; assert(rq
->urgency
>= 0);
2000 if (queue_empty(queue
)) {
2001 if (rq
->highq
!= IDLEPRI
)
2002 clrbit(MAXPRI
- rq
->highq
, rq
->bitmap
);
2003 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2010 * realtime_queue_insert:
2012 * Enqueue a thread for realtime execution.
2015 realtime_queue_insert(
2018 run_queue_t rq
= &rt_runq
;
2019 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
2020 uint64_t deadline
= thread
->realtime
.deadline
;
2021 boolean_t preempt
= FALSE
;
2023 simple_lock(&rt_lock
);
2025 if (queue_empty(queue
)) {
2026 enqueue_tail(queue
, (queue_entry_t
)thread
);
2028 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2029 if (thread
->sched_pri
> rq
->highq
)
2030 rq
->highq
= thread
->sched_pri
;
2034 register thread_t entry
= (thread_t
)queue_first(queue
);
2037 if ( queue_end(queue
, (queue_entry_t
)entry
) ||
2038 deadline
< entry
->realtime
.deadline
) {
2039 entry
= (thread_t
)queue_prev((queue_entry_t
)entry
);
2043 entry
= (thread_t
)queue_next((queue_entry_t
)entry
);
2046 if ((queue_entry_t
)entry
== queue
)
2049 insque((queue_entry_t
)thread
, (queue_entry_t
)entry
);
2052 thread
->runq
= RT_RUNQ
;
2053 rq
->count
++; rq
->urgency
++;
2055 simple_unlock(&rt_lock
);
2063 * Dispatch a thread for realtime execution.
2065 * Thread must be locked. Associated pset must
2066 * be locked, and is returned unlocked.
2070 processor_t processor
,
2073 processor_set_t pset
= processor
->processor_set
;
2076 * Dispatch directly onto idle processor.
2078 if (processor
->state
== PROCESSOR_IDLE
) {
2079 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2080 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2082 processor
->next_thread
= thread
;
2083 processor
->deadline
= thread
->realtime
.deadline
;
2084 processor
->state
= PROCESSOR_DISPATCHING
;
2087 if (processor
!= current_processor())
2088 machine_signal_idle(processor
);
2092 if (realtime_queue_insert(thread
)) {
2093 if (processor
== current_processor())
2094 ast_on(AST_PREEMPT
| AST_URGENT
);
2096 cause_ast_check(processor
);
2103 * processor_enqueue:
2105 * Enqueue thread on a processor run queue. Thread must be locked,
2106 * and not already be on a run queue.
2108 * Returns TRUE if a preemption is indicated based on the state
2111 * The run queue must be locked (see run_queue_remove()
2116 processor_t processor
,
2120 run_queue_t rq
= &processor
->runq
;
2121 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
2122 boolean_t result
= FALSE
;
2124 if (queue_empty(queue
)) {
2125 enqueue_tail(queue
, (queue_entry_t
)thread
);
2127 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2128 if (thread
->sched_pri
> rq
->highq
) {
2129 rq
->highq
= thread
->sched_pri
;
2134 if (options
& SCHED_TAILQ
)
2135 enqueue_tail(queue
, (queue_entry_t
)thread
);
2137 enqueue_head(queue
, (queue_entry_t
)thread
);
2139 thread
->runq
= processor
;
2140 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2150 * Dispatch a thread for execution on a
2153 * Thread must be locked. Associated pset must
2154 * be locked, and is returned unlocked.
2158 processor_t processor
,
2162 processor_set_t pset
= processor
->processor_set
;
2166 * Dispatch directly onto idle processor.
2168 if (processor
->state
== PROCESSOR_IDLE
) {
2169 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2170 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2172 processor
->next_thread
= thread
;
2173 processor
->deadline
= UINT64_MAX
;
2174 processor
->state
= PROCESSOR_DISPATCHING
;
2177 if (processor
!= current_processor())
2178 machine_signal_idle(processor
);
2183 * Set preemption mode.
2185 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2186 preempt
= (AST_PREEMPT
| AST_URGENT
);
2188 if (thread
->sched_mode
& TH_MODE_TIMESHARE
&& thread
->sched_pri
< thread
->priority
)
2191 preempt
= (options
& SCHED_PREEMPT
)? AST_PREEMPT
: AST_NONE
;
2193 if (!processor_enqueue(processor
, thread
, options
))
2196 if (preempt
!= AST_NONE
) {
2197 if (processor
== current_processor()) {
2198 if (csw_check(processor
) != AST_NONE
)
2202 if ( (processor
->state
== PROCESSOR_RUNNING
||
2203 processor
->state
== PROCESSOR_SHUTDOWN
) &&
2204 thread
->sched_pri
>= processor
->current_pri
) {
2205 cause_ast_check(processor
);
2209 if ( processor
->state
== PROCESSOR_SHUTDOWN
&&
2210 thread
->sched_pri
>= processor
->current_pri
) {
2211 cause_ast_check(processor
);
2217 #define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
2222 * Return the next sibling pset containing
2223 * available processors.
2225 * Returns the original pset if none other is
2228 static processor_set_t
2230 processor_set_t pset
)
2232 processor_set_t nset
= pset
;
2235 nset
= next_pset(nset
);
2236 } while (nset
->processor_count
< 1 && nset
!= pset
);
2244 * Choose a processor for the thread, beginning at
2245 * the pset. Accepts an optional processor hint in
2248 * Returns a processor, possibly from a different pset.
2250 * The thread must be locked. The pset must be locked,
2251 * and the resulting pset is locked on return.
2255 processor_set_t pset
,
2256 processor_t processor
,
2259 processor_set_t nset
, cset
= pset
;
2260 processor_meta_t pmeta
= PROCESSOR_META_NULL
;
2263 * Prefer the hinted processor, when appropriate.
2265 if (processor
!= PROCESSOR_NULL
) {
2266 processor_t mprocessor
;
2268 if (processor
->processor_meta
!= PROCESSOR_META_NULL
)
2269 processor
= processor
->processor_meta
->primary
;
2271 mprocessor
= machine_choose_processor(pset
, processor
);
2272 if (mprocessor
!= PROCESSOR_NULL
)
2273 processor
= mprocessor
;
2275 if (processor
->processor_set
!= pset
|| processor
->state
== PROCESSOR_INACTIVE
||
2276 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2277 processor
= PROCESSOR_NULL
;
2279 if (processor
->state
== PROCESSOR_IDLE
)
2283 processor
= machine_choose_processor(pset
, processor
);
2285 if (processor
!= PROCESSOR_NULL
) {
2286 if (processor
->processor_set
!= pset
|| processor
->state
== PROCESSOR_INACTIVE
||
2287 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2288 processor
= PROCESSOR_NULL
;
2290 if (processor
->state
== PROCESSOR_IDLE
)
2296 * Iterate through the processor sets to locate
2297 * an appropriate processor.
2301 * Choose an idle processor.
2303 if (!queue_empty(&cset
->idle_queue
))
2304 return ((processor_t
)queue_first(&cset
->idle_queue
));
2306 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2308 * For an RT thread, iterate through active processors, first fit.
2310 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2311 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2312 if (thread
->sched_pri
> processor
->current_pri
||
2313 thread
->realtime
.deadline
< processor
->deadline
)
2316 if (pmeta
== PROCESSOR_META_NULL
) {
2317 if (processor
->processor_meta
!= PROCESSOR_META_NULL
&&
2318 !queue_empty(&processor
->processor_meta
->idle_queue
))
2319 pmeta
= processor
->processor_meta
;
2322 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2325 if (pmeta
!= PROCESSOR_META_NULL
)
2326 return ((processor_t
)queue_first(&pmeta
->idle_queue
));
2328 processor
= PROCESSOR_NULL
;
2332 * Check any hinted processors in the processor set if available.
2334 if (cset
->low_pri
!= PROCESSOR_NULL
&& cset
->low_pri
->state
!= PROCESSOR_INACTIVE
&&
2335 cset
->low_pri
->state
!= PROCESSOR_SHUTDOWN
&& cset
->low_pri
->state
!= PROCESSOR_OFF_LINE
&&
2336 (processor
== PROCESSOR_NULL
||
2337 (thread
->sched_pri
> BASEPRI_DEFAULT
&& cset
->low_pri
->current_pri
< thread
->sched_pri
))) {
2338 processor
= cset
->low_pri
;
2341 if (cset
->low_count
!= PROCESSOR_NULL
&& cset
->low_count
->state
!= PROCESSOR_INACTIVE
&&
2342 cset
->low_count
->state
!= PROCESSOR_SHUTDOWN
&& cset
->low_count
->state
!= PROCESSOR_OFF_LINE
&&
2343 (processor
== PROCESSOR_NULL
|| (thread
->sched_pri
<= BASEPRI_DEFAULT
&&
2344 cset
->low_count
->runq
.count
< processor
->runq
.count
))) {
2345 processor
= cset
->low_count
;
2349 * Otherwise, choose an available processor in the set.
2351 if (processor
== PROCESSOR_NULL
) {
2352 processor
= (processor_t
)dequeue_head(&cset
->active_queue
);
2353 if (processor
!= PROCESSOR_NULL
)
2354 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2357 if (processor
!= PROCESSOR_NULL
&& pmeta
== PROCESSOR_META_NULL
) {
2358 if (processor
->processor_meta
!= PROCESSOR_META_NULL
&&
2359 !queue_empty(&processor
->processor_meta
->idle_queue
))
2360 pmeta
= processor
->processor_meta
;
2365 * Move onto the next processor set.
2367 nset
= next_pset(cset
);
2375 } while (nset
!= pset
);
2378 * Make sure that we pick a running processor,
2379 * and that the correct processor set is locked.
2382 if (pmeta
!= PROCESSOR_META_NULL
) {
2383 if (cset
!= pmeta
->primary
->processor_set
) {
2386 cset
= pmeta
->primary
->processor_set
;
2390 if (!queue_empty(&pmeta
->idle_queue
))
2391 return ((processor_t
)queue_first(&pmeta
->idle_queue
));
2393 pmeta
= PROCESSOR_META_NULL
;
2397 * If we haven't been able to choose a processor,
2398 * pick the boot processor and return it.
2400 if (processor
== PROCESSOR_NULL
) {
2401 processor
= master_processor
;
2404 * Check that the correct processor set is
2407 if (cset
!= processor
->processor_set
) {
2410 cset
= processor
->processor_set
;
2418 * Check that the processor set for the chosen
2419 * processor is locked.
2421 if (cset
!= processor
->processor_set
) {
2424 cset
= processor
->processor_set
;
2429 * We must verify that the chosen processor is still available.
2431 if (processor
->state
== PROCESSOR_INACTIVE
||
2432 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2433 processor
= PROCESSOR_NULL
;
2434 } while (processor
== PROCESSOR_NULL
);
2442 * Dispatch thread for execution, onto an idle
2443 * processor or run queue, and signal a preemption
2446 * Thread must be locked.
2453 processor_t processor
;
2454 processor_set_t pset
;
2457 assert(thread_runnable(thread
));
2461 * Update priority if needed.
2463 if (thread
->sched_stamp
!= sched_tick
)
2464 update_priority(thread
);
2466 assert(thread
->runq
== PROCESSOR_NULL
);
2468 if (thread
->bound_processor
== PROCESSOR_NULL
) {
2472 if (thread
->affinity_set
!= AFFINITY_SET_NULL
) {
2474 * Use affinity set policy hint.
2476 pset
= thread
->affinity_set
->aset_pset
;
2479 processor
= choose_processor(pset
, PROCESSOR_NULL
, thread
);
2482 if (thread
->last_processor
!= PROCESSOR_NULL
) {
2484 * Simple (last processor) affinity case.
2486 processor
= thread
->last_processor
;
2487 pset
= processor
->processor_set
;
2491 * Choose a different processor in certain cases.
2493 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2495 * If the processor is executing an RT thread with
2496 * an earlier deadline, choose another.
2498 if (thread
->sched_pri
<= processor
->current_pri
||
2499 thread
->realtime
.deadline
>= processor
->deadline
)
2500 processor
= choose_processor(pset
, PROCESSOR_NULL
, thread
);
2503 processor
= choose_processor(pset
, processor
, thread
);
2509 * Utilitize a per task hint to spread threads
2510 * among the available processor sets.
2512 task_t task
= thread
->task
;
2514 pset
= task
->pset_hint
;
2515 if (pset
== PROCESSOR_SET_NULL
)
2516 pset
= current_processor()->processor_set
;
2518 pset
= choose_next_pset(pset
);
2521 processor
= choose_processor(pset
, PROCESSOR_NULL
, thread
);
2522 task
->pset_hint
= processor
->processor_set
;
2529 * Unconditionally dispatch on the processor.
2531 processor
= thread
->bound_processor
;
2532 pset
= processor
->processor_set
;
2537 * Dispatch the thread on the choosen processor.
2539 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
)
2540 realtime_setrun(processor
, thread
);
2542 processor_setrun(processor
, thread
, options
);
2549 processor_set_t pset
= task
->pset_hint
;
2551 if (pset
!= PROCESSOR_SET_NULL
)
2552 pset
= choose_next_pset(pset
);
2558 * processor_queue_shutdown:
2560 * Shutdown a processor run queue by
2561 * re-dispatching non-bound threads.
2563 * Associated pset must be locked, and is
2564 * returned unlocked.
2567 processor_queue_shutdown(
2568 processor_t processor
)
2570 processor_set_t pset
= processor
->processor_set
;
2571 run_queue_t rq
= &processor
->runq
;
2572 queue_t queue
= rq
->queues
+ rq
->highq
;
2573 int pri
= rq
->highq
, count
= rq
->count
;
2574 thread_t next
, thread
;
2575 queue_head_t tqueue
;
2577 queue_init(&tqueue
);
2580 thread
= (thread_t
)queue_first(queue
);
2581 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2582 next
= (thread_t
)queue_next((queue_entry_t
)thread
);
2584 if (thread
->bound_processor
== PROCESSOR_NULL
) {
2585 remqueue(queue
, (queue_entry_t
)thread
);
2587 thread
->runq
= PROCESSOR_NULL
;
2589 if (testbit(pri
, sched_preempt_pri
)) {
2590 rq
->urgency
--; assert(rq
->urgency
>= 0);
2592 if (queue_empty(queue
)) {
2594 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2595 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2598 enqueue_tail(&tqueue
, (queue_entry_t
)thread
);
2610 while ((thread
= (thread_t
)dequeue_head(&tqueue
)) != THREAD_NULL
) {
2611 thread_lock(thread
);
2613 thread_setrun(thread
, SCHED_TAILQ
);
2615 thread_unlock(thread
);
2620 * Check for a preemption point in
2621 * the current context.
2623 * Called at splsched.
2627 processor_t processor
)
2629 ast_t result
= AST_NONE
;
2632 if (first_timeslice(processor
)) {
2634 if (runq
->highq
>= BASEPRI_RTQUEUES
)
2635 return (AST_PREEMPT
| AST_URGENT
);
2637 if (runq
->highq
> processor
->current_pri
) {
2638 if (runq
->urgency
> 0)
2639 return (AST_PREEMPT
| AST_URGENT
);
2641 result
|= AST_PREEMPT
;
2644 runq
= &processor
->runq
;
2645 if (runq
->highq
> processor
->current_pri
) {
2646 if (runq
->urgency
> 0)
2647 return (AST_PREEMPT
| AST_URGENT
);
2649 result
|= AST_PREEMPT
;
2654 if (runq
->highq
>= processor
->current_pri
) {
2655 if (runq
->urgency
> 0)
2656 return (AST_PREEMPT
| AST_URGENT
);
2658 result
|= AST_PREEMPT
;
2661 runq
= &processor
->runq
;
2662 if (runq
->highq
>= processor
->current_pri
) {
2663 if (runq
->urgency
> 0)
2664 return (AST_PREEMPT
| AST_URGENT
);
2666 result
|= AST_PREEMPT
;
2670 if (result
!= AST_NONE
)
2673 if (processor
->current_pri
< BASEPRI_RTQUEUES
&& processor
->processor_meta
!= PROCESSOR_META_NULL
&&
2674 processor
->processor_meta
->primary
!= processor
)
2675 return (AST_PREEMPT
);
2677 if (machine_processor_is_inactive(processor
))
2678 return (AST_PREEMPT
);
2680 if (processor
->active_thread
->state
& TH_SUSP
)
2681 return (AST_PREEMPT
);
2689 * Set the scheduled priority of the specified thread.
2691 * This may cause the thread to change queues.
2693 * Thread must be locked.
2700 boolean_t removed
= run_queue_remove(thread
);
2702 thread
->sched_pri
= priority
;
2704 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
2706 if (thread
->state
& TH_RUN
) {
2707 processor_t processor
= thread
->last_processor
;
2709 if (thread
== current_thread()) {
2712 processor
->current_pri
= priority
;
2713 if ((preempt
= csw_check(processor
)) != AST_NONE
)
2717 if ( processor
!= PROCESSOR_NULL
&&
2718 processor
->active_thread
== thread
)
2719 cause_ast_check(processor
);
2733 if (rq
!= thread
->runq
)
2734 panic("run_queue_check: thread runq");
2736 if (thread
->sched_pri
> MAXPRI
|| thread
->sched_pri
< MINPRI
)
2737 panic("run_queue_check: thread sched_pri");
2739 q
= &rq
->queues
[thread
->sched_pri
];
2740 qe
= queue_first(q
);
2741 while (!queue_end(q
, qe
)) {
2742 if (qe
== (queue_entry_t
)thread
)
2745 qe
= queue_next(qe
);
2748 panic("run_queue_check: end");
2756 * Remove a thread from a current run queue and
2757 * return TRUE if successful.
2759 * Thread must be locked.
2765 processor_t processor
= thread
->runq
;
2768 * If processor is PROCESSOR_NULL, the thread will stay out of the
2769 * run queues because the caller locked the thread. Otherwise
2770 * the thread is on a run queue, but could be chosen for dispatch
2773 if (processor
!= PROCESSOR_NULL
) {
2778 * The processor run queues are locked by the
2779 * processor set. Real-time priorities use a
2780 * global queue with a dedicated lock.
2782 if (thread
->sched_pri
< BASEPRI_RTQUEUES
) {
2783 rqlock
= &processor
->processor_set
->sched_lock
;
2784 rq
= &processor
->runq
;
2787 rqlock
= &rt_lock
; rq
= &rt_runq
;
2790 simple_lock(rqlock
);
2792 if (processor
== thread
->runq
) {
2794 * Thread is on a run queue and we have a lock on
2797 remqueue(&rq
->queues
[0], (queue_entry_t
)thread
);
2799 if (testbit(thread
->sched_pri
, sched_preempt_pri
)) {
2800 rq
->urgency
--; assert(rq
->urgency
>= 0);
2803 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
2804 /* update run queue status */
2805 if (thread
->sched_pri
!= IDLEPRI
)
2806 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2807 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2810 thread
->runq
= PROCESSOR_NULL
;
2814 * The thread left the run queue before we could
2815 * lock the run queue.
2817 assert(thread
->runq
== PROCESSOR_NULL
);
2818 processor
= PROCESSOR_NULL
;
2821 simple_unlock(rqlock
);
2824 return (processor
!= PROCESSOR_NULL
);
2828 * steal_processor_thread:
2830 * Locate a thread to steal from the processor and
2833 * Associated pset must be locked. Returns THREAD_NULL
2837 steal_processor_thread(
2838 processor_t processor
)
2840 run_queue_t rq
= &processor
->runq
;
2841 queue_t queue
= rq
->queues
+ rq
->highq
;
2842 int pri
= rq
->highq
, count
= rq
->count
;
2846 thread
= (thread_t
)queue_first(queue
);
2847 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2848 if (thread
->bound_processor
== PROCESSOR_NULL
) {
2849 remqueue(queue
, (queue_entry_t
)thread
);
2851 thread
->runq
= PROCESSOR_NULL
;
2853 if (testbit(pri
, sched_preempt_pri
)) {
2854 rq
->urgency
--; assert(rq
->urgency
>= 0);
2856 if (queue_empty(queue
)) {
2858 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2859 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2866 thread
= (thread_t
)queue_next((queue_entry_t
)thread
);
2872 return (THREAD_NULL
);
2876 * Locate and steal a thread, beginning
2879 * The pset must be locked, and is returned
2882 * Returns the stolen thread, or THREAD_NULL on
2887 processor_set_t pset
)
2889 processor_set_t nset
, cset
= pset
;
2890 processor_t processor
;
2894 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2895 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2896 if (processor
->runq
.count
> 0) {
2897 thread
= steal_processor_thread(processor
);
2898 if (thread
!= THREAD_NULL
) {
2899 remqueue(&cset
->active_queue
, (queue_entry_t
)processor
);
2900 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2908 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2911 nset
= next_pset(cset
);
2919 } while (nset
!= pset
);
2923 return (THREAD_NULL
);
2927 * This is the processor idle loop, which just looks for other threads
2928 * to execute. Processor idle threads invoke this without supplying a
2929 * current thread to idle without an asserted wait state.
2931 * Returns a the next thread to execute if dispatched directly.
2936 processor_t processor
)
2938 processor_set_t pset
= processor
->processor_set
;
2939 thread_t new_thread
;
2944 KERNEL_DEBUG_CONSTANT(
2945 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_START
, (uintptr_t)thread_tid(thread
), 0, 0, 0, 0);
2947 timer_switch(&PROCESSOR_DATA(processor
, system_state
),
2948 mach_absolute_time(), &PROCESSOR_DATA(processor
, idle_state
));
2949 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, idle_state
);
2951 while (processor
->next_thread
== THREAD_NULL
&& processor
->runq
.count
== 0 && rt_runq
.count
== 0 &&
2952 (thread
== THREAD_NULL
|| ((thread
->state
& (TH_WAIT
|TH_SUSP
)) == TH_WAIT
&& !thread
->wake_active
))) {
2957 if (processor
->state
== PROCESSOR_INACTIVE
&& !machine_processor_is_inactive(processor
))
2961 timer_switch(&PROCESSOR_DATA(processor
, idle_state
),
2962 mach_absolute_time(), &PROCESSOR_DATA(processor
, system_state
));
2963 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
2967 state
= processor
->state
;
2968 if (state
== PROCESSOR_DISPATCHING
) {
2970 * Commmon case -- cpu dispatched.
2972 new_thread
= processor
->next_thread
;
2973 processor
->next_thread
= THREAD_NULL
;
2974 processor
->state
= PROCESSOR_RUNNING
;
2976 if ( processor
->runq
.highq
> new_thread
->sched_pri
||
2977 (rt_runq
.highq
> 0 && rt_runq
.highq
>= new_thread
->sched_pri
) ) {
2978 processor
->deadline
= UINT64_MAX
;
2982 thread_lock(new_thread
);
2983 thread_setrun(new_thread
, SCHED_HEADQ
);
2984 thread_unlock(new_thread
);
2986 KERNEL_DEBUG_CONSTANT(
2987 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (uintptr_t)thread_tid(thread
), state
, 0, 0, 0);
2989 return (THREAD_NULL
);
2994 KERNEL_DEBUG_CONSTANT(
2995 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (uintptr_t)thread_tid(thread
), state
, (uintptr_t)thread_tid(new_thread
), 0, 0);
2997 return (new_thread
);
3000 if (state
== PROCESSOR_IDLE
) {
3001 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
3003 processor
->state
= PROCESSOR_RUNNING
;
3004 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
3007 if (state
== PROCESSOR_INACTIVE
) {
3008 processor
->state
= PROCESSOR_RUNNING
;
3009 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
3012 if (state
== PROCESSOR_SHUTDOWN
) {
3014 * Going off-line. Force a
3017 if ((new_thread
= processor
->next_thread
) != THREAD_NULL
) {
3018 processor
->next_thread
= THREAD_NULL
;
3019 processor
->deadline
= UINT64_MAX
;
3023 thread_lock(new_thread
);
3024 thread_setrun(new_thread
, SCHED_HEADQ
);
3025 thread_unlock(new_thread
);
3027 KERNEL_DEBUG_CONSTANT(
3028 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (uintptr_t)thread_tid(thread
), state
, 0, 0, 0);
3030 return (THREAD_NULL
);
3036 KERNEL_DEBUG_CONSTANT(
3037 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (uintptr_t)thread_tid(thread
), state
, 0, 0, 0);
3039 return (THREAD_NULL
);
3043 * Each processor has a dedicated thread which
3044 * executes the idle loop when there is no suitable
3050 processor_t processor
= current_processor();
3051 thread_t new_thread
;
3053 new_thread
= processor_idle(THREAD_NULL
, processor
);
3054 if (new_thread
!= THREAD_NULL
) {
3055 thread_run(processor
->idle_thread
, (thread_continue_t
)idle_thread
, NULL
, new_thread
);
3059 thread_block((thread_continue_t
)idle_thread
);
3065 processor_t processor
)
3067 kern_return_t result
;
3071 result
= kernel_thread_create((thread_continue_t
)idle_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
3072 if (result
!= KERN_SUCCESS
)
3076 thread_lock(thread
);
3077 thread
->bound_processor
= processor
;
3078 processor
->idle_thread
= thread
;
3079 thread
->sched_pri
= thread
->priority
= IDLEPRI
;
3080 thread
->state
= (TH_RUN
| TH_IDLE
);
3081 thread_unlock(thread
);
3084 thread_deallocate(thread
);
3086 return (KERN_SUCCESS
);
3089 static uint64_t sched_tick_deadline
;
3094 * Kicks off scheduler services.
3096 * Called at splsched.
3101 kern_return_t result
;
3104 result
= kernel_thread_start_priority((thread_continue_t
)sched_tick_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
3105 if (result
!= KERN_SUCCESS
)
3106 panic("sched_startup");
3108 thread_deallocate(thread
);
3111 * Yield to the sched_tick_thread while it times
3112 * a series of context switches back. It stores
3113 * the baseline value in sched_cswtime.
3115 * The current thread is the only other thread
3116 * active at this point.
3118 while (sched_cswtime
== 0)
3119 thread_block(THREAD_CONTINUE_NULL
);
3121 thread_daemon_init();
3123 thread_call_initialize();
3127 * sched_tick_thread:
3129 * Perform periodic bookkeeping functions about ten
3133 sched_tick_continue(void)
3135 uint64_t abstime
= mach_absolute_time();
3140 * Compute various averages.
3145 * Scan the run queues for threads which
3146 * may need to be updated.
3148 thread_update_scan();
3150 clock_deadline_for_periodic_event(sched_tick_interval
, abstime
,
3151 &sched_tick_deadline
);
3153 assert_wait_deadline((event_t
)sched_tick_thread
, THREAD_UNINT
, sched_tick_deadline
);
3154 thread_block((thread_continue_t
)sched_tick_continue
);
3159 * Time a series of context switches to determine
3160 * a baseline. Toss the high and low and return
3161 * the one-way value.
3166 uint32_t new, hi
, low
, accum
;
3170 accum
= hi
= low
= 0;
3171 for (i
= 0; i
< tries
; ++i
) {
3172 abstime
= mach_absolute_time();
3173 thread_block(THREAD_CONTINUE_NULL
);
3175 new = (uint32_t)(mach_absolute_time() - abstime
);
3178 accum
= hi
= low
= new;
3189 return ((accum
- hi
- low
) / (2 * (tries
- 2)));
3193 sched_tick_thread(void)
3195 sched_cswtime
= time_cswitch();
3197 sched_tick_deadline
= mach_absolute_time();
3199 sched_tick_continue();
3204 * thread_update_scan / runq_scan:
3206 * Scan the run queues to account for timesharing threads
3207 * which need to be updated.
3209 * Scanner runs in two passes. Pass one squirrels likely
3210 * threads away in an array, pass two does the update.
3212 * This is necessary because the run queue is locked for
3213 * the candidate scan, but the thread is locked for the update.
3215 * Array should be sized to make forward progress, without
3216 * disabling preemption for long periods.
3219 #define THREAD_UPDATE_SIZE 128
3221 static thread_t thread_update_array
[THREAD_UPDATE_SIZE
];
3222 static int thread_update_count
= 0;
3225 * Scan a runq for candidate threads.
3227 * Returns TRUE if retry is needed.
3235 register thread_t thread
;
3237 if ((count
= runq
->count
) > 0) {
3238 q
= runq
->queues
+ runq
->highq
;
3240 queue_iterate(q
, thread
, thread_t
, links
) {
3241 if ( thread
->sched_stamp
!= sched_tick
&&
3242 (thread
->sched_mode
& TH_MODE_TIMESHARE
) ) {
3243 if (thread_update_count
== THREAD_UPDATE_SIZE
)
3246 thread_update_array
[thread_update_count
++] = thread
;
3247 thread_reference_internal(thread
);
3261 thread_update_scan(void)
3263 boolean_t restart_needed
= FALSE
;
3264 processor_t processor
= processor_list
;
3265 processor_set_t pset
;
3271 pset
= processor
->processor_set
;
3276 restart_needed
= runq_scan(&processor
->runq
);
3284 thread
= processor
->idle_thread
;
3285 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
3286 if (thread_update_count
== THREAD_UPDATE_SIZE
) {
3287 restart_needed
= TRUE
;
3291 thread_update_array
[thread_update_count
++] = thread
;
3292 thread_reference_internal(thread
);
3294 } while ((processor
= processor
->processor_list
) != NULL
);
3297 * Ok, we now have a collection of candidates -- fix them.
3299 while (thread_update_count
> 0) {
3300 thread
= thread_update_array
[--thread_update_count
];
3301 thread_update_array
[thread_update_count
] = THREAD_NULL
;
3304 thread_lock(thread
);
3305 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
3306 thread
->sched_stamp
!= sched_tick
)
3307 update_priority(thread
);
3308 thread_unlock(thread
);
3311 thread_deallocate(thread
);
3313 } while (restart_needed
);
3317 * Just in case someone doesn't use the macro
3319 #undef thread_wakeup
3328 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
3332 preemption_enabled(void)
3334 return (get_preemption_level() == 0 && ml_get_interrupts_enabled());
3342 return ((thread
->state
& (TH_RUN
|TH_WAIT
)) == TH_RUN
);
3347 #include <ddb/db_output.h>
3348 #define printf kdbprintf
3349 void db_sched(void);
3354 iprintf("Scheduling Statistics:\n");
3356 iprintf("Thread invocations: csw %d same %d\n",
3357 c_thread_invoke_csw
, c_thread_invoke_same
);
3359 iprintf("Thread block: calls %d\n",
3360 c_thread_block_calls
);
3361 iprintf("Idle thread:\n\thandoff %d block %d\n",
3362 c_idle_thread_handoff
,
3363 c_idle_thread_block
);
3364 iprintf("Sched thread blocks: %d\n", c_sched_thread_block
);
3365 #endif /* MACH_COUNTERS */
3369 #include <ddb/db_output.h>
3370 void db_show_thread_log(void);
3373 db_show_thread_log(void)
3376 #endif /* MACH_KDB */