]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sched_prim.c
xnu-1504.9.37.tar.gz
[apple/xnu.git] / osfmk / kern / sched_prim.c
CommitLineData
1c79356b 1/*
c910b4d9 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_FREE_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: sched_prim.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1986
62 *
63 * Scheduling primitives
64 *
65 */
66
67#include <debug.h>
1c79356b 68#include <mach_kdb.h>
1c79356b
A
69
70#include <ddb/db_output.h>
91447636
A
71
72#include <mach/mach_types.h>
1c79356b 73#include <mach/machine.h>
91447636
A
74#include <mach/policy.h>
75#include <mach/sync_policy.h>
76
1c79356b
A
77#include <machine/machine_routines.h>
78#include <machine/sched_param.h>
0c530ab8 79#include <machine/machine_cpu.h>
91447636
A
80
81#include <kern/kern_types.h>
1c79356b
A
82#include <kern/clock.h>
83#include <kern/counters.h>
84#include <kern/cpu_number.h>
85#include <kern/cpu_data.h>
91447636 86#include <kern/debug.h>
1c79356b
A
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>
91447636
A
98#include <kern/wait_queue.h>
99
1c79356b
A
100#include <vm/pmap.h>
101#include <vm/vm_kern.h>
102#include <vm/vm_map.h>
91447636 103
b0d623f7
A
104#include <mach/sdt.h>
105
1c79356b
A
106#include <sys/kdebug.h>
107
0c530ab8 108#include <kern/pms.h>
3a60a9f5 109
2d21ac55
A
110struct run_queue rt_runq;
111#define RT_RUNQ ((processor_t)-1)
112decl_simple_lock_data(static,rt_lock);
113
0b4e3aa0 114#define DEFAULT_PREEMPTION_RATE 100 /* (1/s) */
1c79356b
A
115int default_preemption_rate = DEFAULT_PREEMPTION_RATE;
116
0b4e3aa0
A
117#define MAX_UNSAFE_QUANTA 800
118int max_unsafe_quanta = MAX_UNSAFE_QUANTA;
119
120#define MAX_POLL_QUANTA 2
121int max_poll_quanta = MAX_POLL_QUANTA;
122
123#define SCHED_POLL_YIELD_SHIFT 4 /* 1/16 */
124int sched_poll_yield_shift = SCHED_POLL_YIELD_SHIFT;
125
55e303ae
A
126uint64_t max_unsafe_computation;
127uint32_t sched_safe_duration;
128uint64_t max_poll_computation;
129
130uint32_t std_quantum;
131uint32_t min_std_quantum;
132
91447636
A
133uint32_t std_quantum_us;
134
55e303ae
A
135uint32_t max_rt_quantum;
136uint32_t min_rt_quantum;
137
91447636
A
138uint32_t sched_cswtime;
139
1c79356b 140unsigned sched_tick;
91447636 141uint32_t sched_tick_interval;
1c79356b 142
2d21ac55
A
143uint32_t sched_pri_shift = INT8_MAX;
144uint32_t sched_fixed_shift;
145
146uint32_t sched_run_count, sched_share_count;
147uint32_t sched_load_average, sched_mach_factor;
148
1c79356b 149/* Forwards */
2d21ac55 150static void load_shift_init(void) __attribute__((section("__TEXT, initcode")));
4a3eedf9 151static void preempt_pri_init(void) __attribute__((section("__TEXT, initcode")));
2d21ac55 152
c910b4d9
A
153static thread_t run_queue_dequeue(
154 run_queue_t runq,
155 integer_t options);
156
b0d623f7
A
157static thread_t choose_thread(
158 processor_t processor,
159 int priority);
160
2d21ac55
A
161static thread_t thread_select_idle(
162 thread_t thread,
163 processor_t processor);
1c79356b 164
2d21ac55
A
165static thread_t processor_idle(
166 thread_t thread,
167 processor_t processor);
91447636 168
2d21ac55 169static thread_t steal_thread(
cf7d32b8
A
170 processor_set_t pset);
171
172static thread_t steal_processor_thread(
55e303ae 173 processor_t processor);
1c79356b 174
91447636 175static void thread_update_scan(void);
1c79356b 176
2d21ac55
A
177#if DEBUG
178extern int debug_task;
179#define TLOG(a, fmt, args...) if(debug_task & a) kprintf(fmt, ## args)
180#else
181#define TLOG(a, fmt, args...) do {} while (0)
182#endif
183
1c79356b 184#if DEBUG
0b4e3aa0 185static
1c79356b
A
186boolean_t thread_runnable(
187 thread_t thread);
188
0b4e3aa0
A
189#endif /*DEBUG*/
190
1c79356b
A
191/*
192 * State machine
193 *
194 * states are combinations of:
195 * R running
196 * W waiting (or on wait queue)
197 * N non-interruptible
198 * O swapped out
199 * I being swapped in
200 *
201 * init action
202 * assert_wait thread_block clear_wait swapout swapin
203 *
204 * R RW, RWN R; setrun - -
205 * RN RWN RN; setrun - -
206 *
207 * RW W R -
208 * RWN WN RN -
209 *
210 * W R; setrun WO
211 * WN RN; setrun -
212 *
213 * RO - - R
214 *
215 */
216
91447636 217int8_t sched_load_shifts[NRQS];
b0d623f7 218int sched_preempt_pri[NRQBM];
91447636 219
1c79356b
A
220void
221sched_init(void)
222{
223 /*
0b4e3aa0
A
224 * Calculate the timeslicing quantum
225 * in us.
1c79356b
A
226 */
227 if (default_preemption_rate < 1)
228 default_preemption_rate = DEFAULT_PREEMPTION_RATE;
0b4e3aa0 229 std_quantum_us = (1000 * 1000) / default_preemption_rate;
1c79356b 230
0b4e3aa0 231 printf("standard timeslicing quantum is %d us\n", std_quantum_us);
1c79356b 232
55e303ae
A
233 sched_safe_duration = (2 * max_unsafe_quanta / default_preemption_rate) *
234 (1 << SCHED_TICK_SHIFT);
235
91447636 236 load_shift_init();
4a3eedf9 237 preempt_pri_init();
2d21ac55
A
238 simple_lock_init(&rt_lock, 0);
239 run_queue_init(&rt_runq);
1c79356b 240 sched_tick = 0;
1c79356b 241 ast_init();
1c79356b
A
242}
243
55e303ae
A
244void
245sched_timebase_init(void)
246{
91447636
A
247 uint64_t abstime;
248 uint32_t shift;
55e303ae 249
91447636 250 /* standard timeslicing quantum */
55e303ae
A
251 clock_interval_to_absolutetime_interval(
252 std_quantum_us, NSEC_PER_USEC, &abstime);
253 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
b0d623f7 254 std_quantum = (uint32_t)abstime;
55e303ae 255
91447636 256 /* smallest remaining quantum (250 us) */
55e303ae
A
257 clock_interval_to_absolutetime_interval(250, NSEC_PER_USEC, &abstime);
258 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
b0d623f7 259 min_std_quantum = (uint32_t)abstime;
55e303ae 260
91447636 261 /* smallest rt computaton (50 us) */
55e303ae
A
262 clock_interval_to_absolutetime_interval(50, NSEC_PER_USEC, &abstime);
263 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
b0d623f7 264 min_rt_quantum = (uint32_t)abstime;
55e303ae 265
91447636 266 /* maximum rt computation (50 ms) */
55e303ae
A
267 clock_interval_to_absolutetime_interval(
268 50, 1000*NSEC_PER_USEC, &abstime);
269 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
b0d623f7 270 max_rt_quantum = (uint32_t)abstime;
55e303ae 271
91447636
A
272 /* scheduler tick interval */
273 clock_interval_to_absolutetime_interval(USEC_PER_SEC >> SCHED_TICK_SHIFT,
274 NSEC_PER_USEC, &abstime);
cf7d32b8 275 assert((abstime >> 32) == 0 && (uint32_t)abstime != 0);
b0d623f7 276 sched_tick_interval = (uint32_t)abstime;
55e303ae 277
91447636
A
278 /*
279 * Compute conversion factor from usage to
280 * timesharing priorities with 5/8 ** n aging.
281 */
282 abstime = (abstime * 5) / 3;
283 for (shift = 0; abstime > BASEPRI_DEFAULT; ++shift)
284 abstime >>= 1;
2d21ac55 285 sched_fixed_shift = shift;
91447636 286
55e303ae
A
287 max_unsafe_computation = max_unsafe_quanta * std_quantum;
288 max_poll_computation = max_poll_quanta * std_quantum;
289}
290
91447636
A
291/*
292 * Set up values for timeshare
293 * loading factors.
294 */
295static void
296load_shift_init(void)
297{
298 int8_t k, *p = sched_load_shifts;
299 uint32_t i, j;
300
301 *p++ = INT8_MIN; *p++ = 0;
302
303 for (i = j = 2, k = 1; i < NRQS; ++k) {
304 for (j <<= 1; i < j; ++i)
305 *p++ = k;
306 }
307}
308
4a3eedf9
A
309static void
310preempt_pri_init(void)
311{
312 int i, *p = sched_preempt_pri;
313
314 for (i = BASEPRI_FOREGROUND + 1; i < MINPRI_KERNEL; ++i)
315 setbit(i, p);
316
317 for (i = BASEPRI_PREEMPT; i <= MAXPRI; ++i)
318 setbit(i, p);
319}
320
1c79356b 321/*
0b4e3aa0 322 * Thread wait timer expiration.
1c79356b
A
323 */
324void
325thread_timer_expire(
91447636
A
326 void *p0,
327 __unused void *p1)
1c79356b
A
328{
329 thread_t thread = p0;
330 spl_t s;
331
332 s = splsched();
55e303ae 333 thread_lock(thread);
91447636 334 if (--thread->wait_timer_active == 0) {
0b4e3aa0
A
335 if (thread->wait_timer_is_set) {
336 thread->wait_timer_is_set = FALSE;
55e303ae 337 clear_wait_internal(thread, THREAD_TIMED_OUT);
0b4e3aa0 338 }
1c79356b 339 }
55e303ae 340 thread_unlock(thread);
1c79356b
A
341 splx(s);
342}
343
b0d623f7
A
344#ifndef __LP64__
345
1c79356b
A
346/*
347 * thread_set_timer:
348 *
349 * Set a timer for the current thread, if the thread
350 * is ready to wait. Must be called between assert_wait()
351 * and thread_block().
352 */
353void
354thread_set_timer(
0b4e3aa0
A
355 uint32_t interval,
356 uint32_t scale_factor)
1c79356b
A
357{
358 thread_t thread = current_thread();
0b4e3aa0 359 uint64_t deadline;
1c79356b
A
360 spl_t s;
361
362 s = splsched();
1c79356b
A
363 thread_lock(thread);
364 if ((thread->state & TH_WAIT) != 0) {
365 clock_interval_to_deadline(interval, scale_factor, &deadline);
91447636
A
366 if (!timer_call_enter(&thread->wait_timer, deadline))
367 thread->wait_timer_active++;
1c79356b
A
368 thread->wait_timer_is_set = TRUE;
369 }
370 thread_unlock(thread);
1c79356b
A
371 splx(s);
372}
373
374void
375thread_set_timer_deadline(
0b4e3aa0 376 uint64_t deadline)
1c79356b
A
377{
378 thread_t thread = current_thread();
379 spl_t s;
380
381 s = splsched();
1c79356b
A
382 thread_lock(thread);
383 if ((thread->state & TH_WAIT) != 0) {
91447636
A
384 if (!timer_call_enter(&thread->wait_timer, deadline))
385 thread->wait_timer_active++;
1c79356b
A
386 thread->wait_timer_is_set = TRUE;
387 }
388 thread_unlock(thread);
1c79356b
A
389 splx(s);
390}
391
392void
393thread_cancel_timer(void)
394{
395 thread_t thread = current_thread();
396 spl_t s;
397
398 s = splsched();
55e303ae 399 thread_lock(thread);
1c79356b
A
400 if (thread->wait_timer_is_set) {
401 if (timer_call_cancel(&thread->wait_timer))
402 thread->wait_timer_active--;
403 thread->wait_timer_is_set = FALSE;
404 }
55e303ae 405 thread_unlock(thread);
1c79356b
A
406 splx(s);
407}
408
b0d623f7
A
409#endif /* __LP64__ */
410
1c79356b 411/*
91447636
A
412 * thread_unblock:
413 *
414 * Unblock thread on wake up.
415 *
416 * Returns TRUE if the thread is still running.
417 *
418 * Thread must be locked.
1c79356b 419 */
91447636
A
420boolean_t
421thread_unblock(
422 thread_t thread,
423 wait_result_t wresult)
1c79356b 424{
91447636 425 boolean_t result = FALSE;
0b4e3aa0 426
91447636 427 /*
2d21ac55 428 * Set wait_result.
91447636
A
429 */
430 thread->wait_result = wresult;
1c79356b 431
91447636 432 /*
2d21ac55 433 * Cancel pending wait timer.
91447636 434 */
1c79356b
A
435 if (thread->wait_timer_is_set) {
436 if (timer_call_cancel(&thread->wait_timer))
437 thread->wait_timer_active--;
438 thread->wait_timer_is_set = FALSE;
439 }
440
91447636 441 /*
2d21ac55
A
442 * Update scheduling state: not waiting,
443 * set running.
91447636
A
444 */
445 thread->state &= ~(TH_WAIT|TH_UNINT);
1c79356b 446
91447636
A
447 if (!(thread->state & TH_RUN)) {
448 thread->state |= TH_RUN;
1c79356b 449
2d21ac55 450 (*thread->sched_call)(SCHED_CALL_UNBLOCK, thread);
1c79356b 451
91447636 452 /*
2d21ac55 453 * Update run counts.
91447636 454 */
2d21ac55 455 sched_run_incr();
91447636 456 if (thread->sched_mode & TH_MODE_TIMESHARE)
2d21ac55 457 sched_share_incr();
1c79356b 458 }
2d21ac55
A
459 else {
460 /*
461 * Signal if idling on another processor.
462 */
463 if (thread->state & TH_IDLE) {
464 processor_t processor = thread->last_processor;
465
466 if (processor != current_processor())
467 machine_signal_idle(processor);
468 }
91447636 469 result = TRUE;
2d21ac55 470 }
1c79356b 471
91447636
A
472 /*
473 * Calculate deadline for real-time threads.
474 */
475 if (thread->sched_mode & TH_MODE_REALTIME) {
476 thread->realtime.deadline = mach_absolute_time();
477 thread->realtime.deadline += thread->realtime.constraint;
0b4e3aa0
A
478 }
479
91447636
A
480 /*
481 * Clear old quantum, fail-safe computation, etc.
482 */
483 thread->current_quantum = 0;
484 thread->computation_metered = 0;
485 thread->reason = AST_NONE;
1c79356b 486
91447636
A
487 KERNEL_DEBUG_CONSTANT(
488 MACHDBG_CODE(DBG_MACH_SCHED,MACH_MAKE_RUNNABLE) | DBG_FUNC_NONE,
b0d623f7
A
489 (uintptr_t)thread_tid(thread), thread->sched_pri, 0, 0, 0);
490
491 DTRACE_SCHED2(wakeup, struct thread *, thread, struct proc *, thread->task->bsd_info);
91447636
A
492
493 return (result);
1c79356b
A
494}
495
496/*
91447636 497 * Routine: thread_go
1c79356b 498 * Purpose:
91447636 499 * Unblock and dispatch thread.
1c79356b
A
500 * Conditions:
501 * thread lock held, IPC locks may be held.
502 * thread must have been pulled from wait queue under same lock hold.
9bccf70c
A
503 * Returns:
504 * KERN_SUCCESS - Thread was set running
505 * KERN_NOT_WAITING - Thread was not waiting
1c79356b 506 */
9bccf70c 507kern_return_t
91447636 508thread_go(
1c79356b 509 thread_t thread,
55e303ae 510 wait_result_t wresult)
1c79356b 511{
1c79356b 512 assert(thread->at_safe_point == FALSE);
9bccf70c 513 assert(thread->wait_event == NO_EVENT64);
1c79356b
A
514 assert(thread->wait_queue == WAIT_QUEUE_NULL);
515
9bccf70c 516 if ((thread->state & (TH_WAIT|TH_TERMINATE)) == TH_WAIT) {
91447636 517 if (!thread_unblock(thread, wresult))
55e303ae 518 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
55e303ae
A
519
520 return (KERN_SUCCESS);
1c79356b 521 }
55e303ae
A
522
523 return (KERN_NOT_WAITING);
1c79356b
A
524}
525
9bccf70c
A
526/*
527 * Routine: thread_mark_wait_locked
528 * Purpose:
529 * Mark a thread as waiting. If, given the circumstances,
530 * it doesn't want to wait (i.e. already aborted), then
531 * indicate that in the return value.
532 * Conditions:
533 * at splsched() and thread is locked.
534 */
535__private_extern__
536wait_result_t
1c79356b 537thread_mark_wait_locked(
9bccf70c
A
538 thread_t thread,
539 wait_interrupt_t interruptible)
1c79356b 540{
55e303ae 541 boolean_t at_safe_point;
1c79356b 542
b0d623f7
A
543 assert(thread == current_thread());
544
9bccf70c
A
545 /*
546 * The thread may have certain types of interrupts/aborts masked
547 * off. Even if the wait location says these types of interrupts
548 * are OK, we have to honor mask settings (outer-scoped code may
549 * not be able to handle aborts at the moment).
550 */
91447636
A
551 if (interruptible > (thread->options & TH_OPT_INTMASK))
552 interruptible = thread->options & TH_OPT_INTMASK;
9bccf70c
A
553
554 at_safe_point = (interruptible == THREAD_ABORTSAFE);
555
55e303ae 556 if ( interruptible == THREAD_UNINT ||
2d21ac55 557 !(thread->sched_mode & TH_MODE_ABORT) ||
55e303ae 558 (!at_safe_point &&
2d21ac55 559 (thread->sched_mode & TH_MODE_ABORTSAFELY))) {
b0d623f7
A
560
561 DTRACE_SCHED(sleep);
562
9bccf70c
A
563 thread->state |= (interruptible) ? TH_WAIT : (TH_WAIT | TH_UNINT);
564 thread->at_safe_point = at_safe_point;
9bccf70c 565 return (thread->wait_result = THREAD_WAITING);
9bccf70c 566 }
55e303ae 567 else
2d21ac55
A
568 if (thread->sched_mode & TH_MODE_ABORTSAFELY)
569 thread->sched_mode &= ~TH_MODE_ISABORTED;
55e303ae 570
9bccf70c 571 return (thread->wait_result = THREAD_INTERRUPTED);
1c79356b
A
572}
573
9bccf70c
A
574/*
575 * Routine: thread_interrupt_level
576 * Purpose:
577 * Set the maximum interruptible state for the
578 * current thread. The effective value of any
579 * interruptible flag passed into assert_wait
580 * will never exceed this.
581 *
582 * Useful for code that must not be interrupted,
583 * but which calls code that doesn't know that.
584 * Returns:
585 * The old interrupt level for the thread.
586 */
587__private_extern__
588wait_interrupt_t
589thread_interrupt_level(
590 wait_interrupt_t new_level)
591{
592 thread_t thread = current_thread();
91447636 593 wait_interrupt_t result = thread->options & TH_OPT_INTMASK;
1c79356b 594
91447636 595 thread->options = (thread->options & ~TH_OPT_INTMASK) | (new_level & TH_OPT_INTMASK);
1c79356b 596
91447636 597 return result;
1c79356b
A
598}
599
600/*
601 * Check to see if an assert wait is possible, without actually doing one.
602 * This is used by debug code in locks and elsewhere to verify that it is
603 * always OK to block when trying to take a blocking lock (since waiting
604 * for the actual assert_wait to catch the case may make it hard to detect
605 * this case.
606 */
607boolean_t
608assert_wait_possible(void)
609{
610
611 thread_t thread;
1c79356b
A
612
613#if DEBUG
614 if(debug_mode) return TRUE; /* Always succeed in debug mode */
615#endif
616
617 thread = current_thread();
618
619 return (thread == NULL || wait_queue_assert_possible(thread));
620}
621
622/*
623 * assert_wait:
624 *
625 * Assert that the current thread is about to go to
626 * sleep until the specified event occurs.
627 */
9bccf70c 628wait_result_t
1c79356b
A
629assert_wait(
630 event_t event,
9bccf70c 631 wait_interrupt_t interruptible)
1c79356b
A
632{
633 register wait_queue_t wq;
634 register int index;
635
636 assert(event != NO_EVENT);
1c79356b
A
637
638 index = wait_hash(event);
639 wq = &wait_queues[index];
91447636 640 return wait_queue_assert_wait(wq, event, interruptible, 0);
9bccf70c
A
641}
642
91447636
A
643wait_result_t
644assert_wait_timeout(
645 event_t event,
646 wait_interrupt_t interruptible,
647 uint32_t interval,
648 uint32_t scale_factor)
55e303ae 649{
91447636
A
650 thread_t thread = current_thread();
651 wait_result_t wresult;
652 wait_queue_t wqueue;
653 uint64_t deadline;
654 spl_t s;
655
55e303ae 656 assert(event != NO_EVENT);
91447636
A
657 wqueue = &wait_queues[wait_hash(event)];
658
659 s = splsched();
660 wait_queue_lock(wqueue);
661 thread_lock(thread);
662
663 clock_interval_to_deadline(interval, scale_factor, &deadline);
b0d623f7 664 wresult = wait_queue_assert_wait64_locked(wqueue, CAST_DOWN(event64_t, event),
91447636
A
665 interruptible, deadline, thread);
666
667 thread_unlock(thread);
668 wait_queue_unlock(wqueue);
669 splx(s);
55e303ae 670
91447636 671 return (wresult);
55e303ae
A
672}
673
674wait_result_t
91447636 675assert_wait_deadline(
55e303ae 676 event_t event,
91447636
A
677 wait_interrupt_t interruptible,
678 uint64_t deadline)
55e303ae
A
679{
680 thread_t thread = current_thread();
91447636
A
681 wait_result_t wresult;
682 wait_queue_t wqueue;
55e303ae
A
683 spl_t s;
684
685 assert(event != NO_EVENT);
91447636 686 wqueue = &wait_queues[wait_hash(event)];
55e303ae
A
687
688 s = splsched();
91447636 689 wait_queue_lock(wqueue);
55e303ae
A
690 thread_lock(thread);
691
b0d623f7 692 wresult = wait_queue_assert_wait64_locked(wqueue, CAST_DOWN(event64_t,event),
91447636 693 interruptible, deadline, thread);
55e303ae
A
694
695 thread_unlock(thread);
91447636 696 wait_queue_unlock(wqueue);
55e303ae
A
697 splx(s);
698
699 return (wresult);
700}
9bccf70c
A
701
702/*
703 * thread_sleep_fast_usimple_lock:
704 *
705 * Cause the current thread to wait until the specified event
706 * occurs. The specified simple_lock is unlocked before releasing
707 * the cpu and re-acquired as part of waking up.
708 *
709 * This is the simple lock sleep interface for components that use a
710 * faster version of simple_lock() than is provided by usimple_lock().
711 */
712__private_extern__ wait_result_t
713thread_sleep_fast_usimple_lock(
714 event_t event,
715 simple_lock_t lock,
716 wait_interrupt_t interruptible)
717{
718 wait_result_t res;
719
720 res = assert_wait(event, interruptible);
721 if (res == THREAD_WAITING) {
722 simple_unlock(lock);
723 res = thread_block(THREAD_CONTINUE_NULL);
724 simple_lock(lock);
725 }
726 return res;
1c79356b
A
727}
728
9bccf70c
A
729
730/*
731 * thread_sleep_usimple_lock:
732 *
733 * Cause the current thread to wait until the specified event
734 * occurs. The specified usimple_lock is unlocked before releasing
735 * the cpu and re-acquired as part of waking up.
736 *
737 * This is the simple lock sleep interface for components where
738 * simple_lock() is defined in terms of usimple_lock().
739 */
740wait_result_t
741thread_sleep_usimple_lock(
742 event_t event,
743 usimple_lock_t lock,
744 wait_interrupt_t interruptible)
745{
746 wait_result_t res;
747
748 res = assert_wait(event, interruptible);
749 if (res == THREAD_WAITING) {
750 usimple_unlock(lock);
751 res = thread_block(THREAD_CONTINUE_NULL);
752 usimple_lock(lock);
753 }
754 return res;
755}
756
9bccf70c
A
757/*
758 * thread_sleep_lock_write:
759 *
760 * Cause the current thread to wait until the specified event
761 * occurs. The specified (write) lock is unlocked before releasing
762 * the cpu. The (write) lock will be re-acquired before returning.
9bccf70c
A
763 */
764wait_result_t
765thread_sleep_lock_write(
766 event_t event,
767 lock_t *lock,
768 wait_interrupt_t interruptible)
769{
770 wait_result_t res;
771
772 res = assert_wait(event, interruptible);
773 if (res == THREAD_WAITING) {
774 lock_write_done(lock);
775 res = thread_block(THREAD_CONTINUE_NULL);
776 lock_write(lock);
777 }
778 return res;
779}
780
1c79356b 781/*
91447636 782 * thread_stop:
1c79356b 783 *
91447636
A
784 * Force a preemption point for a thread and wait
785 * for it to stop running. Arbitrates access among
786 * multiple stop requests. (released by unstop)
1c79356b 787 *
91447636
A
788 * The thread must enter a wait state and stop via a
789 * separate means.
1c79356b 790 *
91447636 791 * Returns FALSE if interrupted.
1c79356b
A
792 */
793boolean_t
794thread_stop(
91447636 795 thread_t thread)
1c79356b 796{
91447636 797 wait_result_t wresult;
2d21ac55 798 spl_t s = splsched();
1c79356b 799
1c79356b 800 wake_lock(thread);
2d21ac55 801 thread_lock(thread);
1c79356b
A
802
803 while (thread->state & TH_SUSP) {
804 thread->wake_active = TRUE;
2d21ac55
A
805 thread_unlock(thread);
806
91447636 807 wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
1c79356b
A
808 wake_unlock(thread);
809 splx(s);
810
91447636
A
811 if (wresult == THREAD_WAITING)
812 wresult = thread_block(THREAD_CONTINUE_NULL);
9bccf70c 813
91447636 814 if (wresult != THREAD_AWAKENED)
1c79356b
A
815 return (FALSE);
816
817 s = splsched();
818 wake_lock(thread);
2d21ac55 819 thread_lock(thread);
1c79356b 820 }
9bccf70c 821
1c79356b 822 thread->state |= TH_SUSP;
1c79356b 823
9bccf70c 824 while (thread->state & TH_RUN) {
9bccf70c
A
825 processor_t processor = thread->last_processor;
826
2d21ac55 827 if (processor != PROCESSOR_NULL && processor->active_thread == thread)
9bccf70c 828 cause_ast_check(processor);
9bccf70c
A
829
830 thread->wake_active = TRUE;
2d21ac55
A
831 thread_unlock(thread);
832
91447636 833 wresult = assert_wait(&thread->wake_active, THREAD_ABORTSAFE);
9bccf70c
A
834 wake_unlock(thread);
835 splx(s);
836
91447636
A
837 if (wresult == THREAD_WAITING)
838 wresult = thread_block(THREAD_CONTINUE_NULL);
9bccf70c 839
91447636 840 if (wresult != THREAD_AWAKENED) {
9bccf70c
A
841 thread_unstop(thread);
842 return (FALSE);
843 }
844
845 s = splsched();
846 wake_lock(thread);
847 thread_lock(thread);
848 }
849
850 thread_unlock(thread);
1c79356b
A
851 wake_unlock(thread);
852 splx(s);
853
854 return (TRUE);
855}
856
857/*
91447636
A
858 * thread_unstop:
859 *
860 * Release a previous stop request and set
861 * the thread running if appropriate.
862 *
863 * Use only after a successful stop operation.
1c79356b
A
864 */
865void
866thread_unstop(
9bccf70c 867 thread_t thread)
1c79356b 868{
9bccf70c 869 spl_t s = splsched();
1c79356b 870
1c79356b
A
871 wake_lock(thread);
872 thread_lock(thread);
873
9bccf70c 874 if ((thread->state & (TH_RUN|TH_WAIT|TH_SUSP)) == TH_SUSP) {
0b4e3aa0 875 thread->state &= ~TH_SUSP;
91447636 876 thread_unblock(thread, THREAD_AWAKENED);
55e303ae
A
877
878 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
1c79356b
A
879 }
880 else
881 if (thread->state & TH_SUSP) {
882 thread->state &= ~TH_SUSP;
883
884 if (thread->wake_active) {
885 thread->wake_active = FALSE;
886 thread_unlock(thread);
2d21ac55
A
887
888 thread_wakeup(&thread->wake_active);
1c79356b
A
889 wake_unlock(thread);
890 splx(s);
1c79356b
A
891
892 return;
893 }
894 }
895
896 thread_unlock(thread);
897 wake_unlock(thread);
898 splx(s);
899}
900
901/*
91447636
A
902 * thread_wait:
903 *
904 * Wait for a thread to stop running. (non-interruptible)
905 *
1c79356b 906 */
91447636 907void
1c79356b 908thread_wait(
91447636 909 thread_t thread)
1c79356b 910{
91447636
A
911 wait_result_t wresult;
912 spl_t s = splsched();
1c79356b 913
1c79356b 914 wake_lock(thread);
9bccf70c 915 thread_lock(thread);
1c79356b 916
9bccf70c 917 while (thread->state & TH_RUN) {
9bccf70c 918 processor_t processor = thread->last_processor;
e7c99d92 919
2d21ac55 920 if (processor != PROCESSOR_NULL && processor->active_thread == thread)
9bccf70c 921 cause_ast_check(processor);
1c79356b
A
922
923 thread->wake_active = TRUE;
2d21ac55
A
924 thread_unlock(thread);
925
91447636 926 wresult = assert_wait(&thread->wake_active, THREAD_UNINT);
1c79356b
A
927 wake_unlock(thread);
928 splx(s);
929
91447636
A
930 if (wresult == THREAD_WAITING)
931 thread_block(THREAD_CONTINUE_NULL);
1c79356b
A
932
933 s = splsched();
934 wake_lock(thread);
9bccf70c 935 thread_lock(thread);
1c79356b 936 }
0b4e3aa0 937
9bccf70c 938 thread_unlock(thread);
1c79356b
A
939 wake_unlock(thread);
940 splx(s);
1c79356b
A
941}
942
1c79356b
A
943/*
944 * Routine: clear_wait_internal
945 *
946 * Clear the wait condition for the specified thread.
947 * Start the thread executing if that is appropriate.
948 * Arguments:
949 * thread thread to awaken
950 * result Wakeup result the thread should see
951 * Conditions:
952 * At splsched
953 * the thread is locked.
9bccf70c
A
954 * Returns:
955 * KERN_SUCCESS thread was rousted out a wait
956 * KERN_FAILURE thread was waiting but could not be rousted
957 * KERN_NOT_WAITING thread was not waiting
1c79356b 958 */
9bccf70c 959__private_extern__ kern_return_t
1c79356b 960clear_wait_internal(
9bccf70c 961 thread_t thread,
55e303ae 962 wait_result_t wresult)
1c79356b 963{
9bccf70c 964 wait_queue_t wq = thread->wait_queue;
55e303ae 965 int i = LockTimeOut;
9bccf70c 966
9bccf70c 967 do {
55e303ae
A
968 if (wresult == THREAD_INTERRUPTED && (thread->state & TH_UNINT))
969 return (KERN_FAILURE);
9bccf70c
A
970
971 if (wq != WAIT_QUEUE_NULL) {
972 if (wait_queue_lock_try(wq)) {
973 wait_queue_pull_thread_locked(wq, thread, TRUE);
974 /* wait queue unlocked, thread still locked */
55e303ae
A
975 }
976 else {
9bccf70c
A
977 thread_unlock(thread);
978 delay(1);
55e303ae 979
9bccf70c 980 thread_lock(thread);
55e303ae
A
981 if (wq != thread->wait_queue)
982 return (KERN_NOT_WAITING);
9bccf70c 983
9bccf70c
A
984 continue;
985 }
1c79356b 986 }
55e303ae 987
91447636 988 return (thread_go(thread, wresult));
55e303ae
A
989 } while (--i > 0);
990
2d21ac55 991 panic("clear_wait_internal: deadlock: thread=%p, wq=%p, cpu=%d\n",
9bccf70c 992 thread, wq, cpu_number());
55e303ae
A
993
994 return (KERN_FAILURE);
1c79356b
A
995}
996
997
998/*
999 * clear_wait:
1000 *
1001 * Clear the wait condition for the specified thread. Start the thread
1002 * executing if that is appropriate.
1003 *
1004 * parameters:
1005 * thread thread to awaken
1006 * result Wakeup result the thread should see
1007 */
9bccf70c 1008kern_return_t
1c79356b 1009clear_wait(
9bccf70c
A
1010 thread_t thread,
1011 wait_result_t result)
1c79356b 1012{
9bccf70c 1013 kern_return_t ret;
1c79356b
A
1014 spl_t s;
1015
1016 s = splsched();
1017 thread_lock(thread);
9bccf70c 1018 ret = clear_wait_internal(thread, result);
1c79356b
A
1019 thread_unlock(thread);
1020 splx(s);
9bccf70c 1021 return ret;
1c79356b
A
1022}
1023
1024
1025/*
1026 * thread_wakeup_prim:
1027 *
1028 * Common routine for thread_wakeup, thread_wakeup_with_result,
1029 * and thread_wakeup_one.
1030 *
1031 */
9bccf70c 1032kern_return_t
1c79356b
A
1033thread_wakeup_prim(
1034 event_t event,
1035 boolean_t one_thread,
9bccf70c 1036 wait_result_t result)
1c79356b
A
1037{
1038 register wait_queue_t wq;
1039 register int index;
1040
1041 index = wait_hash(event);
1042 wq = &wait_queues[index];
1043 if (one_thread)
9bccf70c 1044 return (wait_queue_wakeup_one(wq, event, result));
1c79356b 1045 else
9bccf70c 1046 return (wait_queue_wakeup_all(wq, event, result));
1c79356b
A
1047}
1048
1049/*
1050 * thread_bind:
1051 *
2d21ac55 1052 * Force the current thread to execute on the specified processor.
1c79356b 1053 *
55e303ae
A
1054 * Returns the previous binding. PROCESSOR_NULL means
1055 * not bound.
1056 *
1057 * XXX - DO NOT export this to users - XXX
1c79356b 1058 */
55e303ae 1059processor_t
1c79356b 1060thread_bind(
2d21ac55 1061 processor_t processor)
1c79356b 1062{
2d21ac55 1063 thread_t self = current_thread();
55e303ae 1064 processor_t prev;
55e303ae 1065 spl_t s;
1c79356b
A
1066
1067 s = splsched();
2d21ac55 1068 thread_lock(self);
55e303ae 1069
2d21ac55
A
1070 prev = self->bound_processor;
1071 self->bound_processor = processor;
55e303ae 1072
2d21ac55 1073 thread_unlock(self);
1c79356b 1074 splx(s);
55e303ae
A
1075
1076 return (prev);
1c79356b
A
1077}
1078
1079/*
2d21ac55
A
1080 * thread_select:
1081 *
1082 * Select a new thread for the current processor to execute.
55e303ae
A
1083 *
1084 * May select the current thread, which must be locked.
1c79356b 1085 */
2d21ac55 1086static thread_t
1c79356b 1087thread_select(
2d21ac55
A
1088 thread_t thread,
1089 processor_t processor)
1c79356b 1090{
2d21ac55 1091 processor_set_t pset = processor->processor_set;
cf7d32b8 1092 thread_t new_thread = THREAD_NULL;
b0d623f7 1093 boolean_t inactive_state;
1c79356b 1094
2d21ac55
A
1095 do {
1096 /*
1097 * Update the priority.
1098 */
1099 if (thread->sched_stamp != sched_tick)
1100 update_priority(thread);
0b4e3aa0 1101
2d21ac55 1102 processor->current_pri = thread->sched_pri;
1c79356b 1103
2d21ac55
A
1104 pset_lock(pset);
1105
b7266188 1106 inactive_state = processor->state != PROCESSOR_SHUTDOWN && machine_processor_is_inactive(processor);
c910b4d9 1107
2d21ac55
A
1108 simple_lock(&rt_lock);
1109
2d21ac55
A
1110 /*
1111 * Test to see if the current thread should continue
1112 * to run on this processor. Must be runnable, and not
1113 * bound to a different processor, nor be in the wrong
1114 * processor set.
1115 */
d1ecb069
A
1116 if (
1117#if CONFIG_EMBEDDED
1118 ((thread->state & ~TH_SUSP) == TH_RUN) &&
1119#else
1120 thread->state == TH_RUN &&
1121#endif
b0d623f7
A
1122 (thread->sched_pri >= BASEPRI_RTQUEUES ||
1123 processor->processor_meta == PROCESSOR_META_NULL ||
1124 processor->processor_meta->primary == processor) &&
2d21ac55
A
1125 (thread->bound_processor == PROCESSOR_NULL ||
1126 thread->bound_processor == processor) &&
1127 (thread->affinity_set == AFFINITY_SET_NULL ||
1128 thread->affinity_set->aset_pset == pset) ) {
1129 if ( thread->sched_pri >= BASEPRI_RTQUEUES &&
1130 first_timeslice(processor) ) {
1131 if (rt_runq.highq >= BASEPRI_RTQUEUES) {
1132 register run_queue_t runq = &rt_runq;
1133 register queue_t q;
1134
1135 q = runq->queues + runq->highq;
1136 if (((thread_t)q->next)->realtime.deadline <
1137 processor->deadline) {
1138 thread = (thread_t)q->next;
1139 ((queue_entry_t)thread)->next->prev = q;
1140 q->next = ((queue_entry_t)thread)->next;
1141 thread->runq = PROCESSOR_NULL;
2d21ac55 1142 runq->count--; runq->urgency--;
4a3eedf9 1143 assert(runq->urgency >= 0);
2d21ac55
A
1144 if (queue_empty(q)) {
1145 if (runq->highq != IDLEPRI)
1146 clrbit(MAXPRI - runq->highq, runq->bitmap);
1147 runq->highq = MAXPRI - ffsbit(runq->bitmap);
1148 }
55e303ae
A
1149 }
1150 }
2d21ac55
A
1151
1152 simple_unlock(&rt_lock);
1153
1154 processor->deadline = thread->realtime.deadline;
1155
1156 pset_unlock(pset);
1157
1158 return (thread);
55e303ae
A
1159 }
1160
b0d623f7
A
1161 if (!inactive_state && rt_runq.highq < thread->sched_pri &&
1162 (new_thread = choose_thread(processor, thread->sched_pri)) == THREAD_NULL) {
55e303ae 1163
2d21ac55 1164 simple_unlock(&rt_lock);
55e303ae 1165
2d21ac55 1166 /* I am the highest priority runnable (non-idle) thread */
1c79356b 1167
cf7d32b8 1168 pset_pri_hint(pset, processor, processor->current_pri);
1c79356b 1169
c910b4d9
A
1170 pset_count_hint(pset, processor, processor->runq.count);
1171
2d21ac55 1172 processor->deadline = UINT64_MAX;
55e303ae 1173
2d21ac55 1174 pset_unlock(pset);
55e303ae 1175
2d21ac55
A
1176 return (thread);
1177 }
1178 }
1179
b0d623f7
A
1180 if (new_thread != THREAD_NULL ||
1181 (processor->runq.highq >= rt_runq.highq &&
1182 (new_thread = choose_thread(processor, MINPRI)) != THREAD_NULL)) {
c910b4d9
A
1183 simple_unlock(&rt_lock);
1184
c910b4d9 1185 if (!inactive_state) {
b0d623f7 1186 pset_pri_hint(pset, processor, new_thread->sched_pri);
c910b4d9
A
1187
1188 pset_count_hint(pset, processor, processor->runq.count);
1189 }
1190
1191 processor->deadline = UINT64_MAX;
1192 pset_unlock(pset);
1193
b0d623f7
A
1194 return (new_thread);
1195 }
c910b4d9 1196
b0d623f7 1197 if (rt_runq.count > 0) {
c910b4d9
A
1198 thread = run_queue_dequeue(&rt_runq, SCHED_HEADQ);
1199 simple_unlock(&rt_lock);
1200
1201 processor->deadline = thread->realtime.deadline;
1202 pset_unlock(pset);
1203
1204 return (thread);
1205 }
2d21ac55
A
1206
1207 simple_unlock(&rt_lock);
55e303ae 1208
c910b4d9
A
1209 processor->deadline = UINT64_MAX;
1210
b0d623f7
A
1211 /*
1212 * Set processor inactive based on
1213 * indication from the platform code.
1214 */
c910b4d9
A
1215 if (inactive_state) {
1216 if (processor->state == PROCESSOR_RUNNING)
1217 remqueue(&pset->active_queue, (queue_entry_t)processor);
1218 else
1219 if (processor->state == PROCESSOR_IDLE)
1220 remqueue(&pset->idle_queue, (queue_entry_t)processor);
1221
1222 processor->state = PROCESSOR_INACTIVE;
1223
1224 pset_unlock(pset);
1225
1226 return (processor->idle_thread);
1227 }
1228
2d21ac55
A
1229 /*
1230 * No runnable threads, attempt to steal
1231 * from other processors.
1232 */
cf7d32b8
A
1233 new_thread = steal_thread(pset);
1234 if (new_thread != THREAD_NULL)
1235 return (new_thread);
2d21ac55 1236
cf7d32b8
A
1237 /*
1238 * If other threads have appeared, shortcut
1239 * around again.
1240 */
1241 if (processor->runq.count > 0 || rt_runq.count > 0)
1242 continue;
1243
1244 pset_lock(pset);
55e303ae 1245
1c79356b
A
1246 /*
1247 * Nothing is runnable, so set this processor idle if it
2d21ac55 1248 * was running.
1c79356b 1249 */
55e303ae
A
1250 if (processor->state == PROCESSOR_RUNNING) {
1251 remqueue(&pset->active_queue, (queue_entry_t)processor);
1252 processor->state = PROCESSOR_IDLE;
1c79356b 1253
b0d623f7
A
1254 if (processor->processor_meta == PROCESSOR_META_NULL || processor->processor_meta->primary == processor) {
1255 enqueue_head(&pset->idle_queue, (queue_entry_t)processor);
1256 pset->low_pri = pset->low_count = processor;
1257 }
1258 else {
1259 enqueue_head(&processor->processor_meta->idle_queue, (queue_entry_t)processor);
0b4c1975
A
1260 pset_unlock(pset);
1261 return (processor->idle_thread);
b0d623f7 1262 }
1c79356b 1263 }
1c79356b 1264
2d21ac55
A
1265 pset_unlock(pset);
1266
1267 /*
1268 * Choose idle thread if fast idle is not possible.
1269 */
1270 if ((thread->state & (TH_IDLE|TH_TERMINATE|TH_SUSP)) || !(thread->state & TH_WAIT) || thread->wake_active)
1271 return (processor->idle_thread);
1272
1273 /*
1274 * Perform idling activities directly without a
1275 * context switch. Return dispatched thread,
1276 * else check again for a runnable thread.
1277 */
1278 new_thread = thread_select_idle(thread, processor);
1279
1280 } while (new_thread == THREAD_NULL);
1281
1282 return (new_thread);
1283}
1284
1285/*
1286 * thread_select_idle:
1287 *
1288 * Idle the processor using the current thread context.
1289 *
1290 * Called with thread locked, then dropped and relocked.
1291 */
1292static thread_t
1293thread_select_idle(
1294 thread_t thread,
1295 processor_t processor)
1296{
1297 thread_t new_thread;
1298
1299 if (thread->sched_mode & TH_MODE_TIMESHARE)
1300 sched_share_decr();
1301 sched_run_decr();
1302
1303 thread->state |= TH_IDLE;
1304 processor->current_pri = IDLEPRI;
1305
1306 thread_unlock(thread);
1307
1308 /*
1309 * Switch execution timing to processor idle thread.
1310 */
1311 processor->last_dispatch = mach_absolute_time();
1312 thread_timer_event(processor->last_dispatch, &processor->idle_thread->system_timer);
1313 PROCESSOR_DATA(processor, kernel_timer) = &processor->idle_thread->system_timer;
1314
1315 /*
1316 * Cancel the quantum timer while idling.
1317 */
1318 timer_call_cancel(&processor->quantum_timer);
1319 processor->timeslice = 0;
1320
1321 (*thread->sched_call)(SCHED_CALL_BLOCK, thread);
1322
1323 /*
1324 * Enable interrupts and perform idling activities. No
1325 * preemption due to TH_IDLE being set.
1326 */
1327 spllo(); new_thread = processor_idle(thread, processor);
1328
cf7d32b8
A
1329 /*
1330 * Return at splsched.
1331 */
2d21ac55
A
1332 (*thread->sched_call)(SCHED_CALL_UNBLOCK, thread);
1333
1334 thread_lock(thread);
1335
1336 /*
1337 * If awakened, switch to thread timer and start a new quantum.
1338 * Otherwise skip; we will context switch to another thread or return here.
1339 */
1340 if (!(thread->state & TH_WAIT)) {
1341 processor->last_dispatch = mach_absolute_time();
1342 thread_timer_event(processor->last_dispatch, &thread->system_timer);
1343 PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
1344
1345 thread_quantum_init(thread);
1346
1347 processor->quantum_end = processor->last_dispatch + thread->current_quantum;
1348 timer_call_enter1(&processor->quantum_timer, thread, processor->quantum_end);
1349 processor->timeslice = 1;
1350
1351 thread->computation_epoch = processor->last_dispatch;
1c79356b
A
1352 }
1353
2d21ac55 1354 thread->state &= ~TH_IDLE;
55e303ae 1355
2d21ac55
A
1356 sched_run_incr();
1357 if (thread->sched_mode & TH_MODE_TIMESHARE)
1358 sched_share_incr();
1359
1360 return (new_thread);
1c79356b
A
1361}
1362
b0d623f7
A
1363/*
1364 * choose_thread:
1365 *
1366 * Locate a thread to execute from the processor run queue
1367 * and return it. Only choose a thread with greater or equal
1368 * priority.
1369 *
1370 * Associated pset must be locked. Returns THREAD_NULL
1371 * on failure.
1372 */
1373static thread_t
1374choose_thread(
1375 processor_t processor,
1376 int priority)
1377{
1378 run_queue_t rq = &processor->runq;
1379 queue_t queue = rq->queues + rq->highq;
1380 int pri = rq->highq, count = rq->count;
1381 thread_t thread;
1382
1383 while (count > 0 && pri >= priority) {
1384 thread = (thread_t)queue_first(queue);
1385 while (!queue_end(queue, (queue_entry_t)thread)) {
1386 if (thread->bound_processor == PROCESSOR_NULL ||
1387 thread->bound_processor == processor) {
1388 remqueue(queue, (queue_entry_t)thread);
1389
1390 thread->runq = PROCESSOR_NULL;
1391 rq->count--;
1392 if (testbit(pri, sched_preempt_pri)) {
1393 rq->urgency--; assert(rq->urgency >= 0);
1394 }
1395 if (queue_empty(queue)) {
1396 if (pri != IDLEPRI)
1397 clrbit(MAXPRI - pri, rq->bitmap);
1398 rq->highq = MAXPRI - ffsbit(rq->bitmap);
1399 }
1400
1401 return (thread);
1402 }
1403 count--;
1404
1405 thread = (thread_t)queue_next((queue_entry_t)thread);
1406 }
1407
1408 queue--; pri--;
1409 }
1410
1411 return (THREAD_NULL);
1412}
1413
1c79356b 1414/*
55e303ae
A
1415 * Perform a context switch and start executing the new thread.
1416 *
91447636 1417 * Returns FALSE on failure, and the thread is re-dispatched.
9bccf70c 1418 *
55e303ae 1419 * Called at splsched.
1c79356b
A
1420 */
1421
55e303ae
A
1422#define funnel_release_check(thread, debug) \
1423MACRO_BEGIN \
1424 if ((thread)->funnel_state & TH_FN_OWNED) { \
1425 (thread)->funnel_state = TH_FN_REFUNNEL; \
1426 KERNEL_DEBUG(0x603242c | DBG_FUNC_NONE, \
1427 (thread)->funnel_lock, (debug), 0, 0, 0); \
1428 funnel_unlock((thread)->funnel_lock); \
1429 } \
1430MACRO_END
1431
1432#define funnel_refunnel_check(thread, debug) \
1433MACRO_BEGIN \
1434 if ((thread)->funnel_state & TH_FN_REFUNNEL) { \
1435 kern_return_t result = (thread)->wait_result; \
1436 \
1437 (thread)->funnel_state = 0; \
1438 KERNEL_DEBUG(0x6032428 | DBG_FUNC_NONE, \
1439 (thread)->funnel_lock, (debug), 0, 0, 0); \
1440 funnel_lock((thread)->funnel_lock); \
1441 KERNEL_DEBUG(0x6032430 | DBG_FUNC_NONE, \
1442 (thread)->funnel_lock, (debug), 0, 0, 0); \
1443 (thread)->funnel_state = TH_FN_OWNED; \
1444 (thread)->wait_result = result; \
1445 } \
1446MACRO_END
1447
2d21ac55 1448static boolean_t
1c79356b 1449thread_invoke(
2d21ac55
A
1450 register thread_t self,
1451 register thread_t thread,
91447636 1452 ast_t reason)
1c79356b 1453{
2d21ac55
A
1454 thread_continue_t continuation = self->continuation;
1455 void *parameter = self->parameter;
9bccf70c 1456 processor_t processor;
1c79356b 1457
b0d623f7
A
1458 if (get_preemption_level() != 0) {
1459 int pl = get_preemption_level();
1460 panic("thread_invoke: preemption_level %d, possible cause: %s",
1461 pl, (pl < 0 ? "unlocking an unlocked mutex or spinlock" :
1462 "blocking while holding a spinlock, or within interrupt context"));
1463 }
0b4e3aa0 1464
2d21ac55 1465 assert(self == current_thread());
91447636 1466
1c79356b 1467 /*
9bccf70c 1468 * Mark thread interruptible.
1c79356b 1469 */
2d21ac55
A
1470 thread_lock(thread);
1471 thread->state &= ~TH_UNINT;
1c79356b 1472
2d21ac55
A
1473#if DEBUG
1474 assert(thread_runnable(thread));
1475#endif
1c79356b 1476
9bccf70c
A
1477 /*
1478 * Allow time constraint threads to hang onto
1479 * a stack.
1480 */
2d21ac55
A
1481 if ((self->sched_mode & TH_MODE_REALTIME) && !self->reserved_stack)
1482 self->reserved_stack = self->kernel_stack;
1c79356b 1483
91447636 1484 if (continuation != NULL) {
2d21ac55 1485 if (!thread->kernel_stack) {
9bccf70c 1486 /*
2d21ac55 1487 * If we are using a privileged stack,
9bccf70c 1488 * check to see whether we can exchange it with
2d21ac55 1489 * that of the other thread.
9bccf70c 1490 */
2d21ac55 1491 if (self->kernel_stack == self->reserved_stack && !thread->reserved_stack)
9bccf70c 1492 goto need_stack;
1c79356b 1493
91447636
A
1494 /*
1495 * Context switch by performing a stack handoff.
1496 */
2d21ac55
A
1497 continuation = thread->continuation;
1498 parameter = thread->parameter;
1c79356b 1499
9bccf70c 1500 processor = current_processor();
2d21ac55
A
1501 processor->active_thread = thread;
1502 processor->current_pri = thread->sched_pri;
1503 if (thread->last_processor != processor && thread->last_processor != NULL) {
1504 if (thread->last_processor->processor_set != processor->processor_set)
1505 thread->ps_switch++;
1506 thread->p_switch++;
1507 }
1508 thread->last_processor = processor;
1509 thread->c_switch++;
1510 ast_context(thread);
1511 thread_unlock(thread);
1c79356b 1512
2d21ac55 1513 self->reason = reason;
91447636
A
1514
1515 processor->last_dispatch = mach_absolute_time();
2d21ac55
A
1516 thread_timer_event(processor->last_dispatch, &thread->system_timer);
1517 PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
1518
1519 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_STACK_HANDOFF)|DBG_FUNC_NONE,
b0d623f7 1520 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
1c79356b 1521
b0d623f7
A
1522 DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, thread->task->bsd_info);
1523
1524 TLOG(1, "thread_invoke: calling machine_stack_handoff\n");
2d21ac55 1525 machine_stack_handoff(self, thread);
9bccf70c 1526
b0d623f7
A
1527 DTRACE_SCHED(on__cpu);
1528
2d21ac55 1529 thread_dispatch(self, thread);
1c79356b 1530
2d21ac55 1531 thread->continuation = thread->parameter = NULL;
1c79356b 1532
2d21ac55 1533 counter(c_thread_invoke_hits++);
1c79356b 1534
2d21ac55 1535 funnel_refunnel_check(thread, 2);
9bccf70c 1536 (void) spllo();
1c79356b 1537
2d21ac55
A
1538 assert(continuation);
1539 call_continuation(continuation, parameter, thread->wait_result);
9bccf70c 1540 /*NOTREACHED*/
9bccf70c 1541 }
2d21ac55 1542 else if (thread == self) {
9bccf70c 1543 /* same thread but with continuation */
2d21ac55 1544 ast_context(self);
9bccf70c 1545 counter(++c_thread_invoke_same);
2d21ac55 1546 thread_unlock(self);
9bccf70c 1547
2d21ac55
A
1548 self->continuation = self->parameter = NULL;
1549
1550 funnel_refunnel_check(self, 3);
9bccf70c 1551 (void) spllo();
55e303ae 1552
2d21ac55 1553 call_continuation(continuation, parameter, self->wait_result);
9bccf70c
A
1554 /*NOTREACHED*/
1555 }
1c79356b 1556 }
9bccf70c
A
1557 else {
1558 /*
2d21ac55 1559 * Check that the other thread has a stack
9bccf70c 1560 */
2d21ac55 1561 if (!thread->kernel_stack) {
9bccf70c 1562need_stack:
2d21ac55
A
1563 if (!stack_alloc_try(thread)) {
1564 counter(c_thread_invoke_misses++);
1565 thread_unlock(thread);
1566 thread_stack_enqueue(thread);
9bccf70c
A
1567 return (FALSE);
1568 }
9bccf70c 1569 }
2d21ac55
A
1570 else if (thread == self) {
1571 ast_context(self);
9bccf70c 1572 counter(++c_thread_invoke_same);
2d21ac55 1573 thread_unlock(self);
9bccf70c
A
1574 return (TRUE);
1575 }
1576 }
1c79356b
A
1577
1578 /*
91447636 1579 * Context switch by full context save.
1c79356b 1580 */
9bccf70c 1581 processor = current_processor();
2d21ac55
A
1582 processor->active_thread = thread;
1583 processor->current_pri = thread->sched_pri;
1584 if (thread->last_processor != processor && thread->last_processor != NULL) {
1585 if (thread->last_processor->processor_set != processor->processor_set)
1586 thread->ps_switch++;
1587 thread->p_switch++;
1588 }
1589 thread->last_processor = processor;
1590 thread->c_switch++;
1591 ast_context(thread);
1592 thread_unlock(thread);
1c79356b 1593
2d21ac55 1594 counter(c_thread_invoke_csw++);
1c79356b 1595
2d21ac55
A
1596 assert(self->runq == PROCESSOR_NULL);
1597 self->reason = reason;
1c79356b 1598
91447636 1599 processor->last_dispatch = mach_absolute_time();
2d21ac55
A
1600 thread_timer_event(processor->last_dispatch, &thread->system_timer);
1601 PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
91447636 1602
2d21ac55 1603 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED) | DBG_FUNC_NONE,
b0d623f7
A
1604 self->reason, (uintptr_t)thread_tid(thread), self->sched_pri, thread->sched_pri, 0);
1605
1606 DTRACE_SCHED2(off__cpu, struct thread *, thread, struct proc *, thread->task->bsd_info);
1c79356b
A
1607
1608 /*
91447636 1609 * This is where we actually switch register context,
2d21ac55
A
1610 * and address space if required. We will next run
1611 * as a result of a subsequent context switch.
91447636 1612 */
2d21ac55 1613 thread = machine_switch_context(self, continuation, thread);
b0d623f7
A
1614 TLOG(1,"thread_invoke: returning machine_switch_context: self %p continuation %p thread %p\n", self, continuation, thread);
1615
1616 DTRACE_SCHED(on__cpu);
1c79356b
A
1617
1618 /*
2d21ac55 1619 * We have been resumed and are set to run.
1c79356b 1620 */
2d21ac55 1621 thread_dispatch(thread, self);
9bccf70c 1622
91447636 1623 if (continuation) {
2d21ac55
A
1624 self->continuation = self->parameter = NULL;
1625
1626 funnel_refunnel_check(self, 3);
9bccf70c 1627 (void) spllo();
55e303ae 1628
2d21ac55 1629 call_continuation(continuation, parameter, self->wait_result);
9bccf70c 1630 /*NOTREACHED*/
1c79356b
A
1631 }
1632
9bccf70c 1633 return (TRUE);
1c79356b
A
1634}
1635
1636/*
2d21ac55 1637 * thread_dispatch:
1c79356b 1638 *
2d21ac55
A
1639 * Handle threads at context switch. Re-dispatch other thread
1640 * if still running, otherwise update run state and perform
1641 * special actions. Update quantum for other thread and begin
1642 * the quantum for ourselves.
91447636
A
1643 *
1644 * Called at splsched.
1c79356b
A
1645 */
1646void
2d21ac55
A
1647thread_dispatch(
1648 thread_t thread,
1649 thread_t self)
1c79356b 1650{
2d21ac55
A
1651 processor_t processor = self->last_processor;
1652
1653 if (thread != THREAD_NULL) {
91447636 1654 /*
2d21ac55
A
1655 * If blocked at a continuation, discard
1656 * the stack.
91447636 1657 */
2d21ac55
A
1658 if (thread->continuation != NULL && thread->kernel_stack != 0)
1659 stack_free(thread);
1660
1661 if (!(thread->state & TH_IDLE)) {
1662 wake_lock(thread);
1663 thread_lock(thread);
9bccf70c 1664
91447636 1665 /*
2d21ac55 1666 * Compute remainder of current quantum.
91447636 1667 */
2d21ac55
A
1668 if ( first_timeslice(processor) &&
1669 processor->quantum_end > processor->last_dispatch )
b0d623f7 1670 thread->current_quantum = (uint32_t)(processor->quantum_end - processor->last_dispatch);
2d21ac55
A
1671 else
1672 thread->current_quantum = 0;
1673
1674 if (thread->sched_mode & TH_MODE_REALTIME) {
1675 /*
1676 * Cancel the deadline if the thread has
1677 * consumed the entire quantum.
1678 */
1679 if (thread->current_quantum == 0) {
1680 thread->realtime.deadline = UINT64_MAX;
1681 thread->reason |= AST_QUANTUM;
1682 }
b7266188 1683 } else {
2d21ac55
A
1684 /*
1685 * For non-realtime threads treat a tiny
1686 * remaining quantum as an expired quantum
1687 * but include what's left next time.
1688 */
1689 if (thread->current_quantum < min_std_quantum) {
1690 thread->reason |= AST_QUANTUM;
1691 thread->current_quantum += std_quantum;
1692 }
1693 }
1694
91447636 1695 /*
2d21ac55
A
1696 * If we are doing a direct handoff then
1697 * take the remainder of the quantum.
91447636 1698 */
2d21ac55
A
1699 if ((thread->reason & (AST_HANDOFF|AST_QUANTUM)) == AST_HANDOFF) {
1700 self->current_quantum = thread->current_quantum;
1701 thread->reason |= AST_QUANTUM;
1702 thread->current_quantum = 0;
91447636 1703 }
91447636 1704
b0d623f7 1705 thread->computation_metered += (processor->last_dispatch - thread->computation_epoch);
2d21ac55
A
1706
1707 if (!(thread->state & TH_WAIT)) {
1708 /*
1709 * Still running.
1710 */
1711 if (thread->reason & AST_QUANTUM)
1712 thread_setrun(thread, SCHED_TAILQ);
1713 else
1714 if (thread->reason & AST_PREEMPT)
1715 thread_setrun(thread, SCHED_HEADQ);
1716 else
1717 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
1718
1719 thread->reason = AST_NONE;
1720
1721 thread_unlock(thread);
1722 wake_unlock(thread);
1723 }
1724 else {
1725 /*
1726 * Waiting.
1727 */
b7266188
A
1728 boolean_t should_terminate = FALSE;
1729
1730 /* Only the first call to thread_dispatch
1731 * after explicit termination should add
1732 * the thread to the termination queue
1733 */
1734 if ((thread->state & (TH_TERMINATE|TH_TERMINATE2)) == TH_TERMINATE) {
1735 should_terminate = TRUE;
1736 thread->state |= TH_TERMINATE2;
1737 }
1738
2d21ac55
A
1739 thread->state &= ~TH_RUN;
1740
1741 if (thread->sched_mode & TH_MODE_TIMESHARE)
1742 sched_share_decr();
1743 sched_run_decr();
1744
b7266188
A
1745 (*thread->sched_call)(SCHED_CALL_BLOCK, thread);
1746
2d21ac55
A
1747 if (thread->wake_active) {
1748 thread->wake_active = FALSE;
1749 thread_unlock(thread);
1750
1751 thread_wakeup(&thread->wake_active);
1752 }
1753 else
1754 thread_unlock(thread);
91447636 1755
2d21ac55 1756 wake_unlock(thread);
91447636 1757
b7266188 1758 if (should_terminate)
2d21ac55
A
1759 thread_terminate_enqueue(thread);
1760 }
1761 }
91447636 1762 }
91447636 1763
2d21ac55 1764 if (!(self->state & TH_IDLE)) {
91447636 1765 /*
2d21ac55 1766 * Get a new quantum if none remaining.
91447636 1767 */
2d21ac55
A
1768 if (self->current_quantum == 0)
1769 thread_quantum_init(self);
91447636
A
1770
1771 /*
2d21ac55 1772 * Set up quantum timer and timeslice.
91447636 1773 */
2d21ac55
A
1774 processor->quantum_end = (processor->last_dispatch + self->current_quantum);
1775 timer_call_enter1(&processor->quantum_timer, self, processor->quantum_end);
91447636 1776
2d21ac55 1777 processor->timeslice = 1;
91447636 1778
b0d623f7 1779 self->computation_epoch = processor->last_dispatch;
91447636
A
1780 }
1781 else {
1782 timer_call_cancel(&processor->quantum_timer);
2d21ac55 1783 processor->timeslice = 0;
91447636
A
1784 }
1785}
1786
b0d623f7
A
1787#include <libkern/OSDebug.h>
1788
1789uint32_t kdebug_thread_block = 0;
1790
1791
91447636 1792/*
2d21ac55 1793 * thread_block_reason:
91447636 1794 *
2d21ac55
A
1795 * Forces a reschedule, blocking the caller if a wait
1796 * has been asserted.
91447636 1797 *
2d21ac55
A
1798 * If a continuation is specified, then thread_invoke will
1799 * attempt to discard the thread's kernel stack. When the
1800 * thread resumes, it will execute the continuation function
1801 * on a new kernel stack.
91447636 1802 */
2d21ac55
A
1803counter(mach_counter_t c_thread_block_calls = 0;)
1804
1805wait_result_t
1806thread_block_reason(
1807 thread_continue_t continuation,
1808 void *parameter,
1809 ast_t reason)
91447636 1810{
2d21ac55
A
1811 register thread_t self = current_thread();
1812 register processor_t processor;
1813 register thread_t new_thread;
1814 spl_t s;
1c79356b
A
1815
1816 counter(++c_thread_block_calls);
1817
1c79356b
A
1818 s = splsched();
1819
55e303ae 1820 if (!(reason & AST_PREEMPT))
91447636 1821 funnel_release_check(self, 2);
1c79356b 1822
55e303ae 1823 processor = current_processor();
1c79356b 1824
9bccf70c
A
1825 /* If we're explicitly yielding, force a subsequent quantum */
1826 if (reason & AST_YIELD)
55e303ae 1827 processor->timeslice = 0;
0b4e3aa0 1828
9bccf70c
A
1829 /* We're handling all scheduling AST's */
1830 ast_off(AST_SCHEDULING);
1c79356b 1831
91447636
A
1832 self->continuation = continuation;
1833 self->parameter = parameter;
1834
b0d623f7
A
1835 if (kdebug_thread_block && kdebug_enable && self->state != TH_RUN) {
1836 uint32_t bt[8];
1837
1838 OSBacktrace((void **)&bt[0], 8);
1839
1840 KERNEL_DEBUG_CONSTANT(0x140004c | DBG_FUNC_START, bt[0], bt[1], bt[2], bt[3], 0);
1841 KERNEL_DEBUG_CONSTANT(0x140004c | DBG_FUNC_END, bt[4], bt[5], bt[6], bt[7], 0);
1842 }
1843
2d21ac55 1844 do {
91447636 1845 thread_lock(self);
2d21ac55 1846 new_thread = thread_select(self, processor);
91447636 1847 thread_unlock(self);
2d21ac55 1848 } while (!thread_invoke(self, new_thread, reason));
1c79356b 1849
91447636 1850 funnel_refunnel_check(self, 5);
1c79356b
A
1851 splx(s);
1852
91447636 1853 return (self->wait_result);
1c79356b
A
1854}
1855
1856/*
1857 * thread_block:
1858 *
9bccf70c 1859 * Block the current thread if a wait has been asserted.
1c79356b 1860 */
91447636 1861wait_result_t
1c79356b 1862thread_block(
9bccf70c 1863 thread_continue_t continuation)
1c79356b 1864{
91447636
A
1865 return thread_block_reason(continuation, NULL, AST_NONE);
1866}
1867
1868wait_result_t
1869thread_block_parameter(
1870 thread_continue_t continuation,
1871 void *parameter)
1872{
1873 return thread_block_reason(continuation, parameter, AST_NONE);
1c79356b
A
1874}
1875
1876/*
1877 * thread_run:
1878 *
91447636 1879 * Switch directly from the current thread to the
55e303ae 1880 * new thread, handing off our quantum if appropriate.
9bccf70c
A
1881 *
1882 * New thread must be runnable, and not on a run queue.
1c79356b 1883 *
55e303ae 1884 * Called at splsched.
1c79356b
A
1885 */
1886int
1887thread_run(
91447636 1888 thread_t self,
9bccf70c 1889 thread_continue_t continuation,
91447636 1890 void *parameter,
9bccf70c 1891 thread_t new_thread)
1c79356b 1892{
9bccf70c
A
1893 ast_t handoff = AST_HANDOFF;
1894
91447636 1895 funnel_release_check(self, 3);
9bccf70c 1896
91447636
A
1897 self->continuation = continuation;
1898 self->parameter = parameter;
9bccf70c 1899
91447636 1900 while (!thread_invoke(self, new_thread, handoff)) {
2d21ac55 1901 processor_t processor = current_processor();
9bccf70c 1902
91447636 1903 thread_lock(self);
2d21ac55 1904 new_thread = thread_select(self, processor);
91447636 1905 thread_unlock(self);
9bccf70c
A
1906 handoff = AST_NONE;
1907 }
1908
91447636 1909 funnel_refunnel_check(self, 6);
9bccf70c 1910
91447636 1911 return (self->wait_result);
1c79356b
A
1912}
1913
1914/*
91447636 1915 * thread_continue:
55e303ae 1916 *
91447636
A
1917 * Called at splsched when a thread first receives
1918 * a new stack after a continuation.
1c79356b
A
1919 */
1920void
91447636 1921thread_continue(
2d21ac55 1922 register thread_t thread)
1c79356b 1923{
91447636
A
1924 register thread_t self = current_thread();
1925 register thread_continue_t continuation;
1926 register void *parameter;
b0d623f7
A
1927
1928 DTRACE_SCHED(on__cpu);
1929
91447636 1930 continuation = self->continuation;
91447636 1931 parameter = self->parameter;
9bccf70c 1932
2d21ac55 1933 thread_dispatch(thread, self);
9bccf70c 1934
2d21ac55 1935 self->continuation = self->parameter = NULL;
1c79356b 1936
91447636 1937 funnel_refunnel_check(self, 4);
1c79356b 1938
2d21ac55 1939 if (thread != THREAD_NULL)
91447636 1940 (void)spllo();
9bccf70c 1941
2d21ac55 1942 TLOG(1, "thread_continue: calling call_continuation \n");
91447636
A
1943 call_continuation(continuation, parameter, self->wait_result);
1944 /*NOTREACHED*/
1c79356b
A
1945}
1946
1947/*
2d21ac55 1948 * run_queue_init:
55e303ae 1949 *
2d21ac55 1950 * Initialize a run queue before first use.
1c79356b 1951 */
2d21ac55
A
1952void
1953run_queue_init(
1954 run_queue_t rq)
1955{
1956 int i;
1957
1958 rq->highq = IDLEPRI;
1959 for (i = 0; i < NRQBM; i++)
1960 rq->bitmap[i] = 0;
1961 setbit(MAXPRI - IDLEPRI, rq->bitmap);
1962 rq->urgency = rq->count = 0;
1963 for (i = 0; i < NRQS; i++)
1964 queue_init(&rq->queues[i]);
1965}
1c79356b 1966
2d21ac55
A
1967/*
1968 * run_queue_dequeue:
1969 *
1970 * Perform a dequeue operation on a run queue,
1971 * and return the resulting thread.
1972 *
1973 * The run queue must be locked (see run_queue_remove()
1974 * for more info), and not empty.
1975 */
1976static thread_t
1977run_queue_dequeue(
1978 run_queue_t rq,
1979 integer_t options)
1980{
1981 thread_t thread;
1982 queue_t queue = rq->queues + rq->highq;
9bccf70c 1983
2d21ac55
A
1984 if (options & SCHED_HEADQ) {
1985 thread = (thread_t)queue->next;
1986 ((queue_entry_t)thread)->next->prev = queue;
1987 queue->next = ((queue_entry_t)thread)->next;
1988 }
1989 else {
1990 thread = (thread_t)queue->prev;
1991 ((queue_entry_t)thread)->prev->next = queue;
1992 queue->prev = ((queue_entry_t)thread)->prev;
9bccf70c 1993 }
1c79356b 1994
2d21ac55
A
1995 thread->runq = PROCESSOR_NULL;
1996 rq->count--;
4a3eedf9
A
1997 if (testbit(rq->highq, sched_preempt_pri)) {
1998 rq->urgency--; assert(rq->urgency >= 0);
1999 }
2d21ac55
A
2000 if (queue_empty(queue)) {
2001 if (rq->highq != IDLEPRI)
2002 clrbit(MAXPRI - rq->highq, rq->bitmap);
2003 rq->highq = MAXPRI - ffsbit(rq->bitmap);
2004 }
1c79356b 2005
2d21ac55 2006 return (thread);
1c79356b
A
2007}
2008
2009/*
2d21ac55
A
2010 * realtime_queue_insert:
2011 *
2012 * Enqueue a thread for realtime execution.
1c79356b 2013 */
2d21ac55
A
2014static boolean_t
2015realtime_queue_insert(
2016 thread_t thread)
1c79356b 2017{
2d21ac55
A
2018 run_queue_t rq = &rt_runq;
2019 queue_t queue = rq->queues + thread->sched_pri;
2020 uint64_t deadline = thread->realtime.deadline;
2021 boolean_t preempt = FALSE;
1c79356b 2022
2d21ac55 2023 simple_lock(&rt_lock);
1c79356b 2024
55e303ae
A
2025 if (queue_empty(queue)) {
2026 enqueue_tail(queue, (queue_entry_t)thread);
2027
2d21ac55
A
2028 setbit(MAXPRI - thread->sched_pri, rq->bitmap);
2029 if (thread->sched_pri > rq->highq)
2030 rq->highq = thread->sched_pri;
2031 preempt = TRUE;
55e303ae
A
2032 }
2033 else {
2034 register thread_t entry = (thread_t)queue_first(queue);
2035
2036 while (TRUE) {
2037 if ( queue_end(queue, (queue_entry_t)entry) ||
2038 deadline < entry->realtime.deadline ) {
2039 entry = (thread_t)queue_prev((queue_entry_t)entry);
2040 break;
2041 }
2042
2043 entry = (thread_t)queue_next((queue_entry_t)entry);
2044 }
2045
2046 if ((queue_entry_t)entry == queue)
2d21ac55 2047 preempt = TRUE;
55e303ae
A
2048
2049 insque((queue_entry_t)thread, (queue_entry_t)entry);
2050 }
2051
2d21ac55 2052 thread->runq = RT_RUNQ;
55e303ae
A
2053 rq->count++; rq->urgency++;
2054
2d21ac55 2055 simple_unlock(&rt_lock);
55e303ae 2056
2d21ac55
A
2057 return (preempt);
2058}
55e303ae 2059
2d21ac55
A
2060/*
2061 * realtime_setrun:
2062 *
2063 * Dispatch a thread for realtime execution.
2064 *
2065 * Thread must be locked. Associated pset must
2066 * be locked, and is returned unlocked.
2067 */
2068static void
2069realtime_setrun(
2070 processor_t processor,
2071 thread_t thread)
2072{
2073 processor_set_t pset = processor->processor_set;
55e303ae 2074
2d21ac55
A
2075 /*
2076 * Dispatch directly onto idle processor.
2077 */
2078 if (processor->state == PROCESSOR_IDLE) {
2079 remqueue(&pset->idle_queue, (queue_entry_t)processor);
cf7d32b8 2080 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
55e303ae 2081
2d21ac55
A
2082 processor->next_thread = thread;
2083 processor->deadline = thread->realtime.deadline;
2084 processor->state = PROCESSOR_DISPATCHING;
2085 pset_unlock(pset);
55e303ae 2086
2d21ac55
A
2087 if (processor != current_processor())
2088 machine_signal_idle(processor);
2089 return;
2090 }
55e303ae 2091
2d21ac55
A
2092 if (realtime_queue_insert(thread)) {
2093 if (processor == current_processor())
2094 ast_on(AST_PREEMPT | AST_URGENT);
2095 else
2096 cause_ast_check(processor);
2097 }
2098
2099 pset_unlock(pset);
2100}
2101
2102/*
2103 * processor_enqueue:
2104 *
2105 * Enqueue thread on a processor run queue. Thread must be locked,
2106 * and not already be on a run queue.
2107 *
2108 * Returns TRUE if a preemption is indicated based on the state
2109 * of the run queue.
2110 *
2111 * The run queue must be locked (see run_queue_remove()
2112 * for more info).
2113 */
2114static boolean_t
2115processor_enqueue(
2116 processor_t processor,
2117 thread_t thread,
2118 integer_t options)
2119{
2120 run_queue_t rq = &processor->runq;
2121 queue_t queue = rq->queues + thread->sched_pri;
2122 boolean_t result = FALSE;
2123
2124 if (queue_empty(queue)) {
2125 enqueue_tail(queue, (queue_entry_t)thread);
2126
2127 setbit(MAXPRI - thread->sched_pri, rq->bitmap);
2128 if (thread->sched_pri > rq->highq) {
2129 rq->highq = thread->sched_pri;
2130 result = TRUE;
55e303ae
A
2131 }
2132 }
2d21ac55
A
2133 else
2134 if (options & SCHED_TAILQ)
2135 enqueue_tail(queue, (queue_entry_t)thread);
2136 else
2137 enqueue_head(queue, (queue_entry_t)thread);
55e303ae 2138
2d21ac55 2139 thread->runq = processor;
4a3eedf9 2140 if (testbit(thread->sched_pri, sched_preempt_pri))
2d21ac55
A
2141 rq->urgency++;
2142 rq->count++;
2143
2144 return (result);
55e303ae
A
2145}
2146
2147/*
2d21ac55 2148 * processor_setrun:
55e303ae 2149 *
2d21ac55
A
2150 * Dispatch a thread for execution on a
2151 * processor.
55e303ae 2152 *
2d21ac55
A
2153 * Thread must be locked. Associated pset must
2154 * be locked, and is returned unlocked.
55e303ae 2155 */
2d21ac55
A
2156static void
2157processor_setrun(
2158 processor_t processor,
2159 thread_t thread,
2160 integer_t options)
55e303ae 2161{
2d21ac55
A
2162 processor_set_t pset = processor->processor_set;
2163 ast_t preempt;
55e303ae 2164
55e303ae 2165 /*
2d21ac55 2166 * Dispatch directly onto idle processor.
55e303ae 2167 */
2d21ac55
A
2168 if (processor->state == PROCESSOR_IDLE) {
2169 remqueue(&pset->idle_queue, (queue_entry_t)processor);
cf7d32b8 2170 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
2d21ac55
A
2171
2172 processor->next_thread = thread;
2173 processor->deadline = UINT64_MAX;
2174 processor->state = PROCESSOR_DISPATCHING;
2175 pset_unlock(pset);
2176
2177 if (processor != current_processor())
2178 machine_signal_idle(processor);
2179 return;
2180 }
55e303ae
A
2181
2182 /*
2d21ac55 2183 * Set preemption mode.
1c79356b 2184 */
4a3eedf9 2185 if (testbit(thread->sched_pri, sched_preempt_pri))
55e303ae 2186 preempt = (AST_PREEMPT | AST_URGENT);
2d21ac55 2187 else
c910b4d9 2188 if (thread->sched_mode & TH_MODE_TIMESHARE && thread->sched_pri < thread->priority)
2d21ac55
A
2189 preempt = AST_NONE;
2190 else
2191 preempt = (options & SCHED_PREEMPT)? AST_PREEMPT: AST_NONE;
9bccf70c 2192
2d21ac55
A
2193 if (!processor_enqueue(processor, thread, options))
2194 preempt = AST_NONE;
9bccf70c 2195
2d21ac55
A
2196 if (preempt != AST_NONE) {
2197 if (processor == current_processor()) {
c910b4d9 2198 if (csw_check(processor) != AST_NONE)
2d21ac55 2199 ast_on(preempt);
9bccf70c
A
2200 }
2201 else
2d21ac55
A
2202 if ( (processor->state == PROCESSOR_RUNNING ||
2203 processor->state == PROCESSOR_SHUTDOWN) &&
2204 thread->sched_pri >= processor->current_pri ) {
2205 cause_ast_check(processor);
2206 }
2207 }
2208 else
2209 if ( processor->state == PROCESSOR_SHUTDOWN &&
2210 thread->sched_pri >= processor->current_pri ) {
2211 cause_ast_check(processor);
2212 }
2213
2214 pset_unlock(pset);
2215}
9bccf70c 2216
2d21ac55
A
2217#define next_pset(p) (((p)->pset_list != PROCESSOR_SET_NULL)? (p)->pset_list: (p)->node->psets)
2218
2219/*
2220 * choose_next_pset:
2221 *
2222 * Return the next sibling pset containing
2223 * available processors.
2224 *
2225 * Returns the original pset if none other is
2226 * suitable.
2227 */
2228static processor_set_t
2229choose_next_pset(
2230 processor_set_t pset)
2231{
2232 processor_set_t nset = pset;
2233
2234 do {
2235 nset = next_pset(nset);
2236 } while (nset->processor_count < 1 && nset != pset);
2237
cf7d32b8 2238 return (nset);
2d21ac55
A
2239}
2240
2241/*
2242 * choose_processor:
2243 *
2244 * Choose a processor for the thread, beginning at
b7266188 2245 * the pset. Accepts an optional processor hint in
2d21ac55
A
2246 * the pset.
2247 *
2248 * Returns a processor, possibly from a different pset.
2249 *
2250 * The thread must be locked. The pset must be locked,
2251 * and the resulting pset is locked on return.
2252 */
2253static processor_t
2254choose_processor(
2255 processor_set_t pset,
b7266188 2256 processor_t processor,
2d21ac55
A
2257 thread_t thread)
2258{
2259 processor_set_t nset, cset = pset;
b0d623f7 2260 processor_meta_t pmeta = PROCESSOR_META_NULL;
0b4c1975
A
2261 processor_t mprocessor;
2262
cf7d32b8 2263 /*
b7266188 2264 * Prefer the hinted processor, when appropriate.
cf7d32b8 2265 */
b7266188 2266
0b4c1975 2267 if (processor != PROCESSOR_NULL) {
7e4a7d39 2268 if (processor->processor_meta != PROCESSOR_META_NULL)
b0d623f7 2269 processor = processor->processor_meta->primary;
0b4c1975 2270 }
b0d623f7 2271
0b4c1975
A
2272 mprocessor = machine_choose_processor(pset, processor);
2273 if (mprocessor != PROCESSOR_NULL)
2274 processor = mprocessor;
b7266188 2275
0b4c1975
A
2276 if (processor != PROCESSOR_NULL) {
2277 if (processor->processor_set != pset ||
2278 processor->state == PROCESSOR_INACTIVE ||
2279 processor->state == PROCESSOR_SHUTDOWN ||
2280 processor->state == PROCESSOR_OFF_LINE)
cf7d32b8
A
2281 processor = PROCESSOR_NULL;
2282 else
0b4c1975
A
2283 if (processor->state == PROCESSOR_IDLE ||
2284 ((thread->sched_pri >= BASEPRI_RTQUEUES) &&
2285 (processor->current_pri < BASEPRI_RTQUEUES)))
2286 return (processor);
b7266188 2287 }
2d21ac55
A
2288
2289 /*
2290 * Iterate through the processor sets to locate
2291 * an appropriate processor.
2292 */
2293 do {
9bccf70c 2294 /*
2d21ac55 2295 * Choose an idle processor.
9bccf70c 2296 */
2d21ac55
A
2297 if (!queue_empty(&cset->idle_queue))
2298 return ((processor_t)queue_first(&cset->idle_queue));
1c79356b 2299
2d21ac55 2300 if (thread->sched_pri >= BASEPRI_RTQUEUES) {
0b4c1975
A
2301 integer_t lowest_priority = MAXPRI + 1;
2302 integer_t lowest_unpaired = MAXPRI + 1;
2303 uint64_t furthest_deadline = 1;
2304 processor_t lp_processor = PROCESSOR_NULL;
2305 processor_t lp_unpaired = PROCESSOR_NULL;
2306 processor_t fd_processor = PROCESSOR_NULL;
2307
2308 lp_processor = cset->low_pri;
2309 /* Consider hinted processor */
2310 if (lp_processor != PROCESSOR_NULL &&
2311 ((lp_processor->processor_meta == PROCESSOR_META_NULL) ||
2312 ((lp_processor == lp_processor->processor_meta->primary) &&
2313 !queue_empty(&lp_processor->processor_meta->idle_queue))) &&
2314 lp_processor->state != PROCESSOR_INACTIVE &&
2315 lp_processor->state != PROCESSOR_SHUTDOWN &&
2316 lp_processor->state != PROCESSOR_OFF_LINE &&
2317 (lp_processor->current_pri < thread->sched_pri))
2318 return lp_processor;
2319
2d21ac55
A
2320 processor = (processor_t)queue_first(&cset->active_queue);
2321 while (!queue_end(&cset->active_queue, (queue_entry_t)processor)) {
0b4c1975
A
2322 /* Discover the processor executing the
2323 * thread with the lowest priority within
2324 * this pset, or the one with the furthest
2325 * deadline
2326 */
2327 integer_t cpri = processor->current_pri;
2328 if (cpri < lowest_priority) {
2329 lowest_priority = cpri;
2330 lp_processor = processor;
2331 }
2d21ac55 2332
0b4c1975
A
2333 if ((cpri >= BASEPRI_RTQUEUES) && (processor->deadline > furthest_deadline)) {
2334 furthest_deadline = processor->deadline;
2335 fd_processor = processor;
b0d623f7
A
2336 }
2337
0b4c1975
A
2338
2339 if (processor->processor_meta != PROCESSOR_META_NULL &&
2340 !queue_empty(&processor->processor_meta->idle_queue)) {
2341 if (cpri < lowest_unpaired) {
2342 lowest_unpaired = cpri;
2343 lp_unpaired = processor;
2344 pmeta = processor->processor_meta;
2345 }
2346 else
2347 if (pmeta == PROCESSOR_META_NULL)
2348 pmeta = processor->processor_meta;
2349 }
2d21ac55
A
2350 processor = (processor_t)queue_next((queue_entry_t)processor);
2351 }
cf7d32b8 2352
0b4c1975
A
2353 if (thread->sched_pri > lowest_unpaired)
2354 return lp_unpaired;
2355
b0d623f7
A
2356 if (pmeta != PROCESSOR_META_NULL)
2357 return ((processor_t)queue_first(&pmeta->idle_queue));
0b4c1975
A
2358 if (thread->sched_pri > lowest_priority)
2359 return lp_processor;
2360 if (thread->realtime.deadline < furthest_deadline)
2361 return fd_processor;
cf7d32b8 2362 processor = PROCESSOR_NULL;
2d21ac55 2363 }
55e303ae 2364 else {
2d21ac55 2365 /*
c910b4d9 2366 * Check any hinted processors in the processor set if available.
2d21ac55 2367 */
c910b4d9
A
2368 if (cset->low_pri != PROCESSOR_NULL && cset->low_pri->state != PROCESSOR_INACTIVE &&
2369 cset->low_pri->state != PROCESSOR_SHUTDOWN && cset->low_pri->state != PROCESSOR_OFF_LINE &&
2370 (processor == PROCESSOR_NULL ||
2371 (thread->sched_pri > BASEPRI_DEFAULT && cset->low_pri->current_pri < thread->sched_pri))) {
2372 processor = cset->low_pri;
2373 }
2374 else
2375 if (cset->low_count != PROCESSOR_NULL && cset->low_count->state != PROCESSOR_INACTIVE &&
2376 cset->low_count->state != PROCESSOR_SHUTDOWN && cset->low_count->state != PROCESSOR_OFF_LINE &&
b0d623f7
A
2377 (processor == PROCESSOR_NULL || (thread->sched_pri <= BASEPRI_DEFAULT &&
2378 cset->low_count->runq.count < processor->runq.count))) {
c910b4d9 2379 processor = cset->low_count;
cf7d32b8 2380 }
9bccf70c 2381
9bccf70c 2382 /*
cf7d32b8 2383 * Otherwise, choose an available processor in the set.
1c79356b 2384 */
cf7d32b8
A
2385 if (processor == PROCESSOR_NULL) {
2386 processor = (processor_t)dequeue_head(&cset->active_queue);
2387 if (processor != PROCESSOR_NULL)
2388 enqueue_tail(&cset->active_queue, (queue_entry_t)processor);
2d21ac55 2389 }
b0d623f7
A
2390 if (processor != PROCESSOR_NULL && pmeta == PROCESSOR_META_NULL) {
2391 if (processor->processor_meta != PROCESSOR_META_NULL &&
0b4c1975 2392 !queue_empty(&processor->processor_meta->idle_queue))
b0d623f7
A
2393 pmeta = processor->processor_meta;
2394 }
2d21ac55
A
2395 }
2396
2397 /*
2398 * Move onto the next processor set.
2399 */
2400 nset = next_pset(cset);
2401
2402 if (nset != pset) {
2403 pset_unlock(cset);
2404
2405 cset = nset;
2406 pset_lock(cset);
2407 }
2408 } while (nset != pset);
2409
2410 /*
cf7d32b8
A
2411 * Make sure that we pick a running processor,
2412 * and that the correct processor set is locked.
2d21ac55 2413 */
cf7d32b8 2414 do {
b0d623f7
A
2415 if (pmeta != PROCESSOR_META_NULL) {
2416 if (cset != pmeta->primary->processor_set) {
2417 pset_unlock(cset);
2418
2419 cset = pmeta->primary->processor_set;
2420 pset_lock(cset);
2421 }
2422
2423 if (!queue_empty(&pmeta->idle_queue))
2424 return ((processor_t)queue_first(&pmeta->idle_queue));
2425
2426 pmeta = PROCESSOR_META_NULL;
2427 }
2428
cf7d32b8
A
2429 /*
2430 * If we haven't been able to choose a processor,
c910b4d9 2431 * pick the boot processor and return it.
cf7d32b8
A
2432 */
2433 if (processor == PROCESSOR_NULL) {
c910b4d9 2434 processor = master_processor;
2d21ac55 2435
cf7d32b8
A
2436 /*
2437 * Check that the correct processor set is
2438 * returned locked.
2439 */
2440 if (cset != processor->processor_set) {
2441 pset_unlock(cset);
2442
2443 cset = processor->processor_set;
2444 pset_lock(cset);
2445 }
2446
2447 return (processor);
2448 }
2449
2450 /*
2451 * Check that the processor set for the chosen
2452 * processor is locked.
2453 */
2454 if (cset != processor->processor_set) {
2455 pset_unlock(cset);
2456
2457 cset = processor->processor_set;
2458 pset_lock(cset);
2459 }
2460
2461 /*
2462 * We must verify that the chosen processor is still available.
2463 */
c910b4d9
A
2464 if (processor->state == PROCESSOR_INACTIVE ||
2465 processor->state == PROCESSOR_SHUTDOWN || processor->state == PROCESSOR_OFF_LINE)
cf7d32b8
A
2466 processor = PROCESSOR_NULL;
2467 } while (processor == PROCESSOR_NULL);
2d21ac55
A
2468
2469 return (processor);
2470}
2471
2472/*
2473 * thread_setrun:
2474 *
2475 * Dispatch thread for execution, onto an idle
2476 * processor or run queue, and signal a preemption
2477 * as appropriate.
2478 *
2479 * Thread must be locked.
2480 */
2481void
2482thread_setrun(
2483 thread_t thread,
2484 integer_t options)
2485{
2486 processor_t processor;
2487 processor_set_t pset;
2488
2489#if DEBUG
2490 assert(thread_runnable(thread));
2491#endif
55e303ae 2492
2d21ac55
A
2493 /*
2494 * Update priority if needed.
2495 */
2496 if (thread->sched_stamp != sched_tick)
2497 update_priority(thread);
2498
2499 assert(thread->runq == PROCESSOR_NULL);
2500
2501 if (thread->bound_processor == PROCESSOR_NULL) {
2502 /*
2503 * Unbound case.
2504 */
2505 if (thread->affinity_set != AFFINITY_SET_NULL) {
2506 /*
2507 * Use affinity set policy hint.
2508 */
2509 pset = thread->affinity_set->aset_pset;
2510 pset_lock(pset);
2511
b7266188 2512 processor = choose_processor(pset, PROCESSOR_NULL, thread);
2d21ac55
A
2513 }
2514 else
2515 if (thread->last_processor != PROCESSOR_NULL) {
2516 /*
2517 * Simple (last processor) affinity case.
2518 */
2519 processor = thread->last_processor;
2520 pset = processor->processor_set;
2521 pset_lock(pset);
0b4c1975 2522 processor = choose_processor(pset, processor, thread);
2d21ac55
A
2523 }
2524 else {
2525 /*
2526 * No Affinity case:
2527 *
cf7d32b8
A
2528 * Utilitize a per task hint to spread threads
2529 * among the available processor sets.
2d21ac55 2530 */
cf7d32b8
A
2531 task_t task = thread->task;
2532
2533 pset = task->pset_hint;
2534 if (pset == PROCESSOR_SET_NULL)
2535 pset = current_processor()->processor_set;
2536
2537 pset = choose_next_pset(pset);
2d21ac55 2538 pset_lock(pset);
9bccf70c 2539
b7266188 2540 processor = choose_processor(pset, PROCESSOR_NULL, thread);
cf7d32b8 2541 task->pset_hint = processor->processor_set;
55e303ae 2542 }
1c79356b
A
2543 }
2544 else {
2d21ac55
A
2545 /*
2546 * Bound case:
2547 *
2548 * Unconditionally dispatch on the processor.
2549 */
2550 processor = thread->bound_processor;
55e303ae 2551 pset = processor->processor_set;
2d21ac55
A
2552 pset_lock(pset);
2553 }
2554
2555 /*
2556 * Dispatch the thread on the choosen processor.
2557 */
2558 if (thread->sched_pri >= BASEPRI_RTQUEUES)
2559 realtime_setrun(processor, thread);
2560 else
2561 processor_setrun(processor, thread, options);
2562}
2563
b0d623f7
A
2564processor_set_t
2565task_choose_pset(
2566 task_t task)
2567{
2568 processor_set_t pset = task->pset_hint;
2569
2570 if (pset != PROCESSOR_SET_NULL)
2571 pset = choose_next_pset(pset);
2572
2573 return (pset);
2574}
2575
2d21ac55
A
2576/*
2577 * processor_queue_shutdown:
2578 *
c910b4d9
A
2579 * Shutdown a processor run queue by
2580 * re-dispatching non-bound threads.
2d21ac55
A
2581 *
2582 * Associated pset must be locked, and is
2583 * returned unlocked.
2584 */
2585void
2586processor_queue_shutdown(
2587 processor_t processor)
2588{
2589 processor_set_t pset = processor->processor_set;
2590 run_queue_t rq = &processor->runq;
2591 queue_t queue = rq->queues + rq->highq;
2592 int pri = rq->highq, count = rq->count;
2593 thread_t next, thread;
2594 queue_head_t tqueue;
2595
2596 queue_init(&tqueue);
2597
2598 while (count > 0) {
2599 thread = (thread_t)queue_first(queue);
2600 while (!queue_end(queue, (queue_entry_t)thread)) {
2601 next = (thread_t)queue_next((queue_entry_t)thread);
2602
b0d623f7 2603 if (thread->bound_processor == PROCESSOR_NULL) {
2d21ac55
A
2604 remqueue(queue, (queue_entry_t)thread);
2605
2606 thread->runq = PROCESSOR_NULL;
2607 rq->count--;
4a3eedf9
A
2608 if (testbit(pri, sched_preempt_pri)) {
2609 rq->urgency--; assert(rq->urgency >= 0);
2610 }
2d21ac55
A
2611 if (queue_empty(queue)) {
2612 if (pri != IDLEPRI)
2613 clrbit(MAXPRI - pri, rq->bitmap);
2614 rq->highq = MAXPRI - ffsbit(rq->bitmap);
9bccf70c 2615 }
2d21ac55
A
2616
2617 enqueue_tail(&tqueue, (queue_entry_t)thread);
9bccf70c 2618 }
2d21ac55
A
2619 count--;
2620
2621 thread = next;
9bccf70c 2622 }
55e303ae 2623
2d21ac55
A
2624 queue--; pri--;
2625 }
2626
2627 pset_unlock(pset);
2628
2d21ac55
A
2629 while ((thread = (thread_t)dequeue_head(&tqueue)) != THREAD_NULL) {
2630 thread_lock(thread);
55e303ae 2631
c910b4d9 2632 thread_setrun(thread, SCHED_TAILQ);
2d21ac55
A
2633
2634 thread_unlock(thread);
9bccf70c
A
2635 }
2636}
2637
2638/*
c910b4d9
A
2639 * Check for a preemption point in
2640 * the current context.
55e303ae
A
2641 *
2642 * Called at splsched.
9bccf70c
A
2643 */
2644ast_t
2645csw_check(
9bccf70c
A
2646 processor_t processor)
2647{
9bccf70c
A
2648 ast_t result = AST_NONE;
2649 run_queue_t runq;
2650
55e303ae 2651 if (first_timeslice(processor)) {
2d21ac55 2652 runq = &rt_runq;
55e303ae
A
2653 if (runq->highq >= BASEPRI_RTQUEUES)
2654 return (AST_PREEMPT | AST_URGENT);
2655
c910b4d9 2656 if (runq->highq > processor->current_pri) {
9bccf70c 2657 if (runq->urgency > 0)
55e303ae 2658 return (AST_PREEMPT | AST_URGENT);
9bccf70c 2659
55e303ae 2660 result |= AST_PREEMPT;
9bccf70c
A
2661 }
2662
2663 runq = &processor->runq;
c910b4d9 2664 if (runq->highq > processor->current_pri) {
9bccf70c 2665 if (runq->urgency > 0)
55e303ae 2666 return (AST_PREEMPT | AST_URGENT);
9bccf70c 2667
55e303ae 2668 result |= AST_PREEMPT;
9bccf70c
A
2669 }
2670 }
2671 else {
2d21ac55 2672 runq = &rt_runq;
c910b4d9 2673 if (runq->highq >= processor->current_pri) {
9bccf70c 2674 if (runq->urgency > 0)
55e303ae 2675 return (AST_PREEMPT | AST_URGENT);
9bccf70c 2676
55e303ae 2677 result |= AST_PREEMPT;
9bccf70c
A
2678 }
2679
2680 runq = &processor->runq;
c910b4d9 2681 if (runq->highq >= processor->current_pri) {
9bccf70c 2682 if (runq->urgency > 0)
55e303ae 2683 return (AST_PREEMPT | AST_URGENT);
9bccf70c 2684
55e303ae 2685 result |= AST_PREEMPT;
9bccf70c 2686 }
1c79356b 2687 }
9bccf70c
A
2688
2689 if (result != AST_NONE)
2690 return (result);
2691
b0d623f7
A
2692 if (processor->current_pri < BASEPRI_RTQUEUES && processor->processor_meta != PROCESSOR_META_NULL &&
2693 processor->processor_meta->primary != processor)
2694 return (AST_PREEMPT);
2695
b7266188 2696 if (machine_processor_is_inactive(processor))
c910b4d9 2697 return (AST_PREEMPT);
9bccf70c 2698
c910b4d9
A
2699 if (processor->active_thread->state & TH_SUSP)
2700 return (AST_PREEMPT);
2701
2702 return (AST_NONE);
1c79356b
A
2703}
2704
2705/*
9bccf70c 2706 * set_sched_pri:
1c79356b 2707 *
55e303ae
A
2708 * Set the scheduled priority of the specified thread.
2709 *
9bccf70c 2710 * This may cause the thread to change queues.
1c79356b 2711 *
55e303ae 2712 * Thread must be locked.
1c79356b
A
2713 */
2714void
9bccf70c 2715set_sched_pri(
2d21ac55
A
2716 thread_t thread,
2717 int priority)
1c79356b 2718{
2d21ac55 2719 boolean_t removed = run_queue_remove(thread);
9bccf70c 2720
9bccf70c 2721 thread->sched_pri = priority;
2d21ac55 2722 if (removed)
55e303ae 2723 thread_setrun(thread, SCHED_PREEMPT | SCHED_TAILQ);
9bccf70c 2724 else
55e303ae 2725 if (thread->state & TH_RUN) {
9bccf70c
A
2726 processor_t processor = thread->last_processor;
2727
2728 if (thread == current_thread()) {
c910b4d9 2729 ast_t preempt;
9bccf70c 2730
9bccf70c 2731 processor->current_pri = priority;
c910b4d9
A
2732 if ((preempt = csw_check(processor)) != AST_NONE)
2733 ast_on(preempt);
9bccf70c
A
2734 }
2735 else
2736 if ( processor != PROCESSOR_NULL &&
55e303ae 2737 processor->active_thread == thread )
9bccf70c 2738 cause_ast_check(processor);
1c79356b
A
2739 }
2740}
2741
91447636
A
2742#if 0
2743
2744static void
2745run_queue_check(
2746 run_queue_t rq,
2747 thread_t thread)
2748{
2749 queue_t q;
2750 queue_entry_t qe;
2751
2752 if (rq != thread->runq)
2753 panic("run_queue_check: thread runq");
2754
2755 if (thread->sched_pri > MAXPRI || thread->sched_pri < MINPRI)
2756 panic("run_queue_check: thread sched_pri");
2757
2758 q = &rq->queues[thread->sched_pri];
2759 qe = queue_first(q);
2760 while (!queue_end(q, qe)) {
2761 if (qe == (queue_entry_t)thread)
2762 return;
2763
2764 qe = queue_next(qe);
2765 }
2766
2767 panic("run_queue_check: end");
2768}
2769
2770#endif /* DEBUG */
2771
1c79356b 2772/*
55e303ae 2773 * run_queue_remove:
1c79356b 2774 *
2d21ac55
A
2775 * Remove a thread from a current run queue and
2776 * return TRUE if successful.
55e303ae
A
2777 *
2778 * Thread must be locked.
1c79356b 2779 */
2d21ac55 2780boolean_t
55e303ae 2781run_queue_remove(
2d21ac55 2782 thread_t thread)
1c79356b 2783{
2d21ac55 2784 processor_t processor = thread->runq;
1c79356b 2785
1c79356b 2786 /*
2d21ac55 2787 * If processor is PROCESSOR_NULL, the thread will stay out of the
55e303ae
A
2788 * run queues because the caller locked the thread. Otherwise
2789 * the thread is on a run queue, but could be chosen for dispatch
2790 * and removed.
1c79356b 2791 */
2d21ac55
A
2792 if (processor != PROCESSOR_NULL) {
2793 void * rqlock;
2794 run_queue_t rq;
55e303ae
A
2795
2796 /*
2d21ac55
A
2797 * The processor run queues are locked by the
2798 * processor set. Real-time priorities use a
2799 * global queue with a dedicated lock.
55e303ae 2800 */
2d21ac55
A
2801 if (thread->sched_pri < BASEPRI_RTQUEUES) {
2802 rqlock = &processor->processor_set->sched_lock;
2803 rq = &processor->runq;
2804 }
2805 else {
2806 rqlock = &rt_lock; rq = &rt_runq;
55e303ae
A
2807 }
2808
2d21ac55 2809 simple_lock(rqlock);
55e303ae 2810
2d21ac55 2811 if (processor == thread->runq) {
1c79356b 2812 /*
55e303ae
A
2813 * Thread is on a run queue and we have a lock on
2814 * that run queue.
1c79356b 2815 */
1c79356b
A
2816 remqueue(&rq->queues[0], (queue_entry_t)thread);
2817 rq->count--;
4a3eedf9
A
2818 if (testbit(thread->sched_pri, sched_preempt_pri)) {
2819 rq->urgency--; assert(rq->urgency >= 0);
2820 }
1c79356b
A
2821
2822 if (queue_empty(rq->queues + thread->sched_pri)) {
2823 /* update run queue status */
2824 if (thread->sched_pri != IDLEPRI)
2825 clrbit(MAXPRI - thread->sched_pri, rq->bitmap);
2826 rq->highq = MAXPRI - ffsbit(rq->bitmap);
2827 }
55e303ae 2828
2d21ac55 2829 thread->runq = PROCESSOR_NULL;
1c79356b
A
2830 }
2831 else {
2832 /*
55e303ae
A
2833 * The thread left the run queue before we could
2834 * lock the run queue.
1c79356b 2835 */
2d21ac55
A
2836 assert(thread->runq == PROCESSOR_NULL);
2837 processor = PROCESSOR_NULL;
1c79356b 2838 }
55e303ae 2839
2d21ac55 2840 simple_unlock(rqlock);
1c79356b
A
2841 }
2842
2d21ac55 2843 return (processor != PROCESSOR_NULL);
1c79356b
A
2844}
2845
2d21ac55 2846/*
cf7d32b8 2847 * steal_processor_thread:
2d21ac55 2848 *
cf7d32b8
A
2849 * Locate a thread to steal from the processor and
2850 * return it.
2d21ac55
A
2851 *
2852 * Associated pset must be locked. Returns THREAD_NULL
2853 * on failure.
2854 */
2855static thread_t
cf7d32b8 2856steal_processor_thread(
2d21ac55 2857 processor_t processor)
91447636 2858{
2d21ac55
A
2859 run_queue_t rq = &processor->runq;
2860 queue_t queue = rq->queues + rq->highq;
2861 int pri = rq->highq, count = rq->count;
cf7d32b8 2862 thread_t thread;
2d21ac55
A
2863
2864 while (count > 0) {
2865 thread = (thread_t)queue_first(queue);
2866 while (!queue_end(queue, (queue_entry_t)thread)) {
b0d623f7 2867 if (thread->bound_processor == PROCESSOR_NULL) {
2d21ac55
A
2868 remqueue(queue, (queue_entry_t)thread);
2869
2870 thread->runq = PROCESSOR_NULL;
2871 rq->count--;
4a3eedf9
A
2872 if (testbit(pri, sched_preempt_pri)) {
2873 rq->urgency--; assert(rq->urgency >= 0);
2874 }
2d21ac55
A
2875 if (queue_empty(queue)) {
2876 if (pri != IDLEPRI)
2877 clrbit(MAXPRI - pri, rq->bitmap);
2878 rq->highq = MAXPRI - ffsbit(rq->bitmap);
2879 }
91447636 2880
2d21ac55
A
2881 return (thread);
2882 }
2883 count--;
91447636 2884
2d21ac55 2885 thread = (thread_t)queue_next((queue_entry_t)thread);
91447636 2886 }
91447636 2887
2d21ac55
A
2888 queue--; pri--;
2889 }
91447636 2890
2d21ac55 2891 return (THREAD_NULL);
91447636
A
2892}
2893
cf7d32b8
A
2894/*
2895 * Locate and steal a thread, beginning
2896 * at the pset.
2897 *
2898 * The pset must be locked, and is returned
2899 * unlocked.
2900 *
2901 * Returns the stolen thread, or THREAD_NULL on
2902 * failure.
2903 */
2904static thread_t
2905steal_thread(
2906 processor_set_t pset)
2907{
2908 processor_set_t nset, cset = pset;
2909 processor_t processor;
2910 thread_t thread;
2911
2912 do {
2913 processor = (processor_t)queue_first(&cset->active_queue);
2914 while (!queue_end(&cset->active_queue, (queue_entry_t)processor)) {
2915 if (processor->runq.count > 0) {
2916 thread = steal_processor_thread(processor);
2917 if (thread != THREAD_NULL) {
2918 remqueue(&cset->active_queue, (queue_entry_t)processor);
2919 enqueue_tail(&cset->active_queue, (queue_entry_t)processor);
2920
cf7d32b8
A
2921 pset_unlock(cset);
2922
2923 return (thread);
2924 }
2925 }
2926
2927 processor = (processor_t)queue_next((queue_entry_t)processor);
2928 }
2929
2930 nset = next_pset(cset);
2931
2932 if (nset != pset) {
2933 pset_unlock(cset);
2934
2935 cset = nset;
2936 pset_lock(cset);
2937 }
2938 } while (nset != pset);
2939
2940 pset_unlock(cset);
2941
2942 return (THREAD_NULL);
2943}
2944
1c79356b 2945/*
2d21ac55
A
2946 * This is the processor idle loop, which just looks for other threads
2947 * to execute. Processor idle threads invoke this without supplying a
2948 * current thread to idle without an asserted wait state.
2949 *
2950 * Returns a the next thread to execute if dispatched directly.
1c79356b 2951 */
2d21ac55
A
2952static thread_t
2953processor_idle(
2954 thread_t thread,
2955 processor_t processor)
1c79356b 2956{
2d21ac55
A
2957 processor_set_t pset = processor->processor_set;
2958 thread_t new_thread;
2959 int state;
1c79356b 2960
2d21ac55 2961 (void)splsched();
1c79356b 2962
2d21ac55 2963 KERNEL_DEBUG_CONSTANT(
b0d623f7 2964 MACHDBG_CODE(DBG_MACH_SCHED,MACH_IDLE) | DBG_FUNC_START, (uintptr_t)thread_tid(thread), 0, 0, 0, 0);
3a60a9f5 2965
2d21ac55
A
2966 timer_switch(&PROCESSOR_DATA(processor, system_state),
2967 mach_absolute_time(), &PROCESSOR_DATA(processor, idle_state));
2968 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, idle_state);
3a60a9f5 2969
cf7d32b8 2970 while (processor->next_thread == THREAD_NULL && processor->runq.count == 0 && rt_runq.count == 0 &&
2d21ac55 2971 (thread == THREAD_NULL || ((thread->state & (TH_WAIT|TH_SUSP)) == TH_WAIT && !thread->wake_active))) {
2d21ac55 2972 machine_idle();
55e303ae
A
2973
2974 (void)splsched();
c910b4d9 2975
b7266188 2976 if (processor->state == PROCESSOR_INACTIVE && !machine_processor_is_inactive(processor))
c910b4d9 2977 break;
55e303ae
A
2978 }
2979
2d21ac55
A
2980 timer_switch(&PROCESSOR_DATA(processor, idle_state),
2981 mach_absolute_time(), &PROCESSOR_DATA(processor, system_state));
2982 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
1c79356b 2983
2d21ac55
A
2984 pset_lock(pset);
2985
55e303ae
A
2986 state = processor->state;
2987 if (state == PROCESSOR_DISPATCHING) {
1c79356b 2988 /*
55e303ae 2989 * Commmon case -- cpu dispatched.
1c79356b 2990 */
2d21ac55
A
2991 new_thread = processor->next_thread;
2992 processor->next_thread = THREAD_NULL;
55e303ae 2993 processor->state = PROCESSOR_RUNNING;
1c79356b 2994
4a3eedf9
A
2995 if ( processor->runq.highq > new_thread->sched_pri ||
2996 (rt_runq.highq > 0 && rt_runq.highq >= new_thread->sched_pri) ) {
2d21ac55 2997 processor->deadline = UINT64_MAX;
55e303ae 2998
2d21ac55 2999 pset_unlock(pset);
1c79356b 3000
2d21ac55
A
3001 thread_lock(new_thread);
3002 thread_setrun(new_thread, SCHED_HEADQ);
3003 thread_unlock(new_thread);
55e303ae 3004
4a3eedf9 3005 KERNEL_DEBUG_CONSTANT(
b0d623f7 3006 MACHDBG_CODE(DBG_MACH_SCHED,MACH_IDLE) | DBG_FUNC_END, (uintptr_t)thread_tid(thread), state, 0, 0, 0);
4a3eedf9 3007
2d21ac55 3008 return (THREAD_NULL);
1c79356b 3009 }
1c79356b 3010
2d21ac55
A
3011 pset_unlock(pset);
3012
4a3eedf9 3013 KERNEL_DEBUG_CONSTANT(
b0d623f7 3014 MACHDBG_CODE(DBG_MACH_SCHED,MACH_IDLE) | DBG_FUNC_END, (uintptr_t)thread_tid(thread), state, (uintptr_t)thread_tid(new_thread), 0, 0);
4a3eedf9 3015
2d21ac55 3016 return (new_thread);
55e303ae
A
3017 }
3018 else
3019 if (state == PROCESSOR_IDLE) {
55e303ae 3020 remqueue(&pset->idle_queue, (queue_entry_t)processor);
1c79356b 3021
2d21ac55 3022 processor->state = PROCESSOR_RUNNING;
cf7d32b8 3023 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
1c79356b 3024 }
55e303ae 3025 else
c910b4d9
A
3026 if (state == PROCESSOR_INACTIVE) {
3027 processor->state = PROCESSOR_RUNNING;
3028 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
3029 }
3030 else
55e303ae
A
3031 if (state == PROCESSOR_SHUTDOWN) {
3032 /*
3033 * Going off-line. Force a
3034 * reschedule.
3035 */
2d21ac55
A
3036 if ((new_thread = processor->next_thread) != THREAD_NULL) {
3037 processor->next_thread = THREAD_NULL;
55e303ae 3038 processor->deadline = UINT64_MAX;
2d21ac55
A
3039
3040 pset_unlock(pset);
55e303ae
A
3041
3042 thread_lock(new_thread);
3043 thread_setrun(new_thread, SCHED_HEADQ);
3044 thread_unlock(new_thread);
55e303ae 3045
4a3eedf9 3046 KERNEL_DEBUG_CONSTANT(
b0d623f7 3047 MACHDBG_CODE(DBG_MACH_SCHED,MACH_IDLE) | DBG_FUNC_END, (uintptr_t)thread_tid(thread), state, 0, 0, 0);
4a3eedf9 3048
2d21ac55
A
3049 return (THREAD_NULL);
3050 }
55e303ae
A
3051 }
3052
2d21ac55
A
3053 pset_unlock(pset);
3054
4a3eedf9 3055 KERNEL_DEBUG_CONSTANT(
b0d623f7 3056 MACHDBG_CODE(DBG_MACH_SCHED,MACH_IDLE) | DBG_FUNC_END, (uintptr_t)thread_tid(thread), state, 0, 0, 0);
4a3eedf9 3057
2d21ac55
A
3058 return (THREAD_NULL);
3059}
3060
cf7d32b8
A
3061/*
3062 * Each processor has a dedicated thread which
3063 * executes the idle loop when there is no suitable
3064 * previous context.
3065 */
2d21ac55
A
3066void
3067idle_thread(void)
3068{
3069 processor_t processor = current_processor();
3070 thread_t new_thread;
3071
3072 new_thread = processor_idle(THREAD_NULL, processor);
3073 if (new_thread != THREAD_NULL) {
3074 thread_run(processor->idle_thread, (thread_continue_t)idle_thread, NULL, new_thread);
3075 /*NOTREACHED*/
3076 }
55e303ae 3077
2d21ac55 3078 thread_block((thread_continue_t)idle_thread);
55e303ae 3079 /*NOTREACHED*/
1c79356b
A
3080}
3081
91447636
A
3082kern_return_t
3083idle_thread_create(
3084 processor_t processor)
1c79356b 3085{
91447636
A
3086 kern_return_t result;
3087 thread_t thread;
3088 spl_t s;
3089
3090 result = kernel_thread_create((thread_continue_t)idle_thread, NULL, MAXPRI_KERNEL, &thread);
3091 if (result != KERN_SUCCESS)
3092 return (result);
3093
3094 s = splsched();
3095 thread_lock(thread);
3096 thread->bound_processor = processor;
3097 processor->idle_thread = thread;
3098 thread->sched_pri = thread->priority = IDLEPRI;
3099 thread->state = (TH_RUN | TH_IDLE);
3100 thread_unlock(thread);
3101 splx(s);
3102
3103 thread_deallocate(thread);
3104
3105 return (KERN_SUCCESS);
1c79356b
A
3106}
3107
55e303ae 3108static uint64_t sched_tick_deadline;
0b4e3aa0 3109
91447636
A
3110/*
3111 * sched_startup:
3112 *
3113 * Kicks off scheduler services.
3114 *
3115 * Called at splsched.
3116 */
0b4e3aa0 3117void
91447636 3118sched_startup(void)
0b4e3aa0 3119{
91447636
A
3120 kern_return_t result;
3121 thread_t thread;
3122
3123 result = kernel_thread_start_priority((thread_continue_t)sched_tick_thread, NULL, MAXPRI_KERNEL, &thread);
3124 if (result != KERN_SUCCESS)
3125 panic("sched_startup");
3126
3127 thread_deallocate(thread);
3128
3129 /*
3130 * Yield to the sched_tick_thread while it times
3131 * a series of context switches back. It stores
3132 * the baseline value in sched_cswtime.
3133 *
3134 * The current thread is the only other thread
3135 * active at this point.
3136 */
3137 while (sched_cswtime == 0)
3138 thread_block(THREAD_CONTINUE_NULL);
3139
3140 thread_daemon_init();
3141
3142 thread_call_initialize();
0b4e3aa0 3143}
1c79356b
A
3144
3145/*
91447636 3146 * sched_tick_thread:
1c79356b 3147 *
55e303ae
A
3148 * Perform periodic bookkeeping functions about ten
3149 * times per second.
1c79356b 3150 */
91447636
A
3151static void
3152sched_tick_continue(void)
1c79356b 3153{
91447636 3154 uint64_t abstime = mach_absolute_time();
1c79356b 3155
91447636 3156 sched_tick++;
1c79356b
A
3157
3158 /*
91447636 3159 * Compute various averages.
1c79356b 3160 */
91447636 3161 compute_averages();
1c79356b
A
3162
3163 /*
91447636
A
3164 * Scan the run queues for threads which
3165 * may need to be updated.
1c79356b 3166 */
91447636 3167 thread_update_scan();
1c79356b
A
3168
3169 clock_deadline_for_periodic_event(sched_tick_interval, abstime,
3170 &sched_tick_deadline);
3171
91447636
A
3172 assert_wait_deadline((event_t)sched_tick_thread, THREAD_UNINT, sched_tick_deadline);
3173 thread_block((thread_continue_t)sched_tick_continue);
1c79356b
A
3174 /*NOTREACHED*/
3175}
3176
91447636
A
3177/*
3178 * Time a series of context switches to determine
3179 * a baseline. Toss the high and low and return
3180 * the one-way value.
3181 */
3182static uint32_t
3183time_cswitch(void)
3184{
3185 uint32_t new, hi, low, accum;
3186 uint64_t abstime;
3187 int i, tries = 7;
3188
3189 accum = hi = low = 0;
3190 for (i = 0; i < tries; ++i) {
3191 abstime = mach_absolute_time();
3192 thread_block(THREAD_CONTINUE_NULL);
3193
b0d623f7 3194 new = (uint32_t)(mach_absolute_time() - abstime);
91447636
A
3195
3196 if (i == 0)
3197 accum = hi = low = new;
3198 else {
3199 if (new < low)
3200 low = new;
3201 else
3202 if (new > hi)
3203 hi = new;
3204 accum += new;
3205 }
3206 }
3207
3208 return ((accum - hi - low) / (2 * (tries - 2)));
3209}
3210
1c79356b
A
3211void
3212sched_tick_thread(void)
3213{
91447636
A
3214 sched_cswtime = time_cswitch();
3215
55e303ae 3216 sched_tick_deadline = mach_absolute_time();
1c79356b 3217
91447636 3218 sched_tick_continue();
1c79356b
A
3219 /*NOTREACHED*/
3220}
3221
1c79356b 3222/*
91447636 3223 * thread_update_scan / runq_scan:
55e303ae 3224 *
91447636
A
3225 * Scan the run queues to account for timesharing threads
3226 * which need to be updated.
1c79356b
A
3227 *
3228 * Scanner runs in two passes. Pass one squirrels likely
91447636 3229 * threads away in an array, pass two does the update.
1c79356b 3230 *
91447636
A
3231 * This is necessary because the run queue is locked for
3232 * the candidate scan, but the thread is locked for the update.
1c79356b 3233 *
91447636
A
3234 * Array should be sized to make forward progress, without
3235 * disabling preemption for long periods.
1c79356b 3236 */
55e303ae 3237
91447636 3238#define THREAD_UPDATE_SIZE 128
55e303ae 3239
91447636
A
3240static thread_t thread_update_array[THREAD_UPDATE_SIZE];
3241static int thread_update_count = 0;
1c79356b
A
3242
3243/*
91447636
A
3244 * Scan a runq for candidate threads.
3245 *
3246 * Returns TRUE if retry is needed.
1c79356b 3247 */
55e303ae 3248static boolean_t
91447636 3249runq_scan(
1c79356b
A
3250 run_queue_t runq)
3251{
91447636 3252 register int count;
1c79356b
A
3253 register queue_t q;
3254 register thread_t thread;
1c79356b 3255
1c79356b
A
3256 if ((count = runq->count) > 0) {
3257 q = runq->queues + runq->highq;
3258 while (count > 0) {
3259 queue_iterate(q, thread, thread_t, links) {
55e303ae 3260 if ( thread->sched_stamp != sched_tick &&
0b4e3aa0 3261 (thread->sched_mode & TH_MODE_TIMESHARE) ) {
91447636 3262 if (thread_update_count == THREAD_UPDATE_SIZE)
55e303ae 3263 return (TRUE);
1c79356b 3264
91447636
A
3265 thread_update_array[thread_update_count++] = thread;
3266 thread_reference_internal(thread);
1c79356b
A
3267 }
3268
3269 count--;
3270 }
3271
3272 q--;
3273 }
3274 }
1c79356b 3275
91447636 3276 return (FALSE);
1c79356b
A
3277}
3278
55e303ae 3279static void
91447636 3280thread_update_scan(void)
1c79356b 3281{
2d21ac55
A
3282 boolean_t restart_needed = FALSE;
3283 processor_t processor = processor_list;
3284 processor_set_t pset;
3285 thread_t thread;
3286 spl_t s;
1c79356b 3287
1c79356b 3288 do {
2d21ac55
A
3289 do {
3290 pset = processor->processor_set;
1c79356b 3291
2d21ac55
A
3292 s = splsched();
3293 pset_lock(pset);
0b4e3aa0 3294
2d21ac55
A
3295 restart_needed = runq_scan(&processor->runq);
3296
3297 pset_unlock(pset);
3298 splx(s);
3299
3300 if (restart_needed)
3301 break;
3302
3303 thread = processor->idle_thread;
3304 if (thread != THREAD_NULL && thread->sched_stamp != sched_tick) {
3305 if (thread_update_count == THREAD_UPDATE_SIZE) {
3306 restart_needed = TRUE;
3307 break;
0b4e3aa0
A
3308 }
3309
2d21ac55
A
3310 thread_update_array[thread_update_count++] = thread;
3311 thread_reference_internal(thread);
1c79356b 3312 }
2d21ac55 3313 } while ((processor = processor->processor_list) != NULL);
1c79356b
A
3314
3315 /*
3316 * Ok, we now have a collection of candidates -- fix them.
3317 */
91447636
A
3318 while (thread_update_count > 0) {
3319 thread = thread_update_array[--thread_update_count];
3320 thread_update_array[thread_update_count] = THREAD_NULL;
55e303ae 3321
1c79356b
A
3322 s = splsched();
3323 thread_lock(thread);
55e303ae
A
3324 if ( !(thread->state & (TH_WAIT|TH_SUSP)) &&
3325 thread->sched_stamp != sched_tick )
3326 update_priority(thread);
1c79356b
A
3327 thread_unlock(thread);
3328 splx(s);
55e303ae 3329
91447636 3330 thread_deallocate(thread);
1c79356b 3331 }
1c79356b
A
3332 } while (restart_needed);
3333}
3334
3335/*
3336 * Just in case someone doesn't use the macro
3337 */
3338#undef thread_wakeup
3339void
3340thread_wakeup(
3341 event_t x);
3342
3343void
3344thread_wakeup(
3345 event_t x)
3346{
3347 thread_wakeup_with_result(x, THREAD_AWAKENED);
3348}
3349
91447636
A
3350boolean_t
3351preemption_enabled(void)
3352{
3353 return (get_preemption_level() == 0 && ml_get_interrupts_enabled());
3354}
9bccf70c 3355
0b4e3aa0 3356#if DEBUG
0b4e3aa0 3357static boolean_t
1c79356b 3358thread_runnable(
0b4e3aa0 3359 thread_t thread)
1c79356b 3360{
0b4e3aa0 3361 return ((thread->state & (TH_RUN|TH_WAIT)) == TH_RUN);
1c79356b 3362}
1c79356b
A
3363#endif /* DEBUG */
3364
3365#if MACH_KDB
3366#include <ddb/db_output.h>
3367#define printf kdbprintf
1c79356b
A
3368void db_sched(void);
3369
3370void
3371db_sched(void)
3372{
3373 iprintf("Scheduling Statistics:\n");
3374 db_indent += 2;
3375 iprintf("Thread invocations: csw %d same %d\n",
3376 c_thread_invoke_csw, c_thread_invoke_same);
3377#if MACH_COUNTERS
3378 iprintf("Thread block: calls %d\n",
3379 c_thread_block_calls);
2d21ac55 3380 iprintf("Idle thread:\n\thandoff %d block %d\n",
1c79356b 3381 c_idle_thread_handoff,
2d21ac55 3382 c_idle_thread_block);
1c79356b
A
3383 iprintf("Sched thread blocks: %d\n", c_sched_thread_block);
3384#endif /* MACH_COUNTERS */
3385 db_indent -= 2;
3386}
55e303ae
A
3387
3388#include <ddb/db_output.h>
3389void db_show_thread_log(void);
3390
3391void
3392db_show_thread_log(void)
3393{
3394}
1c79356b 3395#endif /* MACH_KDB */