1 /* Problem 1-2: 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 "threads/synch.h"
12 #include "threads/thread.h"
14 static void test_preempt (void);
15 static void test_fifo (void);
16 static void test_donate_return (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);
29 test_donate_return ();
32 static thread_func simple_thread_func;
33 static thread_func acquire_thread_func;
39 "Testing priority preemption.\n");
40 thread_create ("high-priority", PRI_DEFAULT + 1, simple_thread_func, NULL);
41 printf ("The high-priority thread should have already completed.\n"
42 "Priority preemption test done.\n");
51 "Testing FIFO preemption.\n"
52 "5 threads will iterate 10 times in the same order each time.\n"
53 "If the order varies then there is a bug.\n");
55 thread_set_priority (PRI_DEFAULT + 2);
56 for (i = 0; i < 10; i++)
59 snprintf (name, sizeof name, "%d", i);
60 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, NULL);
62 thread_set_priority (PRI_DEFAULT);
64 printf ("FIFO preemption test done.\n");
68 test_donate_return (void)
73 "Testing priority donation.\n"
74 "If the statements printed below are all true, you pass.\n");
76 lock_init (&lock, "donor");
78 thread_create ("acquire1", PRI_DEFAULT + 1, acquire_thread_func, &lock);
79 printf ("This thread should have priority %d. Actual priority: %d.\n",
80 PRI_DEFAULT + 1, thread_get_priority ());
81 thread_create ("acquire2", PRI_DEFAULT + 2, acquire_thread_func, &lock);
82 printf ("This thread should have priority %d. Actual priority: %d.\n",
83 PRI_DEFAULT + 2, thread_get_priority ());
85 printf ("acquire2 and acquire1 must already have finished, in that order.\n"
86 "This should be the last line before finishing this test.\n"
87 "Priority donation test done.\n");
91 simple_thread_func (void *aux UNUSED)
95 for (i = 0; i < 5; i++)
97 printf ("Thread %s iteration %d\n", thread_name (), i);
100 printf ("Thread %s done!\n", thread_name ());
104 acquire_thread_func (void *lock_)
106 struct lock *lock = lock_;
109 printf ("%s: got the lock\n", thread_name ());
111 printf ("%s: done\n", thread_name ());