Small loader cleanups.
[pintos-anon] / src / threads / thread.c
index 5e8a32911bfadb80ebf79e30e294d4222d425edb..218305c00a140384e20d2ffa42a9597b40970cf7 100644 (file)
 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
 
 static struct list run_queue;
+static struct thread *idle_thread;
+
+static void
+idle (void *aux UNUSED) 
+{
+  for (;;) 
+    {
+      /* Wait for an interrupt. */
+      asm ("hlt");
+
+      /* Let someone else run. */
+      intr_disable ();
+      thread_sleep ();
+      intr_enable ();
+    }
+}
 
 void
 thread_init (void) 
 {
   list_init (&run_queue);
+  idle_thread = thread_create ("idle", idle, NULL);
 }
 
 struct thread_root_frame 
@@ -30,7 +47,8 @@ static void
 thread_root (void (*function) (void *aux), void *aux) 
 {
   ASSERT (function != NULL);
-  
+
+  intr_enable ();
   function (aux);
   thread_exit ();
 }
@@ -165,14 +183,6 @@ find_next_to_run (void)
     return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
 }
 
-static void
-idle (void) 
-{
-  static int idle = 0;
-  if (idle++ == 0)
-    printk ("idle\n");
-}
-
 void
 thread_destroy (struct thread *t) 
 {
@@ -187,7 +197,9 @@ void schedule_tail (struct thread *prev);
 void
 schedule_tail (struct thread *prev) 
 {
+#ifdef USERPROG
   struct thread *cur = thread_current ();
+#endif
 
   ASSERT (intr_get_level () == IF_OFF);
 
@@ -209,8 +221,9 @@ thread_schedule (void)
   cur = thread_current ();
   ASSERT (cur->status != THREAD_RUNNING);
 
-  while ((next = find_next_to_run ()) == NULL)
-    idle ();
+  next = find_next_to_run ();
+  if (next == NULL)
+    next = idle_thread;
 
   next->status = THREAD_RUNNING;
   if (cur != next)
@@ -247,6 +260,7 @@ thread_start (struct thread *t)
     list_remove (&t->rq_elem);
   t->status = THREAD_RUNNING;
   switch_threads (NULL, t);
+  NOT_REACHED ();
 }
 
 void