+ if (SCHED(can_update_priority)(thread))
+ SCHED(update_priority)(thread);
+ else
+ SCHED(lightweight_update_priority)(thread);
+
+ if (thread->sched_mode != TH_MODE_REALTIME)
+ SCHED(quantum_expire)(thread);
+
+ processor_state_update_from_thread(processor, thread);
+
+ /*
+ * This quantum is up, give this thread another.
+ */
+ processor->first_timeslice = FALSE;
+
+ thread_quantum_init(thread);
+
+ /* Reload precise timing global policy to thread-local policy */
+ thread->precise_user_kernel_time = use_precise_user_kernel_time(thread);
+
+ /*
+ * Since non-precise user/kernel time doesn't update the state/thread timer
+ * during privilege transitions, synthesize an event now.
+ */
+ if (!thread->precise_user_kernel_time) {
+ timer_switch(PROCESSOR_DATA(processor, current_state),
+ ctime,
+ PROCESSOR_DATA(processor, current_state));
+ timer_switch(PROCESSOR_DATA(processor, thread_timer),
+ ctime,
+ PROCESSOR_DATA(processor, thread_timer));
+ }
+
+
+ processor->quantum_end = ctime + thread->quantum_remaining;
+
+ /*
+ * Context switch check
+ *
+ * non-urgent flags don't affect kernel threads, so upgrade to urgent
+ * to ensure that rebalancing and non-recommendation kick in quickly.
+ */
+
+ ast_t check_reason = AST_QUANTUM;
+ if (thread->task == kernel_task)
+ check_reason |= AST_URGENT;
+
+ if ((preempt = csw_check(processor, check_reason)) != AST_NONE)
+ ast_on(preempt);
+
+ /*
+ * AST_KEVENT does not send an IPI when setting the AST,
+ * to avoid waiting for the next context switch to propagate the AST,
+ * the AST is propagated here at quantum expiration.
+ */
+ ast_propagate(thread);
+
+ thread_unlock(thread);
+
+ timer_call_quantum_timer_enter(&processor->quantum_timer, thread,
+ processor->quantum_end, ctime);
+
+ /* Tell platform layer that we are still running this thread */
+ urgency = thread_get_urgency(thread, &ignore1, &ignore2);
+ machine_thread_going_on_core(thread, urgency, 0, 0, ctime);
+ machine_switch_perfcontrol_state_update(QUANTUM_EXPIRY, ctime,
+ 0, thread);
+
+#if defined(CONFIG_SCHED_TIMESHARE_CORE)
+ sched_timeshare_consider_maintenance(ctime);
+#endif /* CONFIG_SCHED_TIMESHARE_CORE */
+
+#if __arm__ || __arm64__
+ if (thread->sched_mode == TH_MODE_REALTIME)
+ sched_consider_recommended_cores(ctime, thread);
+#endif /* __arm__ || __arm64__ */
+
+ KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_QUANTUM_EXPIRED) | DBG_FUNC_END, preempt, 0, 0, 0, 0);
+}
+
+/*
+ * sched_set_thread_base_priority:
+ *
+ * Set the base priority of the thread
+ * and reset its scheduled priority.
+ *
+ * This is the only path to change base_pri.
+ *
+ * Called with the thread locked.
+ */
+void
+sched_set_thread_base_priority(thread_t thread, int priority)
+{
+ assert(priority >= MINPRI);
+ uint64_t ctime = 0;
+
+ if (thread->sched_mode == TH_MODE_REALTIME)
+ assert(priority <= BASEPRI_RTQUEUES);
+ else
+ assert(priority < BASEPRI_RTQUEUES);
+
+ int old_base_pri = thread->base_pri;
+ thread->base_pri = priority;
+
+ if ((thread->state & TH_RUN) == TH_RUN) {
+ assert(thread->last_made_runnable_time != THREAD_NOT_RUNNABLE);
+ ctime = mach_approximate_time();
+ thread->last_basepri_change_time = ctime;
+ } else {
+ assert(thread->last_basepri_change_time == THREAD_NOT_RUNNABLE);
+ assert(thread->last_made_runnable_time == THREAD_NOT_RUNNABLE);
+ }
+
+ /*
+ * Currently the perfcontrol_attr depends on the base pri of the
+ * thread. Therefore, we use this function as the hook for the
+ * perfcontrol callout.
+ */
+ if (thread == current_thread() && old_base_pri != priority) {
+ if (!ctime) {
+ ctime = mach_approximate_time();
+ }
+ machine_switch_perfcontrol_state_update(PERFCONTROL_ATTR_UPDATE,
+ ctime, PERFCONTROL_CALLOUT_WAKE_UNSAFE, thread);
+ }
+ sched_update_thread_bucket(thread);
+
+ thread_recompute_sched_pri(thread, FALSE);
+}
+
+/*
+ * thread_recompute_sched_pri:
+ *
+ * Reset the scheduled priority of the thread
+ * according to its base priority if the
+ * thread has not been promoted or depressed.
+ *
+ * This is the standard way to push base_pri changes into sched_pri,
+ * or to recalculate the appropriate sched_pri after clearing
+ * a promotion or depression.
+ *
+ * Called at splsched with the thread locked.
+ */
+void
+thread_recompute_sched_pri(
+ thread_t thread,
+ boolean_t override_depress)
+{
+ int priority;
+
+ if (thread->sched_mode == TH_MODE_TIMESHARE)
+ priority = SCHED(compute_timeshare_priority)(thread);