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/thread.h"
13 static void test_preempt (void);
14 static void test_fifo (void);
15 static void test_donate_return (void);
20 /* Make sure our prority is the default. */
21 ASSERT (thread_get_priority () == PRI_DEFAULT);
25 test_donate_return ();
28 static thread_func simple_thread_func;
29 static thread_func acquire_thread_func;
35 "Testing priority preemption.\n");
36 thread_create ("high-priority", PRI_DEFAULT + 1, simple_thread_func, NULL);
37 printf ("The high-priority thread should have already completed.\n"
38 "Priority preemption test done.\n");
47 "Testing FIFO preemption.\n"
48 "5 threads will iterate 10 times in the same order each time.\n"
49 "If the order varies then there is a bug.\n");
51 thread_set_priority (PRI_DEFAULT + 2);
52 for (i = 0; i < 5; i++)
55 snprintf (name, sizeof name, "%d", i);
56 thread_create (name, PRI_DEFAULT + 1, simple_thread_func, NULL);
58 thread_set_priority (PRI_DEFAULT);
60 printf ("FIFO preemption test done.\n");
64 test_donate_return (void)
69 "Testing priority donation.\n"
70 "If the statements printed below are all true, you pass.\n");
72 lock_init (&lock, "donor");
74 thread_create ("acquire1", PRI_DEFAULT + 1, acquire_thread_func, &lock);
75 printf ("This thread should have priority %d. Actual priority: %d.\n",
76 PRI_DEFAULT + 1, thread_get_priority ());
77 thread_create ("acquire2", PRI_DEFAULT + 2, acquire_thread_func, &lock);
78 printf ("This thread should have priority %d. Actual priority: %d.\n",
79 PRI_DEFAULT + 2, thread_get_priority ());
81 printf ("acquire1 and acquire2 must already have finished, in that order.\n"
82 "This should be the last line before finishing this test.\n"
83 "Priority donation test done.\n");
87 simple_thread_func (void *aux UNUSED)
91 for (i = 0; i < 5; i++)
93 printf ("Thread %s iteration %d\n", thread_name (), i);
96 printf ("Thread %s done!\n", thread_name ());
100 acquire_thread_func (void *lock_)
102 struct lock *lock = lock_;
105 printf ("%s: got the lock\n", thread_name ());
107 printf ("%s: done\n", thread_name ());