Add a little demo/test code for the `threads' project.
authorBen Pfaff <blp@cs.stanford.edu>
Sat, 11 Sep 2004 19:32:54 +0000 (19:32 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Sat, 11 Sep 2004 19:32:54 +0000 (19:32 +0000)
src/Makefile.build
src/threads/init.c
src/threads/test.c [new file with mode: 0644]
src/threads/test.h [new file with mode: 0644]

index e55ce566e8dfefc61c45824744c4102ef2f5ff75..e77230089f0eaa4a605da627b3971c8c1445c864 100644 (file)
@@ -25,6 +25,7 @@ threads_SRC += threads/paging.c               # Page tables.
 threads_SRC += threads/palloc.c                # Page allocator.
 threads_SRC += threads/malloc.c                # Subpage allocator.
 threads_SRC += threads/start.S         # Startup code.
+threads_SRC += threads/test.c          # Test code.
 
 # Device driver code.
 devices_SRC  = devices/timer.c         # Timer device.
index 729c25ffcd528957ea96e5f22cf2cf1a439d2cfb..589a23efc5b0ec779068052dcbbaaa136f036200 100644 (file)
@@ -18,6 +18,7 @@
 #include "threads/mmu.h"
 #include "threads/paging.h"
 #include "threads/palloc.h"
+#include "threads/test.h"
 #include "threads/thread.h"
 #ifdef USERPROG
 #include "userprog/exception.h"
@@ -105,6 +106,8 @@ main (void)
       printf ("\nExecuting '%s':\n", initial_program);
       thread_execute (initial_program); 
     }
+#else
+  test ();
 #endif
 
   /* Terminate this thread. */
diff --git a/src/threads/test.c b/src/threads/test.c
new file mode 100644 (file)
index 0000000..6f7d10e
--- /dev/null
@@ -0,0 +1,85 @@
+#include "threads/test.h"
+#include <stdio.h>
+#include "threads/synch.h"
+#include "threads/thread.h"
+#include "devices/timer.h"
+
+static void test_sleep_once (void);
+
+void
+test (void) 
+{
+  test_sleep_once ();
+//  test_sleep_multiple ();
+}
+\f
+struct sleep_once_data 
+  {
+    int duration;               /* Number of ticks to sleep. */
+    volatile int *counter;      /* Counter to check. */
+    struct semaphore done;      /* Completion semaphore. */
+    struct thread *thread;      /* Thread. */
+  };
+
+static void sleep_once (void *);
+
+static void
+test_sleep_once (void) 
+{
+  struct sleep_once_data t[5];
+  const int t_cnt = sizeof t / sizeof *t;
+  volatile int counter;
+  int i;
+
+  printf ("\nTesting one sleep per thread...\n");
+
+  /* Start all the threads. */
+  counter = 0;
+  for (i = 0; i < t_cnt; i++)
+    {
+      char name[16];
+      snprintf (name, sizeof name, "once %d", i);
+
+      t[i].duration = i * 10;
+      t[i].counter = &counter;
+      sema_init (&t[i].done, 0, name);
+      t[i].thread = thread_create (name, sleep_once, &t[i]);
+    }
+  
+  /* Wait for all the threads to finish. */
+  for (i = 0; i < t_cnt; i++) 
+    {
+#ifdef THREAD_JOIN_IMPLEMENTED
+      thread_join (t[i].thread);
+#else
+      sema_down (&t[i].done);
+#endif
+    }
+
+  printf ("...done\n");
+}
+
+static void
+sleep_once (void *t_) 
+{
+  struct sleep_once_data *t = t_;
+
+  /* Sleep. */
+  timer_sleep (t->duration);
+
+  /* Check ordering. */
+  if (*t->counter > t->duration)
+    printf ("%s: Out of order sleep completion (%d > %d)!\n",
+            thread_name (thread_current ()),
+            *t->counter, t->duration);
+  else 
+    {
+      *t->counter = t->duration;
+      printf ("%s: Sleep %d complete\n",
+              thread_name (thread_current ()), t->duration);
+    }
+
+  /* Signal completion. */
+  sema_up (&t->done);
+}
+
diff --git a/src/threads/test.h b/src/threads/test.h
new file mode 100644 (file)
index 0000000..dde08fa
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef THREADS_TEST_H
+#define THREADS_TEST_H
+
+void test (void);
+
+#endif /* threads/test.h */