+ /* Mark us as running. */
+ cur->status = THREAD_RUNNING;
+
+#ifdef USERPROG
+ /* Activate the new address space. */
+ addrspace_activate (cur);
+#endif
+
+ /* If the thread we switched from is dying, destroy it.
+ This must happen late because it's not a good idea to
+ e.g. destroy the page table you're currently using. */
+ if (prev != NULL && prev->status == THREAD_DYING)
+ destroy_thread (prev);
+}
+
+/* Schedules a new process. At entry, interrupts must be off and
+ the running process's state must have been changed from
+ running to some other state. This function finds another
+ thread to run and switches to it. */
+static void
+schedule (void)
+{
+ struct thread *cur = running_thread ();
+ struct thread *next = next_thread_to_run ();
+ struct thread *prev = NULL;
+
+ ASSERT (intr_get_level () == INTR_OFF);
+ ASSERT (cur->status != THREAD_RUNNING);
+ ASSERT (is_thread (next));
+
+ if (cur != next)
+ prev = switch_threads (cur, next);
+ schedule_tail (prev);