From: Ben Pfaff Date: Sat, 11 Sep 2004 19:32:54 +0000 (+0000) Subject: Add a little demo/test code for the `threads' project. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?p=pintos-anon;a=commitdiff_plain;h=9eb8439ff5957091606e0ca9dc184a333d748274 Add a little demo/test code for the `threads' project. --- diff --git a/src/Makefile.build b/src/Makefile.build index e55ce56..e772300 100644 --- a/src/Makefile.build +++ b/src/Makefile.build @@ -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. diff --git a/src/threads/init.c b/src/threads/init.c index 729c25f..589a23e 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -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 index 0000000..6f7d10e --- /dev/null +++ b/src/threads/test.c @@ -0,0 +1,85 @@ +#include "threads/test.h" +#include +#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 (); +} + +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 index 0000000..dde08fa --- /dev/null +++ b/src/threads/test.h @@ -0,0 +1,6 @@ +#ifndef THREADS_TEST_H +#define THREADS_TEST_H + +void test (void); + +#endif /* threads/test.h */