00001
00002
00003
00004
00005 #include <stdio.h>
00006 #include "tests/threads/tests.h"
00007 #include "threads/init.h"
00008 #include "threads/malloc.h"
00009 #include "threads/synch.h"
00010 #include "threads/thread.h"
00011 #include "devices/timer.h"
00012
00013 static void test_sleep (int thread_cnt, int iterations);
00014
00015 void
00016 test_alarm_single (void)
00017 {
00018 test_sleep (5, 1);
00019 }
00020
00021 void
00022 test_alarm_multiple (void)
00023 {
00024 test_sleep (5, 7);
00025 }
00026
00027
00028 struct sleep_test
00029 {
00030 int64_t start;
00031 int iterations;
00032
00033
00034 struct lock output_lock;
00035 int *output_pos;
00036 };
00037
00038
00039 struct sleep_thread
00040 {
00041 struct sleep_test *test;
00042 int id;
00043 int duration;
00044 int iterations;
00045 };
00046
00047 static void sleeper (void *);
00048
00049
00050 static void
00051 test_sleep (int thread_cnt, int iterations)
00052 {
00053 struct sleep_test test;
00054 struct sleep_thread *threads;
00055 int *output, *op;
00056 int product;
00057 int i;
00058
00059
00060 ASSERT (!thread_mlfqs);
00061
00062 msg ("Creating %d threads to sleep %d times each.", thread_cnt, iterations);
00063 msg ("Thread 0 sleeps 10 ticks each time,");
00064 msg ("thread 1 sleeps 20 ticks each time, and so on.");
00065 msg ("If successful, product of iteration count and");
00066 msg ("sleep duration will appear in nondescending order.");
00067
00068
00069 threads = malloc (sizeof *threads * thread_cnt);
00070 output = malloc (sizeof *output * iterations * thread_cnt * 2);
00071 if (threads == NULL || output == NULL)
00072 PANIC ("couldn't allocate memory for test");
00073
00074
00075 test.start = timer_ticks () + 100;
00076 test.iterations = iterations;
00077 lock_init (&test.output_lock);
00078 test.output_pos = output;
00079
00080
00081 ASSERT (output != NULL);
00082 for (i = 0; i < thread_cnt; i++)
00083 {
00084 struct sleep_thread *t = threads + i;
00085 char name[16];
00086
00087 t->test = &test;
00088 t->id = i;
00089 t->duration = (i + 1) * 10;
00090 t->iterations = 0;
00091
00092 snprintf (name, sizeof name, "thread %d", i);
00093 thread_create (name, PRI_DEFAULT, sleeper, t);
00094 }
00095
00096
00097 timer_sleep (100 + thread_cnt * iterations * 10 + 100);
00098
00099
00100
00101 lock_acquire (&test.output_lock);
00102
00103
00104 product = 0;
00105 for (op = output; op < test.output_pos; op++)
00106 {
00107 struct sleep_thread *t;
00108 int new_prod;
00109
00110 ASSERT (*op >= 0 && *op < thread_cnt);
00111 t = threads + *op;
00112
00113 new_prod = ++t->iterations * t->duration;
00114
00115 msg ("thread %d: duration=%d, iteration=%d, product=%d",
00116 t->id, t->duration, t->iterations, new_prod);
00117
00118 if (new_prod >= product)
00119 product = new_prod;
00120 else
00121 fail ("thread %d woke up out of order (%d > %d)!",
00122 t->id, product, new_prod);
00123 }
00124
00125
00126 for (i = 0; i < thread_cnt; i++)
00127 if (threads[i].iterations != iterations)
00128 fail ("thread %d woke up %d times instead of %d",
00129 i, threads[i].iterations, iterations);
00130
00131 lock_release (&test.output_lock);
00132 free (output);
00133 free (threads);
00134 }
00135
00136
00137 static void
00138 sleeper (void *t_)
00139 {
00140 struct sleep_thread *t = t_;
00141 struct sleep_test *test = t->test;
00142 int i;
00143
00144 for (i = 1; i <= test->iterations; i++)
00145 {
00146 int64_t sleep_until = test->start + i * t->duration;
00147 timer_sleep (sleep_until - timer_ticks ());
00148 lock_acquire (&test->output_lock);
00149 *test->output_pos++ = t->id;
00150 lock_release (&test->output_lock);
00151 }
00152 }