Comment fixes.
[pintos-anon] / src / tests / threads / priority-donate-nest.c
1 /* Based on a test originally submitted for Stanford's CS 140 in
2    winter 1999 by Matt Franklin <startled@leland.stanford.edu>,
3    Greg Hutchins <gmh@leland.stanford.edu>, Yu Ping Hu
4    <yph@cs.stanford.edu>.  Modified by arens. */
5
6 #include <stdio.h>
7 #include "tests/threads/tests.h"
8 #include "threads/init.h"
9 #include "threads/synch.h"
10 #include "threads/thread.h"
11
12 struct locks 
13   {
14     struct lock *a;
15     struct lock *b;
16   };
17
18 static thread_func medium_thread_func;
19 static thread_func high_thread_func;
20
21 void
22 test_priority_donate_nest (void) 
23 {
24   struct lock a, b;
25   struct locks locks;
26
27   /* This test does not work with the MLFQS. */
28   ASSERT (!enable_mlfqs);
29
30   /* Make sure our priority is the default. */
31   ASSERT (thread_get_priority () == PRI_DEFAULT);
32
33   lock_init (&a);
34   lock_init (&b);
35
36   lock_acquire (&a);
37
38   locks.a = &a;
39   locks.b = &b;
40   thread_create ("medium", PRI_DEFAULT - 1, medium_thread_func, &locks);
41   thread_yield ();
42   msg ("Low thread should have priority %d.  Actual priority: %d.",
43        PRI_DEFAULT - 1, thread_get_priority ());
44
45   thread_create ("high", PRI_DEFAULT - 2, high_thread_func, &b);
46   thread_yield ();
47   msg ("Low thread should have priority %d.  Actual priority: %d.",
48        PRI_DEFAULT - 2, thread_get_priority ());
49
50   lock_release (&a);
51   thread_yield ();
52   msg ("Medium thread should just have finished.");
53   msg ("Low thread should have priority %d.  Actual priority: %d.",
54        PRI_DEFAULT, thread_get_priority ());
55 }
56
57 static void
58 medium_thread_func (void *locks_) 
59 {
60   struct locks *locks = locks_;
61
62   lock_acquire (locks->b);
63   lock_acquire (locks->a);
64
65   msg ("Medium thread should have priority %d.  Actual priority: %d.",
66        PRI_DEFAULT - 2, thread_get_priority ());
67   msg ("Medium thread got the lock.");
68
69   lock_release (locks->a);
70   thread_yield ();
71
72   lock_release (locks->b);
73   thread_yield ();
74
75   msg ("High thread should have just finished.");
76   msg ("Middle thread finished.");
77 }
78
79 static void
80 high_thread_func (void *lock_) 
81 {
82   struct lock *lock = lock_;
83
84   lock_acquire (lock);
85   msg ("High thread got the lock.");
86   lock_release (lock);
87   msg ("High thread finished.");
88 }