2 * Copyright (c) 2000-2008 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
thread_select_idle(
155 processor_t processor
);
157 static thread_t
processor_idle(
159 processor_t processor
);
161 static thread_t
choose_thread(
162 processor_t processor
);
164 static thread_t
steal_thread(
165 processor_set_t pset
);
167 static thread_t
steal_processor_thread(
168 processor_t processor
);
170 static void thread_update_scan(void);
173 extern int debug_task
;
174 #define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
176 #define TLOG(a, fmt, args...) do {} while (0)
181 boolean_t
thread_runnable(
189 * states are combinations of:
191 * W waiting (or on wait queue)
192 * N non-interruptible
197 * assert_wait thread_block clear_wait swapout swapin
199 * R RW, RWN R; setrun - -
200 * RN RWN RN; setrun - -
213 * Waiting protocols and implementation:
215 * Each thread may be waiting for exactly one event; this event
216 * is set using assert_wait(). That thread may be awakened either
217 * by performing a thread_wakeup_prim() on its event,
218 * or by directly waking that thread up with clear_wait().
220 * The implementation of wait events uses a hash table. Each
221 * bucket is queue of threads having the same hash function
222 * value; the chain for the queue (linked list) is the run queue
223 * field. [It is not possible to be waiting and runnable at the
226 * Locks on both the thread and on the hash buckets govern the
227 * wait event field and the queue chain field. Because wakeup
228 * operations only have the event as an argument, the event hash
229 * bucket must be locked before any thread.
231 * Scheduling operations may also occur at interrupt level; therefore,
232 * interrupts below splsched() must be prevented when holding
233 * thread or hash bucket locks.
235 * The wait event hash table declarations are as follows:
240 struct wait_queue wait_queues
[NUMQUEUES
];
242 #define wait_hash(event) \
243 ((((int)(event) < 0)? ~(int)(event): (int)(event)) % NUMQUEUES)
245 int8_t sched_load_shifts
[NRQS
];
246 int sched_preempt_pri
[NRQBM
];
252 * Calculate the timeslicing quantum
255 if (default_preemption_rate
< 1)
256 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
257 std_quantum_us
= (1000 * 1000) / default_preemption_rate
;
259 printf("standard timeslicing quantum is %d us\n", std_quantum_us
);
261 sched_safe_duration
= (2 * max_unsafe_quanta
/ default_preemption_rate
) *
262 (1 << SCHED_TICK_SHIFT
);
267 simple_lock_init(&rt_lock
, 0);
268 run_queue_init(&rt_runq
);
274 sched_timebase_init(void)
279 /* standard timeslicing quantum */
280 clock_interval_to_absolutetime_interval(
281 std_quantum_us
, NSEC_PER_USEC
, &abstime
);
282 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
283 std_quantum
= abstime
;
285 /* smallest remaining quantum (250 us) */
286 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC
, &abstime
);
287 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
288 min_std_quantum
= abstime
;
290 /* smallest rt computaton (50 us) */
291 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC
, &abstime
);
292 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
293 min_rt_quantum
= abstime
;
295 /* maximum rt computation (50 ms) */
296 clock_interval_to_absolutetime_interval(
297 50, 1000*NSEC_PER_USEC
, &abstime
);
298 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
299 max_rt_quantum
= abstime
;
301 /* scheduler tick interval */
302 clock_interval_to_absolutetime_interval(USEC_PER_SEC
>> SCHED_TICK_SHIFT
,
303 NSEC_PER_USEC
, &abstime
);
304 assert((abstime
>> 32) == 0 && (uint32_t)abstime
!= 0);
305 sched_tick_interval
= abstime
;
308 * Compute conversion factor from usage to
309 * timesharing priorities with 5/8 ** n aging.
311 abstime
= (abstime
* 5) / 3;
312 for (shift
= 0; abstime
> BASEPRI_DEFAULT
; ++shift
)
314 sched_fixed_shift
= shift
;
316 max_unsafe_computation
= max_unsafe_quanta
* std_quantum
;
317 max_poll_computation
= max_poll_quanta
* std_quantum
;
321 wait_queues_init(void)
325 for (i
= 0; i
< NUMQUEUES
; i
++) {
326 wait_queue_init(&wait_queues
[i
], SYNC_POLICY_FIFO
);
331 * Set up values for timeshare
335 load_shift_init(void)
337 int8_t k
, *p
= sched_load_shifts
;
340 *p
++ = INT8_MIN
; *p
++ = 0;
342 for (i
= j
= 2, k
= 1; i
< NRQS
; ++k
) {
343 for (j
<<= 1; i
< j
; ++i
)
349 preempt_pri_init(void)
351 int i
, *p
= sched_preempt_pri
;
353 for (i
= BASEPRI_FOREGROUND
+ 1; i
< MINPRI_KERNEL
; ++i
)
356 for (i
= BASEPRI_PREEMPT
; i
<= MAXPRI
; ++i
)
361 * Thread wait timer expiration.
368 thread_t thread
= p0
;
373 if (--thread
->wait_timer_active
== 0) {
374 if (thread
->wait_timer_is_set
) {
375 thread
->wait_timer_is_set
= FALSE
;
376 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
379 thread_unlock(thread
);
386 * Set a timer for the current thread, if the thread
387 * is ready to wait. Must be called between assert_wait()
388 * and thread_block().
393 uint32_t scale_factor
)
395 thread_t thread
= current_thread();
401 if ((thread
->state
& TH_WAIT
) != 0) {
402 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
403 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
404 thread
->wait_timer_active
++;
405 thread
->wait_timer_is_set
= TRUE
;
407 thread_unlock(thread
);
412 thread_set_timer_deadline(
415 thread_t thread
= current_thread();
420 if ((thread
->state
& TH_WAIT
) != 0) {
421 if (!timer_call_enter(&thread
->wait_timer
, deadline
))
422 thread
->wait_timer_active
++;
423 thread
->wait_timer_is_set
= TRUE
;
425 thread_unlock(thread
);
430 thread_cancel_timer(void)
432 thread_t thread
= current_thread();
437 if (thread
->wait_timer_is_set
) {
438 if (timer_call_cancel(&thread
->wait_timer
))
439 thread
->wait_timer_active
--;
440 thread
->wait_timer_is_set
= FALSE
;
442 thread_unlock(thread
);
449 * Unblock thread on wake up.
451 * Returns TRUE if the thread is still running.
453 * Thread must be locked.
458 wait_result_t wresult
)
460 boolean_t result
= FALSE
;
465 thread
->wait_result
= wresult
;
468 * Cancel pending wait timer.
470 if (thread
->wait_timer_is_set
) {
471 if (timer_call_cancel(&thread
->wait_timer
))
472 thread
->wait_timer_active
--;
473 thread
->wait_timer_is_set
= FALSE
;
477 * Update scheduling state: not waiting,
480 thread
->state
&= ~(TH_WAIT
|TH_UNINT
);
482 if (!(thread
->state
& TH_RUN
)) {
483 thread
->state
|= TH_RUN
;
485 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
491 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
496 * Signal if idling on another processor.
498 if (thread
->state
& TH_IDLE
) {
499 processor_t processor
= thread
->last_processor
;
501 if (processor
!= current_processor())
502 machine_signal_idle(processor
);
509 * Calculate deadline for real-time threads.
511 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
512 thread
->realtime
.deadline
= mach_absolute_time();
513 thread
->realtime
.deadline
+= thread
->realtime
.constraint
;
517 * Clear old quantum, fail-safe computation, etc.
519 thread
->current_quantum
= 0;
520 thread
->computation_metered
= 0;
521 thread
->reason
= AST_NONE
;
523 KERNEL_DEBUG_CONSTANT(
524 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_MAKE_RUNNABLE
) | DBG_FUNC_NONE
,
525 (int)thread
, (int)thread
->sched_pri
, 0, 0, 0);
533 * Unblock and dispatch thread.
535 * thread lock held, IPC locks may be held.
536 * thread must have been pulled from wait queue under same lock hold.
538 * KERN_SUCCESS - Thread was set running
539 * KERN_NOT_WAITING - Thread was not waiting
544 wait_result_t wresult
)
546 assert(thread
->at_safe_point
== FALSE
);
547 assert(thread
->wait_event
== NO_EVENT64
);
548 assert(thread
->wait_queue
== WAIT_QUEUE_NULL
);
550 if ((thread
->state
& (TH_WAIT
|TH_TERMINATE
)) == TH_WAIT
) {
551 if (!thread_unblock(thread
, wresult
))
552 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
554 return (KERN_SUCCESS
);
557 return (KERN_NOT_WAITING
);
561 * Routine: thread_mark_wait_locked
563 * Mark a thread as waiting. If, given the circumstances,
564 * it doesn't want to wait (i.e. already aborted), then
565 * indicate that in the return value.
567 * at splsched() and thread is locked.
571 thread_mark_wait_locked(
573 wait_interrupt_t interruptible
)
575 boolean_t at_safe_point
;
578 * The thread may have certain types of interrupts/aborts masked
579 * off. Even if the wait location says these types of interrupts
580 * are OK, we have to honor mask settings (outer-scoped code may
581 * not be able to handle aborts at the moment).
583 if (interruptible
> (thread
->options
& TH_OPT_INTMASK
))
584 interruptible
= thread
->options
& TH_OPT_INTMASK
;
586 at_safe_point
= (interruptible
== THREAD_ABORTSAFE
);
588 if ( interruptible
== THREAD_UNINT
||
589 !(thread
->sched_mode
& TH_MODE_ABORT
) ||
591 (thread
->sched_mode
& TH_MODE_ABORTSAFELY
))) {
592 thread
->state
|= (interruptible
) ? TH_WAIT
: (TH_WAIT
| TH_UNINT
);
593 thread
->at_safe_point
= at_safe_point
;
594 return (thread
->wait_result
= THREAD_WAITING
);
597 if (thread
->sched_mode
& TH_MODE_ABORTSAFELY
)
598 thread
->sched_mode
&= ~TH_MODE_ISABORTED
;
600 return (thread
->wait_result
= THREAD_INTERRUPTED
);
604 * Routine: thread_interrupt_level
606 * Set the maximum interruptible state for the
607 * current thread. The effective value of any
608 * interruptible flag passed into assert_wait
609 * will never exceed this.
611 * Useful for code that must not be interrupted,
612 * but which calls code that doesn't know that.
614 * The old interrupt level for the thread.
618 thread_interrupt_level(
619 wait_interrupt_t new_level
)
621 thread_t thread
= current_thread();
622 wait_interrupt_t result
= thread
->options
& TH_OPT_INTMASK
;
624 thread
->options
= (thread
->options
& ~TH_OPT_INTMASK
) | (new_level
& TH_OPT_INTMASK
);
630 * Check to see if an assert wait is possible, without actually doing one.
631 * This is used by debug code in locks and elsewhere to verify that it is
632 * always OK to block when trying to take a blocking lock (since waiting
633 * for the actual assert_wait to catch the case may make it hard to detect
637 assert_wait_possible(void)
643 if(debug_mode
) return TRUE
; /* Always succeed in debug mode */
646 thread
= current_thread();
648 return (thread
== NULL
|| wait_queue_assert_possible(thread
));
654 * Assert that the current thread is about to go to
655 * sleep until the specified event occurs.
660 wait_interrupt_t interruptible
)
662 register wait_queue_t wq
;
665 assert(event
!= NO_EVENT
);
667 index
= wait_hash(event
);
668 wq
= &wait_queues
[index
];
669 return wait_queue_assert_wait(wq
, event
, interruptible
, 0);
675 wait_interrupt_t interruptible
,
677 uint32_t scale_factor
)
679 thread_t thread
= current_thread();
680 wait_result_t wresult
;
685 assert(event
!= NO_EVENT
);
686 wqueue
= &wait_queues
[wait_hash(event
)];
689 wait_queue_lock(wqueue
);
692 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
693 wresult
= wait_queue_assert_wait64_locked(wqueue
, (uint32_t)event
,
694 interruptible
, deadline
, thread
);
696 thread_unlock(thread
);
697 wait_queue_unlock(wqueue
);
704 assert_wait_deadline(
706 wait_interrupt_t interruptible
,
709 thread_t thread
= current_thread();
710 wait_result_t wresult
;
714 assert(event
!= NO_EVENT
);
715 wqueue
= &wait_queues
[wait_hash(event
)];
718 wait_queue_lock(wqueue
);
721 wresult
= wait_queue_assert_wait64_locked(wqueue
, (uint32_t)event
,
722 interruptible
, deadline
, thread
);
724 thread_unlock(thread
);
725 wait_queue_unlock(wqueue
);
732 * thread_sleep_fast_usimple_lock:
734 * Cause the current thread to wait until the specified event
735 * occurs. The specified simple_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 that use a
739 * faster version of simple_lock() than is provided by usimple_lock().
741 __private_extern__ wait_result_t
742 thread_sleep_fast_usimple_lock(
745 wait_interrupt_t interruptible
)
749 res
= assert_wait(event
, interruptible
);
750 if (res
== THREAD_WAITING
) {
752 res
= thread_block(THREAD_CONTINUE_NULL
);
760 * thread_sleep_usimple_lock:
762 * Cause the current thread to wait until the specified event
763 * occurs. The specified usimple_lock is unlocked before releasing
764 * the cpu and re-acquired as part of waking up.
766 * This is the simple lock sleep interface for components where
767 * simple_lock() is defined in terms of usimple_lock().
770 thread_sleep_usimple_lock(
773 wait_interrupt_t interruptible
)
777 res
= assert_wait(event
, interruptible
);
778 if (res
== THREAD_WAITING
) {
779 usimple_unlock(lock
);
780 res
= thread_block(THREAD_CONTINUE_NULL
);
787 * thread_sleep_mutex:
789 * Cause the current thread to wait until the specified event
790 * occurs. The specified mutex is unlocked before releasing
791 * the cpu. The mutex will be re-acquired before returning.
793 * JMM - Add hint to make sure mutex is available before rousting
799 wait_interrupt_t interruptible
)
803 res
= assert_wait(event
, interruptible
);
804 if (res
== THREAD_WAITING
) {
806 res
= thread_block(THREAD_CONTINUE_NULL
);
813 * thread_sleep_mutex_deadline:
815 * Cause the current thread to wait until the specified event
816 * (or deadline) occurs. The specified mutex is unlocked before
817 * releasing the cpu. The mutex will be re-acquired before returning.
820 thread_sleep_mutex_deadline(
824 wait_interrupt_t interruptible
)
828 res
= assert_wait_deadline(event
, interruptible
, deadline
);
829 if (res
== THREAD_WAITING
) {
831 res
= thread_block(THREAD_CONTINUE_NULL
);
838 * thread_sleep_lock_write:
840 * Cause the current thread to wait until the specified event
841 * occurs. The specified (write) lock is unlocked before releasing
842 * the cpu. The (write) lock will be re-acquired before returning.
845 thread_sleep_lock_write(
848 wait_interrupt_t interruptible
)
852 res
= assert_wait(event
, interruptible
);
853 if (res
== THREAD_WAITING
) {
854 lock_write_done(lock
);
855 res
= thread_block(THREAD_CONTINUE_NULL
);
864 * Force a preemption point for a thread and wait
865 * for it to stop running. Arbitrates access among
866 * multiple stop requests. (released by unstop)
868 * The thread must enter a wait state and stop via a
871 * Returns FALSE if interrupted.
877 wait_result_t wresult
;
878 spl_t s
= splsched();
883 while (thread
->state
& TH_SUSP
) {
884 thread
->wake_active
= TRUE
;
885 thread_unlock(thread
);
887 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
891 if (wresult
== THREAD_WAITING
)
892 wresult
= thread_block(THREAD_CONTINUE_NULL
);
894 if (wresult
!= THREAD_AWAKENED
)
902 thread
->state
|= TH_SUSP
;
904 while (thread
->state
& TH_RUN
) {
905 processor_t processor
= thread
->last_processor
;
907 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
908 cause_ast_check(processor
);
910 thread
->wake_active
= TRUE
;
911 thread_unlock(thread
);
913 wresult
= assert_wait(&thread
->wake_active
, THREAD_ABORTSAFE
);
917 if (wresult
== THREAD_WAITING
)
918 wresult
= thread_block(THREAD_CONTINUE_NULL
);
920 if (wresult
!= THREAD_AWAKENED
) {
921 thread_unstop(thread
);
930 thread_unlock(thread
);
940 * Release a previous stop request and set
941 * the thread running if appropriate.
943 * Use only after a successful stop operation.
949 spl_t s
= splsched();
954 if ((thread
->state
& (TH_RUN
|TH_WAIT
|TH_SUSP
)) == TH_SUSP
) {
955 thread
->state
&= ~TH_SUSP
;
956 thread_unblock(thread
, THREAD_AWAKENED
);
958 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
961 if (thread
->state
& TH_SUSP
) {
962 thread
->state
&= ~TH_SUSP
;
964 if (thread
->wake_active
) {
965 thread
->wake_active
= FALSE
;
966 thread_unlock(thread
);
968 thread_wakeup(&thread
->wake_active
);
976 thread_unlock(thread
);
984 * Wait for a thread to stop running. (non-interruptible)
991 wait_result_t wresult
;
992 spl_t s
= splsched();
997 while (thread
->state
& TH_RUN
) {
998 processor_t processor
= thread
->last_processor
;
1000 if (processor
!= PROCESSOR_NULL
&& processor
->active_thread
== thread
)
1001 cause_ast_check(processor
);
1003 thread
->wake_active
= TRUE
;
1004 thread_unlock(thread
);
1006 wresult
= assert_wait(&thread
->wake_active
, THREAD_UNINT
);
1007 wake_unlock(thread
);
1010 if (wresult
== THREAD_WAITING
)
1011 thread_block(THREAD_CONTINUE_NULL
);
1015 thread_lock(thread
);
1018 thread_unlock(thread
);
1019 wake_unlock(thread
);
1024 * Routine: clear_wait_internal
1026 * Clear the wait condition for the specified thread.
1027 * Start the thread executing if that is appropriate.
1029 * thread thread to awaken
1030 * result Wakeup result the thread should see
1033 * the thread is locked.
1035 * KERN_SUCCESS thread was rousted out a wait
1036 * KERN_FAILURE thread was waiting but could not be rousted
1037 * KERN_NOT_WAITING thread was not waiting
1039 __private_extern__ kern_return_t
1040 clear_wait_internal(
1042 wait_result_t wresult
)
1044 wait_queue_t wq
= thread
->wait_queue
;
1045 int i
= LockTimeOut
;
1048 if (wresult
== THREAD_INTERRUPTED
&& (thread
->state
& TH_UNINT
))
1049 return (KERN_FAILURE
);
1051 if (wq
!= WAIT_QUEUE_NULL
) {
1052 if (wait_queue_lock_try(wq
)) {
1053 wait_queue_pull_thread_locked(wq
, thread
, TRUE
);
1054 /* wait queue unlocked, thread still locked */
1057 thread_unlock(thread
);
1060 thread_lock(thread
);
1061 if (wq
!= thread
->wait_queue
)
1062 return (KERN_NOT_WAITING
);
1068 return (thread_go(thread
, wresult
));
1071 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
1072 thread
, wq
, cpu_number());
1074 return (KERN_FAILURE
);
1081 * Clear the wait condition for the specified thread. Start the thread
1082 * executing if that is appropriate.
1085 * thread thread to awaken
1086 * result Wakeup result the thread should see
1091 wait_result_t result
)
1097 thread_lock(thread
);
1098 ret
= clear_wait_internal(thread
, result
);
1099 thread_unlock(thread
);
1106 * thread_wakeup_prim:
1108 * Common routine for thread_wakeup, thread_wakeup_with_result,
1109 * and thread_wakeup_one.
1115 boolean_t one_thread
,
1116 wait_result_t result
)
1118 register wait_queue_t wq
;
1121 index
= wait_hash(event
);
1122 wq
= &wait_queues
[index
];
1124 return (wait_queue_wakeup_one(wq
, event
, result
));
1126 return (wait_queue_wakeup_all(wq
, event
, result
));
1132 * Force the current thread to execute on the specified processor.
1134 * Returns the previous binding. PROCESSOR_NULL means
1137 * XXX - DO NOT export this to users - XXX
1141 processor_t processor
)
1143 thread_t self
= current_thread();
1150 prev
= self
->bound_processor
;
1151 self
->bound_processor
= processor
;
1153 thread_unlock(self
);
1162 * Select a new thread for the current processor to execute.
1164 * May select the current thread, which must be locked.
1169 processor_t processor
)
1171 processor_set_t pset
= processor
->processor_set
;
1172 thread_t new_thread
= THREAD_NULL
;
1173 boolean_t other_runnable
;
1177 * Update the priority.
1179 if (thread
->sched_stamp
!= sched_tick
)
1180 update_priority(thread
);
1182 processor
->current_pri
= thread
->sched_pri
;
1186 simple_lock(&rt_lock
);
1189 * Check for other runnable threads.
1191 other_runnable
= processor
->runq
.count
> 0 || rt_runq
.count
> 0;
1194 * Test to see if the current thread should continue
1195 * to run on this processor. Must be runnable, and not
1196 * bound to a different processor, nor be in the wrong
1199 if ( thread
->state
== TH_RUN
&&
1200 (thread
->bound_processor
== PROCESSOR_NULL
||
1201 thread
->bound_processor
== processor
) &&
1202 (thread
->affinity_set
== AFFINITY_SET_NULL
||
1203 thread
->affinity_set
->aset_pset
== pset
) ) {
1204 if ( thread
->sched_pri
>= BASEPRI_RTQUEUES
&&
1205 first_timeslice(processor
) ) {
1206 if (rt_runq
.highq
>= BASEPRI_RTQUEUES
) {
1207 register run_queue_t runq
= &rt_runq
;
1210 q
= runq
->queues
+ runq
->highq
;
1211 if (((thread_t
)q
->next
)->realtime
.deadline
<
1212 processor
->deadline
) {
1213 thread
= (thread_t
)q
->next
;
1214 ((queue_entry_t
)thread
)->next
->prev
= q
;
1215 q
->next
= ((queue_entry_t
)thread
)->next
;
1216 thread
->runq
= PROCESSOR_NULL
;
1217 runq
->count
--; runq
->urgency
--;
1218 assert(runq
->urgency
>= 0);
1219 if (queue_empty(q
)) {
1220 if (runq
->highq
!= IDLEPRI
)
1221 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
1222 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
1227 simple_unlock(&rt_lock
);
1229 processor
->deadline
= thread
->realtime
.deadline
;
1236 if ( (!other_runnable
||
1237 (processor
->runq
.highq
< thread
->sched_pri
&&
1238 rt_runq
.highq
< thread
->sched_pri
)) ) {
1240 simple_unlock(&rt_lock
);
1242 /* I am the highest priority runnable (non-idle) thread */
1244 pset_pri_hint(pset
, processor
, processor
->current_pri
);
1246 processor
->deadline
= UINT64_MAX
;
1255 return choose_thread(processor
);
1257 simple_unlock(&rt_lock
);
1260 * No runnable threads, attempt to steal
1261 * from other processors.
1263 new_thread
= steal_thread(pset
);
1264 if (new_thread
!= THREAD_NULL
)
1265 return (new_thread
);
1268 * If other threads have appeared, shortcut
1271 if (processor
->runq
.count
> 0 || rt_runq
.count
> 0)
1277 * Nothing is runnable, so set this processor idle if it
1280 if (processor
->state
== PROCESSOR_RUNNING
) {
1281 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
1282 processor
->state
= PROCESSOR_IDLE
;
1284 enqueue_head(&pset
->idle_queue
, (queue_entry_t
)processor
);
1285 pset
->low_pri
= processor
;
1289 processor
->deadline
= UINT64_MAX
;
1294 * Choose idle thread if fast idle is not possible.
1296 if ((thread
->state
& (TH_IDLE
|TH_TERMINATE
|TH_SUSP
)) || !(thread
->state
& TH_WAIT
) || thread
->wake_active
)
1297 return (processor
->idle_thread
);
1300 * Perform idling activities directly without a
1301 * context switch. Return dispatched thread,
1302 * else check again for a runnable thread.
1304 new_thread
= thread_select_idle(thread
, processor
);
1306 } while (new_thread
== THREAD_NULL
);
1308 return (new_thread
);
1312 * thread_select_idle:
1314 * Idle the processor using the current thread context.
1316 * Called with thread locked, then dropped and relocked.
1321 processor_t processor
)
1323 thread_t new_thread
;
1325 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1329 thread
->state
|= TH_IDLE
;
1330 processor
->current_pri
= IDLEPRI
;
1332 thread_unlock(thread
);
1335 * Switch execution timing to processor idle thread.
1337 processor
->last_dispatch
= mach_absolute_time();
1338 thread_timer_event(processor
->last_dispatch
, &processor
->idle_thread
->system_timer
);
1339 PROCESSOR_DATA(processor
, kernel_timer
) = &processor
->idle_thread
->system_timer
;
1342 * Cancel the quantum timer while idling.
1344 timer_call_cancel(&processor
->quantum_timer
);
1345 processor
->timeslice
= 0;
1347 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1350 * Enable interrupts and perform idling activities. No
1351 * preemption due to TH_IDLE being set.
1353 spllo(); new_thread
= processor_idle(thread
, processor
);
1356 * Return at splsched.
1358 (*thread
->sched_call
)(SCHED_CALL_UNBLOCK
, thread
);
1360 thread_lock(thread
);
1363 * If awakened, switch to thread timer and start a new quantum.
1364 * Otherwise skip; we will context switch to another thread or return here.
1366 if (!(thread
->state
& TH_WAIT
)) {
1367 processor
->last_dispatch
= mach_absolute_time();
1368 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1369 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1371 thread_quantum_init(thread
);
1373 processor
->quantum_end
= processor
->last_dispatch
+ thread
->current_quantum
;
1374 timer_call_enter1(&processor
->quantum_timer
, thread
, processor
->quantum_end
);
1375 processor
->timeslice
= 1;
1377 thread
->computation_epoch
= processor
->last_dispatch
;
1380 thread
->state
&= ~TH_IDLE
;
1383 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1386 return (new_thread
);
1390 * Perform a context switch and start executing the new thread.
1392 * Returns FALSE on failure, and the thread is re-dispatched.
1394 * Called at splsched.
1397 #define funnel_release_check(thread, debug) \
1399 if ((thread)->funnel_state & TH_FN_OWNED) { \
1400 (thread)->funnel_state = TH_FN_REFUNNEL; \
1401 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE, \
1402 (thread)->funnel_lock, (debug), 0, 0, 0); \
1403 funnel_unlock((thread)->funnel_lock); \
1407 #define funnel_refunnel_check(thread, debug) \
1409 if ((thread)->funnel_state & TH_FN_REFUNNEL) { \
1410 kern_return_t result = (thread)->wait_result; \
1412 (thread)->funnel_state = 0; \
1413 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, \
1414 (thread)->funnel_lock, (debug), 0, 0, 0); \
1415 funnel_lock((thread)->funnel_lock); \
1416 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE, \
1417 (thread)->funnel_lock, (debug), 0, 0, 0); \
1418 (thread)->funnel_state = TH_FN_OWNED; \
1419 (thread)->wait_result = result; \
1425 register thread_t self
,
1426 register thread_t thread
,
1429 thread_continue_t continuation
= self
->continuation
;
1430 void *parameter
= self
->parameter
;
1431 processor_t processor
;
1433 if (get_preemption_level() != 0)
1434 panic("thread_invoke: preemption_level %d\n",
1435 get_preemption_level());
1437 assert(self
== current_thread());
1440 * Mark thread interruptible.
1442 thread_lock(thread
);
1443 thread
->state
&= ~TH_UNINT
;
1446 assert(thread_runnable(thread
));
1450 * Allow time constraint threads to hang onto
1453 if ((self
->sched_mode
& TH_MODE_REALTIME
) && !self
->reserved_stack
)
1454 self
->reserved_stack
= self
->kernel_stack
;
1456 if (continuation
!= NULL
) {
1457 if (!thread
->kernel_stack
) {
1459 * If we are using a privileged stack,
1460 * check to see whether we can exchange it with
1461 * that of the other thread.
1463 if (self
->kernel_stack
== self
->reserved_stack
&& !thread
->reserved_stack
)
1467 * Context switch by performing a stack handoff.
1469 continuation
= thread
->continuation
;
1470 parameter
= thread
->parameter
;
1472 processor
= current_processor();
1473 processor
->active_thread
= thread
;
1474 processor
->current_pri
= thread
->sched_pri
;
1475 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1476 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1477 thread
->ps_switch
++;
1480 thread
->last_processor
= processor
;
1482 ast_context(thread
);
1483 thread_unlock(thread
);
1485 self
->reason
= reason
;
1487 processor
->last_dispatch
= mach_absolute_time();
1488 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1489 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1491 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
, MACH_STACK_HANDOFF
)|DBG_FUNC_NONE
,
1492 self
->reason
, (int)thread
, self
->sched_pri
, thread
->sched_pri
, 0);
1494 TLOG(1, "thread_invoke: calling machine_stack_handoff\n");
1495 machine_stack_handoff(self
, thread
);
1497 thread_dispatch(self
, thread
);
1499 thread
->continuation
= thread
->parameter
= NULL
;
1501 counter(c_thread_invoke_hits
++);
1503 funnel_refunnel_check(thread
, 2);
1506 assert(continuation
);
1507 call_continuation(continuation
, parameter
, thread
->wait_result
);
1510 else if (thread
== self
) {
1511 /* same thread but with continuation */
1513 counter(++c_thread_invoke_same
);
1514 thread_unlock(self
);
1516 self
->continuation
= self
->parameter
= NULL
;
1518 funnel_refunnel_check(self
, 3);
1521 call_continuation(continuation
, parameter
, self
->wait_result
);
1527 * Check that the other thread has a stack
1529 if (!thread
->kernel_stack
) {
1531 if (!stack_alloc_try(thread
)) {
1532 counter(c_thread_invoke_misses
++);
1533 thread_unlock(thread
);
1534 thread_stack_enqueue(thread
);
1538 else if (thread
== self
) {
1540 counter(++c_thread_invoke_same
);
1541 thread_unlock(self
);
1547 * Context switch by full context save.
1549 processor
= current_processor();
1550 processor
->active_thread
= thread
;
1551 processor
->current_pri
= thread
->sched_pri
;
1552 if (thread
->last_processor
!= processor
&& thread
->last_processor
!= NULL
) {
1553 if (thread
->last_processor
->processor_set
!= processor
->processor_set
)
1554 thread
->ps_switch
++;
1557 thread
->last_processor
= processor
;
1559 ast_context(thread
);
1560 thread_unlock(thread
);
1562 counter(c_thread_invoke_csw
++);
1564 assert(self
->runq
== PROCESSOR_NULL
);
1565 self
->reason
= reason
;
1567 processor
->last_dispatch
= mach_absolute_time();
1568 thread_timer_event(processor
->last_dispatch
, &thread
->system_timer
);
1569 PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
1571 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED
,MACH_SCHED
) | DBG_FUNC_NONE
,
1572 (int)self
->reason
, (int)thread
, self
->sched_pri
, thread
->sched_pri
, 0);
1575 * This is where we actually switch register context,
1576 * and address space if required. We will next run
1577 * as a result of a subsequent context switch.
1579 thread
= machine_switch_context(self
, continuation
, thread
);
1580 TLOG(1,"thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self
, continuation
, thread
);
1583 * We have been resumed and are set to run.
1585 thread_dispatch(thread
, self
);
1588 self
->continuation
= self
->parameter
= NULL
;
1590 funnel_refunnel_check(self
, 3);
1593 call_continuation(continuation
, parameter
, self
->wait_result
);
1603 * Handle threads at context switch. Re-dispatch other thread
1604 * if still running, otherwise update run state and perform
1605 * special actions. Update quantum for other thread and begin
1606 * the quantum for ourselves.
1608 * Called at splsched.
1615 processor_t processor
= self
->last_processor
;
1617 if (thread
!= THREAD_NULL
) {
1619 * If blocked at a continuation, discard
1622 if (thread
->continuation
!= NULL
&& thread
->kernel_stack
!= 0)
1625 if (!(thread
->state
& TH_IDLE
)) {
1627 thread_lock(thread
);
1630 * Compute remainder of current quantum.
1632 if ( first_timeslice(processor
) &&
1633 processor
->quantum_end
> processor
->last_dispatch
)
1634 thread
->current_quantum
= (processor
->quantum_end
- processor
->last_dispatch
);
1636 thread
->current_quantum
= 0;
1638 if (thread
->sched_mode
& TH_MODE_REALTIME
) {
1640 * Cancel the deadline if the thread has
1641 * consumed the entire quantum.
1643 if (thread
->current_quantum
== 0) {
1644 thread
->realtime
.deadline
= UINT64_MAX
;
1645 thread
->reason
|= AST_QUANTUM
;
1650 * For non-realtime threads treat a tiny
1651 * remaining quantum as an expired quantum
1652 * but include what's left next time.
1654 if (thread
->current_quantum
< min_std_quantum
) {
1655 thread
->reason
|= AST_QUANTUM
;
1656 thread
->current_quantum
+= std_quantum
;
1661 * If we are doing a direct handoff then
1662 * take the remainder of the quantum.
1664 if ((thread
->reason
& (AST_HANDOFF
|AST_QUANTUM
)) == AST_HANDOFF
) {
1665 self
->current_quantum
= thread
->current_quantum
;
1666 thread
->reason
|= AST_QUANTUM
;
1667 thread
->current_quantum
= 0;
1670 thread
->last_switch
= processor
->last_dispatch
;
1672 thread
->computation_metered
+= (thread
->last_switch
- thread
->computation_epoch
);
1674 if (!(thread
->state
& TH_WAIT
)) {
1678 if (thread
->reason
& AST_QUANTUM
)
1679 thread_setrun(thread
, SCHED_TAILQ
);
1681 if (thread
->reason
& AST_PREEMPT
)
1682 thread_setrun(thread
, SCHED_HEADQ
);
1684 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
1686 thread
->reason
= AST_NONE
;
1688 thread_unlock(thread
);
1689 wake_unlock(thread
);
1695 thread
->state
&= ~TH_RUN
;
1697 if (thread
->sched_mode
& TH_MODE_TIMESHARE
)
1701 if (thread
->wake_active
) {
1702 thread
->wake_active
= FALSE
;
1703 thread_unlock(thread
);
1705 thread_wakeup(&thread
->wake_active
);
1708 thread_unlock(thread
);
1710 wake_unlock(thread
);
1712 (*thread
->sched_call
)(SCHED_CALL_BLOCK
, thread
);
1714 if (thread
->state
& TH_TERMINATE
)
1715 thread_terminate_enqueue(thread
);
1720 if (!(self
->state
& TH_IDLE
)) {
1722 * Get a new quantum if none remaining.
1724 if (self
->current_quantum
== 0)
1725 thread_quantum_init(self
);
1728 * Set up quantum timer and timeslice.
1730 processor
->quantum_end
= (processor
->last_dispatch
+ self
->current_quantum
);
1731 timer_call_enter1(&processor
->quantum_timer
, self
, processor
->quantum_end
);
1733 processor
->timeslice
= 1;
1735 self
->last_switch
= processor
->last_dispatch
;
1737 self
->computation_epoch
= self
->last_switch
;
1740 timer_call_cancel(&processor
->quantum_timer
);
1741 processor
->timeslice
= 0;
1746 * thread_block_reason:
1748 * Forces a reschedule, blocking the caller if a wait
1749 * has been asserted.
1751 * If a continuation is specified, then thread_invoke will
1752 * attempt to discard the thread's kernel stack. When the
1753 * thread resumes, it will execute the continuation function
1754 * on a new kernel stack.
1756 counter(mach_counter_t c_thread_block_calls
= 0;)
1759 thread_block_reason(
1760 thread_continue_t continuation
,
1764 register thread_t self
= current_thread();
1765 register processor_t processor
;
1766 register thread_t new_thread
;
1769 counter(++c_thread_block_calls
);
1773 if (!(reason
& AST_PREEMPT
))
1774 funnel_release_check(self
, 2);
1776 processor
= current_processor();
1778 /* If we're explicitly yielding, force a subsequent quantum */
1779 if (reason
& AST_YIELD
)
1780 processor
->timeslice
= 0;
1782 /* We're handling all scheduling AST's */
1783 ast_off(AST_SCHEDULING
);
1785 self
->continuation
= continuation
;
1786 self
->parameter
= parameter
;
1790 new_thread
= thread_select(self
, processor
);
1791 thread_unlock(self
);
1792 } while (!thread_invoke(self
, new_thread
, reason
));
1794 funnel_refunnel_check(self
, 5);
1797 return (self
->wait_result
);
1803 * Block the current thread if a wait has been asserted.
1807 thread_continue_t continuation
)
1809 return thread_block_reason(continuation
, NULL
, AST_NONE
);
1813 thread_block_parameter(
1814 thread_continue_t continuation
,
1817 return thread_block_reason(continuation
, parameter
, AST_NONE
);
1823 * Switch directly from the current thread to the
1824 * new thread, handing off our quantum if appropriate.
1826 * New thread must be runnable, and not on a run queue.
1828 * Called at splsched.
1833 thread_continue_t continuation
,
1835 thread_t new_thread
)
1837 ast_t handoff
= AST_HANDOFF
;
1839 funnel_release_check(self
, 3);
1841 self
->continuation
= continuation
;
1842 self
->parameter
= parameter
;
1844 while (!thread_invoke(self
, new_thread
, handoff
)) {
1845 processor_t processor
= current_processor();
1848 new_thread
= thread_select(self
, processor
);
1849 thread_unlock(self
);
1853 funnel_refunnel_check(self
, 6);
1855 return (self
->wait_result
);
1861 * Called at splsched when a thread first receives
1862 * a new stack after a continuation.
1866 register thread_t thread
)
1868 register thread_t self
= current_thread();
1869 register thread_continue_t continuation
;
1870 register void *parameter
;
1872 continuation
= self
->continuation
;
1873 parameter
= self
->parameter
;
1875 thread_dispatch(thread
, self
);
1877 self
->continuation
= self
->parameter
= NULL
;
1879 funnel_refunnel_check(self
, 4);
1881 if (thread
!= THREAD_NULL
)
1884 TLOG(1, "thread_continue: calling call_continuation \n");
1885 call_continuation(continuation
, parameter
, self
->wait_result
);
1892 * Initialize a run queue before first use.
1900 rq
->highq
= IDLEPRI
;
1901 for (i
= 0; i
< NRQBM
; i
++)
1903 setbit(MAXPRI
- IDLEPRI
, rq
->bitmap
);
1904 rq
->urgency
= rq
->count
= 0;
1905 for (i
= 0; i
< NRQS
; i
++)
1906 queue_init(&rq
->queues
[i
]);
1910 * run_queue_dequeue:
1912 * Perform a dequeue operation on a run queue,
1913 * and return the resulting thread.
1915 * The run queue must be locked (see run_queue_remove()
1916 * for more info), and not empty.
1924 queue_t queue
= rq
->queues
+ rq
->highq
;
1926 if (options
& SCHED_HEADQ
) {
1927 thread
= (thread_t
)queue
->next
;
1928 ((queue_entry_t
)thread
)->next
->prev
= queue
;
1929 queue
->next
= ((queue_entry_t
)thread
)->next
;
1932 thread
= (thread_t
)queue
->prev
;
1933 ((queue_entry_t
)thread
)->prev
->next
= queue
;
1934 queue
->prev
= ((queue_entry_t
)thread
)->prev
;
1937 thread
->runq
= PROCESSOR_NULL
;
1939 if (testbit(rq
->highq
, sched_preempt_pri
)) {
1940 rq
->urgency
--; assert(rq
->urgency
>= 0);
1942 if (queue_empty(queue
)) {
1943 if (rq
->highq
!= IDLEPRI
)
1944 clrbit(MAXPRI
- rq
->highq
, rq
->bitmap
);
1945 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
1952 * realtime_queue_insert:
1954 * Enqueue a thread for realtime execution.
1957 realtime_queue_insert(
1960 run_queue_t rq
= &rt_runq
;
1961 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
1962 uint64_t deadline
= thread
->realtime
.deadline
;
1963 boolean_t preempt
= FALSE
;
1965 simple_lock(&rt_lock
);
1967 if (queue_empty(queue
)) {
1968 enqueue_tail(queue
, (queue_entry_t
)thread
);
1970 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
1971 if (thread
->sched_pri
> rq
->highq
)
1972 rq
->highq
= thread
->sched_pri
;
1976 register thread_t entry
= (thread_t
)queue_first(queue
);
1979 if ( queue_end(queue
, (queue_entry_t
)entry
) ||
1980 deadline
< entry
->realtime
.deadline
) {
1981 entry
= (thread_t
)queue_prev((queue_entry_t
)entry
);
1985 entry
= (thread_t
)queue_next((queue_entry_t
)entry
);
1988 if ((queue_entry_t
)entry
== queue
)
1991 insque((queue_entry_t
)thread
, (queue_entry_t
)entry
);
1994 thread
->runq
= RT_RUNQ
;
1995 rq
->count
++; rq
->urgency
++;
1997 simple_unlock(&rt_lock
);
2005 * Dispatch a thread for realtime execution.
2007 * Thread must be locked. Associated pset must
2008 * be locked, and is returned unlocked.
2012 processor_t processor
,
2015 processor_set_t pset
= processor
->processor_set
;
2018 * Dispatch directly onto idle processor.
2020 if (processor
->state
== PROCESSOR_IDLE
) {
2021 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2023 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2025 processor
->next_thread
= thread
;
2026 processor
->deadline
= thread
->realtime
.deadline
;
2027 processor
->state
= PROCESSOR_DISPATCHING
;
2030 if (processor
!= current_processor())
2031 machine_signal_idle(processor
);
2035 if (realtime_queue_insert(thread
)) {
2036 if (processor
== current_processor())
2037 ast_on(AST_PREEMPT
| AST_URGENT
);
2039 cause_ast_check(processor
);
2046 * processor_enqueue:
2048 * Enqueue thread on a processor run queue. Thread must be locked,
2049 * and not already be on a run queue.
2051 * Returns TRUE if a preemption is indicated based on the state
2054 * The run queue must be locked (see run_queue_remove()
2059 processor_t processor
,
2063 run_queue_t rq
= &processor
->runq
;
2064 queue_t queue
= rq
->queues
+ thread
->sched_pri
;
2065 boolean_t result
= FALSE
;
2067 if (queue_empty(queue
)) {
2068 enqueue_tail(queue
, (queue_entry_t
)thread
);
2070 setbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2071 if (thread
->sched_pri
> rq
->highq
) {
2072 rq
->highq
= thread
->sched_pri
;
2077 if (options
& SCHED_TAILQ
)
2078 enqueue_tail(queue
, (queue_entry_t
)thread
);
2080 enqueue_head(queue
, (queue_entry_t
)thread
);
2082 thread
->runq
= processor
;
2083 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2093 * Dispatch a thread for execution on a
2096 * Thread must be locked. Associated pset must
2097 * be locked, and is returned unlocked.
2101 processor_t processor
,
2105 processor_set_t pset
= processor
->processor_set
;
2109 * Dispatch directly onto idle processor.
2111 if (processor
->state
== PROCESSOR_IDLE
) {
2112 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2114 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2116 processor
->next_thread
= thread
;
2117 processor
->deadline
= UINT64_MAX
;
2118 processor
->state
= PROCESSOR_DISPATCHING
;
2121 if (processor
!= current_processor())
2122 machine_signal_idle(processor
);
2127 * Set preemption mode.
2129 if (testbit(thread
->sched_pri
, sched_preempt_pri
))
2130 preempt
= (AST_PREEMPT
| AST_URGENT
);
2132 if (thread
->sched_mode
& TH_MODE_TIMESHARE
&& thread
->priority
< BASEPRI_BACKGROUND
)
2135 preempt
= (options
& SCHED_PREEMPT
)? AST_PREEMPT
: AST_NONE
;
2137 if (!processor_enqueue(processor
, thread
, options
))
2140 if (preempt
!= AST_NONE
) {
2141 if (processor
== current_processor()) {
2142 thread_t self
= processor
->active_thread
;
2144 if (csw_needed(self
, processor
))
2148 if ( (processor
->state
== PROCESSOR_RUNNING
||
2149 processor
->state
== PROCESSOR_SHUTDOWN
) &&
2150 thread
->sched_pri
>= processor
->current_pri
) {
2151 cause_ast_check(processor
);
2155 if ( processor
->state
== PROCESSOR_SHUTDOWN
&&
2156 thread
->sched_pri
>= processor
->current_pri
) {
2157 cause_ast_check(processor
);
2163 #define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
2168 * Return the next sibling pset containing
2169 * available processors.
2171 * Returns the original pset if none other is
2174 static processor_set_t
2176 processor_set_t pset
)
2178 processor_set_t nset
= pset
;
2181 nset
= next_pset(nset
);
2182 } while (nset
->processor_count
< 1 && nset
!= pset
);
2190 * Choose a processor for the thread, beginning at
2193 * Returns a processor, possibly from a different pset.
2195 * The thread must be locked. The pset must be locked,
2196 * and the resulting pset is locked on return.
2200 processor_set_t pset
,
2203 processor_set_t nset
, cset
= pset
;
2204 processor_t processor
= thread
->last_processor
;
2207 * Prefer the last processor, when appropriate.
2209 if (processor
!= PROCESSOR_NULL
) {
2210 if (processor
->processor_set
!= pset
||
2211 processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2212 processor
= PROCESSOR_NULL
;
2214 if (processor
->state
== PROCESSOR_IDLE
|| processor
->current_pri
< thread
->sched_pri
)
2219 * Iterate through the processor sets to locate
2220 * an appropriate processor.
2224 * Choose an idle processor.
2226 if (!queue_empty(&cset
->idle_queue
))
2227 return ((processor_t
)queue_first(&cset
->idle_queue
));
2229 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2231 * For an RT thread, iterate through active processors, first fit.
2233 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2234 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2235 if (thread
->sched_pri
> processor
->current_pri
||
2236 thread
->realtime
.deadline
< processor
->deadline
)
2239 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2242 processor
= PROCESSOR_NULL
;
2246 * Check the low hint processor in the processor set if available.
2248 if (cset
->low_pri
!= PROCESSOR_NULL
&&
2249 cset
->low_pri
->state
!= PROCESSOR_SHUTDOWN
&& cset
->low_pri
->state
!= PROCESSOR_OFF_LINE
) {
2250 if (processor
== PROCESSOR_NULL
|| cset
->low_pri
->current_pri
< thread
->sched_pri
)
2251 processor
= cset
->low_pri
;
2255 * Otherwise, choose an available processor in the set.
2257 if (processor
== PROCESSOR_NULL
) {
2258 processor
= (processor_t
)dequeue_head(&cset
->active_queue
);
2259 if (processor
!= PROCESSOR_NULL
)
2260 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2265 * Move onto the next processor set.
2267 nset
= next_pset(cset
);
2275 } while (nset
!= pset
);
2278 * Make sure that we pick a running processor,
2279 * and that the correct processor set is locked.
2283 * If we haven't been able to choose a processor,
2284 * pick the current one and return it.
2286 if (processor
== PROCESSOR_NULL
) {
2287 processor
= current_processor();
2290 * Check that the correct processor set is
2293 if (cset
!= processor
->processor_set
) {
2296 cset
= processor
->processor_set
;
2304 * Check that the processor set for the chosen
2305 * processor is locked.
2307 if (cset
!= processor
->processor_set
) {
2310 cset
= processor
->processor_set
;
2315 * We must verify that the chosen processor is still available.
2317 if (processor
->state
== PROCESSOR_SHUTDOWN
|| processor
->state
== PROCESSOR_OFF_LINE
)
2318 processor
= PROCESSOR_NULL
;
2319 } while (processor
== PROCESSOR_NULL
);
2327 * Dispatch thread for execution, onto an idle
2328 * processor or run queue, and signal a preemption
2331 * Thread must be locked.
2338 processor_t processor
;
2339 processor_set_t pset
;
2342 assert(thread_runnable(thread
));
2346 * Update priority if needed.
2348 if (thread
->sched_stamp
!= sched_tick
)
2349 update_priority(thread
);
2351 assert(thread
->runq
== PROCESSOR_NULL
);
2353 if (thread
->bound_processor
== PROCESSOR_NULL
) {
2357 if (thread
->affinity_set
!= AFFINITY_SET_NULL
) {
2359 * Use affinity set policy hint.
2361 pset
= thread
->affinity_set
->aset_pset
;
2364 processor
= choose_processor(pset
, thread
);
2367 if (thread
->last_processor
!= PROCESSOR_NULL
) {
2369 * Simple (last processor) affinity case.
2371 processor
= thread
->last_processor
;
2372 pset
= processor
->processor_set
;
2376 * Choose a different processor in certain cases.
2378 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
) {
2380 * If the processor is executing an RT thread with
2381 * an earlier deadline, choose another.
2383 if (thread
->sched_pri
<= processor
->current_pri
||
2384 thread
->realtime
.deadline
>= processor
->deadline
)
2385 processor
= choose_processor(pset
, thread
);
2388 processor
= choose_processor(pset
, thread
);
2394 * Utilitize a per task hint to spread threads
2395 * among the available processor sets.
2397 task_t task
= thread
->task
;
2399 pset
= task
->pset_hint
;
2400 if (pset
== PROCESSOR_SET_NULL
)
2401 pset
= current_processor()->processor_set
;
2403 pset
= choose_next_pset(pset
);
2406 processor
= choose_processor(pset
, thread
);
2407 task
->pset_hint
= processor
->processor_set
;
2414 * Unconditionally dispatch on the processor.
2416 processor
= thread
->bound_processor
;
2417 pset
= processor
->processor_set
;
2422 * Dispatch the thread on the choosen processor.
2424 if (thread
->sched_pri
>= BASEPRI_RTQUEUES
)
2425 realtime_setrun(processor
, thread
);
2427 processor_setrun(processor
, thread
, options
);
2431 * processor_queue_shutdown:
2433 * Shutdown a processor run queue by moving
2434 * non-bound threads to the current processor.
2436 * Associated pset must be locked, and is
2437 * returned unlocked.
2440 processor_queue_shutdown(
2441 processor_t processor
)
2443 processor_set_t pset
= processor
->processor_set
;
2444 run_queue_t rq
= &processor
->runq
;
2445 queue_t queue
= rq
->queues
+ rq
->highq
;
2446 int pri
= rq
->highq
, count
= rq
->count
;
2447 thread_t next
, thread
;
2448 queue_head_t tqueue
;
2450 queue_init(&tqueue
);
2453 thread
= (thread_t
)queue_first(queue
);
2454 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2455 next
= (thread_t
)queue_next((queue_entry_t
)thread
);
2457 if (thread
->bound_processor
!= processor
) {
2458 remqueue(queue
, (queue_entry_t
)thread
);
2460 thread
->runq
= PROCESSOR_NULL
;
2462 if (testbit(pri
, sched_preempt_pri
)) {
2463 rq
->urgency
--; assert(rq
->urgency
>= 0);
2465 if (queue_empty(queue
)) {
2467 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2468 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2471 enqueue_tail(&tqueue
, (queue_entry_t
)thread
);
2483 processor
= current_processor();
2484 pset
= processor
->processor_set
;
2486 while ((thread
= (thread_t
)dequeue_head(&tqueue
)) != THREAD_NULL
) {
2487 thread_lock(thread
);
2488 thread
->last_processor
= PROCESSOR_NULL
;
2492 processor_enqueue(processor
, thread
, SCHED_TAILQ
);
2496 thread_unlock(thread
);
2501 * Check for a possible preemption point in
2502 * the (current) thread.
2504 * Called at splsched.
2509 processor_t processor
)
2511 int current_pri
= thread
->sched_pri
;
2512 ast_t result
= AST_NONE
;
2515 if (first_timeslice(processor
)) {
2517 if (runq
->highq
>= BASEPRI_RTQUEUES
)
2518 return (AST_PREEMPT
| AST_URGENT
);
2520 if (runq
->highq
> current_pri
) {
2521 if (runq
->urgency
> 0)
2522 return (AST_PREEMPT
| AST_URGENT
);
2524 result
|= AST_PREEMPT
;
2527 runq
= &processor
->runq
;
2528 if (runq
->highq
> current_pri
) {
2529 if (runq
->urgency
> 0)
2530 return (AST_PREEMPT
| AST_URGENT
);
2532 result
|= AST_PREEMPT
;
2537 if (runq
->highq
>= current_pri
) {
2538 if (runq
->urgency
> 0)
2539 return (AST_PREEMPT
| AST_URGENT
);
2541 result
|= AST_PREEMPT
;
2544 runq
= &processor
->runq
;
2545 if (runq
->highq
>= current_pri
) {
2546 if (runq
->urgency
> 0)
2547 return (AST_PREEMPT
| AST_URGENT
);
2549 result
|= AST_PREEMPT
;
2553 if (result
!= AST_NONE
)
2556 if (thread
->state
& TH_SUSP
)
2557 result
|= AST_PREEMPT
;
2565 * Set the scheduled priority of the specified thread.
2567 * This may cause the thread to change queues.
2569 * Thread must be locked.
2576 boolean_t removed
= run_queue_remove(thread
);
2578 thread
->sched_pri
= priority
;
2580 thread_setrun(thread
, SCHED_PREEMPT
| SCHED_TAILQ
);
2582 if (thread
->state
& TH_RUN
) {
2583 processor_t processor
= thread
->last_processor
;
2585 if (thread
== current_thread()) {
2586 ast_t preempt
= csw_check(thread
, processor
);
2588 if (preempt
!= AST_NONE
)
2590 processor
->current_pri
= priority
;
2593 if ( processor
!= PROCESSOR_NULL
&&
2594 processor
->active_thread
== thread
)
2595 cause_ast_check(processor
);
2609 if (rq
!= thread
->runq
)
2610 panic("run_queue_check: thread runq");
2612 if (thread
->sched_pri
> MAXPRI
|| thread
->sched_pri
< MINPRI
)
2613 panic("run_queue_check: thread sched_pri");
2615 q
= &rq
->queues
[thread
->sched_pri
];
2616 qe
= queue_first(q
);
2617 while (!queue_end(q
, qe
)) {
2618 if (qe
== (queue_entry_t
)thread
)
2621 qe
= queue_next(qe
);
2624 panic("run_queue_check: end");
2632 * Remove a thread from a current run queue and
2633 * return TRUE if successful.
2635 * Thread must be locked.
2641 processor_t processor
= thread
->runq
;
2644 * If processor is PROCESSOR_NULL, the thread will stay out of the
2645 * run queues because the caller locked the thread. Otherwise
2646 * the thread is on a run queue, but could be chosen for dispatch
2649 if (processor
!= PROCESSOR_NULL
) {
2654 * The processor run queues are locked by the
2655 * processor set. Real-time priorities use a
2656 * global queue with a dedicated lock.
2658 if (thread
->sched_pri
< BASEPRI_RTQUEUES
) {
2659 rqlock
= &processor
->processor_set
->sched_lock
;
2660 rq
= &processor
->runq
;
2663 rqlock
= &rt_lock
; rq
= &rt_runq
;
2666 simple_lock(rqlock
);
2668 if (processor
== thread
->runq
) {
2670 * Thread is on a run queue and we have a lock on
2673 remqueue(&rq
->queues
[0], (queue_entry_t
)thread
);
2675 if (testbit(thread
->sched_pri
, sched_preempt_pri
)) {
2676 rq
->urgency
--; assert(rq
->urgency
>= 0);
2679 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
2680 /* update run queue status */
2681 if (thread
->sched_pri
!= IDLEPRI
)
2682 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
2683 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2686 thread
->runq
= PROCESSOR_NULL
;
2690 * The thread left the run queue before we could
2691 * lock the run queue.
2693 assert(thread
->runq
== PROCESSOR_NULL
);
2694 processor
= PROCESSOR_NULL
;
2697 simple_unlock(rqlock
);
2700 return (processor
!= PROCESSOR_NULL
);
2706 * Choose a thread to execute from the run queues
2709 * Called with pset scheduling lock and rt lock held,
2710 * released on return.
2714 processor_t processor
)
2716 processor_set_t pset
= processor
->processor_set
;
2719 if (processor
->runq
.count
> 0 && processor
->runq
.highq
>= rt_runq
.highq
) {
2720 simple_unlock(&rt_lock
);
2722 thread
= run_queue_dequeue(&processor
->runq
, SCHED_HEADQ
);
2724 pset_pri_hint(pset
, processor
, thread
->sched_pri
);
2726 processor
->deadline
= UINT64_MAX
;
2732 thread
= run_queue_dequeue(&rt_runq
, SCHED_HEADQ
);
2733 simple_unlock(&rt_lock
);
2735 processor
->deadline
= thread
->realtime
.deadline
;
2742 * steal_processor_thread:
2744 * Locate a thread to steal from the processor and
2747 * Associated pset must be locked. Returns THREAD_NULL
2751 steal_processor_thread(
2752 processor_t processor
)
2754 run_queue_t rq
= &processor
->runq
;
2755 queue_t queue
= rq
->queues
+ rq
->highq
;
2756 int pri
= rq
->highq
, count
= rq
->count
;
2760 thread
= (thread_t
)queue_first(queue
);
2761 while (!queue_end(queue
, (queue_entry_t
)thread
)) {
2762 if (thread
->bound_processor
!= processor
) {
2763 remqueue(queue
, (queue_entry_t
)thread
);
2765 thread
->runq
= PROCESSOR_NULL
;
2767 if (testbit(pri
, sched_preempt_pri
)) {
2768 rq
->urgency
--; assert(rq
->urgency
>= 0);
2770 if (queue_empty(queue
)) {
2772 clrbit(MAXPRI
- pri
, rq
->bitmap
);
2773 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
2780 thread
= (thread_t
)queue_next((queue_entry_t
)thread
);
2786 return (THREAD_NULL
);
2790 * Locate and steal a thread, beginning
2793 * The pset must be locked, and is returned
2796 * Returns the stolen thread, or THREAD_NULL on
2801 processor_set_t pset
)
2803 processor_set_t nset
, cset
= pset
;
2804 processor_t processor
;
2808 processor
= (processor_t
)queue_first(&cset
->active_queue
);
2809 while (!queue_end(&cset
->active_queue
, (queue_entry_t
)processor
)) {
2810 if (processor
->runq
.count
> 0) {
2811 thread
= steal_processor_thread(processor
);
2812 if (thread
!= THREAD_NULL
) {
2813 remqueue(&cset
->active_queue
, (queue_entry_t
)processor
);
2814 enqueue_tail(&cset
->active_queue
, (queue_entry_t
)processor
);
2816 processor
->deadline
= UINT64_MAX
;
2823 processor
= (processor_t
)queue_next((queue_entry_t
)processor
);
2826 nset
= next_pset(cset
);
2834 } while (nset
!= pset
);
2838 return (THREAD_NULL
);
2842 * This is the processor idle loop, which just looks for other threads
2843 * to execute. Processor idle threads invoke this without supplying a
2844 * current thread to idle without an asserted wait state.
2846 * Returns a the next thread to execute if dispatched directly.
2851 processor_t processor
)
2853 processor_set_t pset
= processor
->processor_set
;
2854 thread_t new_thread
;
2860 pmsDown(); /* Step power down */
2863 KERNEL_DEBUG_CONSTANT(
2864 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_START
, (int)thread
, 0, 0, 0, 0);
2866 timer_switch(&PROCESSOR_DATA(processor
, system_state
),
2867 mach_absolute_time(), &PROCESSOR_DATA(processor
, idle_state
));
2868 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, idle_state
);
2870 while (processor
->next_thread
== THREAD_NULL
&& processor
->runq
.count
== 0 && rt_runq
.count
== 0 &&
2871 (thread
== THREAD_NULL
|| ((thread
->state
& (TH_WAIT
|TH_SUSP
)) == TH_WAIT
&& !thread
->wake_active
))) {
2877 timer_switch(&PROCESSOR_DATA(processor
, idle_state
),
2878 mach_absolute_time(), &PROCESSOR_DATA(processor
, system_state
));
2879 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
2884 pmsStep(0); /* Step up out of idle power */
2887 state
= processor
->state
;
2888 if (state
== PROCESSOR_DISPATCHING
) {
2890 * Commmon case -- cpu dispatched.
2892 new_thread
= processor
->next_thread
;
2893 processor
->next_thread
= THREAD_NULL
;
2894 processor
->state
= PROCESSOR_RUNNING
;
2896 if ( processor
->runq
.highq
> new_thread
->sched_pri
||
2897 (rt_runq
.highq
> 0 && rt_runq
.highq
>= new_thread
->sched_pri
) ) {
2898 processor
->deadline
= UINT64_MAX
;
2902 thread_lock(new_thread
);
2903 thread_setrun(new_thread
, SCHED_HEADQ
);
2904 thread_unlock(new_thread
);
2906 KERNEL_DEBUG_CONSTANT(
2907 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, 0, 0, 0);
2909 return (THREAD_NULL
);
2914 KERNEL_DEBUG_CONSTANT(
2915 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, (int)new_thread
, 0, 0);
2917 return (new_thread
);
2920 if (state
== PROCESSOR_IDLE
) {
2921 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
2924 processor
->state
= PROCESSOR_RUNNING
;
2925 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
2928 if (state
== PROCESSOR_SHUTDOWN
) {
2930 * Going off-line. Force a
2933 if ((new_thread
= processor
->next_thread
) != THREAD_NULL
) {
2934 processor
->next_thread
= THREAD_NULL
;
2935 processor
->deadline
= UINT64_MAX
;
2939 thread_lock(new_thread
);
2940 thread_setrun(new_thread
, SCHED_HEADQ
);
2941 thread_unlock(new_thread
);
2943 KERNEL_DEBUG_CONSTANT(
2944 MACHDBG_CODE(DBG_MACH_SCHED
,MACH_IDLE
) | DBG_FUNC_END
, (int)thread
, (int)state
, 0, 0, 0);
2946 return (THREAD_NULL
);
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
);
2959 * Each processor has a dedicated thread which
2960 * executes the idle loop when there is no suitable
2966 processor_t processor
= current_processor();
2967 thread_t new_thread
;
2969 new_thread
= processor_idle(THREAD_NULL
, processor
);
2970 if (new_thread
!= THREAD_NULL
) {
2971 thread_run(processor
->idle_thread
, (thread_continue_t
)idle_thread
, NULL
, new_thread
);
2975 thread_block((thread_continue_t
)idle_thread
);
2981 processor_t processor
)
2983 kern_return_t result
;
2987 result
= kernel_thread_create((thread_continue_t
)idle_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
2988 if (result
!= KERN_SUCCESS
)
2992 thread_lock(thread
);
2993 thread
->bound_processor
= processor
;
2994 processor
->idle_thread
= thread
;
2995 thread
->sched_pri
= thread
->priority
= IDLEPRI
;
2996 thread
->state
= (TH_RUN
| TH_IDLE
);
2997 thread_unlock(thread
);
3000 thread_deallocate(thread
);
3002 return (KERN_SUCCESS
);
3005 static uint64_t sched_tick_deadline
;
3010 * Kicks off scheduler services.
3012 * Called at splsched.
3017 kern_return_t result
;
3020 result
= kernel_thread_start_priority((thread_continue_t
)sched_tick_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
3021 if (result
!= KERN_SUCCESS
)
3022 panic("sched_startup");
3024 thread_deallocate(thread
);
3027 * Yield to the sched_tick_thread while it times
3028 * a series of context switches back. It stores
3029 * the baseline value in sched_cswtime.
3031 * The current thread is the only other thread
3032 * active at this point.
3034 while (sched_cswtime
== 0)
3035 thread_block(THREAD_CONTINUE_NULL
);
3037 thread_daemon_init();
3039 thread_call_initialize();
3043 * sched_tick_thread:
3045 * Perform periodic bookkeeping functions about ten
3049 sched_tick_continue(void)
3051 uint64_t abstime
= mach_absolute_time();
3056 * Compute various averages.
3061 * Scan the run queues for threads which
3062 * may need to be updated.
3064 thread_update_scan();
3066 clock_deadline_for_periodic_event(sched_tick_interval
, abstime
,
3067 &sched_tick_deadline
);
3069 assert_wait_deadline((event_t
)sched_tick_thread
, THREAD_UNINT
, sched_tick_deadline
);
3070 thread_block((thread_continue_t
)sched_tick_continue
);
3075 * Time a series of context switches to determine
3076 * a baseline. Toss the high and low and return
3077 * the one-way value.
3082 uint32_t new, hi
, low
, accum
;
3086 accum
= hi
= low
= 0;
3087 for (i
= 0; i
< tries
; ++i
) {
3088 abstime
= mach_absolute_time();
3089 thread_block(THREAD_CONTINUE_NULL
);
3091 new = mach_absolute_time() - abstime
;
3094 accum
= hi
= low
= new;
3105 return ((accum
- hi
- low
) / (2 * (tries
- 2)));
3109 sched_tick_thread(void)
3111 sched_cswtime
= time_cswitch();
3113 sched_tick_deadline
= mach_absolute_time();
3115 sched_tick_continue();
3120 * thread_update_scan / runq_scan:
3122 * Scan the run queues to account for timesharing threads
3123 * which need to be updated.
3125 * Scanner runs in two passes. Pass one squirrels likely
3126 * threads away in an array, pass two does the update.
3128 * This is necessary because the run queue is locked for
3129 * the candidate scan, but the thread is locked for the update.
3131 * Array should be sized to make forward progress, without
3132 * disabling preemption for long periods.
3135 #define THREAD_UPDATE_SIZE 128
3137 static thread_t thread_update_array
[THREAD_UPDATE_SIZE
];
3138 static int thread_update_count
= 0;
3141 * Scan a runq for candidate threads.
3143 * Returns TRUE if retry is needed.
3151 register thread_t thread
;
3153 if ((count
= runq
->count
) > 0) {
3154 q
= runq
->queues
+ runq
->highq
;
3156 queue_iterate(q
, thread
, thread_t
, links
) {
3157 if ( thread
->sched_stamp
!= sched_tick
&&
3158 (thread
->sched_mode
& TH_MODE_TIMESHARE
) ) {
3159 if (thread_update_count
== THREAD_UPDATE_SIZE
)
3162 thread_update_array
[thread_update_count
++] = thread
;
3163 thread_reference_internal(thread
);
3177 thread_update_scan(void)
3179 boolean_t restart_needed
= FALSE
;
3180 processor_t processor
= processor_list
;
3181 processor_set_t pset
;
3187 pset
= processor
->processor_set
;
3192 restart_needed
= runq_scan(&processor
->runq
);
3200 thread
= processor
->idle_thread
;
3201 if (thread
!= THREAD_NULL
&& thread
->sched_stamp
!= sched_tick
) {
3202 if (thread_update_count
== THREAD_UPDATE_SIZE
) {
3203 restart_needed
= TRUE
;
3207 thread_update_array
[thread_update_count
++] = thread
;
3208 thread_reference_internal(thread
);
3210 } while ((processor
= processor
->processor_list
) != NULL
);
3213 * Ok, we now have a collection of candidates -- fix them.
3215 while (thread_update_count
> 0) {
3216 thread
= thread_update_array
[--thread_update_count
];
3217 thread_update_array
[thread_update_count
] = THREAD_NULL
;
3220 thread_lock(thread
);
3221 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
3222 thread
->sched_stamp
!= sched_tick
)
3223 update_priority(thread
);
3224 thread_unlock(thread
);
3227 thread_deallocate(thread
);
3229 } while (restart_needed
);
3233 * Just in case someone doesn't use the macro
3235 #undef thread_wakeup
3244 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
3248 preemption_enabled(void)
3250 return (get_preemption_level() == 0 && ml_get_interrupts_enabled());
3258 return ((thread
->state
& (TH_RUN
|TH_WAIT
)) == TH_RUN
);
3263 #include <ddb/db_output.h>
3264 #define printf kdbprintf
3265 void db_sched(void);
3270 iprintf("Scheduling Statistics:\n");
3272 iprintf("Thread invocations: csw %d same %d\n",
3273 c_thread_invoke_csw
, c_thread_invoke_same
);
3275 iprintf("Thread block: calls %d\n",
3276 c_thread_block_calls
);
3277 iprintf("Idle thread:\n\thandoff %d block %d\n",
3278 c_idle_thread_handoff
,
3279 c_idle_thread_block
);
3280 iprintf("Sched thread blocks: %d\n", c_sched_thread_block
);
3281 #endif /* MACH_COUNTERS */
3285 #include <ddb/db_output.h>
3286 void db_show_thread_log(void);
3289 db_show_thread_log(void)
3292 #endif /* MACH_KDB */