Clean up threads.
[pintos-anon] / src / threads / thread.c
index be607cfbe2273c8e065bffb2f775bf4865e0a432..bae893d5f872d70f1cce8803fe03bf9e2fc62846 100644 (file)
@@ -9,17 +9,27 @@
 #include "random.h"
 #include "switch.h"
 
+/* Offset of `stack' member within `struct thread'.
+   Used by switch.S, which can't figure it out on its own. */
 uint32_t thread_stack_ofs = offsetof (struct thread, stack);
 
+/* List of processes in THREAD_READY state, that is, processes
+   that are ready to run but not actually running. */
 static struct list run_queue;
+
+/* Thread to run when nothing else is ready. */
 static struct thread *idle_thread;
 
+static struct thread *find_next_to_run (void);
+
+/* Idle thread.  Executes when no other thread is ready to run. */
 static void
 idle (void *aux UNUSED) 
 {
   for (;;) 
     {
       /* Wait for an interrupt. */
+      DEBUG (idle, "idle");
       asm ("hlt");
 
       /* Let someone else run. */
@@ -29,30 +39,57 @@ idle (void *aux UNUSED)
     }
 }
 
+/* Initializes the threading system and starts an initial thread
+   which is immediately scheduled.  Never returns to the caller.
+   The initial thread is named NAME and executes FUNCTION passing
+   AUX as the argument. */
 void
 thread_init (void) 
 {
+  ASSERT (intr_get_level () == IF_OFF);
+
+  /* Initialize run queue. */
   list_init (&run_queue);
+
+  /* Create idle thread. */
   idle_thread = thread_create ("idle", idle, NULL);
+  idle_thread->status = THREAD_BLOCKED;
 }
 
-struct thread_root_frame 
+void
+thread_start (void) 
+{
+  struct thread *t = find_next_to_run ();
+  if (t->status == THREAD_READY)
+    list_remove (&t->rq_elem);
+  t->status = THREAD_RUNNING;
+  switch_threads (NULL, t);
+
+  NOT_REACHED ();
+}
+\f
+/* Stack frame for kernel_thread(). */
+struct kernel_thread_frame 
   {
     void *eip;                  /* Return address. */
     void (*function) (void *);  /* Function to call. */
     void *aux;                  /* Auxiliary data for function. */
   };
 
+/* Function used as the basis for a kernel thread. */
 static void
-thread_root (void (*function) (void *aux), void *aux) 
+kernel_thread (void (*function) (void *aux), void *aux) 
 {
   ASSERT (function != NULL);
 
-  intr_enable ();
-  function (aux);
-  thread_exit ();
+  intr_enable ();       /* The scheduler runs with interrupts off. */
+  function (aux);       /* Execute the thread function. */
+  thread_exit ();       /* If function() returns, kill the thread. */
 }
 
+/* Creates a new thread named NAME and initializes its fields.
+   Returns the new thread if successful or a null pointer on
+   failure. */
 static struct thread *
 new_thread (const char *name) 
 {
@@ -65,26 +102,34 @@ new_thread (const char *name)
     {
       strlcpy (t->name, name, sizeof t->name);
       t->stack = (uint8_t *) t + PGSIZE;
-      t->status = THREAD_BLOCKED;
+      t->status = THREAD_INITIALIZING;
     }
   
   return t;
 }
 
+/* Allocates a SIZE-byte frame within thread T's stack and
+   returns a pointer to the frame's base. */
 static void *
 alloc_frame (struct thread *t, size_t size) 
 {
+  /* Stack data is always allocated in word-size units. */
   ASSERT (size % sizeof (uint32_t) == 0);
 
   t->stack -= size;
   return t->stack;
 }
 
+/* Creates a new kernel thread named NAME, which executes
+   FUNCTION passing AUX as the argument.  The thread is added to
+   the ready queue.  Thus, it may be scheduled even before
+   thread_create() returns.  If you need to ensure ordering, then
+   use synchronization, such as a semaphore. */
 struct thread *
 thread_create (const char *name, void (*function) (void *aux), void *aux) 
 {
   struct thread *t;
-  struct thread_root_frame *rf;
+  struct kernel_thread_frame *kf;
   struct switch_entry_frame *ef;
   struct switch_threads_frame *sf;
 
@@ -92,17 +137,17 @@ thread_create (const char *name, void (*function) (void *aux), void *aux)
 
   t = new_thread (name);
 
-  /* Stack frame for thread_root(). */
-  rf = alloc_frame (t, sizeof *rf);
-  rf->eip = NULL;
-  rf->function = function;
-  rf->aux = aux;
+  /* Stack frame for kernel_thread(). */
+  kf = alloc_frame (t, sizeof *kf);
+  kf->eip = NULL;
+  kf->function = function;
+  kf->aux = aux;
 
   /* Stack frame for switch_entry(). */
   ef = alloc_frame (t, sizeof *ef);
-  ef->eip = (void (*) (void)) thread_root;
+  ef->eip = (void (*) (void)) kernel_thread;
 
-  /* Stack frame for thread_switch(). */
+  /* Stack frame for switch_threads(). */
   sf = alloc_frame (t, sizeof *sf);
   sf->eip = switch_entry;
 
@@ -137,7 +182,7 @@ thread_execute (const char *filename)
     return false;
   
   if (!addrspace_load (&t->addrspace, filename, &start)) 
-    panic ("%s: program load failed", filename);
+    PANIC ("%s: program load failed", filename);
 
   /* Interrupt frame. */
   if_ = alloc_frame (t, sizeof *if_);
@@ -153,7 +198,7 @@ thread_execute (const char *filename)
   ef = alloc_frame (t, sizeof *ef);
   ef->eip = intr_exit;
 
-  /* Stack frame for thread_switch(). */
+  /* Stack frame for switch_threads(). */
   sf = alloc_frame (t, sizeof *sf);
   sf->eip = switch_entry;
 
@@ -178,7 +223,7 @@ static struct thread *
 find_next_to_run (void) 
 {
   if (list_empty (&run_queue))
-    return NULL;
+    return idle_thread;
   else
     return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
 }
@@ -197,12 +242,10 @@ void schedule_tail (struct thread *prev);
 void
 schedule_tail (struct thread *prev) 
 {
-  struct thread *cur = thread_current ();
-
   ASSERT (intr_get_level () == IF_OFF);
 
 #ifdef USERPROG
-  addrspace_activate (&cur->addrspace);
+  addrspace_activate (&thread_current ()->addrspace);
 #endif
 
   if (prev != NULL && prev->status == THREAD_DYING) 
@@ -220,8 +263,6 @@ thread_schedule (void)
   ASSERT (cur->status != THREAD_RUNNING);
 
   next = find_next_to_run ();
-  if (next == NULL)
-    next = idle_thread;
 
   next->status = THREAD_RUNNING;
   if (cur != next)
@@ -249,18 +290,6 @@ thread_yield (void)
   intr_set_level (old_level);
 }
 
-void
-thread_start (struct thread *t) 
-{
-  ASSERT (intr_get_level () == IF_OFF);
-
-  if (t->status == THREAD_READY) 
-    list_remove (&t->rq_elem);
-  t->status = THREAD_RUNNING;
-  switch_threads (NULL, t);
-  NOT_REACHED ();
-}
-
 void
 thread_exit (void) 
 {
@@ -281,37 +310,3 @@ thread_sleep (void)
   thread_current ()->status = THREAD_BLOCKED;
   thread_schedule ();
 }
-
-static void
-tfunc (void *aux UNUSED) 
-{
-  for (;;) 
-    {
-      size_t count, i;
-      if (random_ulong () % 5 == 0)
-        {
-          printk ("%s exiting\n", thread_current ()->name);
-          break;
-        }
-      count = random_ulong () % 25 * 10000;
-      printk ("%s waiting %zu: ", thread_current ()->name, count);
-      for (i = 0; i < count; i++);
-      printk ("%s\n", thread_current ()->name);
-    }
-}
-
-void
-thread_self_test (void)
-{
-  struct thread *t;
-  int i;
-    
-  for (i = 0; i < 4; i++) 
-    {
-      char name[2];
-      name[0] = 'a' + i;
-      name[1] = 0;
-      t = thread_create (name, tfunc, NULL); 
-    }
-  thread_start (t); 
-}