• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /macosx-10.10/xnu-2782.1.97/osfmk/kern/

Lines Matching refs:thread

74 #include <kern/thread.h>
86 * Recalculate the quantum and priority for a thread.
97 thread_t thread = p1;
106 * We bill CPU time to both the individual thread and its task.
109 * thread, we must credit the ledger before taking the thread lock. The ledger
110 * pointers are only manipulated by the thread itself at the ast boundary.
112 ledger_credit(thread->t_ledger, task_ledgers.cpu_time, thread->quantum_remaining);
113 ledger_credit(thread->t_threadledger, thread_ledgers.cpu_time, thread->quantum_remaining);
115 if (thread->t_bankledger) {
116 ledger_credit(thread->t_bankledger, bank_ledgers.cpu_time,
117 (thread->quantum_remaining - thread->t_deduct_bank_ledger_time));
119 thread->t_deduct_bank_ledger_time = 0;
124 thread_lock(thread);
130 thread->last_run_time = ctime;
138 if ((thread->sched_mode == TH_MODE_REALTIME || thread->sched_mode == TH_MODE_FIXED) &&
139 !(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) &&
140 !(thread->options & TH_OPT_SYSTEM_CRITICAL)) {
143 new_computation = ctime - thread->computation_epoch;
144 new_computation += thread->computation_metered;
147 (uintptr_t)thread->sched_pri, (uintptr_t)thread->sched_mode, 0, 0, 0);
149 thread->safe_release = ctime + sched_safe_duration;
151 sched_thread_mode_demote(thread, TH_SFLAG_FAILSAFE);
158 if (SCHED(can_update_priority)(thread))
159 SCHED(update_priority)(thread);
161 SCHED(lightweight_update_priority)(thread);
163 SCHED(quantum_expire)(thread);
165 processor->current_pri = thread->sched_pri;
166 processor->current_thmode = thread->sched_mode;
169 * This quantum is up, give this thread another.
174 thread_quantum_init(thread);
176 /* Reload precise timing global policy to thread-local policy */
177 thread->precise_user_kernel_time = use_precise_user_kernel_time(thread);
180 * Since non-precise user/kernel time doesn't update the state/thread timer
183 if (!thread->precise_user_kernel_time) {
192 processor->quantum_end = ctime + thread->quantum_remaining;
193 timer_call_enter1(&processor->quantum_timer, thread,
202 thread_unlock(thread);
212 * Set the base priority of the thread
215 * Called with the thread locked.
218 sched_set_thread_base_priority(thread_t thread, int priority)
220 thread->priority = priority;
221 SCHED(compute_priority)(thread, FALSE);
228 sched_traditional_quantum_expire(thread_t thread __unused)
231 * No special behavior when a timeshare, fixed, or realtime thread
237 lightweight_update_priority(thread_t thread)
239 if (thread->sched_mode == TH_MODE_TIMESHARE) {
242 thread_timer_delta(thread, delta);
249 if (thread->pri_shift < INT8_MAX)
250 thread->sched_usage += delta;
252 thread->cpu_delta += delta;
256 * the thread has not been promoted
259 if ( !(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) &&
260 !(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) )
261 compute_my_priority(thread);
309 * Reset the scheduled priority of the thread
311 * thread has not been promoted or depressed.
313 * Called with the thread locked.
317 register thread_t thread,
322 if (thread->sched_mode == TH_MODE_TIMESHARE)
323 priority = do_priority_computation(thread);
325 priority = thread->priority;
327 if ((!(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) || (priority > thread->sched_pri)) &&
328 (!(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) || override_depress)) {
329 set_sched_pri(thread, priority);
337 * a timesharing thread.
339 * Only for use on the current thread
342 * Called with the thread locked.
346 register thread_t thread)
350 priority = do_priority_computation(thread);
351 assert(thread->runq == PROCESSOR_NULL);
353 if (priority != thread->sched_pri) {
355 (uintptr_t)thread_tid(thread),
356 thread->priority,
357 thread->sched_pri,
361 thread->sched_pri = priority;
369 * Called with the thread locked.
373 thread_t thread)
375 if (sched_tick == thread->sched_stamp)
386 * Called with the thread locked.
390 register thread_t thread)
395 ticks = sched_tick - thread->sched_stamp;
397 thread->sched_stamp += ticks;
399 thread->pri_shift = sched_combined_fgbg_pri_shift;
400 else if (thread->sched_flags & TH_SFLAG_THROTTLED)
401 thread->pri_shift = sched_background_pri_shift;
403 thread->pri_shift = sched_pri_shift;
412 thread_timer_delta(thread, delta);
421 if (thread->pri_shift < INT8_MAX)
422 thread->sched_usage += delta;
424 thread->cpu_usage += delta + thread->cpu_delta;
425 thread->cpu_delta = 0;
429 thread->cpu_usage =
430 (thread->cpu_usage >> shiftp->shift1) +
431 (thread->cpu_usage >> shiftp->shift2);
432 thread->sched_usage =
433 (thread->sched_usage >> shiftp->shift1) +
434 (thread->sched_usage >> shiftp->shift2);
437 thread->cpu_usage =
438 (thread->cpu_usage >> shiftp->shift1) -
439 (thread->cpu_usage >> -(shiftp->shift2));
440 thread->sched_usage =
441 (thread->sched_usage >> shiftp->shift1) -
442 (thread->sched_usage >> -(shiftp->shift2));
446 thread->cpu_usage = thread->cpu_delta = 0;
447 thread->sched_usage = 0;
453 if ((thread->sched_flags & TH_SFLAG_FAILSAFE) &&
454 mach_absolute_time() >= thread->safe_release) {
455 sched_thread_mode_undemote(thread, TH_SFLAG_FAILSAFE);
462 if ( (thread->sched_mode == TH_MODE_TIMESHARE) &&
463 !(thread->sched_flags & TH_SFLAG_PROMOTED_MASK) &&
464 !(thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) ) {
467 new_pri = do_priority_computation(thread);
468 if (new_pri != thread->sched_pri) {
469 boolean_t removed = thread_run_queue_remove(thread);
472 if (sched_use_combined_fgbg_decay && ((thread)->task->max_priority > MAXPRI_THROTTLE) && (new_pri == MAXPRI_THROTTLE)) {
474 int alt_pri = thread->priority - (thread->sched_usage >> sched_pri_shift);
476 printf("thread %p would have decayed to only %d instead of %d\n", thread, alt_pri, new_pri);
482 (uintptr_t)thread_tid(thread),
483 thread->priority,
484 thread->sched_pri,
487 thread->sched_pri = new_pri;
490 thread_setrun(thread, SCHED_TAILQ);
500 /* sched_mode == TH_MODE_TIMESHARE controls whether a thread has a timeshare count when it has a run count */
502 void sched_share_incr(thread_t thread) {
503 assert((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN);
504 assert(thread->sched_mode == TH_MODE_TIMESHARE);
505 assert(thread->SHARE_COUNT == 0);
506 thread->SHARE_COUNT++;
510 void sched_share_decr(thread_t thread) {
511 assert((thread->state & (TH_RUN|TH_IDLE)) != TH_RUN || thread->sched_mode != TH_MODE_TIMESHARE);
512 assert(thread->SHARE_COUNT == 1);
514 thread->SHARE_COUNT--;
517 /* TH_SFLAG_THROTTLED controls whether a thread has a background count when it has a run count and a share count */
519 void sched_background_incr(thread_t thread) {
520 assert((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN);
521 assert(thread->sched_mode == TH_MODE_TIMESHARE);
522 assert((thread->sched_flags & TH_SFLAG_THROTTLED) == TH_SFLAG_THROTTLED);
524 assert(thread->BG_COUNT == 0);
525 thread->BG_COUNT++;
530 assert(thread->SHARE_COUNT == 1);
533 void sched_background_decr(thread_t thread) {
534 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN && thread->sched_mode == TH_MODE_TIMESHARE)
535 assert((thread->sched_flags & TH_SFLAG_THROTTLED) != TH_SFLAG_THROTTLED);
536 assert(thread->BG_COUNT == 1);
538 thread->BG_COUNT--;
540 assert(thread->BG_COUNT == 0);
543 assert(thread->SHARE_COUNT == 1);
548 assert_thread_sched_count(thread_t thread) {
550 assert(thread->BG_COUNT == 0 || thread->BG_COUNT == 1);
551 assert(thread->SHARE_COUNT == 0 || thread->SHARE_COUNT == 1);
554 if (thread->BG_COUNT == 1)
555 assert(thread->SHARE_COUNT == 1);
556 if (thread->SHARE_COUNT == 0)
557 assert(thread->BG_COUNT == 0);
559 if ((thread->state & (TH_RUN|TH_IDLE)) != TH_RUN ||
560 (thread->sched_mode != TH_MODE_TIMESHARE))
561 assert(thread->SHARE_COUNT == 0);
563 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN &&
564 (thread->sched_mode == TH_MODE_TIMESHARE))
565 assert(thread->SHARE_COUNT == 1);
567 if ((thread->state & (TH_RUN|TH_IDLE)) != TH_RUN ||
568 (thread->sched_mode != TH_MODE_TIMESHARE) ||
569 !(thread->sched_flags & TH_SFLAG_THROTTLED))
570 assert(thread->BG_COUNT == 0);
572 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN &&
573 (thread->sched_mode == TH_MODE_TIMESHARE) &&
574 (thread->sched_flags & TH_SFLAG_THROTTLED))
575 assert(thread->BG_COUNT == 1);
581 * Set the thread's true scheduling mode
582 * Called with thread mutex and thread locked
583 * The thread has already been removed from the runqueue.
588 sched_set_thread_mode(thread_t thread, sched_mode_t new_mode)
590 assert_thread_sched_count(thread);
592 sched_mode_t old_mode = thread->sched_mode;
594 thread->sched_mode = new_mode;
600 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN) {
601 if (thread->sched_flags & TH_SFLAG_THROTTLED)
602 sched_background_decr(thread);
604 sched_share_decr(thread);
611 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN) {
612 sched_share_incr(thread);
614 if (thread->sched_flags & TH_SFLAG_THROTTLED)
615 sched_background_incr(thread);
625 assert_thread_sched_count(thread);
629 * Demote the true scheduler mode to timeshare (called with the thread locked)
632 sched_thread_mode_demote(thread_t thread, uint32_t reason)
635 assert((thread->sched_flags & reason) != reason);
636 assert_thread_sched_count(thread);
638 if (thread->policy_reset)
641 if (thread->sched_flags & TH_SFLAG_DEMOTED_MASK) {
643 thread->sched_flags |= reason;
647 assert(thread->saved_mode == TH_MODE_NONE);
649 boolean_t removed = thread_run_queue_remove(thread);
651 if (thread->sched_mode == TH_MODE_REALTIME)
652 thread->priority = DEPRESSPRI;
654 thread->sched_flags |= reason;
656 thread->saved_mode = thread->sched_mode;
658 sched_set_thread_mode(thread, TH_MODE_TIMESHARE);
661 thread_setrun(thread, SCHED_TAILQ);
663 assert_thread_sched_count(thread);
667 * Un-demote the true scheduler mode back to the saved mode (called with the thread locked)
670 sched_thread_mode_undemote(thread_t thread, uint32_t reason)
673 assert((thread->sched_flags & reason) == reason);
674 assert(thread->saved_mode != TH_MODE_NONE);
675 assert(thread->sched_mode == TH_MODE_TIMESHARE);
676 assert(thread->policy_reset == 0);
678 assert_thread_sched_count(thread);
680 thread->sched_flags &= ~reason;
682 if (thread->sched_flags & TH_SFLAG_DEMOTED_MASK) {
687 boolean_t removed = thread_run_queue_remove(thread);
689 sched_set_thread_mode(thread, thread->saved_mode);
691 thread->saved_mode = TH_MODE_NONE;
693 if (thread->sched_mode == TH_MODE_REALTIME) {
694 thread->priority = BASEPRI_RTQUEUES;
697 SCHED(compute_priority)(thread, FALSE);
700 thread_setrun(thread, SCHED_TAILQ);
704 * Set the thread to be categorized as 'background'
705 * Called with thread mutex and thread lock held
710 sched_set_thread_throttled(thread_t thread, boolean_t wants_throttle)
712 if (thread->policy_reset)
715 assert(((thread->sched_flags & TH_SFLAG_THROTTLED) ? TRUE : FALSE) != wants_throttle);
717 assert_thread_sched_count(thread);
720 * When backgrounding a thread, iOS has the semantic that
728 thread->sched_flags |= TH_SFLAG_THROTTLED;
729 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN && thread->sched_mode == TH_MODE_TIMESHARE) {
730 sched_background_incr(thread);
733 assert_thread_sched_count(thread);
736 thread->sched_flags &= ~TH_SFLAG_THROTTLED;
737 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN && thread->sched_mode == TH_MODE_TIMESHARE) {
738 sched_background_decr(thread);
741 assert_thread_sched_count(thread);
745 assert_thread_sched_count(thread);