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 "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 /* Make sure our prority is the default. */
22 ASSERT (thread_get_priority () == PRI_DEFAULT);
26 test_donate_return ();
29 static thread_func simple_thread_func;
30 static thread_func acquire_thread_func;
36 "Testing priority preemption.\n");
37 thread_create ("high-priority", PRI_DEFAULT + 1, simple_thread_func, NULL);
38 printf ("The high-priority thread should have already completed.\n"
39 "Priority preemption test done.\n");
48 "Testing FIFO preemption.\n"
49 "5 threads will iterate 10 times in the same order each time.\n"
50 "If the order varies then there is a bug.\n");
52 thread_set_priority (PRI_DEFAULT + 2);
53 for (i = 0; i < 5; i++)
56 snprintf (name, sizeof name, "%d", i);
57 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, NULL);
59 thread_set_priority (PRI_DEFAULT);
61 printf ("FIFO preemption test done.\n");
65 test_donate_return (void)
70 "Testing priority donation.\n"
71 "If the statements printed below are all true, you pass.\n");
73 lock_init (&lock, "donor");
75 thread_create ("acquire1", PRI_DEFAULT + 1, acquire_thread_func, &lock);
76 printf ("This thread should have priority %d. Actual priority: %d.\n",
77 PRI_DEFAULT + 1, thread_get_priority ());
78 thread_create ("acquire2", PRI_DEFAULT + 2, acquire_thread_func, &lock);
79 printf ("This thread should have priority %d. Actual priority: %d.\n",
80 PRI_DEFAULT + 2, thread_get_priority ());
82 printf ("acquire2 and acquire1 must already have finished, in that order.\n"
83 "This should be the last line before finishing this test.\n"
84 "Priority donation test done.\n");
88 simple_thread_func (void *aux UNUSED)
92 for (i = 0; i < 5; i++)
94 printf ("Thread %s iteration %d\n", thread_name (), i);
97 printf ("Thread %s done!\n", thread_name ());
101 acquire_thread_func (void *lock_)
103 struct lock *lock = lock_;
106 printf ("%s: got the lock\n", thread_name ());
108 printf ("%s: done\n", thread_name ());