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 <sys/kdebug.h>
106 #include <kern/pms.h>
108 struct run_queue rt_runq
;
109 #define RT_RUNQ ((processor_t)-1)
110 decl_simple_lock_data(static,rt_lock
);
112 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
113 int default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
115 #define MAX_UNSAFE_QUANTA 800
116 int max_unsafe_quanta
= MAX_UNSAFE_QUANTA
;
118 #define MAX_POLL_QUANTA 2
119 int max_poll_quanta
= MAX_POLL_QUANTA
;
121 #define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
122 int sched_poll_yield_shift
= SCHED_POLL_YIELD_SHIFT
;
124 uint64_t max_unsafe_computation
;
125 uint32_t sched_safe_duration
;
126 uint64_t max_poll_computation
;
128 uint32_t std_quantum
;
129 uint32_t min_std_quantum
;
131 uint32_t std_quantum_us
;
133 uint32_t max_rt_quantum
;
134 uint32_t min_rt_quantum
;
136 uint32_t sched_cswtime
;
139 uint32_t sched_tick_interval
;
141 uint32_t sched_pri_shift
= INT8_MAX
;
142 uint32_t sched_fixed_shift
;
144 uint32_t sched_run_count
, sched_share_count
;
145 uint32_t sched_load_average
, sched_mach_factor
;
148 void wait_queues_init(void) __attribute__((section("__TEXT, initcode")));
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
thread_select_idle(
159 processor_t processor
);
161 static thread_t
processor_idle(
163 processor_t processor
);
165 static thread_t
steal_thread(
166 processor_set_t pset
);
168 static thread_t
steal_processor_thread(
169 processor_t processor
);
171 static void thread_update_scan(void);
174 extern int debug_task
;
175 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
177 #define TLOG(a, fmt, args...) do {} while (0)
182 boolean_t
thread_runnable(
190 * states are combinations of:
192 * W waiting (or on wait queue)
193 * N non-interruptible
198 * assert_wait thread_block clear_wait swapout swapin
200 * R RW, RWN R; setrun - -
201 * RN RWN RN; setrun - -
214 * Waiting protocols and implementation:
216 * Each thread may be waiting for exactly one event; this event
217 * is set using assert_wait(). That thread may be awakened either
218 * by performing a thread_wakeup_prim() on its event,
219 * or by directly waking that thread up with clear_wait().
221 * The implementation of wait events uses a hash table. Each
222 * bucket is queue of threads having the same hash function
223 * value; the chain for the queue (linked list) is the run queue
224 * field. [It is not possible to be waiting and runnable at the
227 * Locks on both the thread and on the hash buckets govern the
228 * wait event field and the queue chain field. Because wakeup
229 * operations only have the event as an argument, the event hash
230 * bucket must be locked before any thread.
232 * Scheduling operations may also occur at interrupt level; therefore,
233 * interrupts below splsched() must be prevented when holding
234 * thread or hash bucket locks.
236 * The wait event hash table declarations are as follows:
241 struct wait_queue wait_queues
[NUMQUEUES
];
243 #define wait_hash(event) \
244 ((((int)(event) < 0)? ~(int)(event): (int)(event)) % NUMQUEUES)
246 int8_t sched_load_shifts
[NRQS
];
247 int sched_preempt_pri
[NRQBM
];
253 * Calculate the timeslicing quantum
256 if (default_preemption_rate
< 1)
257 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
258 std_quantum_us
= (1000 * 1000) / default_preemption_rate
;
260 printf("standard timeslicing quantum is %d us\n", std_quantum_us
);
262 sched_safe_duration
= (2 * max_unsafe_quanta
/ default_preemption_rate
) *
263 (1 << SCHED_TICK_SHIFT
);
268 simple_lock_init(&rt_lock
, 0);
269 run_queue_init(&rt_runq
);
275 sched_timebase_init(void)
280 /* standard timeslicing quantum */
281 clock_interval_to_absolutetime_interval(
282 std_quantum_us
, NSEC_PER_USEC
, &abstime
);
283 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
284 std_quantum
= abstime
;
286 /* smallest remaining quantum (250 us) */
287 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC
, &abstime
);
288 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
289 min_std_quantum
= abstime
;
291 /* smallest rt computaton (50 us) */
292 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC
, &abstime
);
293 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
294 min_rt_quantum
= abstime
;
296 /* maximum rt computation (50 ms) */
297 clock_interval_to_absolutetime_interval(
298 50, 1000*NSEC_PER_USEC
, &abstime
);
299 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
300 max_rt_quantum
= abstime
;
302 /* scheduler tick interval */
303 clock_interval_to_absolutetime_interval(USEC_PER_SEC
>> SCHED_TICK_SHIFT
,
304 NSEC_PER_USEC
, &abstime
);
305 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
306 sched_tick_interval
= abstime
;
309 * Compute conversion factor from usage to
310 * timesharing priorities with 5/8 ** n aging.
312 abstime
= (abstime
* 5) / 3;
313 for (shift
= 0; abstime
> BASEPRI_DEFAULT
; ++shift
)
315 sched_fixed_shift
= shift
;
317 max_unsafe_computation
= max_unsafe_quanta
* std_quantum
;
318 max_poll_computation
= max_poll_quanta
* std_quantum
;
322 wait_queues_init(void)
326 for (i
= 0; i
< NUMQUEUES
; i
++) {
327 wait_queue_init(&wait_queues
[i
], SYNC_POLICY_FIFO
);
332 * Set up values for timeshare
336 load_shift_init(void)
338 int8_t k
, *p
= sched_load_shifts
;
341 *p
++ = INT8_MIN
; *p
++ = 0;
343 for (i
= j
= 2, k
= 1; i
< NRQS
; ++k
) {
344 for (j
<<= 1; i
< j
; ++i
)
350 preempt_pri_init(void)
352 int i
, *p
= sched_preempt_pri
;
354 for (i
= BASEPRI_FOREGROUND
+ 1; i
< MINPRI_KERNEL
; ++i
)
357 for (i
= BASEPRI_PREEMPT
; i
<= MAXPRI
; ++i
)
362 * Thread wait timer expiration.
369 thread_t thread
= p0
;
374 if (--thread
->wait_timer_active
== 0) {
375 if (thread
->wait_timer_is_set
) {
376 thread
->wait_timer_is_set
= FALSE
;
377 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
380 thread_unlock(thread
);
387 * Set a timer for the current thread, if the thread
388 * is ready to wait. Must be called between assert_wait()
389 * and thread_block().
394 uint32_t scale_factor
)
396 thread_t thread
= current_thread();
402 if ((thread
->state
& TH_WAIT
) != 0) {
403 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
404 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
405 thread
->wait_timer_active
++;
406 thread
->wait_timer_is_set
= TRUE
;
408 thread_unlock(thread
);
413 thread_set_timer_deadline(
416 thread_t thread
= current_thread();
421 if ((thread
->state
& TH_WAIT
) != 0) {
422 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
423 thread
->wait_timer_active
++;
424 thread
->wait_timer_is_set
= TRUE
;
426 thread_unlock(thread
);
431 thread_cancel_timer(void)
433 thread_t thread
= current_thread();
438 if (thread
->wait_timer_is_set
) {
439 if (timer_call_cancel(&thread
->wait_timer
))
440 thread
->wait_timer_active
--;
441 thread
->wait_timer_is_set
= FALSE
;
443 thread_unlock(thread
);
450 * Unblock thread on wake up.
452 * Returns TRUE if the thread is still running.
454 * Thread must be locked.
459 wait_result_t wresult
)
461 boolean_t result
= FALSE
;
466 thread
->wait_result
= wresult
;
469 * Cancel pending wait timer.
471 if (thread
->wait_timer_is_set
) {
472 if (timer_call_cancel(&thread
->wait_timer
))
473 thread
->wait_timer_active
--;
474 thread
->wait_timer_is_set
= FALSE
;
478 * Update scheduling state: not waiting,
481 thread
->state
&= ~(TH_WAIT
|TH_UNINT
);
483 if (!(thread
->state
& TH_RUN
)) {
484 thread
->state
|= TH_RUN
;
486 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
492 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
497 * Signal if idling on another processor.
499 if (thread
->state
& TH_IDLE
) {
500 processor_t processor
= thread
->last_processor
;
502 if (processor
!= current_processor())
503 machine_signal_idle(processor
);
510 * Calculate deadline for real-time threads.
512 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
513 thread
->realtime
.deadline
= mach_absolute_time();
514 thread
->realtime
.deadline
+= thread
->realtime
.constraint
;
518 * Clear old quantum, fail-safe computation, etc.
520 thread
->current_quantum
= 0;
521 thread
->computation_metered
= 0;
522 thread
->reason
= AST_NONE
;
524 KERNEL_DEBUG_CONSTANT(
525 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_MAKE_RUNNABLE
) | DBG_FUNC_NONE
,
526 (int)thread
, (int)thread
->sched_pri
, 0, 0, 0);
534 * Unblock and dispatch thread.
536 * thread lock held, IPC locks may be held.
537 * thread must have been pulled from wait queue under same lock hold.
539 * KERN_SUCCESS - Thread was set running
540 * KERN_NOT_WAITING - Thread was not waiting
545 wait_result_t wresult
)
547 assert(thread
->at_safe_point
== FALSE
);
548 assert(thread
->wait_event
== NO_EVENT64
);
549 assert(thread
->wait_queue
== WAIT_QUEUE_NULL
);
551 if ((thread
->state
& (TH_WAIT
|TH_TERMINATE
)) == TH_WAIT
) {
552 if (!thread_unblock(thread
, wresult
))
553 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
555 return (KERN_SUCCESS
);
558 return (KERN_NOT_WAITING
);
562 * Routine: thread_mark_wait_locked
564 * Mark a thread as waiting. If, given the circumstances,
565 * it doesn't want to wait (i.e. already aborted), then
566 * indicate that in the return value.
568 * at splsched() and thread is locked.
572 thread_mark_wait_locked(
574 wait_interrupt_t interruptible
)
576 boolean_t at_safe_point
;
579 * The thread may have certain types of interrupts/aborts masked
580 * off. Even if the wait location says these types of interrupts
581 * are OK, we have to honor mask settings (outer-scoped code may
582 * not be able to handle aborts at the moment).
584 if (interruptible
> (thread
->options
& TH_OPT_INTMASK
))
585 interruptible
= thread
->options
& TH_OPT_INTMASK
;
587 at_safe_point
= (interruptible
== THREAD_ABORTSAFE
);
589 if ( interruptible
== THREAD_UNINT
||
590 !(thread
->sched_mode
& TH_MODE_ABORT
) ||
592 (thread
->sched_mode
& TH_MODE_ABORTSAFELY
))) {
593 thread
->state
|= (interruptible
) ? TH_WAIT
: (TH_WAIT
| TH_UNINT
);
594 thread
->at_safe_point
= at_safe_point
;
595 return (thread
->wait_result
= THREAD_WAITING
);
598 if (thread
->sched_mode
& TH_MODE_ABORTSAFELY
)
599 thread
->sched_mode
&= ~TH_MODE_ISABORTED
;
601 return (thread
->wait_result
= THREAD_INTERRUPTED
);
605 * Routine: thread_interrupt_level
607 * Set the maximum interruptible state for the
608 * current thread. The effective value of any
609 * interruptible flag passed into assert_wait
610 * will never exceed this.
612 * Useful for code that must not be interrupted,
613 * but which calls code that doesn't know that.
615 * The old interrupt level for the thread.
619 thread_interrupt_level(
620 wait_interrupt_t new_level
)
622 thread_t thread
= current_thread();
623 wait_interrupt_t result
= thread
->options
& TH_OPT_INTMASK
;
625 thread
->options
= (thread
->options
& ~TH_OPT_INTMASK
) | (new_level
& TH_OPT_INTMASK
);
631 * Check to see if an assert wait is possible, without actually doing one.
632 * This is used by debug code in locks and elsewhere to verify that it is
633 * always OK to block when trying to take a blocking lock (since waiting
634 * for the actual assert_wait to catch the case may make it hard to detect
638 assert_wait_possible(void)
644 if(debug_mode
) return TRUE
; /* Always succeed in debug mode */
647 thread
= current_thread();
649 return (thread
== NULL
|| wait_queue_assert_possible(thread
));
655 * Assert that the current thread is about to go to
656 * sleep until the specified event occurs.
661 wait_interrupt_t interruptible
)
663 register wait_queue_t wq
;
666 assert(event
!= NO_EVENT
);
668 index
= wait_hash(event
);
669 wq
= &wait_queues
[index
];
670 return wait_queue_assert_wait(wq
, event
, interruptible
, 0);
676 wait_interrupt_t interruptible
,
678 uint32_t scale_factor
)
680 thread_t thread
= current_thread();
681 wait_result_t wresult
;
686 assert(event
!= NO_EVENT
);
687 wqueue
= &wait_queues
[wait_hash(event
)];
690 wait_queue_lock(wqueue
);
693 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
694 wresult
= wait_queue_assert_wait64_locked(wqueue
, (uint32_t)event
,
695 interruptible
, deadline
, thread
);
697 thread_unlock(thread
);
698 wait_queue_unlock(wqueue
);
705 assert_wait_deadline(
707 wait_interrupt_t interruptible
,
710 thread_t thread
= current_thread();
711 wait_result_t wresult
;
715 assert(event
!= NO_EVENT
);
716 wqueue
= &wait_queues
[wait_hash(event
)];
719 wait_queue_lock(wqueue
);
722 wresult
= wait_queue_assert_wait64_locked(wqueue
, (uint32_t)event
,
723 interruptible
, deadline
, thread
);
725 thread_unlock(thread
);
726 wait_queue_unlock(wqueue
);
733 * thread_sleep_fast_usimple_lock:
735 * Cause the current thread to wait until the specified event
736 * occurs. The specified simple_lock is unlocked before releasing
737 * the cpu and re-acquired as part of waking up.
739 * This is the simple lock sleep interface for components that use a
740 * faster version of simple_lock() than is provided by usimple_lock().
742 __private_extern__ wait_result_t
743 thread_sleep_fast_usimple_lock(
746 wait_interrupt_t interruptible
)
750 res
= assert_wait(event
, interruptible
);
751 if (res
== THREAD_WAITING
) {
753 res
= thread_block(THREAD_CONTINUE_NULL
);
761 * thread_sleep_usimple_lock:
763 * Cause the current thread to wait until the specified event
764 * occurs. The specified usimple_lock is unlocked before releasing
765 * the cpu and re-acquired as part of waking up.
767 * This is the simple lock sleep interface for components where
768 * simple_lock() is defined in terms of usimple_lock().
771 thread_sleep_usimple_lock(
774 wait_interrupt_t interruptible
)
778 res
= assert_wait(event
, interruptible
);
779 if (res
== THREAD_WAITING
) {
780 usimple_unlock(lock
);
781 res
= thread_block(THREAD_CONTINUE_NULL
);
788 * thread_sleep_mutex:
790 * Cause the current thread to wait until the specified event
791 * occurs. The specified mutex is unlocked before releasing
792 * the cpu. The mutex will be re-acquired before returning.
794 * JMM - Add hint to make sure mutex is available before rousting
800 wait_interrupt_t interruptible
)
804 res
= assert_wait(event
, interruptible
);
805 if (res
== THREAD_WAITING
) {
807 res
= thread_block(THREAD_CONTINUE_NULL
);
814 * thread_sleep_mutex_deadline:
816 * Cause the current thread to wait until the specified event
817 * (or deadline) occurs. The specified mutex is unlocked before
818 * releasing the cpu. The mutex will be re-acquired before returning.
821 thread_sleep_mutex_deadline(
825 wait_interrupt_t interruptible
)
829 res
= assert_wait_deadline(event
, interruptible
, deadline
);
830 if (res
== THREAD_WAITING
) {
832 res
= thread_block(THREAD_CONTINUE_NULL
);
839 * thread_sleep_lock_write:
841 * Cause the current thread to wait until the specified event
842 * occurs. The specified (write) lock is unlocked before releasing
843 * the cpu. The (write) lock will be re-acquired before returning.
846 thread_sleep_lock_write(
849 wait_interrupt_t interruptible
)
853 res
= assert_wait(event
, interruptible
);
854 if (res
== THREAD_WAITING
) {
855 lock_write_done(lock
);
856 res
= thread_block(THREAD_CONTINUE_NULL
);
865 * Force a preemption point for a thread and wait
866 * for it to stop running. Arbitrates access among
867 * multiple stop requests. (released by unstop)
869 * The thread must enter a wait state and stop via a
872 * Returns FALSE if interrupted.
878 wait_result_t wresult
;
879 spl_t s
= splsched();
884 while (thread
->state
& TH_SUSP
) {
885 thread
->wake_active
= TRUE
;
886 thread_unlock(thread
);
888 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
892 if (wresult
== THREAD_WAITING
)
893 wresult
= thread_block(THREAD_CONTINUE_NULL
);
895 if (wresult
!= THREAD_AWAKENED
)
903 thread
->state
|= TH_SUSP
;
905 while (thread
->state
& TH_RUN
) {
906 processor_t processor
= thread
->last_processor
;
908 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
909 cause_ast_check(processor
);
911 thread
->wake_active
= TRUE
;
912 thread_unlock(thread
);
914 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
918 if (wresult
== THREAD_WAITING
)
919 wresult
= thread_block(THREAD_CONTINUE_NULL
);
921 if (wresult
!= THREAD_AWAKENED
) {
922 thread_unstop(thread
);
931 thread_unlock(thread
);
941 * Release a previous stop request and set
942 * the thread running if appropriate.
944 * Use only after a successful stop operation.
950 spl_t s
= splsched();
955 if ((thread
->state
& (TH_RUN
|TH_WAIT
|TH_SUSP
)) == TH_SUSP
) {
956 thread
->state
&= ~TH_SUSP
;
957 thread_unblock(thread
, THREAD_AWAKENED
);
959 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
962 if (thread
->state
& TH_SUSP
) {
963 thread
->state
&= ~TH_SUSP
;
965 if (thread
->wake_active
) {
966 thread
->wake_active
= FALSE
;
967 thread_unlock(thread
);
969 thread_wakeup(&thread
->wake_active
);
977 thread_unlock(thread
);
985 * Wait for a thread to stop running. (non-interruptible)
992 wait_result_t wresult
;
993 spl_t s
= splsched();
998 while (thread
->state
& TH_RUN
) {
999 processor_t processor
= thread
->last_processor
;
1001 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
1002 cause_ast_check(processor
);
1004 thread
->wake_active
= TRUE
;
1005 thread_unlock(thread
);
1007 wresult
= assert_wait(&thread
->wake_active
, THREAD_UNINT
);
1008 wake_unlock(thread
);
1011 if (wresult
== THREAD_WAITING
)
1012 thread_block(THREAD_CONTINUE_NULL
);
1016 thread_lock(thread
);
1019 thread_unlock(thread
);
1020 wake_unlock(thread
);
1025 * Routine: clear_wait_internal
1027 * Clear the wait condition for the specified thread.
1028 * Start the thread executing if that is appropriate.
1030 * thread thread to awaken
1031 * result Wakeup result the thread should see
1034 * the thread is locked.
1036 * KERN_SUCCESS thread was rousted out a wait
1037 * KERN_FAILURE thread was waiting but could not be rousted
1038 * KERN_NOT_WAITING thread was not waiting
1040 __private_extern__ kern_return_t
1041 clear_wait_internal(
1043 wait_result_t wresult
)
1045 wait_queue_t wq
= thread
->wait_queue
;
1046 int i
= LockTimeOut
;
1049 if (wresult
== THREAD_INTERRUPTED
&& (thread
->state
& TH_UNINT
))
1050 return (KERN_FAILURE
);
1052 if (wq
!= WAIT_QUEUE_NULL
) {
1053 if (wait_queue_lock_try(wq
)) {
1054 wait_queue_pull_thread_locked(wq
, thread
, TRUE
);
1055 /* wait queue unlocked, thread still locked */
1058 thread_unlock(thread
);
1061 thread_lock(thread
);
1062 if (wq
!= thread
->wait_queue
)
1063 return (KERN_NOT_WAITING
);
1069 return (thread_go(thread
, wresult
));
1072 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
1073 thread
, wq
, cpu_number());
1075 return (KERN_FAILURE
);
1082 * Clear the wait condition for the specified thread. Start the thread
1083 * executing if that is appropriate.
1086 * thread thread to awaken
1087 * result Wakeup result the thread should see
1092 wait_result_t result
)
1098 thread_lock(thread
);
1099 ret
= clear_wait_internal(thread
, result
);
1100 thread_unlock(thread
);
1107 * thread_wakeup_prim:
1109 * Common routine for thread_wakeup, thread_wakeup_with_result,
1110 * and thread_wakeup_one.
1116 boolean_t one_thread
,
1117 wait_result_t result
)
1119 register wait_queue_t wq
;
1122 index
= wait_hash(event
);
1123 wq
= &wait_queues
[index
];
1125 return (wait_queue_wakeup_one(wq
, event
, result
));
1127 return (wait_queue_wakeup_all(wq
, event
, result
));
1133 * Force the current thread to execute on the specified processor.
1135 * Returns the previous binding. PROCESSOR_NULL means
1138 * XXX - DO NOT export this to users - XXX
1142 processor_t processor
)
1144 thread_t self
= current_thread();
1151 prev
= self
->bound_processor
;
1152 self
->bound_processor
= processor
;
1154 thread_unlock(self
);
1163 * Select a new thread for the current processor to execute.
1165 * May select the current thread, which must be locked.
1170 processor_t processor
)
1172 processor_set_t pset
= processor
->processor_set
;
1173 thread_t new_thread
= THREAD_NULL
;
1174 boolean_t other_runnable
, inactive_state
;
1178 * Update the priority.
1180 if (thread
->sched_stamp
!= sched_tick
)
1181 update_priority(thread
);
1183 processor
->current_pri
= thread
->sched_pri
;
1187 inactive_state
= processor
->state
!= PROCESSOR_SHUTDOWN
&& machine_cpu_is_inactive(processor
->cpu_num
);
1189 simple_lock(&rt_lock
);
1192 * Check for other runnable threads.
1194 other_runnable
= processor
->runq
.count
> 0 || rt_runq
.count
> 0;
1197 * Test to see if the current thread should continue
1198 * to run on this processor. Must be runnable, and not
1199 * bound to a different processor, nor be in the wrong
1202 if ( thread
->state
== TH_RUN
&&
1203 (thread
->bound_processor
== PROCESSOR_NULL
||
1204 thread
->bound_processor
== processor
) &&
1205 (thread
->affinity_set
== AFFINITY_SET_NULL
||
1206 thread
->affinity_set
->aset_pset
== pset
) ) {
1207 if ( thread
->sched_pri
>= BASEPRI_RTQUEUES
&&
1208 first_timeslice(processor
) ) {
1209 if (rt_runq
.highq
>= BASEPRI_RTQUEUES
) {
1210 register run_queue_t runq
= &rt_runq
;
1213 q
= runq
->queues
+ runq
->highq
;
1214 if (((thread_t
)q
->next
)->realtime
.deadline
<
1215 processor
->deadline
) {
1216 thread
= (thread_t
)q
->next
;
1217 ((queue_entry_t
)thread
)->next
->prev
= q
;
1218 q
->next
= ((queue_entry_t
)thread
)->next
;
1219 thread
->runq
= PROCESSOR_NULL
;
1220 runq
->count
--; runq
->urgency
--;
1221 assert(runq
->urgency
>= 0);
1222 if (queue_empty(q
)) {
1223 if (runq
->highq
!= IDLEPRI
)
1224 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
1225 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
1230 simple_unlock(&rt_lock
);
1232 processor
->deadline
= thread
->realtime
.deadline
;
1239 if (!inactive_state
&&
1241 (processor
->runq
.highq
< thread
->sched_pri
&&
1242 rt_runq
.highq
< thread
->sched_pri
)) ) {
1244 simple_unlock(&rt_lock
);
1246 /* I am the highest priority runnable (non-idle) thread */
1248 pset_pri_hint(pset
, processor
, processor
->current_pri
);
1250 pset_count_hint(pset
, processor
, processor
->runq
.count
);
1252 processor
->deadline
= UINT64_MAX
;
1260 if (other_runnable
) {
1261 if (processor
->runq
.count
> 0 && processor
->runq
.highq
>= rt_runq
.highq
) {
1262 simple_unlock(&rt_lock
);
1264 thread
= run_queue_dequeue(&processor
->runq
, SCHED_HEADQ
);
1266 if (!inactive_state
) {
1267 pset_pri_hint(pset
, processor
, thread
->sched_pri
);
1269 pset_count_hint(pset
, processor
, processor
->runq
.count
);
1272 processor
->deadline
= UINT64_MAX
;
1278 thread
= run_queue_dequeue(&rt_runq
, SCHED_HEADQ
);
1279 simple_unlock(&rt_lock
);
1281 processor
->deadline
= thread
->realtime
.deadline
;
1287 simple_unlock(&rt_lock
);
1289 processor
->deadline
= UINT64_MAX
;
1291 if (inactive_state
) {
1292 if (processor
->state
== PROCESSOR_RUNNING
)
1293 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
1295 if (processor
->state
== PROCESSOR_IDLE
)
1296 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
1298 processor
->state
= PROCESSOR_INACTIVE
;
1302 return (processor
->idle_thread
);
1306 * No runnable threads, attempt to steal
1307 * from other processors.
1309 new_thread
= steal_thread(pset
);
1310 if (new_thread
!= THREAD_NULL
)
1311 return (new_thread
);
1314 * If other threads have appeared, shortcut
1317 if (processor
->runq
.count
> 0 || rt_runq
.count
> 0)
1323 * Nothing is runnable, so set this processor idle if it
1326 if (processor
->state
== PROCESSOR_RUNNING
) {
1327 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
1328 processor
->state
= PROCESSOR_IDLE
;
1330 enqueue_head(&pset
->idle_queue
, (queue_entry_t
)processor
);
1331 pset
->low_pri
= pset
->low_count
= processor
;
1337 * Choose idle thread if fast idle is not possible.
1339 if ((thread
->state
& (TH_IDLE
|TH_TERMINATE
|TH_SUSP
)) || !(thread
->state
& TH_WAIT
) || thread
->wake_active
)
1340 return (processor
->idle_thread
);
1343 * Perform idling activities directly without a
1344 * context switch. Return dispatched thread,
1345 * else check again for a runnable thread.
1347 new_thread
= thread_select_idle(thread
, processor
);
1349 } while (new_thread
== THREAD_NULL
);
1351 return (new_thread
);
1355 * thread_select_idle:
1357 * Idle the processor using the current thread context.
1359 * Called with thread locked, then dropped and relocked.
1364 processor_t processor
)
1366 thread_t new_thread
;
1368 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1372 thread
->state
|= TH_IDLE
;
1373 processor
->current_pri
= IDLEPRI
;
1375 thread_unlock(thread
);
1378 * Switch execution timing to processor idle thread.
1380 processor
->last_dispatch
= mach_absolute_time();
1381 thread_timer_event(processor
->last_dispatch
, &processor
->idle_thread
->system_timer
);
1382 PROCESSOR_DATA(processor
, kernel_timer
) = &processor
->idle_thread
->system_timer
;
1385 * Cancel the quantum timer while idling.
1387 timer_call_cancel(&processor
->quantum_timer
);
1388 processor
->timeslice
= 0;
1390 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1393 * Enable interrupts and perform idling activities. No
1394 * preemption due to TH_IDLE being set.
1396 spllo(); new_thread
= processor_idle(thread
, processor
);
1399 * Return at splsched.
1401 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
1403 thread_lock(thread
);
1406 * If awakened, switch to thread timer and start a new quantum.
1407 * Otherwise skip; we will context switch to another thread or return here.
1409 if (!(thread
->state
& TH_WAIT
)) {
1410 processor
->last_dispatch
= mach_absolute_time();
1411 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1412 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1414 thread_quantum_init(thread
);
1416 processor
->quantum_end
= processor
->last_dispatch
+ thread
->current_quantum
;
1417 timer_call_enter1(&processor
->quantum_timer
, thread
, processor
->quantum_end
);
1418 processor
->timeslice
= 1;
1420 thread
->computation_epoch
= processor
->last_dispatch
;
1423 thread
->state
&= ~TH_IDLE
;
1426 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1429 return (new_thread
);
1433 * Perform a context switch and start executing the new thread.
1435 * Returns FALSE on failure, and the thread is re-dispatched.
1437 * Called at splsched.
1440 #define funnel_release_check(thread, debug) \
1442 if ((thread)->funnel_state & TH_FN_OWNED) { \
1443 (thread)->funnel_state = TH_FN_REFUNNEL; \
1444 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE, \
1445 (thread)->funnel_lock, (debug), 0, 0, 0); \
1446 funnel_unlock((thread)->funnel_lock); \
1450 #define funnel_refunnel_check(thread, debug) \
1452 if ((thread)->funnel_state & TH_FN_REFUNNEL) { \
1453 kern_return_t result = (thread)->wait_result; \
1455 (thread)->funnel_state = 0; \
1456 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, \
1457 (thread)->funnel_lock, (debug), 0, 0, 0); \
1458 funnel_lock((thread)->funnel_lock); \
1459 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE, \
1460 (thread)->funnel_lock, (debug), 0, 0, 0); \
1461 (thread)->funnel_state = TH_FN_OWNED; \
1462 (thread)->wait_result = result; \
1468 register thread_t self
,
1469 register thread_t thread
,
1472 thread_continue_t continuation
= self
->continuation
;
1473 void *parameter
= self
->parameter
;
1474 processor_t processor
;
1476 if (get_preemption_level() != 0)
1477 panic("thread_invoke: preemption_level %d\n",
1478 get_preemption_level());
1480 assert(self
== current_thread());
1483 * Mark thread interruptible.
1485 thread_lock(thread
);
1486 thread
->state
&= ~TH_UNINT
;
1489 assert(thread_runnable(thread
));
1493 * Allow time constraint threads to hang onto
1496 if ((self
->sched_mode
& TH_MODE_REALTIME
) && !self
->reserved_stack
)
1497 self
->reserved_stack
= self
->kernel_stack
;
1499 if (continuation
!= NULL
) {
1500 if (!thread
->kernel_stack
) {
1502 * If we are using a privileged stack,
1503 * check to see whether we can exchange it with
1504 * that of the other thread.
1506 if (self
->kernel_stack
== self
->reserved_stack
&& !thread
->reserved_stack
)
1510 * Context switch by performing a stack handoff.
1512 continuation
= thread
->continuation
;
1513 parameter
= thread
->parameter
;
1515 processor
= current_processor();
1516 processor
->active_thread
= thread
;
1517 processor
->current_pri
= thread
->sched_pri
;
1518 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1519 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1520 thread
->ps_switch
++;
1523 thread
->last_processor
= processor
;
1525 ast_context(thread
);
1526 thread_unlock(thread
);
1528 self
->reason
= reason
;
1530 processor
->last_dispatch
= mach_absolute_time();
1531 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1532 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1534 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_HANDOFF
)|DBG_FUNC_NONE
,
1535 self
->reason
, (int)thread
, self
->sched_pri
, thread
->sched_pri
, 0);
1537 TLOG(1, "thread_invoke: calling machine_stack_handoff\n");
1538 machine_stack_handoff(self
, thread
);
1540 thread_dispatch(self
, thread
);
1542 thread
->continuation
= thread
->parameter
= NULL
;
1544 counter(c_thread_invoke_hits
++);
1546 funnel_refunnel_check(thread
, 2);
1549 assert(continuation
);
1550 call_continuation(continuation
, parameter
, thread
->wait_result
);
1553 else if (thread
== self
) {
1554 /* same thread but with continuation */
1556 counter(++c_thread_invoke_same
);
1557 thread_unlock(self
);
1559 self
->continuation
= self
->parameter
= NULL
;
1561 funnel_refunnel_check(self
, 3);
1564 call_continuation(continuation
, parameter
, self
->wait_result
);
1570 * Check that the other thread has a stack
1572 if (!thread
->kernel_stack
) {
1574 if (!stack_alloc_try(thread
)) {
1575 counter(c_thread_invoke_misses
++);
1576 thread_unlock(thread
);
1577 thread_stack_enqueue(thread
);
1581 else if (thread
== self
) {
1583 counter(++c_thread_invoke_same
);
1584 thread_unlock(self
);
1590 * Context switch by full context save.
1592 processor
= current_processor();
1593 processor
->active_thread
= thread
;
1594 processor
->current_pri
= thread
->sched_pri
;
1595 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1596 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1597 thread
->ps_switch
++;
1600 thread
->last_processor
= processor
;
1602 ast_context(thread
);
1603 thread_unlock(thread
);
1605 counter(c_thread_invoke_csw
++);
1607 assert(self
->runq
== PROCESSOR_NULL
);
1608 self
->reason
= reason
;
1610 processor
->last_dispatch
= mach_absolute_time();
1611 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1612 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1614 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED
) | DBG_FUNC_NONE
,
1615 (int)self
->reason
, (int)thread
, self
->sched_pri
, thread
->sched_pri
, 0);
1618 * This is where we actually switch register context,
1619 * and address space if required. We will next run
1620 * as a result of a subsequent context switch.
1622 thread
= machine_switch_context(self
, continuation
, thread
);
1623 TLOG(1,"thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self
, continuation
, thread
);
1626 * We have been resumed and are set to run.
1628 thread_dispatch(thread
, self
);
1631 self
->continuation
= self
->parameter
= NULL
;
1633 funnel_refunnel_check(self
, 3);
1636 call_continuation(continuation
, parameter
, self
->wait_result
);
1646 * Handle threads at context switch. Re-dispatch other thread
1647 * if still running, otherwise update run state and perform
1648 * special actions. Update quantum for other thread and begin
1649 * the quantum for ourselves.
1651 * Called at splsched.
1658 processor_t processor
= self
->last_processor
;
1660 if (thread
!= THREAD_NULL
) {
1662 * If blocked at a continuation, discard
1665 if (thread
->continuation
!= NULL
&& thread
->kernel_stack
!= 0)
1668 if (!(thread
->state
& TH_IDLE
)) {
1670 thread_lock(thread
);
1673 * Compute remainder of current quantum.
1675 if ( first_timeslice(processor
) &&
1676 processor
->quantum_end
> processor
->last_dispatch
)
1677 thread
->current_quantum
= (processor
->quantum_end
- processor
->last_dispatch
);
1679 thread
->current_quantum
= 0;
1681 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
1683 * Cancel the deadline if the thread has
1684 * consumed the entire quantum.
1686 if (thread
->current_quantum
== 0) {
1687 thread
->realtime
.deadline
= UINT64_MAX
;
1688 thread
->reason
|= AST_QUANTUM
;
1693 * For non-realtime threads treat a tiny
1694 * remaining quantum as an expired quantum
1695 * but include what's left next time.
1697 if (thread
->current_quantum
< min_std_quantum
) {
1698 thread
->reason
|= AST_QUANTUM
;
1699 thread
->current_quantum
+= std_quantum
;
1704 * If we are doing a direct handoff then
1705 * take the remainder of the quantum.
1707 if ((thread
->reason
& (AST_HANDOFF
|AST_QUANTUM
)) == AST_HANDOFF
) {
1708 self
->current_quantum
= thread
->current_quantum
;
1709 thread
->reason
|= AST_QUANTUM
;
1710 thread
->current_quantum
= 0;
1713 thread
->last_switch
= processor
->last_dispatch
;
1715 thread
->computation_metered
+= (thread
->last_switch
- thread
->computation_epoch
);
1717 if (!(thread
->state
& TH_WAIT
)) {
1721 if (thread
->reason
& AST_QUANTUM
)
1722 thread_setrun(thread
, SCHED_TAILQ
);
1724 if (thread
->reason
& AST_PREEMPT
)
1725 thread_setrun(thread
, SCHED_HEADQ
);
1727 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
1729 thread
->reason
= AST_NONE
;
1731 thread_unlock(thread
);
1732 wake_unlock(thread
);
1738 thread
->state
&= ~TH_RUN
;
1740 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1744 if (thread
->wake_active
) {
1745 thread
->wake_active
= FALSE
;
1746 thread_unlock(thread
);
1748 thread_wakeup(&thread
->wake_active
);
1751 thread_unlock(thread
);
1753 wake_unlock(thread
);
1755 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1757 if (thread
->state
& TH_TERMINATE
)
1758 thread_terminate_enqueue(thread
);
1763 if (!(self
->state
& TH_IDLE
)) {
1765 * Get a new quantum if none remaining.
1767 if (self
->current_quantum
== 0)
1768 thread_quantum_init(self
);
1771 * Set up quantum timer and timeslice.
1773 processor
->quantum_end
= (processor
->last_dispatch
+ self
->current_quantum
);
1774 timer_call_enter1(&processor
->quantum_timer
, self
, processor
->quantum_end
);
1776 processor
->timeslice
= 1;
1778 self
->last_switch
= processor
->last_dispatch
;
1780 self
->computation_epoch
= self
->last_switch
;
1783 timer_call_cancel(&processor
->quantum_timer
);
1784 processor
->timeslice
= 0;
1789 * thread_block_reason:
1791 * Forces a reschedule, blocking the caller if a wait
1792 * has been asserted.
1794 * If a continuation is specified, then thread_invoke will
1795 * attempt to discard the thread's kernel stack. When the
1796 * thread resumes, it will execute the continuation function
1797 * on a new kernel stack.
1799 counter(mach_counter_t c_thread_block_calls
= 0;)
1802 thread_block_reason(
1803 thread_continue_t continuation
,
1807 register thread_t self
= current_thread();
1808 register processor_t processor
;
1809 register thread_t new_thread
;
1812 counter(++c_thread_block_calls
);
1816 if (!(reason
& AST_PREEMPT
))
1817 funnel_release_check(self
, 2);
1819 processor
= current_processor();
1821 /* If we're explicitly yielding, force a subsequent quantum */
1822 if (reason
& AST_YIELD
)
1823 processor
->timeslice
= 0;
1825 /* We're handling all scheduling AST's */
1826 ast_off(AST_SCHEDULING
);
1828 self
->continuation
= continuation
;
1829 self
->parameter
= parameter
;
1833 new_thread
= thread_select(self
, processor
);
1834 thread_unlock(self
);
1835 } while (!thread_invoke(self
, new_thread
, reason
));
1837 funnel_refunnel_check(self
, 5);
1840 return (self
->wait_result
);
1846 * Block the current thread if a wait has been asserted.
1850 thread_continue_t continuation
)
1852 return thread_block_reason(continuation
, NULL
, AST_NONE
);
1856 thread_block_parameter(
1857 thread_continue_t continuation
,
1860 return thread_block_reason(continuation
, parameter
, AST_NONE
);
1866 * Switch directly from the current thread to the
1867 * new thread, handing off our quantum if appropriate.
1869 * New thread must be runnable, and not on a run queue.
1871 * Called at splsched.
1876 thread_continue_t continuation
,
1878 thread_t new_thread
)
1880 ast_t handoff
= AST_HANDOFF
;
1882 funnel_release_check(self
, 3);
1884 self
->continuation
= continuation
;
1885 self
->parameter
= parameter
;
1887 while (!thread_invoke(self
, new_thread
, handoff
)) {
1888 processor_t processor
= current_processor();
1891 new_thread
= thread_select(self
, processor
);
1892 thread_unlock(self
);
1896 funnel_refunnel_check(self
, 6);
1898 return (self
->wait_result
);
1904 * Called at splsched when a thread first receives
1905 * a new stack after a continuation.
1909 register thread_t thread
)
1911 register thread_t self
= current_thread();
1912 register thread_continue_t continuation
;
1913 register void *parameter
;
1915 continuation
= self
->continuation
;
1916 parameter
= self
->parameter
;
1918 thread_dispatch(thread
, self
);
1920 self
->continuation
= self
->parameter
= NULL
;
1922 funnel_refunnel_check(self
, 4);
1924 if (thread
!= THREAD_NULL
)
1927 TLOG(1, "thread_continue: calling call_continuation \n");
1928 call_continuation(continuation
, parameter
, self
->wait_result
);
1935 * Initialize a run queue before first use.
1943 rq
->highq
= IDLEPRI
;
1944 for (i
= 0; i
< NRQBM
; i
++)
1946 setbit(MAXPRI
- IDLEPRI
, rq
->bitmap
);
1947 rq
->urgency
= rq
->count
= 0;
1948 for (i
= 0; i
< NRQS
; i
++)
1949 queue_init(&rq
->queues
[i
]);
1953 * run_queue_dequeue:
1955 * Perform a dequeue operation on a run queue,
1956 * and return the resulting thread.
1958 * The run queue must be locked (see run_queue_remove()
1959 * for more info), and not empty.
1967 queue_t queue
= rq
->queues
+ rq
->highq
;
1969 if (options
& SCHED_HEADQ
) {
1970 thread
= (thread_t
)queue
->next
;
1971 ((queue_entry_t
)thread
)->next
->prev
= queue
;
1972 queue
->next
= ((queue_entry_t
)thread
)->next
;
1975 thread
= (thread_t
)queue
->prev
;
1976 ((queue_entry_t
)thread
)->prev
->next
= queue
;
1977 queue
->prev
= ((queue_entry_t
)thread
)->prev
;
1980 thread
->runq
= PROCESSOR_NULL
;
1982 if (testbit(rq
->highq
, sched_preempt_pri
)) {
1983 rq
->urgency
--; assert(rq
->urgency
>= 0);
1985 if (queue_empty(queue
)) {
1986 if (rq
->highq
!= IDLEPRI
)
1987 clrbit(MAXPRI
- rq
->highq
, rq
->bitmap
);
1988 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
1995 * realtime_queue_insert:
1997 * Enqueue a thread for realtime execution.
2000 realtime_queue_insert(
2003 run_queue_t rq
= &rt_runq
;
2004 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
2005 uint64_t deadline
= thread
->realtime
.deadline
;
2006 boolean_t preempt
= FALSE
;
2008 simple_lock(&rt_lock
);
2010 if (queue_empty(queue
)) {
2011 enqueue_tail(queue
, (queue_entry_t
)thread
);
2013 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2014 if (thread
->sched_pri
> rq
->highq
)
2015 rq
->highq
= thread
->sched_pri
;
2019 register thread_t entry
= (thread_t
)queue_first(queue
);
2022 if ( queue_end(queue
, (queue_entry_t
)entry
) ||
2023 deadline
< entry
->realtime
.deadline
) {
2024 entry
= (thread_t
)queue_prev((queue_entry_t
)entry
);
2028 entry
= (thread_t
)queue_next((queue_entry_t
)entry
);
2031 if ((queue_entry_t
)entry
== queue
)
2034 insque((queue_entry_t
)thread
, (queue_entry_t
)entry
);
2037 thread
->runq
= RT_RUNQ
;
2038 rq
->count
++; rq
->urgency
++;
2040 simple_unlock(&rt_lock
);
2048 * Dispatch a thread for realtime execution.
2050 * Thread must be locked. Associated pset must
2051 * be locked, and is returned unlocked.
2055 processor_t processor
,
2058 processor_set_t pset
= processor
->processor_set
;
2061 * Dispatch directly onto idle processor.
2063 if (processor
->state
== PROCESSOR_IDLE
) {
2064 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2065 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2067 processor
->next_thread
= thread
;
2068 processor
->deadline
= thread
->realtime
.deadline
;
2069 processor
->state
= PROCESSOR_DISPATCHING
;
2072 if (processor
!= current_processor())
2073 machine_signal_idle(processor
);
2077 if (realtime_queue_insert(thread
)) {
2078 if (processor
== current_processor())
2079 ast_on(AST_PREEMPT
| AST_URGENT
);
2081 cause_ast_check(processor
);
2088 * processor_enqueue:
2090 * Enqueue thread on a processor run queue. Thread must be locked,
2091 * and not already be on a run queue.
2093 * Returns TRUE if a preemption is indicated based on the state
2096 * The run queue must be locked (see run_queue_remove()
2101 processor_t processor
,
2105 run_queue_t rq
= &processor
->runq
;
2106 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
2107 boolean_t result
= FALSE
;
2109 if (queue_empty(queue
)) {
2110 enqueue_tail(queue
, (queue_entry_t
)thread
);
2112 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2113 if (thread
->sched_pri
> rq
->highq
) {
2114 rq
->highq
= thread
->sched_pri
;
2119 if (options
& SCHED_TAILQ
)
2120 enqueue_tail(queue
, (queue_entry_t
)thread
);
2122 enqueue_head(queue
, (queue_entry_t
)thread
);
2124 thread
->runq
= processor
;
2125 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2135 * Dispatch a thread for execution on a
2138 * Thread must be locked. Associated pset must
2139 * be locked, and is returned unlocked.
2143 processor_t processor
,
2147 processor_set_t pset
= processor
->processor_set
;
2151 * Dispatch directly onto idle processor.
2153 if (processor
->state
== PROCESSOR_IDLE
) {
2154 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2155 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2157 processor
->next_thread
= thread
;
2158 processor
->deadline
= UINT64_MAX
;
2159 processor
->state
= PROCESSOR_DISPATCHING
;
2162 if (processor
!= current_processor())
2163 machine_signal_idle(processor
);
2168 * Set preemption mode.
2170 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2171 preempt
= (AST_PREEMPT
| AST_URGENT
);
2173 if (thread
->sched_mode
& TH_MODE_TIMESHARE
&& thread
->sched_pri
< thread
->priority
)
2176 preempt
= (options
& SCHED_PREEMPT
)? AST_PREEMPT
: AST_NONE
;
2178 if (!processor_enqueue(processor
, thread
, options
))
2181 if (preempt
!= AST_NONE
) {
2182 if (processor
== current_processor()) {
2183 if (csw_check(processor
) != AST_NONE
)
2187 if ( (processor
->state
== PROCESSOR_RUNNING
||
2188 processor
->state
== PROCESSOR_SHUTDOWN
) &&
2189 thread
->sched_pri
>= processor
->current_pri
) {
2190 cause_ast_check(processor
);
2194 if ( processor
->state
== PROCESSOR_SHUTDOWN
&&
2195 thread
->sched_pri
>= processor
->current_pri
) {
2196 cause_ast_check(processor
);
2202 #define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
2207 * Return the next sibling pset containing
2208 * available processors.
2210 * Returns the original pset if none other is
2213 static processor_set_t
2215 processor_set_t pset
)
2217 processor_set_t nset
= pset
;
2220 nset
= next_pset(nset
);
2221 } while (nset
->processor_count
< 1 && nset
!= pset
);
2229 * Choose a processor for the thread, beginning at
2232 * Returns a processor, possibly from a different pset.
2234 * The thread must be locked. The pset must be locked,
2235 * and the resulting pset is locked on return.
2239 processor_set_t pset
,
2242 processor_set_t nset
, cset
= pset
;
2243 processor_t processor
= thread
->last_processor
;
2246 * Prefer the last processor, when appropriate.
2248 if (processor
!= PROCESSOR_NULL
) {
2249 if (processor
->processor_set
!= pset
|| processor
->state
== PROCESSOR_INACTIVE
||
2250 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2251 processor
= PROCESSOR_NULL
;
2253 if (processor
->state
== PROCESSOR_IDLE
|| ( thread
->sched_pri
> BASEPRI_DEFAULT
&& processor
->current_pri
< thread
->sched_pri
))
2258 * Iterate through the processor sets to locate
2259 * an appropriate processor.
2263 * Choose an idle processor.
2265 if (!queue_empty(&cset
->idle_queue
))
2266 return ((processor_t
)queue_first(&cset
->idle_queue
));
2268 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2270 * For an RT thread, iterate through active processors, first fit.
2272 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2273 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2274 if (thread
->sched_pri
> processor
->current_pri
||
2275 thread
->realtime
.deadline
< processor
->deadline
)
2278 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2281 processor
= PROCESSOR_NULL
;
2285 * Check any hinted processors in the processor set if available.
2287 if (cset
->low_pri
!= PROCESSOR_NULL
&& cset
->low_pri
->state
!= PROCESSOR_INACTIVE
&&
2288 cset
->low_pri
->state
!= PROCESSOR_SHUTDOWN
&& cset
->low_pri
->state
!= PROCESSOR_OFF_LINE
&&
2289 (processor
== PROCESSOR_NULL
||
2290 (thread
->sched_pri
> BASEPRI_DEFAULT
&& cset
->low_pri
->current_pri
< thread
->sched_pri
))) {
2291 processor
= cset
->low_pri
;
2294 if (cset
->low_count
!= PROCESSOR_NULL
&& cset
->low_count
->state
!= PROCESSOR_INACTIVE
&&
2295 cset
->low_count
->state
!= PROCESSOR_SHUTDOWN
&& cset
->low_count
->state
!= PROCESSOR_OFF_LINE
&&
2296 (processor
== PROCESSOR_NULL
||
2297 ( thread
->sched_pri
<= BASEPRI_DEFAULT
&& cset
->low_count
->runq
.count
< processor
->runq
.count
))) {
2298 processor
= cset
->low_count
;
2302 * Otherwise, choose an available processor in the set.
2304 if (processor
== PROCESSOR_NULL
) {
2305 processor
= (processor_t
)dequeue_head(&cset
->active_queue
);
2306 if (processor
!= PROCESSOR_NULL
)
2307 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2312 * Move onto the next processor set.
2314 nset
= next_pset(cset
);
2322 } while (nset
!= pset
);
2325 * Make sure that we pick a running processor,
2326 * and that the correct processor set is locked.
2330 * If we haven't been able to choose a processor,
2331 * pick the boot processor and return it.
2333 if (processor
== PROCESSOR_NULL
) {
2334 processor
= master_processor
;
2337 * Check that the correct processor set is
2340 if (cset
!= processor
->processor_set
) {
2343 cset
= processor
->processor_set
;
2351 * Check that the processor set for the chosen
2352 * processor is locked.
2354 if (cset
!= processor
->processor_set
) {
2357 cset
= processor
->processor_set
;
2362 * We must verify that the chosen processor is still available.
2364 if (processor
->state
== PROCESSOR_INACTIVE
||
2365 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2366 processor
= PROCESSOR_NULL
;
2367 } while (processor
== PROCESSOR_NULL
);
2375 * Dispatch thread for execution, onto an idle
2376 * processor or run queue, and signal a preemption
2379 * Thread must be locked.
2386 processor_t processor
;
2387 processor_set_t pset
;
2390 assert(thread_runnable(thread
));
2394 * Update priority if needed.
2396 if (thread
->sched_stamp
!= sched_tick
)
2397 update_priority(thread
);
2399 assert(thread
->runq
== PROCESSOR_NULL
);
2401 if (thread
->bound_processor
== PROCESSOR_NULL
) {
2405 if (thread
->affinity_set
!= AFFINITY_SET_NULL
) {
2407 * Use affinity set policy hint.
2409 pset
= thread
->affinity_set
->aset_pset
;
2412 processor
= choose_processor(pset
, thread
);
2415 if (thread
->last_processor
!= PROCESSOR_NULL
) {
2417 * Simple (last processor) affinity case.
2419 processor
= thread
->last_processor
;
2420 pset
= processor
->processor_set
;
2424 * Choose a different processor in certain cases.
2426 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2428 * If the processor is executing an RT thread with
2429 * an earlier deadline, choose another.
2431 if (thread
->sched_pri
<= processor
->current_pri
||
2432 thread
->realtime
.deadline
>= processor
->deadline
)
2433 processor
= choose_processor(pset
, thread
);
2436 processor
= choose_processor(pset
, thread
);
2442 * Utilitize a per task hint to spread threads
2443 * among the available processor sets.
2445 task_t task
= thread
->task
;
2447 pset
= task
->pset_hint
;
2448 if (pset
== PROCESSOR_SET_NULL
)
2449 pset
= current_processor()->processor_set
;
2451 pset
= choose_next_pset(pset
);
2454 processor
= choose_processor(pset
, thread
);
2455 task
->pset_hint
= processor
->processor_set
;
2462 * Unconditionally dispatch on the processor.
2464 processor
= thread
->bound_processor
;
2465 pset
= processor
->processor_set
;
2470 * Dispatch the thread on the choosen processor.
2472 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
)
2473 realtime_setrun(processor
, thread
);
2475 processor_setrun(processor
, thread
, options
);
2479 * processor_queue_shutdown:
2481 * Shutdown a processor run queue by
2482 * re-dispatching non-bound threads.
2484 * Associated pset must be locked, and is
2485 * returned unlocked.
2488 processor_queue_shutdown(
2489 processor_t processor
)
2491 processor_set_t pset
= processor
->processor_set
;
2492 run_queue_t rq
= &processor
->runq
;
2493 queue_t queue
= rq
->queues
+ rq
->highq
;
2494 int pri
= rq
->highq
, count
= rq
->count
;
2495 thread_t next
, thread
;
2496 queue_head_t tqueue
;
2498 queue_init(&tqueue
);
2501 thread
= (thread_t
)queue_first(queue
);
2502 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2503 next
= (thread_t
)queue_next((queue_entry_t
)thread
);
2505 if (thread
->bound_processor
!= processor
) {
2506 remqueue(queue
, (queue_entry_t
)thread
);
2508 thread
->runq
= PROCESSOR_NULL
;
2510 if (testbit(pri
, sched_preempt_pri
)) {
2511 rq
->urgency
--; assert(rq
->urgency
>= 0);
2513 if (queue_empty(queue
)) {
2515 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2516 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2519 enqueue_tail(&tqueue
, (queue_entry_t
)thread
);
2531 while ((thread
= (thread_t
)dequeue_head(&tqueue
)) != THREAD_NULL
) {
2532 thread_lock(thread
);
2534 thread_setrun(thread
, SCHED_TAILQ
);
2536 thread_unlock(thread
);
2541 * Check for a preemption point in
2542 * the current context.
2544 * Called at splsched.
2548 processor_t processor
)
2550 ast_t result
= AST_NONE
;
2553 if (first_timeslice(processor
)) {
2555 if (runq
->highq
>= BASEPRI_RTQUEUES
)
2556 return (AST_PREEMPT
| AST_URGENT
);
2558 if (runq
->highq
> processor
->current_pri
) {
2559 if (runq
->urgency
> 0)
2560 return (AST_PREEMPT
| AST_URGENT
);
2562 result
|= AST_PREEMPT
;
2565 runq
= &processor
->runq
;
2566 if (runq
->highq
> processor
->current_pri
) {
2567 if (runq
->urgency
> 0)
2568 return (AST_PREEMPT
| AST_URGENT
);
2570 result
|= AST_PREEMPT
;
2575 if (runq
->highq
>= processor
->current_pri
) {
2576 if (runq
->urgency
> 0)
2577 return (AST_PREEMPT
| AST_URGENT
);
2579 result
|= AST_PREEMPT
;
2582 runq
= &processor
->runq
;
2583 if (runq
->highq
>= processor
->current_pri
) {
2584 if (runq
->urgency
> 0)
2585 return (AST_PREEMPT
| AST_URGENT
);
2587 result
|= AST_PREEMPT
;
2591 if (result
!= AST_NONE
)
2594 if (machine_cpu_is_inactive(processor
->cpu_num
))
2595 return (AST_PREEMPT
);
2597 if (processor
->active_thread
->state
& TH_SUSP
)
2598 return (AST_PREEMPT
);
2606 * Set the scheduled priority of the specified thread.
2608 * This may cause the thread to change queues.
2610 * Thread must be locked.
2617 boolean_t removed
= run_queue_remove(thread
);
2619 thread
->sched_pri
= priority
;
2621 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
2623 if (thread
->state
& TH_RUN
) {
2624 processor_t processor
= thread
->last_processor
;
2626 if (thread
== current_thread()) {
2629 processor
->current_pri
= priority
;
2630 if ((preempt
= csw_check(processor
)) != AST_NONE
)
2634 if ( processor
!= PROCESSOR_NULL
&&
2635 processor
->active_thread
== thread
)
2636 cause_ast_check(processor
);
2650 if (rq
!= thread
->runq
)
2651 panic("run_queue_check: thread runq");
2653 if (thread
->sched_pri
> MAXPRI
|| thread
->sched_pri
< MINPRI
)
2654 panic("run_queue_check: thread sched_pri");
2656 q
= &rq
->queues
[thread
->sched_pri
];
2657 qe
= queue_first(q
);
2658 while (!queue_end(q
, qe
)) {
2659 if (qe
== (queue_entry_t
)thread
)
2662 qe
= queue_next(qe
);
2665 panic("run_queue_check: end");
2673 * Remove a thread from a current run queue and
2674 * return TRUE if successful.
2676 * Thread must be locked.
2682 processor_t processor
= thread
->runq
;
2685 * If processor is PROCESSOR_NULL, the thread will stay out of the
2686 * run queues because the caller locked the thread. Otherwise
2687 * the thread is on a run queue, but could be chosen for dispatch
2690 if (processor
!= PROCESSOR_NULL
) {
2695 * The processor run queues are locked by the
2696 * processor set. Real-time priorities use a
2697 * global queue with a dedicated lock.
2699 if (thread
->sched_pri
< BASEPRI_RTQUEUES
) {
2700 rqlock
= &processor
->processor_set
->sched_lock
;
2701 rq
= &processor
->runq
;
2704 rqlock
= &rt_lock
; rq
= &rt_runq
;
2707 simple_lock(rqlock
);
2709 if (processor
== thread
->runq
) {
2711 * Thread is on a run queue and we have a lock on
2714 remqueue(&rq
->queues
[0], (queue_entry_t
)thread
);
2716 if (testbit(thread
->sched_pri
, sched_preempt_pri
)) {
2717 rq
->urgency
--; assert(rq
->urgency
>= 0);
2720 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
2721 /* update run queue status */
2722 if (thread
->sched_pri
!= IDLEPRI
)
2723 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2724 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2727 thread
->runq
= PROCESSOR_NULL
;
2731 * The thread left the run queue before we could
2732 * lock the run queue.
2734 assert(thread
->runq
== PROCESSOR_NULL
);
2735 processor
= PROCESSOR_NULL
;
2738 simple_unlock(rqlock
);
2741 return (processor
!= PROCESSOR_NULL
);
2745 * steal_processor_thread:
2747 * Locate a thread to steal from the processor and
2750 * Associated pset must be locked. Returns THREAD_NULL
2754 steal_processor_thread(
2755 processor_t processor
)
2757 run_queue_t rq
= &processor
->runq
;
2758 queue_t queue
= rq
->queues
+ rq
->highq
;
2759 int pri
= rq
->highq
, count
= rq
->count
;
2763 thread
= (thread_t
)queue_first(queue
);
2764 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2765 if (thread
->bound_processor
!= processor
) {
2766 remqueue(queue
, (queue_entry_t
)thread
);
2768 thread
->runq
= PROCESSOR_NULL
;
2770 if (testbit(pri
, sched_preempt_pri
)) {
2771 rq
->urgency
--; assert(rq
->urgency
>= 0);
2773 if (queue_empty(queue
)) {
2775 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2776 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2783 thread
= (thread_t
)queue_next((queue_entry_t
)thread
);
2789 return (THREAD_NULL
);
2793 * Locate and steal a thread, beginning
2796 * The pset must be locked, and is returned
2799 * Returns the stolen thread, or THREAD_NULL on
2804 processor_set_t pset
)
2806 processor_set_t nset
, cset
= pset
;
2807 processor_t processor
;
2811 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2812 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2813 if (processor
->runq
.count
> 0) {
2814 thread
= steal_processor_thread(processor
);
2815 if (thread
!= THREAD_NULL
) {
2816 remqueue(&cset
->active_queue
, (queue_entry_t
)processor
);
2817 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2825 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2828 nset
= next_pset(cset
);
2836 } while (nset
!= pset
);
2840 return (THREAD_NULL
);
2844 * This is the processor idle loop, which just looks for other threads
2845 * to execute. Processor idle threads invoke this without supplying a
2846 * current thread to idle without an asserted wait state.
2848 * Returns a the next thread to execute if dispatched directly.
2853 processor_t processor
)
2855 processor_set_t pset
= processor
->processor_set
;
2856 thread_t new_thread
;
2862 pmsDown(); /* Step power down */
2865 KERNEL_DEBUG_CONSTANT(
2866 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_START
, (int)thread
, 0, 0, 0, 0);
2868 timer_switch(&PROCESSOR_DATA(processor
, system_state
),
2869 mach_absolute_time(), &PROCESSOR_DATA(processor
, idle_state
));
2870 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, idle_state
);
2872 while (processor
->next_thread
== THREAD_NULL
&& processor
->runq
.count
== 0 && rt_runq
.count
== 0 &&
2873 (thread
== THREAD_NULL
|| ((thread
->state
& (TH_WAIT
|TH_SUSP
)) == TH_WAIT
&& !thread
->wake_active
))) {
2878 if (processor
->state
== PROCESSOR_INACTIVE
&& !machine_cpu_is_inactive(processor
->cpu_num
))
2882 timer_switch(&PROCESSOR_DATA(processor
, idle_state
),
2883 mach_absolute_time(), &PROCESSOR_DATA(processor
, system_state
));
2884 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
2889 pmsStep(0); /* Step up out of idle power */
2892 state
= processor
->state
;
2893 if (state
== PROCESSOR_DISPATCHING
) {
2895 * Commmon case -- cpu dispatched.
2897 new_thread
= processor
->next_thread
;
2898 processor
->next_thread
= THREAD_NULL
;
2899 processor
->state
= PROCESSOR_RUNNING
;
2901 if ( processor
->runq
.highq
> new_thread
->sched_pri
||
2902 (rt_runq
.highq
> 0 && rt_runq
.highq
>= new_thread
->sched_pri
) ) {
2903 processor
->deadline
= UINT64_MAX
;
2907 thread_lock(new_thread
);
2908 thread_setrun(new_thread
, SCHED_HEADQ
);
2909 thread_unlock(new_thread
);
2911 KERNEL_DEBUG_CONSTANT(
2912 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, 0, 0, 0);
2914 return (THREAD_NULL
);
2919 KERNEL_DEBUG_CONSTANT(
2920 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, (int)new_thread
, 0, 0);
2922 return (new_thread
);
2925 if (state
== PROCESSOR_IDLE
) {
2926 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2928 processor
->state
= PROCESSOR_RUNNING
;
2929 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2932 if (state
== PROCESSOR_INACTIVE
) {
2933 processor
->state
= PROCESSOR_RUNNING
;
2934 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2937 if (state
== PROCESSOR_SHUTDOWN
) {
2939 * Going off-line. Force a
2942 if ((new_thread
= processor
->next_thread
) != THREAD_NULL
) {
2943 processor
->next_thread
= THREAD_NULL
;
2944 processor
->deadline
= UINT64_MAX
;
2948 thread_lock(new_thread
);
2949 thread_setrun(new_thread
, SCHED_HEADQ
);
2950 thread_unlock(new_thread
);
2952 KERNEL_DEBUG_CONSTANT(
2953 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, 0, 0, 0);
2955 return (THREAD_NULL
);
2961 KERNEL_DEBUG_CONSTANT(
2962 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, 0, 0, 0);
2964 return (THREAD_NULL
);
2968 * Each processor has a dedicated thread which
2969 * executes the idle loop when there is no suitable
2975 processor_t processor
= current_processor();
2976 thread_t new_thread
;
2978 new_thread
= processor_idle(THREAD_NULL
, processor
);
2979 if (new_thread
!= THREAD_NULL
) {
2980 thread_run(processor
->idle_thread
, (thread_continue_t
)idle_thread
, NULL
, new_thread
);
2984 thread_block((thread_continue_t
)idle_thread
);
2990 processor_t processor
)
2992 kern_return_t result
;
2996 result
= kernel_thread_create((thread_continue_t
)idle_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
2997 if (result
!= KERN_SUCCESS
)
3001 thread_lock(thread
);
3002 thread
->bound_processor
= processor
;
3003 processor
->idle_thread
= thread
;
3004 thread
->sched_pri
= thread
->priority
= IDLEPRI
;
3005 thread
->state
= (TH_RUN
| TH_IDLE
);
3006 thread_unlock(thread
);
3009 thread_deallocate(thread
);
3011 return (KERN_SUCCESS
);
3014 static uint64_t sched_tick_deadline
;
3019 * Kicks off scheduler services.
3021 * Called at splsched.
3026 kern_return_t result
;
3029 result
= kernel_thread_start_priority((thread_continue_t
)sched_tick_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
3030 if (result
!= KERN_SUCCESS
)
3031 panic("sched_startup");
3033 thread_deallocate(thread
);
3036 * Yield to the sched_tick_thread while it times
3037 * a series of context switches back. It stores
3038 * the baseline value in sched_cswtime.
3040 * The current thread is the only other thread
3041 * active at this point.
3043 while (sched_cswtime
== 0)
3044 thread_block(THREAD_CONTINUE_NULL
);
3046 thread_daemon_init();
3048 thread_call_initialize();
3052 * sched_tick_thread:
3054 * Perform periodic bookkeeping functions about ten
3058 sched_tick_continue(void)
3060 uint64_t abstime
= mach_absolute_time();
3065 * Compute various averages.
3070 * Scan the run queues for threads which
3071 * may need to be updated.
3073 thread_update_scan();
3075 clock_deadline_for_periodic_event(sched_tick_interval
, abstime
,
3076 &sched_tick_deadline
);
3078 assert_wait_deadline((event_t
)sched_tick_thread
, THREAD_UNINT
, sched_tick_deadline
);
3079 thread_block((thread_continue_t
)sched_tick_continue
);
3084 * Time a series of context switches to determine
3085 * a baseline. Toss the high and low and return
3086 * the one-way value.
3091 uint32_t new, hi
, low
, accum
;
3095 accum
= hi
= low
= 0;
3096 for (i
= 0; i
< tries
; ++i
) {
3097 abstime
= mach_absolute_time();
3098 thread_block(THREAD_CONTINUE_NULL
);
3100 new = mach_absolute_time() - abstime
;
3103 accum
= hi
= low
= new;
3114 return ((accum
- hi
- low
) / (2 * (tries
- 2)));
3118 sched_tick_thread(void)
3120 sched_cswtime
= time_cswitch();
3122 sched_tick_deadline
= mach_absolute_time();
3124 sched_tick_continue();
3129 * thread_update_scan / runq_scan:
3131 * Scan the run queues to account for timesharing threads
3132 * which need to be updated.
3134 * Scanner runs in two passes. Pass one squirrels likely
3135 * threads away in an array, pass two does the update.
3137 * This is necessary because the run queue is locked for
3138 * the candidate scan, but the thread is locked for the update.
3140 * Array should be sized to make forward progress, without
3141 * disabling preemption for long periods.
3144 #define THREAD_UPDATE_SIZE 128
3146 static thread_t thread_update_array
[THREAD_UPDATE_SIZE
];
3147 static int thread_update_count
= 0;
3150 * Scan a runq for candidate threads.
3152 * Returns TRUE if retry is needed.
3160 register thread_t thread
;
3162 if ((count
= runq
->count
) > 0) {
3163 q
= runq
->queues
+ runq
->highq
;
3165 queue_iterate(q
, thread
, thread_t
, links
) {
3166 if ( thread
->sched_stamp
!= sched_tick
&&
3167 (thread
->sched_mode
& TH_MODE_TIMESHARE
) ) {
3168 if (thread_update_count
== THREAD_UPDATE_SIZE
)
3171 thread_update_array
[thread_update_count
++] = thread
;
3172 thread_reference_internal(thread
);
3186 thread_update_scan(void)
3188 boolean_t restart_needed
= FALSE
;
3189 processor_t processor
= processor_list
;
3190 processor_set_t pset
;
3196 pset
= processor
->processor_set
;
3201 restart_needed
= runq_scan(&processor
->runq
);
3209 thread
= processor
->idle_thread
;
3210 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
3211 if (thread_update_count
== THREAD_UPDATE_SIZE
) {
3212 restart_needed
= TRUE
;
3216 thread_update_array
[thread_update_count
++] = thread
;
3217 thread_reference_internal(thread
);
3219 } while ((processor
= processor
->processor_list
) != NULL
);
3222 * Ok, we now have a collection of candidates -- fix them.
3224 while (thread_update_count
> 0) {
3225 thread
= thread_update_array
[--thread_update_count
];
3226 thread_update_array
[thread_update_count
] = THREAD_NULL
;
3229 thread_lock(thread
);
3230 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
3231 thread
->sched_stamp
!= sched_tick
)
3232 update_priority(thread
);
3233 thread_unlock(thread
);
3236 thread_deallocate(thread
);
3238 } while (restart_needed
);
3242 * Just in case someone doesn't use the macro
3244 #undef thread_wakeup
3253 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
3257 preemption_enabled(void)
3259 return (get_preemption_level() == 0 && ml_get_interrupts_enabled());
3267 return ((thread
->state
& (TH_RUN
|TH_WAIT
)) == TH_RUN
);
3272 #include <ddb/db_output.h>
3273 #define printf kdbprintf
3274 void db_sched(void);
3279 iprintf("Scheduling Statistics:\n");
3281 iprintf("Thread invocations: csw %d same %d\n",
3282 c_thread_invoke_csw
, c_thread_invoke_same
);
3284 iprintf("Thread block: calls %d\n",
3285 c_thread_block_calls
);
3286 iprintf("Idle thread:\n\thandoff %d block %d\n",
3287 c_idle_thread_handoff
,
3288 c_idle_thread_block
);
3289 iprintf("Sched thread blocks: %d\n", c_sched_thread_block
);
3290 #endif /* MACH_COUNTERS */
3294 #include <ddb/db_output.h>
3295 void db_show_thread_log(void);
3298 db_show_thread_log(void)
3301 #endif /* MACH_KDB */