Redo makefiles.
[pintos-anon] / src / threads / synch.c
index 39e43c3d57868935224f2a77e7305b96e4cff6b6..7993543016448f5fa55b4ed5378a022e181c0fc6 100644 (file)
@@ -1,15 +1,35 @@
+/* This file is derived from source code for the Nachos
+   instructional operating system.  The Nachos copyright notice
+   is reproduced in full below. */
+
+/* Copyright (c) 1992-1996 The Regents of the University of California.
+   All rights reserved.
+
+   Permission to use, copy, modify, and distribute this software
+   and its documentation for any purpose, without fee, and
+   without written agreement is hereby granted, provided that the
+   above copyright notice and the following two paragraphs appear
+   in all copies of this software.
+
+   IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO
+   ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
+   CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE
+   AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA
+   HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+   THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
+   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+   PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
+   BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
+   PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
+   MODIFICATIONS.
+*/
+
 #include "synch.h"
 #include "interrupt.h"
-#include "lib.h"
-#include "malloc.h"
 #include "thread.h"
-
-/* One thread in a list. */
-struct thread_elem
-  {
-    struct list_elem elem;
-    struct thread *thread;      
-  };
+#include "lib/lib.h"
 
 /* Initializes semaphore SEMA to VALUE and names it NAME (for
    debugging purposes only).  A semaphore is a nonnegative
@@ -31,15 +51,17 @@ sema_init (struct semaphore *sema, unsigned value, const char *name)
   list_init (&sema->waiters);
 }
 
-/* Waits for SEMA's value to become positive and then
-   atomically decrements it.
+/* Down or "P" operation on a semaphore.  Waits for SEMA's value
+   to become positive and then atomically decrements it.
 
    This function may sleep, so it must not be called within an
-   interrupt handler. */
+   interrupt handler.  This function may be called with
+   interrupts disabled, but interrupts will be turned back on if
+   we need to sleep. */
 void
 sema_down (struct semaphore *sema) 
 {
-  enum if_level old_level;
+  enum intr_level old_level;
 
   ASSERT (sema != NULL);
   ASSERT (!intr_context ());
@@ -47,30 +69,28 @@ sema_down (struct semaphore *sema)
   old_level = intr_disable ();
   while (sema->value == 0) 
     {
-      struct thread_elem te;
-      te.thread = thread_current ();
-      list_push_back (&sema->waiters, &te.elem);
-      thread_sleep ();
+      list_push_back (&sema->waiters, &thread_current ()->elem);
+      thread_block ();
     }
   sema->value--;
   intr_set_level (old_level);
 }
 
-/* Increments SEMA's value.  Wakes up one thread of those waiting
-   for SEMA, if any. 
+/* Up or "V" operation on a semaphore.  Increments SEMA's value
+   and wakes up one thread of those waiting for SEMA, if any.
 
    This function may be called from an interrupt handler. */
 void
 sema_up (struct semaphore *sema) 
 {
-  enum if_level old_level;
+  enum intr_level old_level;
 
   ASSERT (sema != NULL);
 
   old_level = intr_disable ();
   if (!list_empty (&sema->waiters)) 
-    thread_ready (list_entry (list_pop_front (&sema->waiters),
-                              struct thread_elem, elem)->thread);
+    thread_unblock (list_entry (list_pop_front (&sema->waiters),
+                                struct thread, elem));
   sema->value++;
   intr_set_level (old_level);
 }
@@ -82,18 +102,7 @@ sema_name (const struct semaphore *sema)
   return sema->name;
 }
 
-static void
-sema_test_helper (void *sema_) 
-{
-  struct semaphore *sema = sema_;
-  int i;
-
-  for (i = 0; i < 10; i++) 
-    {
-      sema_down (&sema[0]);
-      sema_up (&sema[1]);
-    }
-}
+static void sema_test_helper (void *sema_);
 
 /* Self-test for semaphores that makes control "ping-pong"
    between a pair of threads.  Insert calls to printk() to see
@@ -105,6 +114,7 @@ sema_self_test (void)
   struct semaphore sema[2];
   int i;
 
+  printk ("Testing semaphores...");
   sema_init (&sema[0], 0, "ping");
   sema_init (&sema[1], 0, "pong");
   thread = thread_create ("sema-test", sema_test_helper, &sema);
@@ -113,13 +123,28 @@ sema_self_test (void)
       sema_up (&sema[0]);
       sema_down (&sema[1]);
     }
+  printk ("done.\n");
+}
+
+/* Thread function used by sema_self_test(). */
+static void
+sema_test_helper (void *sema_) 
+{
+  struct semaphore *sema = sema_;
+  int i;
+
+  for (i = 0; i < 10; i++) 
+    {
+      sema_down (&sema[0]);
+      sema_up (&sema[1]);
+    }
 }
 \f
 /* Initializes LOCK and names it NAME (for debugging purposes).
    A lock can be held by at most a single thread at any given
-   time.  Our locks are not "recursive", meaning that it is an
-   error for a thread that holds a lock to attempt to reacquire
-   it.
+   time.  Our locks are not "recursive", that is, it is an error
+   for the thread currently holding a lock to try to acquire that
+   lock.
 
    A lock is a specialization of a semaphore with an initial
    value of 1.  The difference between a lock and such a
@@ -147,11 +172,13 @@ lock_init (struct lock *lock, const char *name)
    thread.
 
    This function may sleep, so it must not be called within an
-   interrupt handler. */
+   interrupt handler.  This function may be called with
+   interrupts disabled, but interrupts will be turned back on if
+   we need to sleep. */
 void
 lock_acquire (struct lock *lock)
 {
-  enum if_level old_level;
+  enum intr_level old_level;
 
   ASSERT (lock != NULL);
   ASSERT (!intr_context ());
@@ -171,7 +198,7 @@ lock_acquire (struct lock *lock)
 void
 lock_release (struct lock *lock) 
 {
-  enum if_level old_level;
+  enum intr_level old_level;
 
   ASSERT (lock != NULL);
   ASSERT (lock_held_by_current_thread (lock));
@@ -205,8 +232,8 @@ lock_name (const struct lock *lock)
 /* One semaphore in a list. */
 struct semaphore_elem 
   {
-    struct list_elem elem;
-    struct semaphore semaphore;
+    list_elem elem;                     /* List element. */
+    struct semaphore semaphore;         /* This semaphore. */
   };
 
 /* Initializes condition variable COND and names it NAME.  A
@@ -229,9 +256,9 @@ cond_init (struct condition *cond, const char *name)
    this function.
 
    The monitor implemented by this function is "Mesa" style, not
-   "Hoare" style.  That is, sending a signal is not atomic with
-   delivering it.  Thus, typically the caller must recheck the
-   condition after the wait completes and, if necessary, wait
+   "Hoare" style, that is, sending and receiving a signal are not
+   an atomic operation.  Thus, typically the caller must recheck
+   the condition after the wait completes and, if necessary, wait
    again.
 
    A given condition variable is associated with only a single
@@ -240,7 +267,9 @@ cond_init (struct condition *cond, const char *name)
    from locks to condition variables.
 
    This function may sleep, so it must not be called within an
-   interrupt handler. */
+   interrupt handler.  This function may be called with
+   interrupts disabled, but interrupts will be turned back on if
+   we need to sleep. */
 void
 cond_wait (struct condition *cond, struct lock *lock) 
 {