Use runtime options instead of conditional compilation for MLFQS,
[pintos-anon] / grading / threads / priority-donate-nest.c
1 /* Problem 1-3: Priority Scheduling tests.
2
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>.
7    Modified by arens. */
8
9 #include "threads/test.h"
10 #include <stdio.h>
11 #include "threads/synch.h"
12 #include "threads/thread.h"
13
14 static void test_donate_nest (void);
15
16 void
17 test (void) 
18 {
19   /* This test does not work with the MLFQS. */
20   ASSERT (!enable_mlfqs);
21
22   /* Make sure our priority is the default. */
23   ASSERT (thread_get_priority () == PRI_DEFAULT);
24
25   test_donate_nest ();
26 }
27 \f
28 static thread_func medium_thread_func;
29 static thread_func high_thread_func;
30
31 struct locks 
32   {
33     struct lock *a;
34     struct lock *b;
35   };
36
37 static void
38 test_donate_nest (void) 
39 {
40   struct lock a, b;
41   struct locks locks;
42
43   printf ("\n"
44           "Testing nested priority donation.\n"
45           "If the statements printed below are all true, you pass.\n");
46
47   lock_init (&a, "a");
48   lock_init (&b, "b");
49
50   lock_acquire (&a);
51
52   locks.a = &a;
53   locks.b = &b;
54   thread_create ("medium", PRI_DEFAULT + 1, medium_thread_func, &locks);
55   thread_yield ();
56   printf ("Low thread should have priority %d.  Actual priority: %d.\n",
57           PRI_DEFAULT + 1, thread_get_priority ());
58
59   thread_create ("high", PRI_DEFAULT + 2, high_thread_func, &b);
60   thread_yield ();
61   printf ("Low thread should have priority %d.  Actual priority: %d.\n",
62           PRI_DEFAULT + 2, thread_get_priority ());
63
64   lock_release (&a);
65   thread_yield ();
66   printf ("Medium thread should just have finished.\n");
67   printf ("Low thread should have priority %d.  Actual priority: %d.\n",
68           PRI_DEFAULT, thread_get_priority ());
69   printf ("Nested priority priority donation test finished.\n");
70 }
71
72 static void
73 medium_thread_func (void *locks_) 
74 {
75   struct locks *locks = locks_;
76
77   lock_acquire (locks->b);
78   lock_acquire (locks->a);
79
80   printf ("Medium thread should have priority %d.  Actual priority: %d.\n",
81           PRI_DEFAULT + 2, thread_get_priority ());
82   printf ("Medium thread got the lock.\n");
83
84   lock_release (locks->a);
85   thread_yield ();
86
87   lock_release (locks->b);
88   thread_yield ();
89
90   printf ("High thread should have just finished.\n");
91   printf ("Middle thread finished.\n");
92 }
93
94 static void
95 high_thread_func (void *lock_) 
96 {
97   struct lock *lock = lock_;
98
99   lock_acquire (lock);
100   printf ("High thread got the lock.\n");
101   lock_release (lock);
102   printf ("High thread finished.\n");
103 }