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 #include "tests/threads/tests.h"
11 #include "threads/init.h"
12 #include "devices/timer.h"
13 #include "threads/malloc.h"
14 #include "threads/synch.h"
15 #include "threads/thread.h"
17 struct simple_thread_data
19 int id; /* Sleeper ID. */
20 int iterations; /* Iterations so far. */
21 struct lock *lock; /* Lock on output. */
22 int **op; /* Output buffer position. */
28 static thread_func simple_thread_func;
31 test_priority_fifo (void)
33 struct simple_thread_data data[THREAD_CNT];
38 /* This test does not work with the MLFQS. */
39 ASSERT (!enable_mlfqs);
41 /* Make sure our priority is the default. */
42 ASSERT (thread_get_priority () == PRI_DEFAULT);
44 msg ("%d threads will iterate %d times in the same order each time.",
45 THREAD_CNT, ITER_CNT);
46 msg ("If the order varies then there is a bug.");
48 output = op = malloc (sizeof *output * THREAD_CNT * ITER_CNT * 2);
49 ASSERT (output != NULL);
52 thread_set_priority (PRI_DEFAULT - 2);
53 for (i = 0; i < THREAD_CNT; i++)
56 struct simple_thread_data *d = data + i;
57 snprintf (name, sizeof name, "%d", i);
62 thread_create (name, PRI_DEFAULT - 1, simple_thread_func, d);
65 thread_set_priority (PRI_DEFAULT);
66 /* All the other threads now run to termination here. */
67 ASSERT (lock.holder == NULL);
70 for (; output < op; output++)
72 struct simple_thread_data *d;
74 ASSERT (*output >= 0 && *output < THREAD_CNT);
76 if (cnt % THREAD_CNT == 0)
77 printf ("(priority-fifo) iteration:");
78 printf (" %d", d->id);
79 if (++cnt % THREAD_CNT == 0)
86 simple_thread_func (void *data_)
88 struct simple_thread_data *data = data_;
91 for (i = 0; i < ITER_CNT; i++)
93 lock_acquire (data->lock);
94 *(*data->op)++ = data->id;
95 lock_release (data->lock);