00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include "tests/threads/tests.h"
00012 #include "threads/init.h"
00013 #include "devices/timer.h"
00014 #include "threads/malloc.h"
00015 #include "threads/synch.h"
00016 #include "threads/thread.h"
00017
00018 struct simple_thread_data
00019 {
00020 int id;
00021 int iterations;
00022 struct lock *lock;
00023 int **op;
00024 };
00025
00026 #define THREAD_CNT 16
00027 #define ITER_CNT 16
00028
00029 static thread_func simple_thread_func;
00030
00031 void
00032 test_priority_fifo (void)
00033 {
00034 struct simple_thread_data data[THREAD_CNT];
00035 struct lock lock;
00036 int *output, *op;
00037 int i, cnt;
00038
00039
00040 ASSERT (!thread_mlfqs);
00041
00042
00043 ASSERT (thread_get_priority () == PRI_DEFAULT);
00044
00045 msg ("%d threads will iterate %d times in the same order each time.",
00046 THREAD_CNT, ITER_CNT);
00047 msg ("If the order varies then there is a bug.");
00048
00049 output = op = malloc (sizeof *output * THREAD_CNT * ITER_CNT * 2);
00050 ASSERT (output != NULL);
00051 lock_init (&lock);
00052
00053 thread_set_priority (PRI_DEFAULT + 2);
00054 for (i = 0; i < THREAD_CNT; i++)
00055 {
00056 char name[16];
00057 struct simple_thread_data *d = data + i;
00058 snprintf (name, sizeof name, "%d", i);
00059 d->id = i;
00060 d->iterations = 0;
00061 d->lock = &lock;
00062 d->op = &op;
00063 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, d);
00064 }
00065
00066 thread_set_priority (PRI_DEFAULT);
00067
00068 ASSERT (lock.holder == NULL);
00069
00070 cnt = 0;
00071 for (; output < op; output++)
00072 {
00073 struct simple_thread_data *d;
00074
00075 ASSERT (*output >= 0 && *output < THREAD_CNT);
00076 d = data + *output;
00077 if (cnt % THREAD_CNT == 0)
00078 printf ("(priority-fifo) iteration:");
00079 printf (" %d", d->id);
00080 if (++cnt % THREAD_CNT == 0)
00081 printf ("\n");
00082 d->iterations++;
00083 }
00084 }
00085
00086 static void
00087 simple_thread_func (void *data_)
00088 {
00089 struct simple_thread_data *data = data_;
00090 int i;
00091
00092 for (i = 0; i < ITER_CNT; i++)
00093 {
00094 lock_acquire (data->lock);
00095 *(*data->op)++ = data->id;
00096 lock_release (data->lock);
00097 thread_yield ();
00098 }
00099 }