Rename printk() to printf().
[pintos-anon] / src / threads / thread.c
index 7b34722777e6ae1beb14cb4b44cc23b633064a7a..85f107d19ff01e988c9eaa3394522c56afe753ef 100644 (file)
@@ -1,17 +1,20 @@
-#include "thread.h"
+#include "threads/thread.h"
+#include <debug.h>
 #include <stddef.h>
-#include "debug.h"
-#include "interrupt.h"
-#include "intr-stubs.h"
-#include "lib.h"
-#include "mmu.h"
-#include "palloc.h"
-#include "random.h"
-#include "switch.h"
+#include <random.h>
+#include <string.h>
+#include "threads/interrupt.h"
+#include "threads/intr-stubs.h"
+#include "threads/mmu.h"
+#include "threads/palloc.h"
+#include "threads/switch.h"
 #ifdef USERPROG
-#include "gdt.h"
+#include "userprog/gdt.h"
 #endif
 
+/* Value for struct thread's `magic' member.
+   Used to detect stack overflow.  See the big comment at the top
+   of thread.h for details. */
 #define THREAD_MAGIC 0x1234abcdu
 
 /* List of processes in THREAD_READY state, that is, processes
@@ -113,7 +116,7 @@ thread_create (const char *name, thread_func *function, void *aux)
   sf->eip = switch_entry;
 
   /* Add to run queue. */
-  thread_wake (t);
+  thread_unblock (t);
 
   return t;
 }
@@ -160,25 +163,29 @@ thread_execute (const char *filename)
   sf->eip = switch_entry;
 
   /* Add to run queue. */
-  thread_wake (t);
+  thread_unblock (t);
 
   return true;
 }
 #endif
 
-/* Transitions T from its current state to THREAD_READY, the
-   ready-to-run state.  On entry, T must be ready or blocked.
+/* Transitions a blocked thread T from its current state to the
+   ready-to-run state.  If T is not blocked, there is no effect.
    (Use thread_yield() to make the running thread ready.) */
 void
-thread_wake (struct thread *t) 
+thread_unblock (struct thread *t) 
 {
+  enum intr_level old_level;
+
   ASSERT (is_thread (t));
-  ASSERT (t->status == THREAD_READY || t->status == THREAD_BLOCKED);
-  if (t->status != THREAD_READY) 
+
+  old_level = intr_disable ();
+  if (t->status == THREAD_BLOCKED) 
     {
-      list_push_back (&run_queue, &t->rq_elem);
+      list_push_back (&run_queue, &t->elem);
       t->status = THREAD_READY;
     }
+  intr_set_level (old_level);
 }
 
 /* Returns the name of thread T. */
@@ -190,7 +197,8 @@ thread_name (struct thread *t)
 }
 
 /* Returns the running thread.
-   This is running_thread() plus a couple of sanity checks. */
+   This is running_thread() plus a couple of sanity checks.
+   See the big comment at the top of thread.h for details. */
 struct thread *
 thread_current (void) 
 {
@@ -214,6 +222,8 @@ thread_exit (void)
 {
   ASSERT (!intr_context ());
 
+  /* Just set our status to dying and schedule another process.
+     We will be destroyed during the call to schedule_tail(). */
   intr_disable ();
   thread_current ()->status = THREAD_DYING;
   schedule ();
@@ -231,16 +241,20 @@ thread_yield (void)
   ASSERT (!intr_context ());
 
   old_level = intr_disable ();
-  list_push_back (&run_queue, &cur->rq_elem);
+  list_push_back (&run_queue, &cur->elem);
   cur->status = THREAD_READY;
   schedule ();
   intr_set_level (old_level);
 }
 
 /* Puts the current thread to sleep.  It will not be scheduled
-   again until awoken by thread_wake(). */
+   again until awoken by thread_unblock().
+
+   This function must be called with interrupts turned off.  It
+   is usually a better idea to use one of the synchronization
+   primitives in synch.h. */
 void
-thread_sleep (void) 
+thread_block (void) 
 {
   ASSERT (!intr_context ());
   ASSERT (intr_get_level () == INTR_OFF);
@@ -261,7 +275,7 @@ idle (void *aux UNUSED)
 
       /* Let someone else run. */
       intr_disable ();
-      thread_sleep ();
+      thread_block ();
       intr_enable ();
     }
 }
@@ -315,7 +329,7 @@ new_thread (const char *name)
   return t;
 }
 
-/* Initializes T as a new thread named NAME. */
+/* Initializes T as a new, blocked thread named NAME. */
 static void
 init_thread (struct thread *t, const char *name)
 {
@@ -350,7 +364,7 @@ next_thread_to_run (void)
   if (list_empty (&run_queue))
     return idle_thread;
   else
-    return list_entry (list_pop_front (&run_queue), struct thread, rq_elem);
+    return list_entry (list_pop_front (&run_queue), struct thread, elem);
 }
 
 /* Destroys T, which must be in the dying state and must not be
@@ -387,13 +401,19 @@ schedule_tail (struct thread *prev)
   
   ASSERT (intr_get_level () == INTR_OFF);
 
+  /* Mark us as running. */
   cur->status = THREAD_RUNNING;
-  if (prev != NULL && prev->status == THREAD_DYING) 
-    destroy_thread (prev);
 
 #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