1 /* Problem 1-3: Priority Scheduling tests.
3 Based on a test originally submitted for Stanford's CS 140 in
4 winter 1999 by by Matt Franklin
5 <startled@leland.stanford.edu>, Greg Hutchins
6 <gmh@leland.stanford.edu>, Yu Ping Hu <yph@cs.stanford.edu>.
10 #error This test not applicable with MLFQS enabled.
13 #include "threads/test.h"
15 #include "devices/timer.h"
16 #include "threads/malloc.h"
17 #include "threads/synch.h"
18 #include "threads/thread.h"
20 static void test_fifo (void);
25 /* Make sure our priority is the default. */
26 ASSERT (thread_get_priority () == PRI_DEFAULT);
31 static thread_func simple_thread_func;
33 struct simple_thread_data
35 int id; /* Sleeper ID. */
36 int iterations; /* Iterations so far. */
37 struct lock *lock; /* Lock on output. */
38 int **op; /* Output buffer position. */
47 struct simple_thread_data data[THREAD_CNT];
53 "Testing FIFO preemption.\n"
54 "%d threads will iterate %d times in the same order each time.\n"
55 "If the order varies then there is a bug.\n",
56 THREAD_CNT, ITER_CNT);
58 output = op = malloc (sizeof *output * THREAD_CNT * ITER_CNT * 2);
59 ASSERT (output != NULL);
60 lock_init (&lock, "output");
62 thread_set_priority (PRI_DEFAULT + 2);
63 for (i = 0; i < THREAD_CNT; i++)
66 struct simple_thread_data *d = data + i;
67 snprintf (name, sizeof name, "%d", i);
72 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, d);
75 /* This should ensure that the iterations start at the
76 beginning of a timer tick. */
78 thread_set_priority (PRI_DEFAULT);
81 for (; output < op; output++)
83 struct simple_thread_data *d;
85 ASSERT (*output >= 0 && *output < THREAD_CNT);
87 if (d->iterations != ITER_CNT)
88 printf ("Thread %d iteration %d\n", d->id, d->iterations);
90 printf ("Thread %d done!\n", d->id);
93 printf ("FIFO preemption test done.\n");
98 simple_thread_func (void *data_)
100 struct simple_thread_data *data = data_;
103 for (i = 0; i <= ITER_CNT; i++)
105 lock_acquire (data->lock);
106 *(*data->op)++ = data->id;
107 lock_release (data->lock);