+/*
+ * Depress thread's priority to lowest possible for the specified interval,
+ * with a value of zero resulting in no timeout being scheduled.
+ */
+void
+thread_depress_abstime(
+ uint64_t interval)
+{
+ register thread_t self = current_thread();
+ uint64_t deadline;
+ spl_t s;
+
+ s = splsched();
+ thread_lock(self);
+ if (!(self->sched_mode & TH_MODE_ISDEPRESSED)) {
+ processor_t myprocessor = self->last_processor;
+
+ self->sched_pri = DEPRESSPRI;
+ myprocessor->current_pri = self->sched_pri;
+ self->sched_mode &= ~TH_MODE_PREEMPT;
+ self->sched_mode |= TH_MODE_DEPRESS;
+
+ if (interval != 0) {
+ clock_absolutetime_interval_to_deadline(interval, &deadline);
+ if (!timer_call_enter(&self->depress_timer, deadline))
+ self->depress_timer_active++;
+ }
+ }
+ thread_unlock(self);
+ splx(s);
+}
+
+void
+thread_depress_ms(
+ mach_msg_timeout_t interval)
+{
+ uint64_t abstime;
+
+ clock_interval_to_absolutetime_interval(
+ interval, 1000*NSEC_PER_USEC, &abstime);
+ thread_depress_abstime(abstime);
+}
+
+/*
+ * Priority depression expiration.
+ */
+void
+thread_depress_expire(
+ void *p0,
+ __unused void *p1)
+{
+ thread_t thread = p0;
+ spl_t s;
+
+ s = splsched();
+ thread_lock(thread);
+ if (--thread->depress_timer_active == 0) {
+ thread->sched_mode &= ~TH_MODE_ISDEPRESSED;
+ compute_priority(thread, FALSE);
+ }
+ thread_unlock(thread);
+ splx(s);
+}
+
+/*
+ * Prematurely abort priority depression if there is one.
+ */
+kern_return_t
+thread_depress_abort_internal(
+ thread_t thread)
+{
+ kern_return_t result = KERN_NOT_DEPRESSED;
+ spl_t s;
+
+ s = splsched();
+ thread_lock(thread);
+ if (!(thread->sched_mode & TH_MODE_POLLDEPRESS)) {
+ if (thread->sched_mode & TH_MODE_ISDEPRESSED) {
+ thread->sched_mode &= ~TH_MODE_ISDEPRESSED;
+ compute_priority(thread, FALSE);
+ result = KERN_SUCCESS;
+ }
+
+ if (timer_call_cancel(&thread->depress_timer))
+ thread->depress_timer_active--;
+ }
+ thread_unlock(thread);
+ splx(s);
+
+ return (result);
+}
+
+void
+thread_poll_yield(
+ thread_t self)
+{
+ spl_t s;
+
+ assert(self == current_thread());
+
+ s = splsched();
+ if (!(self->sched_mode & (TH_MODE_REALTIME|TH_MODE_TIMESHARE))) {
+ uint64_t total_computation, abstime;
+
+ abstime = mach_absolute_time();
+ total_computation = abstime - self->computation_epoch;
+ total_computation += self->computation_metered;
+ if (total_computation >= max_poll_computation) {
+ processor_t myprocessor = current_processor();
+ ast_t preempt;
+
+ thread_lock(self);
+ if (!(self->sched_mode & TH_MODE_ISDEPRESSED)) {
+ self->sched_pri = DEPRESSPRI;
+ myprocessor->current_pri = self->sched_pri;
+ self->sched_mode &= ~TH_MODE_PREEMPT;
+ }
+ self->computation_epoch = abstime;
+ self->computation_metered = 0;
+ self->sched_mode |= TH_MODE_POLLDEPRESS;
+
+ abstime += (total_computation >> sched_poll_yield_shift);
+ if (!timer_call_enter(&self->depress_timer, abstime))
+ self->depress_timer_active++;
+ thread_unlock(self);
+
+ if ((preempt = csw_check(self, myprocessor)) != AST_NONE)
+ ast_on(preempt);
+ }
+ }
+ splx(s);