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>.
9 #include "threads/test.h"
11 #include "devices/timer.h"
12 #include "threads/malloc.h"
13 #include "threads/synch.h"
14 #include "threads/thread.h"
16 static void test_fifo (void);
21 /* This test does not work with the MLFQS. */
22 ASSERT (!enable_mlfqs);
24 /* Make sure our priority is the default. */
25 ASSERT (thread_get_priority () == PRI_DEFAULT);
30 static thread_func simple_thread_func;
32 struct simple_thread_data
34 int id; /* Sleeper ID. */
35 int iterations; /* Iterations so far. */
36 struct lock *lock; /* Lock on output. */
37 int **op; /* Output buffer position. */
46 struct simple_thread_data data[THREAD_CNT];
52 "Testing FIFO preemption.\n"
53 "%d threads will iterate %d times in the same order each time.\n"
54 "If the order varies then there is a bug.\n",
55 THREAD_CNT, ITER_CNT);
57 output = op = malloc (sizeof *output * THREAD_CNT * ITER_CNT * 2);
58 ASSERT (output != NULL);
59 lock_init (&lock, "output");
61 thread_set_priority (PRI_DEFAULT + 2);
62 for (i = 0; i < THREAD_CNT; i++)
65 struct simple_thread_data *d = data + i;
66 snprintf (name, sizeof name, "%d", i);
71 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, d);
74 /* This should ensure that the iterations start at the
75 beginning of a timer tick. */
77 thread_set_priority (PRI_DEFAULT);
80 for (; output < op; output++)
82 struct simple_thread_data *d;
84 ASSERT (*output >= 0 && *output < THREAD_CNT);
86 if (d->iterations != ITER_CNT)
87 printf ("Thread %d iteration %d\n", d->id, d->iterations);
89 printf ("Thread %d done!\n", d->id);
92 printf ("FIFO preemption test done.\n");
97 simple_thread_func (void *data_)
99 struct simple_thread_data *data = data_;
102 for (i = 0; i <= ITER_CNT; i++)
104 lock_acquire (data->lock);
105 *(*data->op)++ = data->id;
106 lock_release (data->lock);