+
+ if (thread == self) {
+ thread_deallocate_internal(thread);
+ thread = THREAD_NULL;
+ }
+ }
+ else
+ thread = THREAD_NULL;
+ }
+ else
+ thread = THREAD_NULL;
+
+ /*
+ * Try to handoff if supplied.
+ */
+ if (thread != THREAD_NULL) {
+ processor_t processor;
+ spl_t s;
+
+ s = splsched();
+ thread_lock(thread);
+
+ /*
+ * Check if the thread is in the right pset,
+ * is not bound to a different processor,
+ * and that realtime is not involved.
+ *
+ * Next, pull it off its run queue. If it
+ * doesn't come, it's not eligible.
+ */
+ processor = current_processor();
+ if (processor->current_pri < BASEPRI_RTQUEUES &&
+ thread->sched_pri < BASEPRI_RTQUEUES &&
+ thread->processor_set == processor->processor_set &&
+ (thread->bound_processor == PROCESSOR_NULL ||
+ thread->bound_processor == processor) &&
+ run_queue_remove(thread) != RUN_QUEUE_NULL ) {
+ /*
+ * Hah, got it!!
+ */
+ thread_unlock(thread);
+
+ thread_deallocate_internal(thread);
+
+ if (option == SWITCH_OPTION_WAIT)
+ assert_wait_timeout((event_t)assert_wait_timeout, THREAD_ABORTSAFE,
+ option_time, 1000*NSEC_PER_USEC);
+ else
+ if (option == SWITCH_OPTION_DEPRESS)
+ thread_depress_ms(option_time);
+
+ self->saved.swtch.option = option;
+
+ thread_run(self, (thread_continue_t)thread_switch_continue, NULL, thread);
+ /* NOTREACHED */
+ }
+
+ thread_unlock(thread);
+ splx(s);
+
+ thread_deallocate(thread);
+ }
+
+ if (option == SWITCH_OPTION_WAIT)
+ assert_wait_timeout((event_t)assert_wait_timeout, THREAD_ABORTSAFE, option_time, 1000*NSEC_PER_USEC);
+ else
+ if (option == SWITCH_OPTION_DEPRESS)
+ thread_depress_ms(option_time);
+
+ self->saved.swtch.option = option;
+
+ thread_block_reason((thread_continue_t)thread_switch_continue, NULL, AST_YIELD);
+
+ if (option == SWITCH_OPTION_DEPRESS)
+ thread_depress_abort_internal(self);
+
+ return (KERN_SUCCESS);
+}
+
+/*
+ * 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++;