a00447e43ac940c4559f8f0974fe9a2cb9293635
[pintos-anon] / grading / threads / priority-donate-multiple.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 #ifdef MLFQS
10 #error This test not applicable with MLFQS enabled.
11 #endif
12
13 #include "threads/test.h"
14 #include <stdio.h>
15 #include "threads/synch.h"
16 #include "threads/thread.h"
17
18 static void test_donate_multiple (void);
19
20 void
21 test (void) 
22 {
23   /* Make sure our priority is the default. */
24   ASSERT (thread_get_priority () == PRI_DEFAULT);
25
26   test_donate_multiple ();
27 }
28 \f
29 static thread_func a_thread_func;
30 static thread_func b_thread_func;
31
32 static void
33 test_donate_multiple (void) 
34 {
35   struct lock a, b;
36
37   printf ("\n"
38           "Testing multiple priority donation.\n"
39           "If the statements printed below are all true, you pass.\n");
40
41   lock_init (&a, "a");
42   lock_init (&b, "b");
43
44   lock_acquire (&a);
45   lock_acquire (&b);
46
47   thread_create ("a", PRI_DEFAULT + 1, a_thread_func, &a);
48   printf ("Main thread should have priority %d.  Actual priority: %d.\n",
49           PRI_DEFAULT + 1, thread_get_priority ());
50
51   thread_create ("b", PRI_DEFAULT + 2, b_thread_func, &b);
52   printf ("Main thread should have priority %d.  Actual priority: %d.\n",
53           PRI_DEFAULT + 2, thread_get_priority ());
54
55   lock_release (&b);
56   printf ("Thread b should have just finished.\n");
57   printf ("Main thread should have priority %d.  Actual priority: %d.\n",
58           PRI_DEFAULT + 1, thread_get_priority ());
59
60   lock_release (&a);
61   printf ("Thread a should have just finished.\n");
62   printf ("Main thread should have priority %d.  Actual priority: %d.\n",
63           PRI_DEFAULT, thread_get_priority ());
64   printf ("Multiple priority priority donation test finished.\n");
65 }
66
67 static void
68 a_thread_func (void *lock_) 
69 {
70   struct lock *lock = lock_;
71
72   lock_acquire (lock);
73   printf ("Thread a acquired lock a.\n");
74   lock_release (lock);
75   printf ("Thread a finished.\n");
76 }
77
78 static void
79 b_thread_func (void *lock_) 
80 {
81   struct lock *lock = lock_;
82
83   lock_acquire (lock);
84   printf ("Thread b acquired lock b.\n");
85   lock_release (lock);
86   printf ("Thread b finished.\n");
87 }