Make tests public. Rewrite most tests. Add tests.
[pintos-anon] / src / tests / 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 <stdio.h>
10 #include "tests/threads/tests.h"
11 #include "threads/init.h"
12 #include "threads/synch.h"
13 #include "threads/thread.h"
14
15 struct locks 
16   {
17     struct lock *a;
18     struct lock *b;
19   };
20
21 static thread_func medium_thread_func;
22 static thread_func high_thread_func;
23
24 void
25 test_priority_donate_nest (void) 
26 {
27   struct lock a, b;
28   struct locks locks;
29
30   /* This test does not work with the MLFQS. */
31   ASSERT (!enable_mlfqs);
32
33   /* Make sure our priority is the default. */
34   ASSERT (thread_get_priority () == PRI_DEFAULT);
35
36   lock_init (&a);
37   lock_init (&b);
38
39   lock_acquire (&a);
40
41   locks.a = &a;
42   locks.b = &b;
43   thread_create ("medium", PRI_DEFAULT - 1, medium_thread_func, &locks);
44   thread_yield ();
45   msg ("Low thread should have priority %d.  Actual priority: %d.",
46        PRI_DEFAULT - 1, thread_get_priority ());
47
48   thread_create ("high", PRI_DEFAULT - 2, high_thread_func, &b);
49   thread_yield ();
50   msg ("Low thread should have priority %d.  Actual priority: %d.",
51        PRI_DEFAULT - 2, thread_get_priority ());
52
53   lock_release (&a);
54   thread_yield ();
55   msg ("Medium thread should just have finished.");
56   msg ("Low thread should have priority %d.  Actual priority: %d.",
57        PRI_DEFAULT, thread_get_priority ());
58 }
59
60 static void
61 medium_thread_func (void *locks_) 
62 {
63   struct locks *locks = locks_;
64
65   lock_acquire (locks->b);
66   lock_acquire (locks->a);
67
68   msg ("Medium thread should have priority %d.  Actual priority: %d.",
69        PRI_DEFAULT - 2, thread_get_priority ());
70   msg ("Medium thread got the lock.");
71
72   lock_release (locks->a);
73   thread_yield ();
74
75   lock_release (locks->b);
76   thread_yield ();
77
78   msg ("High thread should have just finished.");
79   msg ("Middle thread finished.");
80 }
81
82 static void
83 high_thread_func (void *lock_) 
84 {
85   struct lock *lock = lock_;
86
87   lock_acquire (lock);
88   msg ("High thread got the lock.");
89   lock_release (lock);
90   msg ("High thread finished.");
91 }