2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * @OSF_FREE_COPYRIGHT@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
54 * Author: Avadis Tevanian, Jr.
57 * Scheduling primitives
64 #include <simple_clock.h>
65 #include <power_save.h>
66 #include <task_swapper.h>
68 #include <ddb/db_output.h>
69 #include <mach/machine.h>
70 #include <machine/machine_routines.h>
71 #include <machine/sched_param.h>
73 #include <kern/clock.h>
74 #include <kern/counters.h>
75 #include <kern/cpu_number.h>
76 #include <kern/cpu_data.h>
77 #include <kern/etap_macros.h>
78 #include <kern/lock.h>
79 #include <kern/macro_help.h>
80 #include <kern/machine.h>
81 #include <kern/misc_protos.h>
82 #include <kern/processor.h>
83 #include <kern/queue.h>
84 #include <kern/sched.h>
85 #include <kern/sched_prim.h>
86 #include <kern/syscall_subr.h>
87 #include <kern/task.h>
88 #include <kern/thread.h>
89 #include <kern/thread_swap.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_map.h>
93 #include <mach/policy.h>
94 #include <mach/sync_policy.h>
96 #include <kern/mk_sp.h> /*** ??? fix so this can be removed ***/
97 #include <sys/kdebug.h>
100 #include <kern/task_swap.h>
101 extern int task_swap_on
;
102 #endif /* TASK_SWAPPER */
106 #define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
107 int default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
109 #define NO_KERNEL_PREEMPT 0
110 #define KERNEL_PREEMPT 1
111 int kernel_preemption_mode
= KERNEL_PREEMPT
;
114 natural_t min_quantum_ms
;
120 #endif /* SIMPLE_CLOCK */
123 void thread_continue(thread_t
);
125 void wait_queues_init(void);
132 thread_t
choose_pset_thread(
133 processor_t myprocessor
,
134 processor_set_t pset
);
136 thread_t
choose_thread(
137 processor_t myprocessor
);
139 int run_queue_enqueue(
144 void idle_thread_continue(void);
145 void do_thread_scan(void);
147 void clear_wait_internal(
152 void dump_run_queues(
154 void dump_run_queue_struct(
158 void dump_processor_set(
170 boolean_t
thread_runnable(
176 * states are combinations of:
178 * W waiting (or on wait queue)
179 * N non-interruptible
184 * assert_wait thread_block clear_wait swapout swapin
186 * R RW, RWN R; setrun - -
187 * RN RWN RN; setrun - -
200 * Waiting protocols and implementation:
202 * Each thread may be waiting for exactly one event; this event
203 * is set using assert_wait(). That thread may be awakened either
204 * by performing a thread_wakeup_prim() on its event,
205 * or by directly waking that thread up with clear_wait().
207 * The implementation of wait events uses a hash table. Each
208 * bucket is queue of threads having the same hash function
209 * value; the chain for the queue (linked list) is the run queue
210 * field. [It is not possible to be waiting and runnable at the
213 * Locks on both the thread and on the hash buckets govern the
214 * wait event field and the queue chain field. Because wakeup
215 * operations only have the event as an argument, the event hash
216 * bucket must be locked before any thread.
218 * Scheduling operations may also occur at interrupt level; therefore,
219 * interrupts below splsched() must be prevented when holding
220 * thread or hash bucket locks.
222 * The wait event hash table declarations are as follows:
227 struct wait_queue wait_queues
[NUMQUEUES
];
229 #define wait_hash(event) \
230 ((((int)(event) < 0)? ~(int)(event): (int)(event)) % NUMQUEUES)
236 * Calculate the minimum quantum
239 if (default_preemption_rate
< 1)
240 default_preemption_rate
= DEFAULT_PREEMPTION_RATE
;
241 min_quantum
= hz
/ default_preemption_rate
;
244 * Round up result (4/5) to an
245 * integral number of ticks.
247 if (((hz
* 10) / default_preemption_rate
) - (min_quantum
* 10) >= 5)
252 min_quantum_ms
= (1000 / hz
) * min_quantum
;
254 printf("scheduling quantum is %d ms\n", min_quantum_ms
);
257 pset_sys_bootstrap(); /* initialize processor mgmt. */
262 #endif /* SIMPLE_CLOCK */
268 wait_queues_init(void)
272 for (i
= 0; i
< NUMQUEUES
; i
++) {
273 wait_queue_init(&wait_queues
[i
], SYNC_POLICY_FIFO
);
278 * Thread timeout routine, called when timer expires.
282 timer_call_param_t p0
,
283 timer_call_param_t p1
)
285 thread_t thread
= p0
;
290 if ( thread
->wait_timer_is_set
&&
291 !timer_call_is_delayed(&thread
->wait_timer
, NULL
) ) {
292 thread
->wait_timer_active
--;
293 thread
->wait_timer_is_set
= FALSE
;
296 clear_wait_internal(thread
, THREAD_TIMED_OUT
);
297 thread_unlock(thread
);
300 if (--thread
->wait_timer_active
== 0)
301 thread_wakeup_one(&thread
->wait_timer_active
);
309 * Set a timer for the current thread, if the thread
310 * is ready to wait. Must be called between assert_wait()
311 * and thread_block().
316 natural_t scale_factor
)
318 thread_t thread
= current_thread();
319 AbsoluteTime deadline
;
325 if ((thread
->state
& TH_WAIT
) != 0) {
326 clock_interval_to_deadline(interval
, scale_factor
, &deadline
);
327 timer_call_enter(&thread
->wait_timer
, deadline
);
328 assert(!thread
->wait_timer_is_set
);
329 thread
->wait_timer_active
++;
330 thread
->wait_timer_is_set
= TRUE
;
332 thread_unlock(thread
);
338 thread_set_timer_deadline(
339 AbsoluteTime deadline
)
341 thread_t thread
= current_thread();
347 if ((thread
->state
& TH_WAIT
) != 0) {
348 timer_call_enter(&thread
->wait_timer
, deadline
);
349 assert(!thread
->wait_timer_is_set
);
350 thread
->wait_timer_active
++;
351 thread
->wait_timer_is_set
= TRUE
;
353 thread_unlock(thread
);
359 thread_cancel_timer(void)
361 thread_t thread
= current_thread();
366 if (thread
->wait_timer_is_set
) {
367 if (timer_call_cancel(&thread
->wait_timer
))
368 thread
->wait_timer_active
--;
369 thread
->wait_timer_is_set
= FALSE
;
376 * thread_depress_timeout:
378 * Timeout routine for priority depression.
381 thread_depress_timeout(
382 thread_call_param_t p0
,
383 thread_call_param_t p1
)
385 thread_t thread
= p0
;
386 sched_policy_t
*policy
;
391 policy
= policy_id_to_sched_policy(thread
->policy
);
392 thread_unlock(thread
);
395 if (policy
!= SCHED_POLICY_NULL
)
396 policy
->sp_ops
.sp_thread_depress_timeout(policy
, thread
);
398 thread_deallocate(thread
);
402 * Set up thread timeout element when thread is created.
408 timer_call_setup(&thread
->wait_timer
, thread_timer_expire
, thread
);
409 thread
->wait_timer_is_set
= FALSE
;
410 thread
->wait_timer_active
= 1;
413 thread_call_setup(&thread
->depress_timer
, thread_depress_timeout
, thread
);
417 thread_timer_terminate(void)
419 thread_t thread
= current_thread();
424 if (thread
->wait_timer_is_set
) {
425 if (timer_call_cancel(&thread
->wait_timer
))
426 thread
->wait_timer_active
--;
427 thread
->wait_timer_is_set
= FALSE
;
430 thread
->wait_timer_active
--;
432 while (thread
->wait_timer_active
> 0) {
433 assert_wait((event_t
)&thread
->wait_timer_active
, THREAD_UNINT
);
437 thread_block((void (*)(void)) 0);
446 thread_deallocate(thread
);
450 * Routine: thread_go_locked
452 * Start a thread running.
454 * thread lock held, IPC locks may be held.
455 * thread must have been pulled from wait queue under same lock hold.
463 sched_policy_t
*policy
;
466 assert(thread
->at_safe_point
== FALSE
);
467 assert(thread
->wait_event
== NO_EVENT
);
468 assert(thread
->wait_queue
== WAIT_QUEUE_NULL
);
470 if (thread
->state
& TH_WAIT
) {
472 thread
->state
&= ~(TH_WAIT
|TH_UNINT
);
473 if (!(thread
->state
& TH_RUN
)) {
474 thread
->state
|= TH_RUN
;
476 if (thread
->state
& TH_SWAPPED_OUT
)
477 thread_swapin(thread
->top_act
, FALSE
);
479 #endif /* THREAD_SWAPPER */
481 policy
= &sched_policy
[thread
->policy
];
482 sfr
= policy
->sp_ops
.sp_thread_unblock(policy
, thread
);
483 assert(sfr
== SF_SUCCESS
);
486 thread
->wait_result
= result
;
491 * The next few lines are a major hack. Hopefully this will get us
492 * around all of the scheduling framework hooha. We can't call
493 * sp_thread_unblock yet because we could still be finishing up the
494 * durn two stage block on another processor and thread_setrun
495 * could be called by s_t_u and we'll really be messed up then.
497 /* Don't mess with this if we are still swapped out */
498 if (!(thread
->state
& TH_SWAPPED_OUT
))
499 thread
->sp_state
= MK_SP_RUNNABLE
;
504 thread_mark_wait_locked(
509 assert(thread
== current_thread());
511 thread
->wait_result
= -1; /* JMM - Needed for non-assert kernel */
512 thread
->state
|= (interruptible
&& thread
->interruptible
) ?
513 TH_WAIT
: (TH_WAIT
| TH_UNINT
);
514 thread
->at_safe_point
= (interruptible
== THREAD_ABORTSAFE
) && (thread
->interruptible
);
515 thread
->sleep_stamp
= sched_tick
;
521 * Routine: assert_wait_timeout
523 * Assert that the thread intends to block,
524 * waiting for a timeout (no user known event).
526 unsigned int assert_wait_timeout_event
;
530 mach_msg_timeout_t msecs
,
535 assert_wait((event_t
)&assert_wait_timeout_event
, interruptible
);
536 thread_set_timer(msecs
, 1000*NSEC_PER_USEC
);
540 * Check to see if an assert wait is possible, without actually doing one.
541 * This is used by debug code in locks and elsewhere to verify that it is
542 * always OK to block when trying to take a blocking lock (since waiting
543 * for the actual assert_wait to catch the case may make it hard to detect
547 assert_wait_possible(void)
551 extern unsigned int debug_mode
;
554 if(debug_mode
) return TRUE
; /* Always succeed in debug mode */
557 thread
= current_thread();
559 return (thread
== NULL
|| wait_queue_assert_possible(thread
));
565 * Assert that the current thread is about to go to
566 * sleep until the specified event occurs.
573 register wait_queue_t wq
;
576 assert(event
!= NO_EVENT
);
577 assert(assert_wait_possible());
579 index
= wait_hash(event
);
580 wq
= &wait_queues
[index
];
581 wait_queue_assert_wait(wq
,
588 * thread_[un]stop(thread)
589 * Once a thread has blocked interruptibly (via assert_wait) prevent
590 * it from running until thread_unstop.
592 * If someone else has already stopped the thread, wait for the
593 * stop to be cleared, and then stop it again.
595 * Return FALSE if interrupted.
597 * NOTE: thread_hold/thread_suspend should be called on the activation
598 * before calling thread_stop. TH_SUSP is only recognized when
599 * a thread blocks and only prevents clear_wait/thread_wakeup
600 * from restarting an interruptible wait. The wake_active flag is
601 * used to indicate that someone is waiting on the thread.
612 while (thread
->state
& TH_SUSP
) {
613 thread
->wake_active
= TRUE
;
614 assert_wait((event_t
)&thread
->wake_active
, THREAD_ABORTSAFE
);
618 thread_block((void (*)(void)) 0);
619 if (current_thread()->wait_result
!= THREAD_AWAKENED
)
626 thread
->state
|= TH_SUSP
;
627 thread_unlock(thread
);
636 * Clear TH_SUSP and if the thread has been stopped and is now runnable,
637 * put it back on the run queue.
643 sched_policy_t
*policy
;
651 if ((thread
->state
& (TH_RUN
|TH_WAIT
|TH_SUSP
/*|TH_UNINT*/)) == TH_SUSP
) {
652 thread
->state
= (thread
->state
& ~TH_SUSP
) | TH_RUN
;
654 if (thread
->state
& TH_SWAPPED_OUT
)
655 thread_swapin(thread
->top_act
, FALSE
);
657 #endif /* THREAD_SWAPPER */
659 policy
= &sched_policy
[thread
->policy
];
660 sfr
= policy
->sp_ops
.sp_thread_unblock(policy
, thread
);
661 assert(sfr
== SF_SUCCESS
);
665 if (thread
->state
& TH_SUSP
) {
666 thread
->state
&= ~TH_SUSP
;
668 if (thread
->wake_active
) {
669 thread
->wake_active
= FALSE
;
670 thread_unlock(thread
);
673 thread_wakeup((event_t
)&thread
->wake_active
);
679 thread_unlock(thread
);
685 * Wait for the thread's RUN bit to clear
696 while (thread
->state
& (TH_RUN
/*|TH_UNINT*/)) {
697 if (thread
->last_processor
!= PROCESSOR_NULL
)
698 cause_ast_check(thread
->last_processor
);
700 thread
->wake_active
= TRUE
;
701 assert_wait((event_t
)&thread
->wake_active
, THREAD_ABORTSAFE
);
705 thread_block((void (*)(void))0);
706 if (current_thread()->wait_result
!= THREAD_AWAKENED
)
721 * thread_stop_wait(thread)
722 * Stop the thread then wait for it to block interruptibly
728 if (thread_stop(thread
)) {
729 if (thread_wait(thread
))
732 thread_unstop(thread
);
740 * Routine: clear_wait_internal
742 * Clear the wait condition for the specified thread.
743 * Start the thread executing if that is appropriate.
745 * thread thread to awaken
746 * result Wakeup result the thread should see
749 * the thread is locked.
757 * If the thread isn't in a wait queue, just set it running. Otherwise,
758 * try to remove it from the queue and, if successful, then set it
759 * running. NEVER interrupt an uninterruptible thread.
761 if (!((result
== THREAD_INTERRUPTED
) && (thread
->state
& TH_UNINT
))) {
762 if (wait_queue_assert_possible(thread
) ||
763 (wait_queue_remove(thread
) == KERN_SUCCESS
)) {
764 thread_go_locked(thread
, result
);
773 * Clear the wait condition for the specified thread. Start the thread
774 * executing if that is appropriate.
777 * thread thread to awaken
778 * result Wakeup result the thread should see
789 clear_wait_internal(thread
, result
);
790 thread_unlock(thread
);
796 * thread_wakeup_prim:
798 * Common routine for thread_wakeup, thread_wakeup_with_result,
799 * and thread_wakeup_one.
805 boolean_t one_thread
,
808 register wait_queue_t wq
;
811 index
= wait_hash(event
);
812 wq
= &wait_queues
[index
];
814 wait_queue_wakeup_one(wq
, event
, result
);
816 wait_queue_wakeup_all(wq
, event
, result
);
822 * Force a thread to execute on the specified processor.
823 * If the thread is currently executing, it may wait until its
824 * time slice is up before switching onto the specified processor.
826 * A processor of PROCESSOR_NULL causes the thread to be unbound.
827 * xxx - DO NOT export this to users.
831 register thread_t thread
,
832 processor_t processor
)
838 thread_bind_locked(thread
, processor
);
839 thread_unlock(thread
);
844 * Select a thread for this processor (the current processor) to run.
845 * May select the current thread, which must already be locked.
849 register processor_t myprocessor
)
851 register thread_t thread
;
852 processor_set_t pset
;
853 register run_queue_t runq
= &myprocessor
->runq
;
854 boolean_t other_runnable
;
855 sched_policy_t
*policy
;
858 * Check for other non-idle runnable threads.
860 myprocessor
->first_quantum
= TRUE
;
861 pset
= myprocessor
->processor_set
;
862 thread
= current_thread();
865 thread
->unconsumed_quantum
= myprocessor
->quantum
;
868 simple_lock(&runq
->lock
);
869 simple_lock(&pset
->runq
.lock
);
871 other_runnable
= runq
->count
> 0 || pset
->runq
.count
> 0;
873 if ( thread
->state
== TH_RUN
&&
875 (runq
->highq
< thread
->sched_pri
&&
876 pset
->runq
.highq
< thread
->sched_pri
)) &&
877 thread
->processor_set
== pset
&&
878 (thread
->bound_processor
== PROCESSOR_NULL
||
879 thread
->bound_processor
== myprocessor
) ) {
881 /* I am the highest priority runnable (non-idle) thread */
882 simple_unlock(&pset
->runq
.lock
);
883 simple_unlock(&runq
->lock
);
885 /* Update the thread's meta-priority */
886 policy
= policy_id_to_sched_policy(thread
->policy
);
887 assert(policy
!= SCHED_POLICY_NULL
);
888 (void)policy
->sp_ops
.sp_thread_update_mpri(policy
, thread
);
891 if (other_runnable
) {
892 simple_unlock(&pset
->runq
.lock
);
893 simple_unlock(&runq
->lock
);
894 thread
= choose_thread(myprocessor
);
897 simple_unlock(&pset
->runq
.lock
);
898 simple_unlock(&runq
->lock
);
901 * Nothing is runnable, so set this processor idle if it
902 * was running. If it was in an assignment or shutdown,
903 * leave it alone. Return its idle thread.
905 simple_lock(&pset
->idle_lock
);
906 if (myprocessor
->state
== PROCESSOR_RUNNING
) {
907 myprocessor
->state
= PROCESSOR_IDLE
;
909 * XXX Until it goes away, put master on end of queue, others
910 * XXX on front so master gets used last.
912 if (myprocessor
== master_processor
)
913 queue_enter(&(pset
->idle_queue
), myprocessor
,
914 processor_t
, processor_queue
);
916 queue_enter_first(&(pset
->idle_queue
), myprocessor
,
917 processor_t
, processor_queue
);
921 simple_unlock(&pset
->idle_lock
);
923 thread
= myprocessor
->idle_thread
;
931 * Stop running the current thread and start running the new thread.
932 * If continuation is non-zero, and the current thread is blocked,
933 * then it will resume by executing continuation on a new stack.
934 * Returns TRUE if the hand-off succeeds.
935 * The reason parameter == AST_QUANTUM if the thread blocked
936 * because its quantum expired.
942 __current_thread(void)
944 return (current_thread());
949 register thread_t old_thread
,
950 register thread_t new_thread
,
952 void (*continuation
)(void))
954 sched_policy_t
*policy
;
959 * Mark thread interruptible.
961 thread_lock(new_thread
);
962 new_thread
->state
&= ~TH_UNINT
;
964 if (cpu_data
[cpu_number()].preemption_level
!= 1)
965 panic("thread_invoke: preemption_level %d\n",
966 cpu_data
[cpu_number()].preemption_level
);
969 assert(thread_runnable(new_thread
));
971 assert(old_thread
->continuation
== (void (*)(void))0);
973 if ((old_thread
->sched_mode
& TH_MODE_REALTIME
) && (!old_thread
->stack_privilege
)) {
974 old_thread
->stack_privilege
= old_thread
->kernel_stack
;
977 if (continuation
!= (void (*)()) 0) {
978 switch (new_thread
->state
& TH_STACK_STATE
) {
979 case TH_STACK_HANDOFF
:
982 * If the old thread has stack privilege, we can't give
983 * his stack away. So go and get him one and treat this
984 * as a traditional context switch.
986 if (old_thread
->stack_privilege
== current_stack())
990 * Make the whole handoff/dispatch atomic to match the
993 disable_preemption();
996 * Set up ast context of new thread and switch to its timer.
998 new_thread
->state
&= ~(TH_STACK_HANDOFF
|TH_UNINT
);
999 new_thread
->last_processor
= current_processor();
1000 ast_context(new_thread
->top_act
, cpu_number());
1001 timer_switch(&new_thread
->system_timer
);
1002 thread_unlock(new_thread
);
1004 old_thread
->continuation
= continuation
;
1005 stack_handoff(old_thread
, new_thread
);
1007 wake_lock(old_thread
);
1008 thread_lock(old_thread
);
1009 act_machine_sv_free(old_thread
->top_act
);
1012 * inline thread_dispatch but don't free stack
1015 switch (old_thread
->state
& (TH_RUN
|TH_WAIT
|TH_UNINT
|TH_IDLE
)) {
1016 sched_policy_t
*policy
;
1019 case TH_RUN
| TH_UNINT
:
1022 * No reason to stop. Put back on a run queue.
1024 old_thread
->state
|= TH_STACK_HANDOFF
;
1026 /* Get pointer to scheduling policy "object" */
1027 policy
= &sched_policy
[old_thread
->policy
];
1029 /* Leave enqueueing thread up to scheduling policy */
1030 sfr
= policy
->sp_ops
.sp_thread_dispatch(policy
, old_thread
);
1031 assert(sfr
== SF_SUCCESS
);
1034 case TH_RUN
| TH_WAIT
| TH_UNINT
:
1035 case TH_RUN
| TH_WAIT
:
1036 old_thread
->sleep_stamp
= sched_tick
;
1039 case TH_WAIT
: /* this happens! */
1043 old_thread
->state
|= TH_STACK_HANDOFF
;
1044 old_thread
->state
&= ~TH_RUN
;
1045 if (old_thread
->state
& TH_TERMINATE
)
1046 thread_reaper_enqueue(old_thread
);
1048 if (old_thread
->wake_active
) {
1049 old_thread
->wake_active
= FALSE
;
1050 thread_unlock(old_thread
);
1051 wake_unlock(old_thread
);
1052 thread_wakeup((event_t
)&old_thread
->wake_active
);
1053 wake_lock(old_thread
);
1054 thread_lock(old_thread
);
1058 case TH_RUN
| TH_IDLE
:
1060 * Drop idle thread -- it is already in
1061 * idle_thread_array.
1063 old_thread
->state
|= TH_STACK_HANDOFF
;
1067 panic("State 0x%x \n",old_thread
->state
);
1070 /* Get pointer to scheduling policy "object" */
1071 policy
= &sched_policy
[old_thread
->policy
];
1073 /* Indicate to sched policy that old thread has stopped execution */
1074 /*** ??? maybe use a macro -- rkc, 1/4/96 ***/
1075 sfr
= policy
->sp_ops
.sp_thread_done(policy
, old_thread
);
1076 assert(sfr
== SF_SUCCESS
);
1077 thread_unlock(old_thread
);
1078 wake_unlock(old_thread
);
1079 thread_lock(new_thread
);
1081 assert(thread_runnable(new_thread
));
1083 /* Get pointer to scheduling policy "object" */
1084 policy
= &sched_policy
[new_thread
->policy
];
1086 /* Indicate to sched policy that new thread has started execution */
1087 /*** ??? maybe use a macro ***/
1088 sfr
= policy
->sp_ops
.sp_thread_begin(policy
, new_thread
);
1089 assert(sfr
== SF_SUCCESS
);
1091 lcont
= new_thread
->continuation
;
1092 new_thread
->continuation
= (void(*)(void))0;
1094 thread_unlock(new_thread
);
1095 enable_preemption();
1097 counter_always(c_thread_invoke_hits
++);
1099 if (new_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1100 kern_return_t save_wait_result
;
1101 new_thread
->funnel_state
= 0;
1102 save_wait_result
= new_thread
->wait_result
;
1103 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, new_thread
->funnel_lock
, 2, 0, 0, 0);
1104 //mutex_lock(new_thread->funnel_lock);
1105 funnel_lock(new_thread
->funnel_lock
);
1106 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
, new_thread
->funnel_lock
, 2, 0, 0, 0);
1107 new_thread
->funnel_state
= TH_FN_OWNED
;
1108 new_thread
->wait_result
= save_wait_result
;
1113 call_continuation(lcont
);
1117 case TH_STACK_COMING_IN
:
1119 * waiting for a stack
1121 thread_swapin(new_thread
);
1122 thread_unlock(new_thread
);
1123 counter_always(c_thread_invoke_misses
++);
1128 * already has a stack - can't handoff
1130 if (new_thread
== old_thread
) {
1132 /* same thread but with continuation */
1133 counter(++c_thread_invoke_same
);
1134 thread_unlock(new_thread
);
1136 if (old_thread
->funnel_state
& TH_FN_REFUNNEL
) {
1137 kern_return_t save_wait_result
;
1139 old_thread
->funnel_state
= 0;
1140 save_wait_result
= old_thread
->wait_result
;
1141 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, old_thread
->funnel_lock
, 3, 0, 0, 0);
1142 funnel_lock(old_thread
->funnel_lock
);
1143 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
, old_thread
->funnel_lock
, 3, 0, 0, 0);
1144 old_thread
->funnel_state
= TH_FN_OWNED
;
1145 old_thread
->wait_result
= save_wait_result
;
1148 call_continuation(continuation
);
1155 * check that the new thread has a stack
1157 if (new_thread
->state
& TH_STACK_STATE
) {
1159 /* has no stack. if not already waiting for one try to get one */
1160 if ((new_thread
->state
& TH_STACK_COMING_IN
) ||
1161 /* not already waiting. nonblocking try to get one */
1162 !stack_alloc_try(new_thread
, thread_continue
))
1164 /* couldn't get one. schedule new thread to get a stack and
1165 return failure so we can try another thread. */
1166 thread_swapin(new_thread
);
1167 thread_unlock(new_thread
);
1168 counter_always(c_thread_invoke_misses
++);
1171 } else if (old_thread
== new_thread
) {
1172 counter(++c_thread_invoke_same
);
1173 thread_unlock(new_thread
);
1177 /* new thread now has a stack. it has been setup to resume in
1178 thread_continue so it can dispatch the old thread, deal with
1179 funnelling and then go to it's true continuation point */
1182 new_thread
->state
&= ~(TH_STACK_HANDOFF
| TH_UNINT
);
1185 * Set up ast context of new thread and switch to its timer.
1187 new_thread
->last_processor
= current_processor();
1188 ast_context(new_thread
->top_act
, cpu_number());
1189 timer_switch(&new_thread
->system_timer
);
1190 assert(thread_runnable(new_thread
));
1193 * N.B. On return from the call to switch_context, 'old_thread'
1194 * points at the thread that yielded to us. Unfortunately, at
1195 * this point, there are no simple_locks held, so if we are preempted
1196 * before the call to thread_dispatch blocks preemption, it is
1197 * possible for 'old_thread' to terminate, leaving us with a
1198 * stale thread pointer.
1200 disable_preemption();
1202 thread_unlock(new_thread
);
1204 counter_always(c_thread_invoke_csw
++);
1205 current_task()->csw
++;
1208 thread_lock(old_thread
);
1209 old_thread
->reason
= reason
;
1210 assert(old_thread
->runq
== RUN_QUEUE_NULL
);
1212 if (continuation
!= (void (*)(void))0)
1213 old_thread
->continuation
= continuation
;
1215 /* Indicate to sched policy that old thread has stopped execution */
1216 policy
= &sched_policy
[old_thread
->policy
];
1217 /*** ??? maybe use a macro -- ***/
1218 sfr
= policy
->sp_ops
.sp_thread_done(policy
, old_thread
);
1219 assert(sfr
== SF_SUCCESS
);
1220 thread_unlock(old_thread
);
1223 * switch_context is machine-dependent. It does the
1224 * machine-dependent components of a context-switch, like
1225 * changing address spaces. It updates active_threads.
1227 old_thread
= switch_context(old_thread
, continuation
, new_thread
);
1229 /* Now on new thread's stack. Set a local variable to refer to it. */
1230 new_thread
= __current_thread();
1231 assert(old_thread
!= new_thread
);
1233 assert(thread_runnable(new_thread
));
1235 thread_lock(new_thread
);
1236 assert(thread_runnable(new_thread
));
1237 /* Indicate to sched policy that new thread has started execution */
1238 policy
= &sched_policy
[new_thread
->policy
];
1239 /*** ??? maybe use a macro -- rkc, 1/4/96 ***/
1240 sfr
= policy
->sp_ops
.sp_thread_begin(policy
, new_thread
);
1241 assert(sfr
== SF_SUCCESS
);
1242 thread_unlock(new_thread
);
1245 * We're back. Now old_thread is the thread that resumed
1246 * us, and we have to dispatch it.
1249 // Code from OSF in Grenoble deleted the following fields. They were
1250 // used in HPPA and 386 code, but not in the PPC for other than
1251 // just setting and resetting. They didn't delete these lines from
1252 // the MACH_RT builds, though, causing compile errors. I'm going
1253 // to make a wild guess and assume we can just delete these.
1255 if (old_thread
->preempt
== TH_NOT_PREEMPTABLE
) {
1257 * Mark that we have been really preempted
1259 old_thread
->preempt
= TH_PREEMPTED
;
1262 thread_dispatch(old_thread
);
1263 enable_preemption();
1265 /* if we get here and 'continuation' is set that means the
1266 * switch_context() path returned and did not call out
1267 * to the continuation. we will do it manually here */
1269 call_continuation(continuation
);
1279 * Called when the launching a new thread, at splsched();
1283 register thread_t old_thread
)
1285 register thread_t self
;
1286 register void (*continuation
)();
1287 sched_policy_t
*policy
;
1290 self
= current_thread();
1293 * We must dispatch the old thread and then
1294 * call the current thread's continuation.
1295 * There might not be an old thread, if we are
1296 * the first thread to run on this processor.
1298 if (old_thread
!= THREAD_NULL
) {
1299 thread_dispatch(old_thread
);
1303 /* Get pointer to scheduling policy "object" */
1304 policy
= &sched_policy
[self
->policy
];
1306 /* Indicate to sched policy that new thread has started execution */
1307 /*** ??? maybe use a macro -- rkc, 1/4/96 ***/
1308 sfr
= policy
->sp_ops
.sp_thread_begin(policy
,self
);
1309 assert(sfr
== SF_SUCCESS
);
1314 continuation
= self
->continuation
;
1315 self
->continuation
= (void (*)(void))0;
1316 thread_unlock(self
);
1319 * N.B. - the following is necessary, since thread_invoke()
1320 * inhibits preemption on entry and reenables before it
1321 * returns. Unfortunately, the first time a newly-created
1322 * thread executes, it magically appears here, and never
1323 * executes the enable_preemption() call in thread_invoke().
1325 enable_preemption();
1327 if (self
->funnel_state
& TH_FN_REFUNNEL
) {
1328 kern_return_t save_wait_result
;
1329 self
->funnel_state
= 0;
1330 save_wait_result
= self
->wait_result
;
1331 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, self
->funnel_lock
, 4, 0, 0, 0);
1332 funnel_lock(self
->funnel_lock
);
1333 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
, self
->funnel_lock
, 4, 0, 0, 0);
1334 self
->wait_result
= save_wait_result
;
1335 self
->funnel_state
= TH_FN_OWNED
;
1339 assert(continuation
);
1344 #if MACH_LDEBUG || MACH_KDB
1346 #define THREAD_LOG_SIZE 300
1360 } thread_log
[THREAD_LOG_SIZE
];
1362 int thread_log_index
;
1364 void check_thread_time(long n
);
1367 int check_thread_time_crash
;
1371 check_thread_time(long us
)
1375 if (!check_thread_time_crash
)
1378 temp
= thread_log
[0].stamp
;
1379 cyctm05_diff (&thread_log
[1].stamp
, &thread_log
[0].stamp
, &temp
);
1381 if (temp
.l
>= us
&& thread_log
[1].info
!= 0x49) /* HACK!!! */
1382 panic ("check_thread_time");
1387 log_thread_action(char * action
, long info1
, long info2
, long info3
)
1391 static unsigned int tstamp
;
1395 for (i
= THREAD_LOG_SIZE
-1; i
> 0; i
--) {
1396 thread_log
[i
] = thread_log
[i
-1];
1399 thread_log
[0].stamp
.h
= 0;
1400 thread_log
[0].stamp
.l
= tstamp
++;
1401 thread_log
[0].thread
= current_thread();
1402 thread_log
[0].info1
= info1
;
1403 thread_log
[0].info2
= info2
;
1404 thread_log
[0].info3
= info3
;
1405 thread_log
[0].action
= action
;
1406 /* strcpy (&thread_log[0].action[0], action);*/
1410 #endif /* MACH_LDEBUG || MACH_KDB */
1413 #include <ddb/db_output.h>
1414 void db_show_thread_log(void);
1417 db_show_thread_log(void)
1421 db_printf ("%s %s %s %s %s %s\n", " Thread ", " Info1 ", " Info2 ",
1422 " Info3 ", " Timestamp ", "Action");
1424 for (i
= 0; i
< THREAD_LOG_SIZE
; i
++) {
1425 db_printf ("%08x %08x %08x %08x %08x/%08x %s\n",
1426 thread_log
[i
].thread
,
1427 thread_log
[i
].info1
,
1428 thread_log
[i
].info2
,
1429 thread_log
[i
].info3
,
1430 thread_log
[i
].stamp
.h
,
1431 thread_log
[i
].stamp
.l
,
1432 thread_log
[i
].action
);
1435 #endif /* MACH_KDB */
1438 * thread_block_reason:
1440 * Block the current thread. If the thread is runnable
1441 * then someone must have woken it up between its request
1442 * to sleep and now. In this case, it goes back on a
1445 * If a continuation is specified, then thread_block will
1446 * attempt to discard the thread's kernel stack. When the
1447 * thread resumes, it will execute the continuation function
1448 * on a new kernel stack.
1450 counter(mach_counter_t c_thread_block_calls
= 0;)
1453 thread_block_reason(
1454 void (*continuation
)(void),
1457 register thread_t thread
= current_thread();
1458 register processor_t myprocessor
;
1459 register thread_t new_thread
;
1462 counter(++c_thread_block_calls
);
1464 check_simple_locks();
1466 machine_clock_assist();
1470 if ((thread
->funnel_state
& TH_FN_OWNED
) && !(reason
& AST_PREEMPT
)) {
1471 thread
->funnel_state
= TH_FN_REFUNNEL
;
1472 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE
, thread
->funnel_lock
, 2, 0, 0, 0);
1473 funnel_unlock(thread
->funnel_lock
);
1476 myprocessor
= current_processor();
1478 thread_lock(thread
);
1479 if (thread
->state
& TH_ABORT
)
1480 clear_wait_internal(thread
, THREAD_INTERRUPTED
);
1482 /* Unconditionally remove either | both */
1483 ast_off(AST_QUANTUM
|AST_BLOCK
|AST_URGENT
);
1485 new_thread
= thread_select(myprocessor
);
1487 assert(thread_runnable(new_thread
));
1488 thread_unlock(thread
);
1489 while (!thread_invoke(thread
, new_thread
, reason
, continuation
)) {
1490 thread_lock(thread
);
1491 new_thread
= thread_select(myprocessor
);
1493 assert(thread_runnable(new_thread
));
1494 thread_unlock(thread
);
1497 if (thread
->funnel_state
& TH_FN_REFUNNEL
) {
1498 kern_return_t save_wait_result
;
1500 save_wait_result
= thread
->wait_result
;
1501 thread
->funnel_state
= 0;
1502 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE
, thread
->funnel_lock
, 5, 0, 0, 0);
1503 funnel_lock(thread
->funnel_lock
);
1504 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE
, thread
->funnel_lock
, 5, 0, 0, 0);
1505 thread
->funnel_state
= TH_FN_OWNED
;
1506 thread
->wait_result
= save_wait_result
;
1511 return thread
->wait_result
;
1517 * Now calls thread_block_reason() which forwards the
1518 * the reason parameter to thread_invoke() so it can
1519 * do the right thing if the thread's quantum expired.
1523 void (*continuation
)(void))
1525 return thread_block_reason(continuation
, 0);
1531 * Switch directly from the current thread to a specified
1532 * thread. Both the current and new threads must be
1540 thread_t old_thread
,
1541 void (*continuation
)(void),
1542 thread_t new_thread
)
1544 while (!thread_invoke(old_thread
, new_thread
, 0, continuation
)) {
1545 register processor_t myprocessor
= current_processor();
1546 thread_lock(old_thread
);
1547 new_thread
= thread_select(myprocessor
);
1548 thread_unlock(old_thread
);
1550 return old_thread
->wait_result
;
1554 * Dispatches a running thread that is not on a runq.
1555 * Called at splsched.
1559 register thread_t thread
)
1561 sched_policy_t
*policy
;
1565 * If we are discarding the thread's stack, we must do it
1566 * before the thread has a chance to run.
1569 thread_lock(thread
);
1572 /* no continuations on i386 for now */
1573 if (thread
->continuation
!= (void (*)())0) {
1574 assert((thread
->state
& TH_STACK_STATE
) == 0);
1575 thread
->state
|= TH_STACK_HANDOFF
;
1577 if (thread
->top_act
) {
1578 act_machine_sv_free(thread
->top_act
);
1583 switch (thread
->state
& (TH_RUN
|TH_WAIT
|TH_UNINT
|TH_IDLE
)) {
1585 case TH_RUN
| TH_UNINT
:
1588 * No reason to stop. Put back on a run queue.
1590 /* Leave enqueueing thread up to scheduling policy */
1591 policy
= &sched_policy
[thread
->policy
];
1592 /*** ??? maybe use a macro ***/
1593 sfr
= policy
->sp_ops
.sp_thread_dispatch(policy
, thread
);
1594 assert(sfr
== SF_SUCCESS
);
1597 case TH_RUN
| TH_WAIT
| TH_UNINT
:
1598 case TH_RUN
| TH_WAIT
:
1599 thread
->sleep_stamp
= sched_tick
;
1601 case TH_WAIT
: /* this happens! */
1606 thread
->state
&= ~TH_RUN
;
1607 if (thread
->state
& TH_TERMINATE
)
1608 thread_reaper_enqueue(thread
);
1610 if (thread
->wake_active
) {
1611 thread
->wake_active
= FALSE
;
1612 thread_unlock(thread
);
1613 wake_unlock(thread
);
1614 thread_wakeup((event_t
)&thread
->wake_active
);
1619 case TH_RUN
| TH_IDLE
:
1621 * Drop idle thread -- it is already in
1622 * idle_thread_array.
1627 panic("State 0x%x \n",thread
->state
);
1629 thread_unlock(thread
);
1630 wake_unlock(thread
);
1634 * Enqueue thread on run queue. Thread must be locked,
1635 * and not already be on a run queue.
1639 register run_queue_t rq
,
1640 register thread_t thread
,
1643 register int whichq
;
1646 whichq
= thread
->sched_pri
;
1647 assert(whichq
>= MINPRI
&& whichq
<= MAXPRI
);
1649 simple_lock(&rq
->lock
); /* lock the run queue */
1650 assert(thread
->runq
== RUN_QUEUE_NULL
);
1652 enqueue_tail(&rq
->queues
[whichq
], (queue_entry_t
)thread
);
1654 enqueue_head(&rq
->queues
[whichq
], (queue_entry_t
)thread
);
1656 setbit(MAXPRI
- whichq
, rq
->bitmap
);
1657 if (whichq
> rq
->highq
)
1660 oldrqcount
= rq
->count
++;
1662 thread
->whichq
= whichq
;
1664 thread_check(thread
, rq
);
1666 simple_unlock(&rq
->lock
);
1668 return (oldrqcount
);
1674 * Make thread runnable; dispatch directly onto an idle processor
1675 * if possible. Else put on appropriate run queue (processor
1676 * if bound, else processor set. Caller must have lock on thread.
1677 * This is always called at splsched.
1678 * The tail parameter, if TRUE || TAIL_Q, indicates that the
1679 * thread should be placed at the tail of the runq. If
1680 * FALSE || HEAD_Q the thread will be placed at the head of the
1685 register thread_t new_thread
,
1686 boolean_t may_preempt
,
1689 register processor_t processor
;
1690 register run_queue_t runq
;
1691 register processor_set_t pset
;
1693 ast_t ast_flags
= AST_BLOCK
;
1695 mp_disable_preemption();
1697 assert(!(new_thread
->state
& TH_SWAPPED_OUT
));
1698 assert(thread_runnable(new_thread
));
1701 * Update priority if needed.
1703 if (new_thread
->sched_stamp
!= sched_tick
)
1704 update_priority(new_thread
);
1706 if (new_thread
->policy
& (POLICY_FIFO
|POLICY_RR
)) {
1707 if ( new_thread
->sched_pri
>= (MAXPRI_KERNBAND
- 2) &&
1708 kernel_preemption_mode
== KERNEL_PREEMPT
)
1709 ast_flags
|= AST_URGENT
;
1712 assert(new_thread
->runq
== RUN_QUEUE_NULL
);
1715 * Try to dispatch the thread directly onto an idle processor.
1717 if ((processor
= new_thread
->bound_processor
) == PROCESSOR_NULL
) {
1719 * Not bound, any processor in the processor set is ok.
1721 pset
= new_thread
->processor_set
;
1722 if (pset
->idle_count
> 0) {
1723 simple_lock(&pset
->idle_lock
);
1724 if (pset
->idle_count
> 0) {
1725 processor
= (processor_t
) queue_first(&pset
->idle_queue
);
1726 queue_remove(&(pset
->idle_queue
), processor
, processor_t
,
1729 processor
->next_thread
= new_thread
;
1730 processor
->state
= PROCESSOR_DISPATCHING
;
1731 simple_unlock(&pset
->idle_lock
);
1732 if(processor
->slot_num
!= cpu_number())
1733 machine_signal_idle(processor
);
1734 mp_enable_preemption();
1737 simple_unlock(&pset
->idle_lock
);
1745 thread
= current_thread();
1746 processor
= current_processor();
1748 pset
== processor
->processor_set
&&
1749 thread
->sched_pri
< new_thread
->sched_pri
) {
1751 * XXX if we have a non-empty local runq or are
1752 * XXX running a bound thread, ought to check for
1753 * XXX another cpu running lower-pri thread to preempt.
1756 * Turn off first_quantum to allow csw.
1758 processor
->first_quantum
= FALSE
;
1764 * Put us on the end of the runq, if we are not preempting
1765 * or the guy we are preempting.
1767 run_queue_enqueue(runq
, new_thread
, tail
);
1771 * Bound, can only run on bound processor. Have to lock
1772 * processor here because it may not be the current one.
1774 if (processor
->state
== PROCESSOR_IDLE
) {
1775 simple_lock(&processor
->lock
);
1776 pset
= processor
->processor_set
;
1777 simple_lock(&pset
->idle_lock
);
1778 if (processor
->state
== PROCESSOR_IDLE
) {
1779 queue_remove(&pset
->idle_queue
, processor
,
1780 processor_t
, processor_queue
);
1782 processor
->next_thread
= new_thread
;
1783 processor
->state
= PROCESSOR_DISPATCHING
;
1784 simple_unlock(&pset
->idle_lock
);
1785 simple_unlock(&processor
->lock
);
1786 if(processor
->slot_num
!= cpu_number())
1787 machine_signal_idle(processor
);
1788 mp_enable_preemption();
1791 simple_unlock(&pset
->idle_lock
);
1792 simple_unlock(&processor
->lock
);
1796 * Cause ast on processor if processor is on line, and the
1797 * currently executing thread is not bound to that processor
1798 * (bound threads have implicit priority over non-bound threads).
1799 * We also avoid sending the AST to the idle thread (if it got
1800 * scheduled in the window between the 'if' above and here),
1801 * since the idle_thread is bound.
1803 runq
= &processor
->runq
;
1804 thread
= current_thread();
1805 if (processor
== current_processor()) {
1806 if ( thread
->bound_processor
== PROCESSOR_NULL
||
1807 thread
->sched_pri
< new_thread
->sched_pri
) {
1808 processor
->first_quantum
= FALSE
;
1812 run_queue_enqueue(runq
, new_thread
, tail
);
1815 thread
= cpu_data
[processor
->slot_num
].active_thread
;
1816 if ( run_queue_enqueue(runq
, new_thread
, tail
) == 0 &&
1817 processor
->state
!= PROCESSOR_OFF_LINE
&&
1818 thread
&& thread
->bound_processor
!= processor
)
1819 cause_ast_check(processor
);
1823 mp_enable_preemption();
1829 * Set the priority of the specified thread to the specified
1830 * priority. This may cause the thread to change queues.
1832 * The thread *must* be locked by the caller.
1840 register struct run_queue
*rq
;
1842 rq
= rem_runq(thread
);
1843 assert(thread
->runq
== RUN_QUEUE_NULL
);
1844 thread
->sched_pri
= pri
;
1845 if (rq
!= RUN_QUEUE_NULL
) {
1847 thread_setrun(thread
, TRUE
, TAIL_Q
);
1849 run_queue_enqueue(rq
, thread
, TAIL_Q
);
1856 * Remove a thread from its run queue.
1857 * The run queue that the process was on is returned
1858 * (or RUN_QUEUE_NULL if not on a run queue). Thread *must* be locked
1859 * before calling this routine. Unusual locking protocol on runq
1860 * field in thread structure makes this code interesting; see thread.h.
1866 register struct run_queue
*rq
;
1870 * If rq is RUN_QUEUE_NULL, the thread will stay out of the
1871 * run_queues because the caller locked the thread. Otherwise
1872 * the thread is on a runq, but could leave.
1874 if (rq
!= RUN_QUEUE_NULL
) {
1875 simple_lock(&rq
->lock
);
1876 if (rq
== thread
->runq
) {
1878 * Thread is in a runq and we have a lock on
1882 thread_check(thread
, rq
);
1884 remqueue(&rq
->queues
[0], (queue_entry_t
)thread
);
1887 if (queue_empty(rq
->queues
+ thread
->sched_pri
)) {
1888 /* update run queue status */
1889 if (thread
->sched_pri
!= IDLEPRI
)
1890 clrbit(MAXPRI
- thread
->sched_pri
, rq
->bitmap
);
1891 rq
->highq
= MAXPRI
- ffsbit(rq
->bitmap
);
1893 thread
->runq
= RUN_QUEUE_NULL
;
1894 simple_unlock(&rq
->lock
);
1898 * The thread left the runq before we could
1899 * lock the runq. It is not on a runq now, and
1900 * can't move again because this routine's
1901 * caller locked the thread.
1903 assert(thread
->runq
== RUN_QUEUE_NULL
);
1904 simple_unlock(&rq
->lock
);
1905 rq
= RUN_QUEUE_NULL
;
1916 * Choose a thread to execute. The thread chosen is removed
1917 * from its run queue. Note that this requires only that the runq
1921 * Check processor runq first; if anything found, run it.
1922 * Else check pset runq; if nothing found, return idle thread.
1924 * Second line of strategy is implemented by choose_pset_thread.
1925 * This is only called on processor startup and when thread_block
1926 * thinks there's something in the processor runq.
1930 processor_t myprocessor
)
1934 register run_queue_t runq
;
1935 processor_set_t pset
;
1937 runq
= &myprocessor
->runq
;
1938 pset
= myprocessor
->processor_set
;
1940 simple_lock(&runq
->lock
);
1941 if (runq
->count
> 0 && runq
->highq
>= pset
->runq
.highq
) {
1942 q
= runq
->queues
+ runq
->highq
;
1944 if (!queue_empty(q
)) {
1945 #endif /*MACH_ASSERT*/
1946 thread
= (thread_t
)q
->next
;
1947 ((queue_entry_t
)thread
)->next
->prev
= q
;
1948 q
->next
= ((queue_entry_t
)thread
)->next
;
1949 thread
->runq
= RUN_QUEUE_NULL
;
1951 if (queue_empty(q
)) {
1952 if (runq
->highq
!= IDLEPRI
)
1953 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
1954 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
1956 simple_unlock(&runq
->lock
);
1960 panic("choose_thread");
1961 #endif /*MACH_ASSERT*/
1965 simple_unlock(&runq
->lock
);
1966 simple_lock(&pset
->runq
.lock
);
1967 return (choose_pset_thread(myprocessor
, pset
));
1972 * choose_pset_thread: choose a thread from processor_set runq or
1973 * set processor idle and choose its idle thread.
1975 * Caller must be at splsched and have a lock on the runq. This
1976 * lock is released by this routine. myprocessor is always the current
1977 * processor, and pset must be its processor set.
1978 * This routine chooses and removes a thread from the runq if there
1979 * is one (and returns it), else it sets the processor idle and
1980 * returns its idle thread.
1984 register processor_t myprocessor
,
1985 processor_set_t pset
)
1987 register run_queue_t runq
;
1988 register thread_t thread
;
1992 if (runq
->count
> 0) {
1993 q
= runq
->queues
+ runq
->highq
;
1995 if (!queue_empty(q
)) {
1996 #endif /*MACH_ASSERT*/
1997 thread
= (thread_t
)q
->next
;
1998 ((queue_entry_t
)thread
)->next
->prev
= q
;
1999 q
->next
= ((queue_entry_t
)thread
)->next
;
2000 thread
->runq
= RUN_QUEUE_NULL
;
2002 if (queue_empty(q
)) {
2003 if (runq
->highq
!= IDLEPRI
)
2004 clrbit(MAXPRI
- runq
->highq
, runq
->bitmap
);
2005 runq
->highq
= MAXPRI
- ffsbit(runq
->bitmap
);
2007 simple_unlock(&runq
->lock
);
2011 panic("choose_pset_thread");
2012 #endif /*MACH_ASSERT*/
2015 simple_unlock(&runq
->lock
);
2018 * Nothing is runnable, so set this processor idle if it
2019 * was running. If it was in an assignment or shutdown,
2020 * leave it alone. Return its idle thread.
2022 simple_lock(&pset
->idle_lock
);
2023 if (myprocessor
->state
== PROCESSOR_RUNNING
) {
2024 myprocessor
->state
= PROCESSOR_IDLE
;
2026 * XXX Until it goes away, put master on end of queue, others
2027 * XXX on front so master gets used last.
2029 if (myprocessor
== master_processor
)
2030 queue_enter(&(pset
->idle_queue
), myprocessor
,
2031 processor_t
, processor_queue
);
2033 queue_enter_first(&(pset
->idle_queue
), myprocessor
,
2034 processor_t
, processor_queue
);
2038 simple_unlock(&pset
->idle_lock
);
2040 return (myprocessor
->idle_thread
);
2044 * no_dispatch_count counts number of times processors go non-idle
2045 * without being dispatched. This should be very rare.
2047 int no_dispatch_count
= 0;
2050 * This is the idle thread, which just looks for other threads
2054 idle_thread_continue(void)
2056 register processor_t myprocessor
;
2057 register volatile thread_t
*threadp
;
2058 register volatile int *gcount
;
2059 register volatile int *lcount
;
2060 register thread_t new_thread
;
2062 register processor_set_t pset
;
2065 mycpu
= cpu_number();
2066 myprocessor
= current_processor();
2067 threadp
= (volatile thread_t
*) &myprocessor
->next_thread
;
2068 lcount
= (volatile int *) &myprocessor
->runq
.count
;
2071 #ifdef MARK_CPU_IDLE
2072 MARK_CPU_IDLE(mycpu
);
2073 #endif /* MARK_CPU_IDLE */
2075 gcount
= (volatile int *)&myprocessor
->processor_set
->runq
.count
;
2078 while ( (*threadp
== (volatile thread_t
)THREAD_NULL
) &&
2079 (*gcount
== 0) && (*lcount
== 0) ) {
2081 /* check for ASTs while we wait */
2083 if (need_ast
[mycpu
] &~ (AST_SCHEDULING
|AST_URGENT
|AST_BSD
|AST_BSD_INIT
)) {
2084 /* don't allow scheduling ASTs */
2085 need_ast
[mycpu
] &= ~(AST_SCHEDULING
|AST_URGENT
|AST_BSD
|AST_BSD_INIT
);
2086 ast_taken(FALSE
, AST_ALL
, TRUE
); /* back at spllo */
2094 machine_clock_assist();
2099 #ifdef MARK_CPU_ACTIVE
2101 MARK_CPU_ACTIVE(mycpu
);
2103 #endif /* MARK_CPU_ACTIVE */
2106 * This is not a switch statement to avoid the
2107 * bounds checking code in the common case.
2109 pset
= myprocessor
->processor_set
;
2110 simple_lock(&pset
->idle_lock
);
2112 state
= myprocessor
->state
;
2113 if (state
== PROCESSOR_DISPATCHING
) {
2115 * Commmon case -- cpu dispatched.
2117 new_thread
= *threadp
;
2118 *threadp
= (volatile thread_t
) THREAD_NULL
;
2119 myprocessor
->state
= PROCESSOR_RUNNING
;
2120 simple_unlock(&pset
->idle_lock
);
2122 thread_lock(new_thread
);
2123 simple_lock(&myprocessor
->runq
.lock
);
2124 simple_lock(&pset
->runq
.lock
);
2125 if ( myprocessor
->runq
.highq
> new_thread
->sched_pri
||
2126 pset
->runq
.highq
> new_thread
->sched_pri
) {
2127 simple_unlock(&pset
->runq
.lock
);
2128 simple_unlock(&myprocessor
->runq
.lock
);
2130 if (new_thread
->bound_processor
!= PROCESSOR_NULL
)
2131 run_queue_enqueue(&myprocessor
->runq
, new_thread
, HEAD_Q
);
2133 run_queue_enqueue(&pset
->runq
, new_thread
, HEAD_Q
);
2134 thread_unlock(new_thread
);
2136 counter(c_idle_thread_block
++);
2137 thread_block(idle_thread_continue
);
2140 simple_unlock(&pset
->runq
.lock
);
2141 simple_unlock(&myprocessor
->runq
.lock
);
2144 * set up quantum for new thread.
2146 if (new_thread
->policy
& (POLICY_RR
|POLICY_FIFO
))
2147 myprocessor
->quantum
= new_thread
->unconsumed_quantum
;
2149 myprocessor
->quantum
= pset
->set_quantum
;
2150 thread_unlock(new_thread
);
2152 myprocessor
->first_quantum
= TRUE
;
2153 counter(c_idle_thread_handoff
++);
2154 thread_run(myprocessor
->idle_thread
,
2155 idle_thread_continue
, new_thread
);
2159 if (state
== PROCESSOR_IDLE
) {
2160 if (myprocessor
->state
!= PROCESSOR_IDLE
) {
2162 * Something happened, try again.
2167 * Processor was not dispatched (Rare).
2168 * Set it running again.
2170 no_dispatch_count
++;
2172 queue_remove(&pset
->idle_queue
, myprocessor
,
2173 processor_t
, processor_queue
);
2174 myprocessor
->state
= PROCESSOR_RUNNING
;
2175 simple_unlock(&pset
->idle_lock
);
2177 counter(c_idle_thread_block
++);
2178 thread_block(idle_thread_continue
);
2181 if ( state
== PROCESSOR_ASSIGN
||
2182 state
== PROCESSOR_SHUTDOWN
) {
2184 * Changing processor sets, or going off-line.
2185 * Release next_thread if there is one. Actual
2186 * thread to run is on a runq.
2188 if ((new_thread
= (thread_t
)*threadp
) != THREAD_NULL
) {
2189 *threadp
= (volatile thread_t
) THREAD_NULL
;
2190 simple_unlock(&pset
->idle_lock
);
2191 thread_lock(new_thread
);
2192 thread_setrun(new_thread
, FALSE
, TAIL_Q
);
2193 thread_unlock(new_thread
);
2195 simple_unlock(&pset
->idle_lock
);
2197 counter(c_idle_thread_block
++);
2198 thread_block(idle_thread_continue
);
2201 simple_unlock(&pset
->idle_lock
);
2202 printf("Bad processor state %d (Cpu %d)\n",
2203 cpu_state(mycpu
), mycpu
);
2204 panic("idle_thread");
2215 thread_t self
= current_thread();
2218 stack_privilege(self
);
2219 thread_swappable(current_act(), FALSE
);
2224 self
->priority
= IDLEPRI
;
2225 self
->sched_pri
= self
->priority
;
2227 thread_unlock(self
);
2230 counter(c_idle_thread_block
++);
2231 thread_block((void(*)(void))0);
2232 idle_thread_continue();
2236 static AbsoluteTime sched_tick_interval
, sched_tick_deadline
;
2241 * Update the priorities of all threads periodically.
2244 sched_tick_thread_continue(void)
2246 AbsoluteTime abstime
;
2249 #endif /* SIMPLE_CLOCK */
2251 clock_get_uptime(&abstime
);
2253 sched_tick
++; /* age usage one more time */
2256 * Compensate for clock drift. sched_usec is an
2257 * exponential average of the number of microseconds in
2258 * a second. It decays in the same fashion as cpu_usage.
2260 new_usec
= sched_usec_elapsed();
2261 sched_usec
= (5*sched_usec
+ 3*new_usec
)/8;
2262 #endif /* SIMPLE_CLOCK */
2265 * Compute the scheduler load factors.
2267 compute_mach_factor();
2270 * Scan the run queues for runnable threads that need to
2271 * have their priorities recalculated.
2275 clock_deadline_for_periodic_event(sched_tick_interval
, abstime
,
2276 &sched_tick_deadline
);
2278 assert_wait((event_t
)sched_tick_thread_continue
, THREAD_INTERRUPTIBLE
);
2279 thread_set_timer_deadline(sched_tick_deadline
);
2280 thread_block(sched_tick_thread_continue
);
2285 sched_tick_thread(void)
2287 thread_t self
= current_thread();
2291 stack_privilege(self
);
2292 thread_swappable(self
->top_act
, FALSE
);
2297 self
->priority
= MAXPRI_STANDARD
;
2298 self
->sched_pri
= self
->priority
;
2300 thread_unlock(self
);
2303 rate
= (1000 >> SCHED_TICK_SHIFT
);
2304 clock_interval_to_absolutetime_interval(rate
, USEC_PER_SEC
,
2305 &sched_tick_interval
);
2306 clock_get_uptime(&sched_tick_deadline
);
2308 thread_block(sched_tick_thread_continue
);
2312 #define MAX_STUCK_THREADS 128
2315 * do_thread_scan: scan for stuck threads. A thread is stuck if
2316 * it is runnable but its priority is so low that it has not
2317 * run for several seconds. Its priority should be higher, but
2318 * won't be until it runs and calls update_priority. The scanner
2319 * finds these threads and does the updates.
2321 * Scanner runs in two passes. Pass one squirrels likely
2322 * thread ids away in an array (takes out references for them).
2323 * Pass two does the priority updates. This is necessary because
2324 * the run queue lock is required for the candidate scan, but
2325 * cannot be held during updates [set_pri will deadlock].
2327 * Array length should be enough so that restart isn't necessary,
2328 * but restart logic is included. Does not scan processor runqs.
2331 thread_t stuck_threads
[MAX_STUCK_THREADS
];
2332 int stuck_count
= 0;
2335 * do_runq_scan is the guts of pass 1. It scans a runq for
2336 * stuck threads. A boolean is returned indicating whether
2337 * a retry is needed.
2344 register thread_t thread
;
2347 boolean_t result
= FALSE
;
2350 simple_lock(&runq
->lock
);
2351 if ((count
= runq
->count
) > 0) {
2352 q
= runq
->queues
+ runq
->highq
;
2354 queue_iterate(q
, thread
, thread_t
, links
) {
2355 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
2356 thread
->policy
== POLICY_TIMESHARE
) {
2357 if (thread
->sched_stamp
!= sched_tick
) {
2359 * Stuck, save its id for later.
2361 if (stuck_count
== MAX_STUCK_THREADS
) {
2363 * !@#$% No more room.
2365 simple_unlock(&runq
->lock
);
2372 * Inline version of thread_reference
2373 * XXX - lock ordering problem here:
2374 * thread locks should be taken before runq
2375 * locks: just try and get the thread's locks
2376 * and ignore this thread if we fail, we might
2377 * have better luck next time.
2379 if (simple_lock_try(&thread
->lock
)) {
2380 thread
->ref_count
++;
2381 thread_unlock(thread
);
2382 stuck_threads
[stuck_count
++] = thread
;
2395 simple_unlock(&runq
->lock
);
2401 boolean_t thread_scan_enabled
= TRUE
;
2404 do_thread_scan(void)
2406 register boolean_t restart_needed
= FALSE
;
2407 register thread_t thread
;
2408 register processor_set_t pset
= &default_pset
;
2409 register processor_t processor
;
2412 if (!thread_scan_enabled
)
2416 restart_needed
= do_runq_scan(&pset
->runq
);
2417 if (!restart_needed
) {
2418 simple_lock(&pset
->processors_lock
);
2419 processor
= (processor_t
)queue_first(&pset
->processors
);
2420 while (!queue_end(&pset
->processors
, (queue_entry_t
)processor
)) {
2421 if (restart_needed
= do_runq_scan(&processor
->runq
))
2424 processor
= (processor_t
)queue_next(&processor
->processors
);
2426 simple_unlock(&pset
->processors_lock
);
2430 * Ok, we now have a collection of candidates -- fix them.
2432 while (stuck_count
> 0) {
2433 thread
= stuck_threads
[--stuck_count
];
2434 stuck_threads
[stuck_count
] = THREAD_NULL
;
2436 thread_lock(thread
);
2437 if (thread
->policy
== POLICY_TIMESHARE
) {
2438 if ( !(thread
->state
& (TH_WAIT
|TH_SUSP
)) &&
2439 thread
->sched_stamp
!= sched_tick
)
2440 update_priority(thread
);
2442 thread_unlock(thread
);
2444 thread_deallocate(thread
);
2447 } while (restart_needed
);
2451 * Just in case someone doesn't use the macro
2453 #undef thread_wakeup
2462 thread_wakeup_with_result(x
, THREAD_AWAKENED
);
2469 sched_policy_t
*policy
;
2471 /* Ask sched policy if thread is runnable */
2472 policy
= policy_id_to_sched_policy(thread
->policy
);
2474 return ((policy
!= SCHED_POLICY_NULL
)?
2475 policy
->sp_ops
.sp_thread_runnable(policy
, thread
) : FALSE
);
2484 printf("processor_set: %08x\n",ps
);
2485 printf("idle_queue: %08x %08x, idle_count: 0x%x\n",
2486 ps
->idle_queue
.next
,ps
->idle_queue
.prev
,ps
->idle_count
);
2487 printf("processors: %08x %08x, processor_count: 0x%x\n",
2488 ps
->processors
.next
,ps
->processors
.prev
,ps
->processor_count
);
2489 printf("tasks: %08x %08x, task_count: 0x%x\n",
2490 ps
->tasks
.next
,ps
->tasks
.prev
,ps
->task_count
);
2491 printf("threads: %08x %08x, thread_count: 0x%x\n",
2492 ps
->threads
.next
,ps
->threads
.prev
,ps
->thread_count
);
2493 printf("ref_count: 0x%x, active: %x\n",
2494 ps
->ref_count
,ps
->active
);
2495 printf("pset_self: %08x, pset_name_self: %08x\n",ps
->pset_self
, ps
->pset_name_self
);
2496 printf("max_priority: 0x%x, policies: 0x%x, set_quantum: 0x%x\n",
2497 ps
->max_priority
, ps
->policies
, ps
->set_quantum
);
2500 #define processor_state(s) (((s)>PROCESSOR_SHUTDOWN)?"*unknown*":states[s])
2506 char *states
[]={"OFF_LINE","RUNNING","IDLE","DISPATCHING",
2507 "ASSIGN","SHUTDOWN"};
2509 printf("processor: %08x\n",p
);
2510 printf("processor_queue: %08x %08x\n",
2511 p
->processor_queue
.next
,p
->processor_queue
.prev
);
2512 printf("state: %8s, next_thread: %08x, idle_thread: %08x\n",
2513 processor_state(p
->state
), p
->next_thread
, p
->idle_thread
);
2514 printf("quantum: %u, first_quantum: %x, last_quantum: %u\n",
2515 p
->quantum
, p
->first_quantum
, p
->last_quantum
);
2516 printf("processor_set: %08x, processor_set_next: %08x\n",
2517 p
->processor_set
, p
->processor_set_next
);
2518 printf("processors: %08x %08x\n", p
->processors
.next
,p
->processors
.prev
);
2519 printf("processor_self: %08x, slot_num: 0x%x\n", p
->processor_self
, p
->slot_num
);
2523 dump_run_queue_struct(
2529 for( i
=0; i
< NRQS
; ) {
2532 printf("%6s",(i
==0)?"runq:":"");
2533 for( j
=0; (j
<8) && (i
< NRQS
); j
++,i
++ ) {
2534 if( rq
->queues
[i
].next
== &rq
->queues
[i
] )
2535 printf( " --------");
2537 printf(" %08x",rq
->queues
[i
].next
);
2541 for( i
=0; i
< NRQBM
; ) {
2542 register unsigned int mask
;
2549 *d
++ = ((rq
->bitmap
[i
]&mask
)?'r':'e');
2553 printf("%8s%s\n",((i
==0)?"bitmap:":""),dump_buf
);
2556 printf("highq: 0x%x, count: %u\n", rq
->highq
, rq
->count
);
2563 register queue_t q1
;
2565 register queue_entry_t e
;
2568 for (i
= 0; i
< NRQS
; i
++) {
2569 if (q1
->next
!= q1
) {
2573 for (t_cnt
=0, e
= q1
->next
; e
!= q1
; e
= e
->next
) {
2574 printf("\t0x%08x",e
);
2575 if( (t_cnt
= ++t_cnt%4
) == 0 )
2582 printf("[%u]\t<empty>\n",i);
2593 register queue_t q1
;
2595 register queue_entry_t e
;
2601 for (i
= MAXPRI
; i
>= 0; i
--) {
2602 if (q1
->next
== q1
) {
2603 if (q1
->prev
!= q1
) {
2604 panic("checkrq: empty at %s", msg
);
2611 for (e
= q1
->next
; e
!= q1
; e
= e
->next
) {
2613 if (e
->next
->prev
!= e
)
2614 panic("checkrq-2 at %s", msg
);
2615 if (e
->prev
->next
!= e
)
2616 panic("checkrq-3 at %s", msg
);
2622 panic("checkrq: count wrong at %s", msg
);
2623 if (rq
->count
!= 0 && highq
> rq
->highq
)
2624 panic("checkrq: highq wrong at %s", msg
);
2629 register thread_t thread
,
2630 register run_queue_t rq
)
2632 register int whichq
= thread
->sched_pri
;
2633 register queue_entry_t queue
, entry
;
2635 if (whichq
< MINPRI
|| whichq
> MAXPRI
)
2636 panic("thread_check: bad pri");
2638 if (whichq
!= thread
->whichq
)
2639 panic("thread_check: whichq");
2641 queue
= &rq
->queues
[whichq
];
2642 entry
= queue_first(queue
);
2643 while (!queue_end(queue
, entry
)) {
2644 if (entry
== (queue_entry_t
)thread
)
2647 entry
= queue_next(entry
);
2650 panic("thread_check: not found");
2656 #include <ddb/db_output.h>
2657 #define printf kdbprintf
2658 extern int db_indent
;
2659 void db_sched(void);
2664 iprintf("Scheduling Statistics:\n");
2666 iprintf("Thread invocations: csw %d same %d\n",
2667 c_thread_invoke_csw
, c_thread_invoke_same
);
2669 iprintf("Thread block: calls %d\n",
2670 c_thread_block_calls
);
2671 iprintf("Idle thread:\n\thandoff %d block %d no_dispatch %d\n",
2672 c_idle_thread_handoff
,
2673 c_idle_thread_block
, no_dispatch_count
);
2674 iprintf("Sched thread blocks: %d\n", c_sched_thread_block
);
2675 #endif /* MACH_COUNTERS */
2678 #endif /* MACH_KDB */